Skip to content

Instantly share code, notes, and snippets.

@the-glima
Created May 25, 2021 12:23
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 the-glima/4d68ba94c4bcd0af93e883b33ce238d1 to your computer and use it in GitHub Desktop.
Save the-glima/4d68ba94c4bcd0af93e883b33ce238d1 to your computer and use it in GitHub Desktop.
Git Diff Files
#!/usr/bin/env bash
main() {
while getopts ':e:c:h:' opt
do
case $opt in
e) extention="$OPTARG" ;;
c) command="$OPTARG" ;;
h) commit_hash="$OPTARG" ;;
*) echo "Wrong option: ${1}." exit 1;;
esac
done
diff_files "${extention}" "${commit_hash}" "${command}"
if [ $OPTIND -eq 1 ]; then echo "No flag option was passed."; fi
}
diff_files() {
extension="${1}"
commit_hash="${2}"
command="${3}"
if [[ -z "${commit_hash}" ]]; then
list=$(git diff-index --name-only HEAD | grep -E "(.*)\\.${extension}$" | xargs)
run_command "${list}" "${extension}" "${command}"
else
list=$(git show --pretty="" --name-only "${commit_hash}" | grep -E "(.*)\\.${extension}$" | xargs)
run_command "${list}" "${extension}" "${command}"
fi
}
filter_deleted_files() {
filtered_list="${1}"
deleted_files=($(git diff-index HEAD --diff-filter=D --summary --name-only))
for file in "${deleted_files[@]}"; do
filtered_list=("${filtered_list[@]/$file}")
done
echo "${filtered_list[@]/deleted_files}"
}
run_command() {
list=$(filter_deleted_files "${1}")
extension="${2}"
command="${3}"
if [[ -z "${extension}" ]]; then
echo "No extention provided."
else
if [[ -z "${list}" ]]; then
echo "No .${extension} files changed."
else
eval "${command} ${list}"
exit $?
fi
fi
}
main "${@}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment