aboutsummaryrefslogtreecommitdiff
path: root/contrib/diffall/git-diffall
blob: 9bbd27f4a5db3352dc97d50e011615650ac0e533 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/bin/sh
# Copyright 2010 - 2012, Tim Henigan <tim.henigan@gmail.com>
#
# Perform a directory diff between commits in the repository using
# the external diff or merge tool specified in the user's config.

USAGE='[--cached] [--copy-back] [-x|--extcmd=<command>] <commit>{0,2} [-- <path>*]

    --cached     Compare to the index rather than the working tree.

    --copy-back  Copy files back to the working tree when the diff
                 tool exits (in case they were modified by the
                 user).  This option is only valid if the diff
                 compared with the working tree.

    -x=<command>
    --extcmd=<command>  Specify a custom command for viewing diffs.
                 git-diffall ignores the configured defaults and
                 runs $command $LOCAL $REMOTE when this option is
                 specified. Additionally, $BASE is set in the
                 environment.
'

SUBDIRECTORY_OK=1
. "$(git --exec-path)/git-sh-setup"

TOOL_MODE=diff
. "$(git --exec-path)/git-mergetool--lib"

merge_tool="$(get_merge_tool)"
if test -z "$merge_tool"
then
	echo "Error: Either the 'diff.tool' or 'merge.tool' option must be set."
	usage
fi

start_dir=$(pwd)

# needed to access tar utility
cdup=$(git rev-parse --show-cdup) &&
cd "$cdup" || {
	echo >&2 "Cannot chdir to $cdup, the toplevel of the working tree"
	exit 1
}

# mktemp is not available on all platforms (missing from msysgit)
# Use a hard-coded tmp dir if it is not available
tmp="$(mktemp -d -t tmp.XXXXXX 2>/dev/null)" || {
	tmp=/tmp/git-diffall-tmp.$$
	mkdir "$tmp" || exit 1
}

trap 'rm -rf "$tmp" 2>/dev/null' EXIT

left=
right=
paths=
dashdash_seen=
compare_staged=
merge_base=
left_dir=
right_dir=
diff_tool=
copy_back=

while test $# != 0
do
	case "$1" in
	-h|--h|--he|--hel|--help)
		usage
		;;
	--cached)
		compare_staged=1
		;;
	--copy-back)
		copy_back=1
		;;
	-x|--e|--ex|--ext|--extc|--extcm|--extcmd)
		if test $# = 1
		then
			echo You must specify the tool for use with --extcmd
			usage
		else
			diff_tool=$2
			shift
		fi
		;;
	--)
		dashdash_seen=1
		;;
	-*)
		echo Invalid option: "$1"
		usage
		;;
	*)
		# could be commit, commit range or path limiter
		case "$1" in
		*...*)
			left=${1%...*}
			right=${1#*...}
			merge_base=1
			;;
		*..*)
			left=${1%..*}
			right=${1#*..}
			;;
		*)
			if test -n "$dashdash_seen"
			then
				paths="$paths$1 "
			elif test -z "$left"
			then
				left=$1
			elif test -z "$right"
			then
				right=$1
			else
				paths="$paths$1 "
			fi
			;;
		esac
		;;
	esac
	shift
done

# Determine the set of files which changed
if test -n "$left" && test -n "$right"
then
	left_dir="cmt-$(git rev-parse --short $left)"
	right_dir="cmt-$(git rev-parse --short $right)"

	if test -n "$compare_staged"
	then
		usage
	elif test -n "$merge_base"
	then
		git diff --name-only "$left"..."$right" -- $paths >"$tmp/filelist"
	else
		git diff --name-only "$left" "$right" -- $paths >"$tmp/filelist"
	fi
elif test -n "$left"
then
	left_dir="cmt-$(git rev-parse --short $left)"

	if test -n "$compare_staged"
	then
		right_dir="staged"
		git diff --name-only --cached "$left" -- $paths >"$tmp/filelist"
	else
		right_dir="working_tree"
		git diff --name-only "$left" -- $paths >"$tmp/filelist"
	fi
else
	left_dir="HEAD"

	if test -n "$compare_staged"
	then
		right_dir="staged"
		git diff --name-only --cached -- $paths >"$tmp/filelist"
	else
		right_dir="working_tree"
		git diff --name-only -- $paths >"$tmp/filelist"
	fi
fi

# Exit immediately if there are no diffs
if test ! -s "$tmp/filelist"
then
	exit 0
fi

if test -n "$copy_back" && test "$right_dir" != "working_tree"
then
	echo "--copy-back is only valid when diff includes the working tree."
	exit 1
fi

# Create the named tmp directories that will hold the files to be compared
mkdir -p "$tmp/$left_dir" "$tmp/$right_dir"

# Populate the tmp/right_dir directory with the files to be compared
if test -n "$right"
then
	while read name
	do
		ls_list=$(git ls-tree $right "$name")
		if test -n "$ls_list"
		then
			mkdir -p "$tmp/$right_dir/$(dirname "$name")"
			git show "$right":"$name" >"$tmp/$right_dir/$name" || true
		fi
	done < "$tmp/filelist"
elif test -n "$compare_staged"
then
	while read name
	do
		ls_list=$(git ls-files -- "$name")
		if test -n "$ls_list"
		then
			mkdir -p "$tmp/$right_dir/$(dirname "$name")"
			git show :"$name" >"$tmp/$right_dir/$name"
		fi
	done < "$tmp/filelist"
else
	# Mac users have gnutar rather than tar
	(tar --ignore-failed-read -c -T "$tmp/filelist" | (cd "$tmp/$right_dir" && tar -x)) || {
		gnutar --ignore-failed-read -c -T "$tmp/filelist" | (cd "$tmp/$right_dir" && gnutar -x)
	}
fi

# Populate the tmp/left_dir directory with the files to be compared
while read name
do
	if test -n "$left"
	then
		ls_list=$(git ls-tree $left "$name")
		if test -n "$ls_list"
		then
			mkdir -p "$tmp/$left_dir/$(dirname "$name")"
			git show "$left":"$name" >"$tmp/$left_dir/$name" || true
		fi
	else
		if test -n "$compare_staged"
		then
			ls_list=$(git ls-tree HEAD "$name")
			if test -n "$ls_list"
			then
				mkdir -p "$tmp/$left_dir/$(dirname "$name")"
				git show HEAD:"$name" >"$tmp/$left_dir/$name"
			fi
		else
			mkdir -p "$tmp/$left_dir/$(dirname "$name")"
			git show :"$name" >"$tmp/$left_dir/$name"
		fi
	fi
done < "$tmp/filelist"

cd "$tmp"
LOCAL="$left_dir"
REMOTE="$right_dir"

if test -n "$diff_tool"
then
	export BASE
	eval $diff_tool '"$LOCAL"' '"$REMOTE"'
else
	run_merge_tool "$merge_tool" false
fi

# Copy files back to the working dir, if requested
if test -n "$copy_back" && test "$right_dir" = "working_tree"
then
	cd "$start_dir"
	git_top_dir=$(git rev-parse --show-toplevel)
	find "$tmp/$right_dir" -type f |
	while read file
	do
		cp "$file" "$git_top_dir/${file#$tmp/$right_dir/}"
	done
fi