scripts

My personal collection of scripts
git clone git://src.gearsix.net/scripts
Log | Files | Refs | Atom | README | LICENSE

git-diffadd.sh (964B)


      1 #!/usr/bin/env sh
      2 # DESCRIPTION: 'git diff', prompt y/n if 'git add'
      3 # ARGUMENTS: git-diffadd file1 file2 file3 ...
      4 # DEPENDENCIES: git
      5 
      6 if [ "$1" = "-h" ] || [ "$1" = "--help" ]
      7 then
      8 	echo "usage: git-diffadd FILE..."
      9 	echo ""
     10 	echo "Preview the 'git diff' of all specified paths (FILE...)"
     11 	echo "and prompt y/n if 'git add' should be run on the file."
     12 	echo "If no FILE... targets are provided, all un-added files"
     13 	echo "will be used"
     14 	exit
     15 fi
     16 
     17 pradd() {
     18 	git diff "$dir/$f"
     19 
     20 	yn=""
     21 	while [ "$yn" = "" ]; do
     22 		printf "git add '%s' (y/n)? " "$f"
     23 		read -r yn
     24 		if [ "$yn" = "y" ]
     25 		then git add "$f"
     26 		elif [ "$yn" != "n" ]
     27 		then yn=""; fi
     28 	done
     29 }
     30 
     31 dir="."
     32 if [ ! "$(git rev-parse --show-toplevel)" ]
     33 then
     34   echo "'git rev-parse --show-toplevel' failed to return repo root dir at '$(pwd)'"
     35   exit 1
     36 else
     37   dir="$(pwd)"
     38 fi
     39 
     40 n=0
     41 for f in "$@"
     42 do
     43 	n=$((n+1))
     44     pradd "$dir/$f"
     45 done
     46 
     47 if [ $n -eq 0 ]
     48 then
     49 	for f in $(git ls-files -m)
     50 	do
     51 		pradd "$f"
     52 	done
     53 fi
     54