Last active
October 2, 2020 06:03
-
-
Save yu-iskw/93476bc3f2fc04ffb89f3d2cb5d39d10 to your computer and use it in GitHub Desktop.
A bash script to get changed files in git
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
# Arguments | |
pattern="${1:?"the first argument for pattern is not set"}" | |
# Constants | |
CURRENT_BRANCH="$(git rev-parse --abbrev-ref HEAD)" | |
# Determine the target | |
if [[ "$CURRENT_BRANCH" == "master" ]] ; then | |
# Compare with only the previous commit | |
DIFF_TARGET="HEAD^ HEAD" | |
else | |
DIFF_TARGET="origin/master" | |
fi | |
# Get changed files which match the pattern. | |
# NOTE: | |
# `|| :` is used to avoid exit by grep, when no line matches the pattern. | |
changed_files=$(git diff $DIFF_TARGET --name-only --no-color | grep -e "$pattern" || :) | |
# Remove empty lines and lines start with `./` | |
echo "$changed_files" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment