Skip to content

Instantly share code, notes, and snippets.

@JamieMason
Created September 9, 2019 10:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JamieMason/8805ef57a9fc0ea74a5fa500aeadf668 to your computer and use it in GitHub Desktop.
Save JamieMason/8805ef57a9fc0ea74a5fa500aeadf668 to your computer and use it in GitHub Desktop.
Get Paths To Files In A Git Repository
#!/usr/bin/env bash
# COMMAND LINE ARGUMENTS
FUNC=${1:-"tracked_by_git"}
FILTER=${2:-"all"}
OUR_BRANCH=${3:-"HEAD"}
THEIR_BRANCH=${4:-"origin/master"}
# LIBRARY CODE
function filter {
regexp="$1"
grep -E "$regexp"
}
function exclude {
regexp="$1"
grep -Ev "$regexp"
}
function branch_changes {
git diff --name-status "$THEIR_BRANCH".."$OUR_BRANCH"
}
function user_email {
mailmap_entry=$(echo "<$(git config user.email)>" | git check-mailmap --stdin)
echo $mailmap_entry | sed -E 's/^.*<|>.*$//g'
}
function with_leading_dot {
while read path; do echo "./$path"; done
}
# AVAILABLE COMMANDS
function touched_by_user {
git log --use-mailmap --no-merges --author="$(user_email)" --diff-filter=ACM --name-only --pretty=format:"" | sort -u
}
function in_git_stage {
git status --short | cut -d ' ' -f3
}
function added_in_git_stage {
git status --short | filter "^ A" | cut -d ' ' -f3
}
function touched_in_branch {
branch_changes | cut -f2
}
function added_in_branch {
branch_changes | filter "^A" | cut -f2
}
function tracked_by_git {
git ls-tree -r HEAD --name-only
}
function src_dirs {
find packages internals -name src -type d
}
# REGULAR EXPRESSIONS
regexp_fixtures="\/fixtures\/"
regexp_images="\.(bmp|gif|jpeg|jpg|png|svg)$"
regexp_js="\.jsx?$"
regexp_json="\.json$"
regexp_md="\.md$"
regexp_package_json="package\.json$"
regexp_packages_dir="packages\/"
regexp_recipes="\/recipes\/"
regexp_rollup="\/rollup\.config\.js"
regexp_scss="\.scss$"
regexp_shell="\.sh$"
regexp_spec_e2e="\.ui\.spec\."
regexp_spec="\.spec\."
regexp_storybook="(\/story\/|\/story.jsx?$)"
# EXECUTE SCRIPT
function apply_filters_to_function {
if [ "$FILTER" = "fixtures" ]; then
$FUNC | filter "$regexp_fixtures"
elif [ "$FILTER" = "images" ]; then
$FUNC | filter "$regexp_images"
elif [ "$FILTER" = "jest" ]; then
$FUNC | filter "$regexp_spec" \
| exclude "$regexp_spec_e2e"
elif [ "$FILTER" = "js-source" ]; then
$FUNC | filter "$regexp_js" \
| filter "$regexp_packages_dir" \
| exclude "$regexp_spec" \
| exclude "$regexp_fixtures" \
| exclude "$regexp_storybook" \
| exclude "$regexp_rollup"
elif [ "$FILTER" = "js" ]; then
$FUNC | filter "$regexp_js"
elif [ "$FILTER" = "json" ]; then
$FUNC | filter "$regexp_json"
elif [ "$FILTER" = "md" ]; then
$FUNC | filter "$regexp_md"
elif [ "$FILTER" = "package-json" ]; then
$FUNC | filter "$regexp_package_json"
elif [ "$FILTER" = "recipes" ]; then
$FUNC | filter "$regexp_recipes"
elif [ "$FILTER" = "scss" ]; then
$FUNC | filter "$regexp_scss"
elif [ "$FILTER" = "shell" ]; then
$FUNC | filter "$regexp_shell"
elif [ "$FILTER" = "storybook" ]; then
$FUNC | filter "$regexp_storybook"
else
$FUNC
fi
}
apply_filters_to_function | with_leading_dot

Get Paths To Files In A Git Repository

Commands

added_in_branch

Files added (not modified or deleted) in our branch.

added_in_git_stage

Files added (not modified or deleted) in git add.

in_git_stage

Files in git add.

src_dirs

Files which are descendants of a "src" Directory.

touched_by_user

Files created or modified at any time in history by the current Git User.

touched_in_branch

Files created or modified in the current branch.

tracked_by_git

Files in the Git Repository.

Filters

See Regular Expressions in the Script Source.

  • fixtures
  • images
  • jest
  • js
  • js
  • json
  • md
  • package
  • recipes
  • scss
  • shell
  • storybook

Usage

./get-paths.sh <command> <filter> <our_branch> <their_branch>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment