Skip to content

Instantly share code, notes, and snippets.

@nathan-fiscaletti
Last active February 14, 2020 00:15
Show Gist options
  • Save nathan-fiscaletti/04700225cefe0b10f0d17ba6871dc88b to your computer and use it in GitHub Desktop.
Save nathan-fiscaletti/04700225cefe0b10f0d17ba6871dc88b to your computer and use it in GitHub Desktop.
Git Diff List - A tool for looking at git diffs without typing in long file paths
#!/bin/bash
## This script makes it so that you don't have to type
## long file paths into `git diff` anymore.
##
## Add this script anywhere on your system and then
## in your ~/.gitconfig add the following:
##
## [alias]
## diff-list = !/Full/Path/To/Script.sh
##
##
## Usage:
##
## In a git repository, you can now run:
##
## git diff-list
##
## This will give you a list of all un-staged files
## you would normally see in `git status`, but with
## integer identifires before them.
##
## You can now run the following to see a diff for
## a file:
##
## git diff-list <id>
##
## Now you don't have to type long paths into `git diff`
main() {
changed_files=$(git diff --color --name-status)
total_changed=0
for file in $changed_files; do
let total_changed=total_changed+1
done
if [ "$#" -ne 1 ]; then
echo ""
echo -e "> \033[1m\033[4mgit difflist <id>\033[0m to view the diff for any file (\033[1m\033[4m<id>\033[0m does not require leading 0s)"
echo ""
echo "Difference:"
echo ""
list_changes "$changed_files"
echo ""
exit
fi
if [ $1 -lt 0 ]; then
echo "Invalid diff id, try again."
exit 1
fi
if [ $1 -gt $total_changed ]; then
echo "Invalid diff id, try again."
exit 1
fi
file=$(get_file_diff "$changed_files" $1)
if [[ "$file" != "0" ]]; then
git diff $file
exit 0
fi
echo "File does not contain any changes."
exit 1
}
get_file_diff() {
change=1
IFS=$'\n'
for file in $1; do
change_type=$(echo $file | awk '{printf $1}')
file_name=$(echo $file | awk '{printf $2}')
if [[ "$change" == "$2" ]]; then
gitdir=${PWD##*/}
echo ${file_name/$gitdir/"."}
return
fi
let change=$change+1
done
echo "0"
}
list_changes() {
change=1
IFS=$'\n'
for file in $1; do
change_type=$(echo $file | awk '{printf $1}')
file_name=$(echo $file | awk '{printf $2}')
printf " [%04d] %s %s\n" $change $change_type $file_name
let change=$change+1
done
}
main $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment