Add git-mergetool to run an appropriate merge conflict resolution program
The git-mergetool program can be used to automatically run an appropriate merge resolution program to resolve merge conflicts. It will automatically run one of kdiff3, tkdiff, meld, xxdiff, or emacs emerge programs. Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Theodore Ts'o committed
Mar 6, 2007 at 00:05 UTC
c4b4a5af1604a7546c0b0786e47bcff8a47a6039
5 files changed
+405
-1
.gitignore
+1
@@ -75,6 +75,7 @@ git-merge-ours
75
git-merge-recursive
76
git-merge-resolve
77
git-merge-stupid
78
+git-mergetool
79
git-mktag
80
git-mktree
81
git-name-rev
Documentation/config.txt
+5
@@ -453,6 +453,11 @@ merge.summary::
453
Whether to include summaries of merged commits in newly created
454
merge commit messages. False by default.
455
456
+merge.tool::
457
+ Controls which merge resolution program is used by
458
+ gitlink:git-mergetool[l]. Valid values are: "kdiff3", "tkdiff",
459
+ "meld", "xxdiff", "emerge"
460
+
461
merge.verbosity::
462
Controls the amount of output shown by the recursive merge
463
strategy. Level 0 outputs nothing except a final error
Documentation/git-mergetool.txt
new
+46
@@ -0,0 +1,46 @@
1
+git-mergetool(1)
2
+================
3
+
4
+NAME
5
+----
6
+git-mergetool - Run merge conflict resolution tools to resolve merge conflicts
7
+
8
+SYNOPSIS
9
+--------
10
+'git-mergetool' [--tool=<tool>] [<file>]...
11
+
12
+DESCRIPTION
13
+-----------
14
+
15
+Use 'git mergetool' to run one of several merge utilities to resolve
16
+merge conflicts. It is typically run after gitlink:git-merge[1].
17
+
18
+If one or more <file> parameters are given, the merge tool program will
19
+be run to resolve differences on each file. If no <file> names are
20
+specified, 'git mergetool' will run the merge tool program on every file
21
+with merge conflicts.
22
+
23
+OPTIONS
24
+-------
25
+-t or --tool=<tool>::
26
+ Use the merge resolution program specified by <tool>.
27
+ Valid merge tools are:
28
+ kdiff3, tkdiff, meld, xxdiff, and emerge.
29
+
30
+ If a merge resolution program is not specified, 'git mergetool'
31
+ will use the configuration variable merge.tool. If the
32
+ configuration variable merge.tool is not set, 'git mergetool'
33
+ will pick a suitable default.
34
+
35
+Author
36
+------
37
+Written by Theodore Y Ts'o <tytso@mit.edu>
38
+
39
+Documentation
40
+--------------
41
+Documentation by Theodore Y Ts'o.
42
+
43
+GIT
44
+---
45
+Part of the gitlink:git[7] suite
46
+
Makefile
+1
-1
@@ -179,7 +179,7 @@ SCRIPT_SH = \
179
git-clean.sh git-clone.sh git-commit.sh \
180
git-fetch.sh git-gc.sh \
181
git-ls-remote.sh \
182
- git-merge-one-file.sh git-parse-remote.sh \
182
+ git-merge-one-file.sh git-mergetool.sh git-parse-remote.sh \
183
git-pull.sh git-rebase.sh \
184
git-repack.sh git-request-pull.sh git-reset.sh \
185
git-sh-setup.sh \
git-mergetool.sh
new
+352
@@ -0,0 +1,352 @@
1
+#!/bin/sh
2
+#
3
+# This program resolves merge conflicts in git
4
+#
5
+# Copyright (c) 2006 Theodore Y. Ts'o
6
+#
7
+# This file is licensed under the GPL v2, or a later version
8
+# at the discretion of Junio C Hammano.
9
+#
10
+
11
+USAGE='[--tool=tool] [file to merge] ...'
12
+SUBDIRECTORY_OK=Yes
13
+. git-sh-setup
14
+require_work_tree
15
+
16
+# Returns true if the mode reflects a symlink
17
+function is_symlink () {
18
+ test "$1" = 120000
19
+}
20
+
21
+function local_present () {
22
+ test -n "$local_mode"
23
+}
24
+
25
+function remote_present () {
26
+ test -n "$remote_mode"
27
+}
28
+
29
+function base_present () {
30
+ test -n "$base_mode"
31
+}
32
+
33
+cleanup_temp_files () {
34
+ if test "$1" = --save-backup ; then
35
+ mv -- "$BACKUP" "$path.orig"
36
+ rm -f -- "$LOCAL" "$REMOTE" "$BASE"
37
+ else
38
+ rm -f -- "$LOCAL" "$REMOTE" "$BASE" "$BACKUP"
39
+ fi
40
+}
41
+
42
+function describe_file () {
43
+ mode="$1"
44
+ branch="$2"
45
+ file="$3"
46
+
47
+ echo -n " "
48
+ if test -z "$mode"; then
49
+ echo -n "'$path' was deleted"
50
+ elif is_symlink "$mode" ; then
51
+ echo -n "'$path' is a symlink containing '"
52
+ cat "$file"
53
+ echo -n "'"
54
+ else
55
+ if base_present; then
56
+ echo -n "'$path' was created"
57
+ else
58
+ echo -n "'$path' was modified"
59
+ fi
60
+ fi
61
+ echo " in the $branch branch"
62
+}
63
+
64
+
65
+resolve_symlink_merge () {
66
+ while /bin/true; do
67
+ echo -n "Use (r)emote or (l)ocal, or (a)bort? "
68
+ read ans
69
+ case "$ans" in
70
+ [lL]*)
71
+ git-checkout-index -f --stage=2 -- "$path"
72
+ git-add -- "$path"
73
+ cleanup_temp_files --save-backup
74
+ return
75
+ ;;
76
+ [rR]*)
77
+ git-checkout-index -f --stage=3 -- "$path"
78
+ git-add -- "$path"
79
+ cleanup_temp_files --save-backup
80
+ return
81
+ ;;
82
+ [qQ]*)
83
+ exit 1
84
+ ;;
85
+ esac
86
+ done
87
+}
88
+
89
+resolve_deleted_merge () {
90
+ while /bin/true; do
91
+ echo -n "Use (m)odified or (d)eleted file, or (a)bort? "
92
+ read ans
93
+ case "$ans" in
94
+ [mM]*)
95
+ git-add -- "$path"
96
+ cleanup_temp_files --save-backup
97
+ return
98
+ ;;
99
+ [dD]*)
100
+ git-rm -- "$path"
101
+ cleanup_temp_files
102
+ return
103
+ ;;
104
+ [qQ]*)
105
+ exit 1
106
+ ;;
107
+ esac
108
+ done
109
+}
110
+
111
+merge_file () {
112
+ path="$1"
113
+
114
+ if test ! -f "$path" ; then
115
+ echo "$path: file not found"
116
+ exit 1
117
+ fi
118
+
119
+ f=`git-ls-files -u -- "$path"`
120
+ if test -z "$f" ; then
121
+ echo "$path: file does not need merging"
122
+ exit 1
123
+ fi
124
+
125
+ BACKUP="$path.BACKUP.$$"
126
+ LOCAL="$path.LOCAL.$$"
127
+ REMOTE="$path.REMOTE.$$"
128
+ BASE="$path.BASE.$$"
129
+
130
+ mv -- "$path" "$BACKUP"
131
+ cp -- "$BACKUP" "$path"
132
+
133
+ base_mode=`git ls-files -u -- "$path" | awk '{if ($3==1) print $1;}'`
134
+ local_mode=`git ls-files -u -- "$path" | awk '{if ($3==2) print $1;}'`
135
+ remote_mode=`git ls-files -u -- "$path" | awk '{if ($3==3) print $1;}'`
136
+
137
+ base_present && git cat-file blob ":1:$path" > "$BASE" 2>/dev/null
138
+ local_present && git cat-file blob ":2:$path" > "$LOCAL" 2>/dev/null
139
+ remote_present && git cat-file blob ":3:$path" > "$REMOTE" 2>/dev/null
140
+
141
+ if test -z "$local_mode" -o -z "$remote_mode"; then
142
+ echo "Deleted merge conflict for $path:"
143
+ describe_file "$local_mode" "local" "$LOCAL"
144
+ describe_file "$remote_mode" "remote" "$REMOTE"
145
+ resolve_deleted_merge
146
+ return
147
+ fi
148
+
149
+ if is_symlink "$local_mode" || is_symlink "$remote_mode"; then
150
+ echo "Symlink merge conflict for $path:"
151
+ describe_file "$local_mode" "local" "$LOCAL"
152
+ describe_file "$remote_mode" "remote" "$REMOTE"
153
+ resolve_symlink_merge
154
+ return
155
+ fi
156
+
157
+ echo "Normal merge conflict for $path:"
158
+ describe_file "$local_mode" "local" "$LOCAL"
159
+ describe_file "$remote_mode" "remote" "$REMOTE"
160
+ echo -n "Hit return to start merge resolution tool ($merge_tool): "
161
+ read ans
162
+
163
+ case "$merge_tool" in
164
+ kdiff3)
165
+ if base_present ; then
166
+ (kdiff3 --auto --L1 "$path (Base)" -L2 "$path (Local)" --L3 "$path (Remote)" \
167
+ -o "$path" -- "$BASE" "$LOCAL" "$REMOTE" > /dev/null 2>&1)
168
+ else
169
+ (kdiff3 --auto -L1 "$path (Local)" --L2 "$path (Remote)" \
170
+ -o "$path" -- "$LOCAL" "$REMOTE" > /dev/null 2>&1)
171
+ fi
172
+ status=$?
173
+ if test "$status" -eq 0; then
174
+ rm "$BACKUP"
175
+ fi
176
+ ;;
177
+ tkdiff)
178
+ if base_present ; then
179
+ tkdiff -a "$BASE" -o "$path" -- "$LOCAL" "$REMOTE"
180
+ else
181
+ tkdiff -o "$path" -- "$LOCAL" "$REMOTE"
182
+ fi
183
+ status=$?
184
+ if test "$status" -eq 0; then
185
+ mv -- "$BACKUP" "$path.orig"
186
+ fi
187
+ ;;
188
+ meld)
189
+ touch "$BACKUP"
190
+ meld -- "$LOCAL" "$path" "$REMOTE"
191
+ if test "$path" -nt "$BACKUP" ; then
192
+ status=0;
193
+ else
194
+ while true; do
195
+ echo "$path seems unchanged."
196
+ echo -n "Was the merge successful? [y/n] "
197
+ read answer < /dev/tty
198
+ case "$answer" in
199
+ y*|Y*) status=0; break ;;
200
+ n*|N*) status=1; break ;;
201
+ esac
202
+ done
203
+ fi
204
+ if test "$status" -eq 0; then
205
+ mv -- "$BACKUP" "$path.orig"
206
+ fi
207
+ ;;
208
+ xxdiff)
209
+ touch "$BACKUP"
210
+ if base_present ; then
211
+ xxdiff -X --show-merged-pane \
212
+ -R 'Accel.SaveAsMerged: "Ctrl-S"' \
213
+ -R 'Accel.Search: "Ctrl+F"' \
214
+ -R 'Accel.SearchForward: "Ctrl-G"' \
215
+ --merged-file "$path" -- "$LOCAL" "$BASE" "$REMOTE"
216
+ else
217
+ xxdiff -X --show-merged-pane \
218
+ -R 'Accel.SaveAsMerged: "Ctrl-S"' \
219
+ -R 'Accel.Search: "Ctrl+F"' \
220
+ -R 'Accel.SearchForward: "Ctrl-G"' \
221
+ --merged-file "$path" -- "$LOCAL" "$REMOTE"
222
+ fi
223
+ if test "$path" -nt "$BACKUP" ; then
224
+ status=0;
225
+ else
226
+ while true; do
227
+ echo "$path seems unchanged."
228
+ echo -n "Was the merge successful? [y/n] "
229
+ read answer < /dev/tty
230
+ case "$answer" in
231
+ y*|Y*) status=0; break ;;
232
+ n*|N*) status=1; break ;;
233
+ esac
234
+ done
235
+ fi
236
+ if test "$status" -eq 0; then
237
+ mv -- "$BACKUP" "$path.orig"
238
+ fi
239
+ ;;
240
+ emerge)
241
+ if base_present ; then
242
+ emacs -f emerge-files-with-ancestor-command "$LOCAL" "$REMOTE" "$BASE" "$path"
243
+ else
244
+ emacs -f emerge-files-command "$LOCAL" "$REMOTE" "$path"
245
+ fi
246
+ status=$?
247
+ if test "$status" -eq 0; then
248
+ mv -- "$BACKUP" "$path.orig"
249
+ fi
250
+ ;;
251
+ esac
252
+ if test "$status" -ne 0; then
253
+ echo "merge of $path failed" 1>&2
254
+ mv -- "$BACKUP" "$path"
255
+ exit 1
256
+ fi
257
+ git add -- "$path"
258
+ cleanup_temp_files
259
+}
260
+
261
+while case $# in 0) break ;; esac
262
+do
263
+ case "$1" in
264
+ -t|--tool*)
265
+ case "$#,$1" in
266
+ *,*=*)
267
+ merge_tool=`expr "z$1" : 'z-[^=]*=\(.*\)'`
268
+ ;;
269
+ 1,*)
270
+ usage ;;
271
+ *)
272
+ merge_tool="$2"
273
+ shift ;;
274
+ esac
275
+ ;;
276
+ --)
277
+ break
278
+ ;;
279
+ -*)
280
+ usage
281
+ ;;
282
+ *)
283
+ break
284
+ ;;
285
+ esac
286
+ shift
287
+done
288
+
289
+if test -z "$merge_tool"; then
290
+ merge_tool=`git-config merge.tool`
291
+ if test $merge_tool = kdiff3 -o $merge_tool = tkdiff -o \
292
+ $merge_tool = xxdiff -o $merge_tool = meld ; then
293
+ unset merge_tool
294
+ fi
295
+fi
296
+
297
+if test -z "$merge_tool" ; then
298
+ if type kdiff3 >/dev/null 2>&1 && test -n "$DISPLAY"; then
299
+ merge_tool="kdiff3";
300
+ elif type tkdiff >/dev/null 2>&1 && test -n "$DISPLAY"; then
301
+ merge_tool=tkdiff
302
+ elif type xxdiff >/dev/null 2>&1 && test -n "$DISPLAY"; then
303
+ merge_tool=xxdiff
304
+ elif type meld >/dev/null 2>&1 && test -n "$DISPLAY"; then
305
+ merge_tool=meld
306
+ elif type emacs >/dev/null 2>&1; then
307
+ merge_tool=emerge
308
+ else
309
+ echo "No available merge resolution programs available."
310
+ exit 1
311
+ fi
312
+fi
313
+
314
+case "$merge_tool" in
315
+ kdiff3|tkdiff|meld|xxdiff)
316
+ if ! type "$merge_tool" > /dev/null 2>&1; then
317
+ echo "The merge tool $merge_tool is not available"
318
+ exit 1
319
+ fi
320
+ ;;
321
+ emerge)
322
+ if ! type "emacs" > /dev/null 2>&1; then
323
+ echo "Emacs is not available"
324
+ exit 1
325
+ fi
326
+ ;;
327
+ *)
328
+ echo "Unknown merge tool: $merge_tool"
329
+ exit 1
330
+ ;;
331
+esac
332
+
333
+if test $# -eq 0 ; then
334
+ files=`git ls-files -u | sed -e 's/^[^ ]* //' | sort -u`
335
+ if test -z "$files" ; then
336
+ echo "No files need merging"
337
+ exit 0
338
+ fi
339
+ echo Merging the files: $files
340
+ git ls-files -u | sed -e 's/^[^ ]* //' | sort -u | while read i
341
+ do
342
+ echo ""
343
+ merge_file "$i" < /dev/tty > /dev/tty
344
+ done
345
+else
346
+ while test $# -gt 0; do
347
+ echo ""
348
+ merge_file "$1"
349
+ shift
350
+ done
351
+fi
352
+exit 0