#!/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 ## ## 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 \033[0m to view the diff for any file (\033[1m\033[4m\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 $@