Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JamieMason/05237a9cea802ce1a1f89ad847c27031 to your computer and use it in GitHub Desktop.
Save JamieMason/05237a9cea802ce1a1f89ad847c27031 to your computer and use it in GitHub Desktop.
Get all changes from Git for the current User/Developer/Author/Committer
#!/usr/bin/env bash
FILTER_ARGUMENT="$1"
function user_email {
mailmap_entry=$(echo "<$(git config user.email)>" | git check-mailmap --stdin)
echo $mailmap_entry | sed -E 's/^.*<|>.*$//g'
}
function files_touched_by_user {
git log --use-mailmap --no-merges --author="$(user_email)" --diff-filter=ACM --name-only --pretty=format:"" | sort -u
}
function files_touched_in_this_branch {
git diff --name-status master | cut -f2
}
function filter_deleted_files {
while read file; do [ -f "$file" ] && echo "$file"; done
}
function all_local_changes {
{ files_touched_by_user & files_touched_in_this_branch; } | sort | uniq | filter_deleted_files
}
function all_local_changes_by_type {
if [ "$FILTER_ARGUMENT" = "JS" ]; then
all_local_changes | grep -E "\.(js|jsx|ts|tsx)$"
elif [ "$FILTER_ARGUMENT" = "CSS" ]; then
all_local_changes | grep -E "\.(css|scss)$"
else
all_local_changes
fi
}
all_local_changes_by_type

Get all changes from Git for the current Author

  1. Get the Email of the current User/Developer/Author/Committer.
  2. Resolve the Email through the Git Mailmap.
  3. Get all files they have ever created or modified.
  4. Get all files modified in the current branch compared to master.
  5. Some files they've modified could have since been deleted by someone else, so filter out files which no longer exist.

Use Case

Instead of running ESLint over the entire codebase in every Pull Request, only lint files which have changed. Plus, in order to present warnings for Technical Debt incurred in the past, also lint files the current Developer has touched in the past.

Usage

Get everything

./all_local_changes.sh

Get js|jsx|ts|tsx files only

./all_local_changes.sh JS

Get css|scss files only

./all_local_changes.sh CSS
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment