Skip to content
Newer
Older
100755 247 lines (220 sloc) 6.63 KB
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 13, 2010
1 #!/bin/sh -e
8efd2a5 @thenigan Updated copyright statement for 2011
thenigan authored Apr 15, 2011
2 # Copyright 2010 - 2011, Tim Henigan <tim.henigan@gmail.com>
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
3 #
4 # Perform a directory diff between commits in the repository using
5 # the external diff tool specified in the 'diff.tool' configuration
6 # option.
7
8eac32f @thenigan Merge branch 'elazarl/copyback'
thenigan authored May 19, 2011
8 USAGE='[--cached] [--copy-back] [-x|--extcmd=<command>] <commit>{0,2} -- <path>*
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
9
9004d60 @thenigan Clean up usage string
thenigan authored May 19, 2011
10 --cached Compare to the index rather than the working tree.
05e27e4 @thenigan Taught diffall to use the '--extcmd' option
thenigan authored Apr 15, 2011
11
9004d60 @thenigan Clean up usage string
thenigan authored May 20, 2011
12 --copy-back Copy files back to the working tree when the diff
13 tool exits (in case they were modified by the
14 user). This option is only valid if the diff
15 compared with the working tree.
8eac32f @thenigan Merge branch 'elazarl/copyback'
thenigan authored May 20, 2011
16
05e27e4 @thenigan Taught diffall to use the '--extcmd' option
thenigan authored Apr 15, 2011
17 -x=<command>
9004d60 @thenigan Clean up usage string
thenigan authored May 20, 2011
18 --extcmd=<command> Specify a custom command for viewing diffs.
19 git-diffall ignores the configured defaults and
20 runs $command $LOCAL $REMOTE when this option is
21 specified. Additionally, $BASE is set in the
22 environment.
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
23 '
24
c349207 @georgevreilly Make git-diffall run from a subdirectory.
georgevreilly authored Dec 31, 2010
25 SUBDIRECTORY_OK=1
0c76102 @georgevreilly Invoke git-sh-setup correctly
georgevreilly authored Dec 31, 2010
26 . "$(git --exec-path)/git-sh-setup"
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
27
717a537 @thenigan Taught diffall to use the 'git-mergetool--lib' helpers
thenigan authored Apr 15, 2011
28 TOOL_MODE=diff
29 . "$(git --exec-path)/git-mergetool--lib"
30
31 merge_tool="$(get_merge_tool)"
32 if [ -z "$merge_tool" ]; then
33 echo "Error: Either the 'diff.tool' or 'merge.tool' option must be set."
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
34 usage
35 fi
36
37 start_dir=$(pwd)
c349207 @georgevreilly Make git-diffall run from a subdirectory.
georgevreilly authored Dec 31, 2010
38
39 # needed to access tar utility
40 cdup=$(git rev-parse --show-cdup) &&
41 cd "$cdup" || {
42 echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree"
43 exit 1
44 }
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
45
46 # mktemp is not available on all platforms (missing from msysgit)
47 # Use a hard-coded tmp dir if it is not available
48 if [ -z $(which mktemp) ]; then
49 tmp=/tmp/git-diffall-tmp
50 else
98a89f0 Fixed mktemp call to work on OS X as well as other OSs
Daniel Bingham authored Oct 27, 2010
51 tmp="$(mktemp -d -t tmp.XXXXXX)"
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
52 fi
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 3, 2011
53 mkdir -p "$tmp"
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
54
55 left=
56 right=
57 paths=
58 path_sep=
59 compare_staged=
06b7bbc @georgevreilly Fix spelling of common_ancestor
georgevreilly authored Dec 31, 2010
60 common_ancestor=
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
61 left_dir=
62 right_dir=
05e27e4 @thenigan Taught diffall to use the '--extcmd' option
thenigan authored Apr 15, 2011
63 diff_tool=
c7fabdd Support --copy-back option.
Elazar authored May 19, 2011
64 copy_back=
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
65
66 while test $# != 0; do
1702c24 @thenigan Eliminated all tabs
thenigan authored Oct 27, 2010
67 case "$1" in
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
68 -h|--h|--he|--hel|--help)
69 usage
70 ;;
71 --cached)
72 compare_staged=1
73 ;;
c7fabdd Support --copy-back option.
Elazar authored May 19, 2011
74 --copy-back)
75 copy_back=1
76 ;;
05e27e4 @thenigan Taught diffall to use the '--extcmd' option
thenigan authored Apr 15, 2011
77 -x|--e|--ex|--ext|--extc|--extcm|--extcmd)
78 diff_tool=$2
79 shift
80 echo "$diff_tool"
81 ;;
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
82 --)
83 path_sep=1
84 ;;
85 -*)
86 echo Invalid option: "$1"
87 usage
88 ;;
89 *)
90 # could be commit, commit range or path limiter
91 case "$1" in
92 *...*)
93 left=${1%...*}
94 right=${1#*...}
06b7bbc @georgevreilly Fix spelling of common_ancestor
georgevreilly authored Dec 31, 2010
95 common_ancestor=1
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
96 ;;
97 *..*)
98 left=${1%..*}
99 right=${1#*..}
100 ;;
101 *)
102 if [ -n "$path_sep" ]; then
103 if [ -z "$paths" ]; then
104 paths=$1
105 else
106 paths="$paths $1"
107 fi
108 elif [ -z "$left" ]; then
109 left=$1
110 elif [ -z "$right" ]; then
111 right=$1
112 else
113 if [ -z "$paths" ]; then
114 paths=$1
115 else
116 paths="$paths $1"
117 fi
118 fi
119 ;;
120 esac
121 ;;
122 esac
123 shift
124 done
125
126 # Determine the set of files which changed
127 if [ -n "$left" ] && [ -n "$right" ]; then
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
128 left_dir="cmt-`git rev-parse --short $left`"
129 right_dir="cmt-`git rev-parse --short $right`"
130
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
131 if [ -n "$compare_staged" ]; then
132 usage
06b7bbc @georgevreilly Fix spelling of common_ancestor
georgevreilly authored Dec 31, 2010
133 elif [ -n "$common_ancestor" ]; then
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
134 git diff --name-only "$left"..."$right" -- "$paths" > "$tmp"/filelist
135 else
136 git diff --name-only "$left" "$right" -- "$paths" > "$tmp"/filelist
137 fi
138 elif [ -n "$left" ]; then
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
139 left_dir="cmt-`git rev-parse --short $left`"
140
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
141 if [ -n "$compare_staged" ]; then
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
142 right_dir="staged"
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
143 git diff --name-only --cached "$left" -- "$paths" > "$tmp"/filelist
144 else
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
145 right_dir="working_tree"
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
146 git diff --name-only "$left" -- "$paths" > "$tmp"/filelist
147 fi
148 else
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
149 left_dir="HEAD"
150
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
151 if [ -n "$compare_staged" ]; then
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
152 right_dir="staged"
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
153 git diff --name-only --cached -- "$paths" > "$tmp"/filelist
154 else
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
155 right_dir="working_tree"
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
156 git diff --name-only -- "$paths" > "$tmp"/filelist
157 fi
158 fi
159
160 # Exit immediately if there are no diffs
161 if [ ! -s "$tmp"/filelist ]; then
162 exit 0
163 fi
164
804fa32 @thenigan Only check --copy-back for validity if there are actually diffs
thenigan authored May 19, 2011
165 if [ -n "$copy_back" ] && [ "$right_dir" != "working_tree" ]; then
166 echo "--copy-back is only valid when diff includes the working tree."
167 exit 1
168 fi
169
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
170 # Create the named tmp directories that will hold the files to be compared
171 mkdir -p "$tmp"/"$left_dir" "$tmp"/"$right_dir"
172
173 # Populate the tmp/right_dir directory with the files to be compared
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
174 if [ -n "$right" ]; then
1702c24 @thenigan Eliminated all tabs
thenigan authored Oct 28, 2010
175 while read name; do
176 ls_list=$(git ls-tree $right $name)
177 if [ -n "$ls_list" ]; then
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
178 mkdir -p "$tmp"/"$right_dir"/"$(dirname "$name")"
179 git show "$right":"$name" > "$tmp"/"$right_dir"/"$name" || true
1702c24 @thenigan Eliminated all tabs
thenigan authored Oct 28, 2010
180 fi
181 done < "$tmp"/filelist
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
182 elif [ -n "$compare_staged" ]; then
1702c24 @thenigan Eliminated all tabs
thenigan authored Oct 28, 2010
183 while read name; do
184 ls_list=$(git ls-files -- $name)
185 if [ -n "$ls_list" ]; then
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
186 mkdir -p "$tmp"/"$right_dir"/"$(dirname "$name")"
187 git show :"$name" > "$tmp"/"$right_dir"/"$name"
1702c24 @thenigan Eliminated all tabs
thenigan authored Oct 28, 2010
188 fi
189 done < "$tmp"/filelist
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
190 else
1702c24 @thenigan Eliminated all tabs
thenigan authored Oct 28, 2010
191 if [ -n "$(which gnutar)" ]; then
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
192 gnutar --ignore-failed-read -c -T "$tmp"/filelist | (cd "$tmp"/"$right_dir" && gnutar -x)
1702c24 @thenigan Eliminated all tabs
thenigan authored Oct 28, 2010
193 else
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
194 tar --ignore-failed-read -c -T "$tmp"/filelist | (cd "$tmp"/"$right_dir" && tar -x)
1702c24 @thenigan Eliminated all tabs
thenigan authored Oct 28, 2010
195 fi
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
196 fi
197
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
198 # Populate the tmp/left_dir directory with the files to be compared
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
199 while read name; do
200 if [ -n "$left" ]; then
7b90395 @georgevreilly Fix indentation
georgevreilly authored Dec 31, 2010
201 ls_list=$(git ls-tree $left $name)
1702c24 @thenigan Eliminated all tabs
thenigan authored Oct 28, 2010
202 if [ -n "$ls_list" ]; then
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
203 mkdir -p "$tmp"/"$left_dir"/"$(dirname "$name")"
204 git show "$left":"$name" > "$tmp"/"$left_dir"/"$name" || true
7b90395 @georgevreilly Fix indentation
georgevreilly authored Dec 31, 2010
205 fi
206 else
207 if [ -n "$compare_staged" ]; then
208 ls_list=$(git ls-tree HEAD $name)
209 if [ -n "$ls_list" ]; then
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
210 mkdir -p "$tmp"/"$left_dir"/"$(dirname "$name")"
211 git show HEAD:"$name" > "$tmp"/"$left_dir"/"$name"
7b90395 @georgevreilly Fix indentation
georgevreilly authored Dec 31, 2010
212 fi
213 else
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
214 mkdir -p "$tmp"/"$left_dir"/"$(dirname "$name")"
215 git show :"$name" > "$tmp"/"$left_dir"/"$name"
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
216 fi
217 fi
218 done < "$tmp"/filelist
219
220 cd "$tmp"
717a537 @thenigan Taught diffall to use the 'git-mergetool--lib' helpers
thenigan authored Apr 15, 2011
221 LOCAL="$left_dir"
222 REMOTE="$right_dir"
223
05e27e4 @thenigan Taught diffall to use the '--extcmd' option
thenigan authored Apr 15, 2011
224 if [ -n "$diff_tool" ]; then
225 export BASE
226 eval $diff_tool '"$LOCAL"' '"$REMOTE"'
227 else
228 run_merge_tool "$merge_tool" false
229 fi
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
230
231 # On exit, remove the tmp directory
232 cleanup () {
233 cd "$start_dir"
c7fabdd Support --copy-back option.
Elazar authored May 19, 2011
234 git_top_dir=$(git rev-parse --show-toplevel)
235 #if --copy-back was specified, copy all files back to git root
236 # directory, in case they were changed
237 if [ -n "$copy_back" ]; then
238 find "$tmp/$right_dir" -type f|while read file; do
239 cp "$file" "$git_top_dir/${file#$tmp/$right_dir/}"
240 done
241 fi
1702c24 @thenigan Eliminated all tabs
thenigan authored Oct 28, 2010
242 rm -rf "$tmp"
e408d98 @thenigan Created script to perform directory diff using external diff tool
thenigan authored Apr 14, 2010
243 }
244
245 trap cleanup EXIT
777dbee @thenigan The script now copies temp files to named directories
thenigan authored Jan 4, 2011
246
Something went wrong with that request. Please try again.