mergetool--lib: Sort tools alphabetically for easier lookup
[git/git.git] / lib / mergetool.tcl
CommitLineData
042c2325
AG
1# git-gui merge conflict resolution
2# parts based on git-mergetool (c) 2006 Theodore Y. Ts'o
3
4proc merge_resolve_one {stage} {
5 global current_diff_path
6
7 switch -- $stage {
8b56a18d
CS
8 1 { set targetquestion [mc "Force resolution to the base version?"] }
9 2 { set targetquestion [mc "Force resolution to this branch?"] }
10 3 { set targetquestion [mc "Force resolution to the other branch?"] }
042c2325
AG
11 }
12
8b56a18d
CS
13 set op_question [strcat $targetquestion "\n" \
14[mc "Note that the diff shows only conflicting changes.
042c2325
AG
15
16%s will be overwritten.
17
18This operation can be undone only by restarting the merge." \
8b56a18d 19 [short_path $current_diff_path]]]
042c2325
AG
20
21 if {[ask_popup $op_question] eq {yes}} {
22 merge_load_stages $current_diff_path [list merge_force_stage $stage]
23 }
24}
25
0aea2842 26proc merge_stage_workdir {path {lno {}}} {
3e34838c 27 global current_diff_path diff_active
0aea2842 28 global current_diff_side ui_workdir
3e34838c
AG
29
30 if {$diff_active} return
31
0aea2842
AG
32 if {$path ne $current_diff_path || $ui_workdir ne $current_diff_side} {
33 show_diff $path $ui_workdir $lno {} [list do_merge_stage_workdir $path]
3e34838c
AG
34 } else {
35 do_merge_stage_workdir $path
36 }
37}
38
39proc do_merge_stage_workdir {path} {
40 global current_diff_path is_conflict_diff
41
42 if {$path ne $current_diff_path} return;
43
44 if {$is_conflict_diff} {
45 if {[ask_popup [mc "File %s seems to have unresolved conflicts, still stage?" \
46 [short_path $path]]] ne {yes}} {
47 return
48 }
49 }
50
51 merge_add_resolution $path
52}
53
042c2325 54proc merge_add_resolution {path} {
29853b90 55 global current_diff_path ui_workdir
042c2325 56
29853b90 57 set after [next_diff_after_action $ui_workdir $path {} {^_?U}]
042c2325
AG
58
59 update_index \
60 [mc "Adding resolution for %s" [short_path $path]] \
61 [list $path] \
62 [concat $after [list ui_ready]]
63}
64
65proc merge_force_stage {stage} {
66 global current_diff_path merge_stages
67
68 if {$merge_stages($stage) ne {}} {
69 git checkout-index -f --stage=$stage -- $current_diff_path
70 } else {
71 file delete -- $current_diff_path
72 }
73
74 merge_add_resolution $current_diff_path
75}
76
77proc merge_load_stages {path cont} {
78 global merge_stages_fd merge_stages merge_stages_buf
79
80 if {[info exists merge_stages_fd]} {
81 catch { kill_file_process $merge_stages_fd }
82 catch { close $merge_stages_fd }
83 }
84
85 set merge_stages(0) {}
86 set merge_stages(1) {}
87 set merge_stages(2) {}
88 set merge_stages(3) {}
89 set merge_stages_buf {}
90
e27430e7 91 set merge_stages_fd [eval git_read ls-files -u -z -- {$path}]
042c2325
AG
92
93 fconfigure $merge_stages_fd -blocking 0 -translation binary -encoding binary
94 fileevent $merge_stages_fd readable [list read_merge_stages $merge_stages_fd $cont]
95}
96
97proc read_merge_stages {fd cont} {
98 global merge_stages_buf merge_stages_fd merge_stages
99
100 append merge_stages_buf [read $fd]
101 set pck [split $merge_stages_buf "\0"]
102 set merge_stages_buf [lindex $pck end]
103
104 if {[eof $fd] && $merge_stages_buf ne {}} {
105 lappend pck {}
106 set merge_stages_buf {}
107 }
108
109 foreach p [lrange $pck 0 end-1] {
110 set fcols [split $p "\t"]
111 set cols [split [lindex $fcols 0] " "]
112 set stage [lindex $cols 2]
113
114 set merge_stages($stage) [lrange $cols 0 1]
115 }
116
117 if {[eof $fd]} {
118 close $fd
119 unset merge_stages_fd
120 eval $cont
121 }
122}
7e30682c
AG
123
124proc merge_resolve_tool {} {
125 global current_diff_path
126
127 merge_load_stages $current_diff_path [list merge_resolve_tool2]
128}
129
130proc merge_resolve_tool2 {} {
131 global current_diff_path merge_stages
132
133 # Validate the stages
134 if {$merge_stages(2) eq {} ||
135 [lindex $merge_stages(2) 0] eq {120000} ||
136 [lindex $merge_stages(2) 0] eq {160000} ||
137 $merge_stages(3) eq {} ||
138 [lindex $merge_stages(3) 0] eq {120000} ||
139 [lindex $merge_stages(3) 0] eq {160000}
140 } {
141 error_popup [mc "Cannot resolve deletion or link conflicts using a tool"]
142 return
143 }
144
145 if {![file exists $current_diff_path]} {
146 error_popup [mc "Conflict file does not exist"]
147 return
148 }
149
150 # Determine the tool to use
151 set tool [get_config merge.tool]
152 if {$tool eq {}} { set tool meld }
153
154 set merge_tool_path [get_config "mergetool.$tool.path"]
155 if {$merge_tool_path eq {}} {
156 switch -- $tool {
157 emerge { set merge_tool_path "emacs" }
48c74a58 158 araxis { set merge_tool_path "compare" }
7e30682c
AG
159 default { set merge_tool_path $tool }
160 }
161 }
162
163 # Make file names
164 set filebase [file rootname $current_diff_path]
165 set fileext [file extension $current_diff_path]
166 set basename [lindex [file split $current_diff_path] end]
167
168 set MERGED $current_diff_path
169 set BASE "./$MERGED.BASE$fileext"
170 set LOCAL "./$MERGED.LOCAL$fileext"
171 set REMOTE "./$MERGED.REMOTE$fileext"
172 set BACKUP "./$MERGED.BACKUP$fileext"
173
174 set base_stage $merge_stages(1)
175
176 # Build the command line
177 switch -- $tool {
0e0f4504 178 araxis {
7e30682c 179 if {$base_stage ne {}} {
0e0f4504
SS
180 set cmdline [list "$merge_tool_path" -wait -merge -3 -a1 \
181 -title1:"'$MERGED (Base)'" -title2:"'$MERGED (Local)'" \
182 -title3:"'$MERGED (Remote)'" \
183 "$BASE" "$LOCAL" "$REMOTE" "$MERGED"]
7e30682c 184 } else {
0e0f4504
SS
185 set cmdline [list "$merge_tool_path" -wait -2 \
186 -title1:"'$MERGED (Local)'" -title2:"'$MERGED (Remote)'" \
187 "$LOCAL" "$REMOTE" "$MERGED"]
7e30682c
AG
188 }
189 }
0e0f4504 190 ecmerge {
7e30682c 191 if {$base_stage ne {}} {
0e0f4504 192 set cmdline [list "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --default --mode=merge3 --to="$MERGED"]
7e30682c 193 } else {
0e0f4504 194 set cmdline [list "$merge_tool_path" "$LOCAL" "$REMOTE" --default --mode=merge2 --to="$MERGED"]
7e30682c
AG
195 }
196 }
0e0f4504
SS
197 emerge {
198 if {$base_stage ne {}} {
199 set cmdline [list "$merge_tool_path" -f emerge-files-with-ancestor-command \
200 "$LOCAL" "$REMOTE" "$BASE" "$basename"]
201 } else {
202 set cmdline [list "$merge_tool_path" -f emerge-files-command \
203 "$LOCAL" "$REMOTE" "$basename"]
204 }
7e30682c
AG
205 }
206 gvimdiff {
207 set cmdline [list "$merge_tool_path" -f "$LOCAL" "$MERGED" "$REMOTE"]
208 }
0e0f4504 209 kdiff3 {
7e30682c 210 if {$base_stage ne {}} {
0e0f4504
SS
211 set cmdline [list "$merge_tool_path" --auto --L1 "$MERGED (Base)" \
212 --L2 "$MERGED (Local)" --L3 "$MERGED (Remote)" -o "$MERGED" "$BASE" "$LOCAL" "$REMOTE"]
7e30682c 213 } else {
0e0f4504
SS
214 set cmdline [list "$merge_tool_path" --auto --L1 "$MERGED (Local)" \
215 --L2 "$MERGED (Remote)" -o "$MERGED" "$LOCAL" "$REMOTE"]
7e30682c
AG
216 }
217 }
0e0f4504
SS
218 meld {
219 set cmdline [list "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"]
220 }
7e30682c
AG
221 opendiff {
222 if {$base_stage ne {}} {
223 set cmdline [list "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"]
224 } else {
225 set cmdline [list "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED"]
226 }
227 }
0e0f4504
SS
228 p4merge {
229 set cmdline [list "$merge_tool_path" "$BASE" "$REMOTE" "$LOCAL" "$MERGED"]
7e30682c 230 }
0e0f4504 231 tkdiff {
7e30682c 232 if {$base_stage ne {}} {
0e0f4504 233 set cmdline [list "$merge_tool_path" -a "$BASE" -o "$MERGED" "$LOCAL" "$REMOTE"]
7e30682c 234 } else {
0e0f4504 235 set cmdline [list "$merge_tool_path" -o "$MERGED" "$LOCAL" "$REMOTE"]
7e30682c
AG
236 }
237 }
0e0f4504
SS
238 vimdiff {
239 error_popup [mc "Not a GUI merge tool: '%s'" $tool]
240 return
241 }
48c74a58
AG
242 winmerge {
243 if {$base_stage ne {}} {
244 # This tool does not support 3-way merges.
245 # Use the 'conflict file' resolution feature instead.
246 set cmdline [list "$merge_tool_path" -e -ub "$MERGED"]
247 } else {
248 set cmdline [list "$merge_tool_path" -e -ub -wl \
249 -dl "Theirs File" -dr "Mine File" "$REMOTE" "$LOCAL" "$MERGED"]
250 }
251 }
0e0f4504 252 xxdiff {
48c74a58 253 if {$base_stage ne {}} {
0e0f4504
SS
254 set cmdline [list "$merge_tool_path" -X --show-merged-pane \
255 -R {Accel.SaveAsMerged: "Ctrl-S"} \
256 -R {Accel.Search: "Ctrl+F"} \
257 -R {Accel.SearchForward: "Ctrl-G"} \
258 --merged-file "$MERGED" "$LOCAL" "$BASE" "$REMOTE"]
48c74a58 259 } else {
0e0f4504
SS
260 set cmdline [list "$merge_tool_path" -X --show-merged-pane \
261 -R {Accel.SaveAsMerged: "Ctrl-S"} \
262 -R {Accel.Search: "Ctrl+F"} \
263 -R {Accel.SearchForward: "Ctrl-G"} \
264 --merged-file "$MERGED" "$LOCAL" "$REMOTE"]
48c74a58
AG
265 }
266 }
7e30682c
AG
267 default {
268 error_popup [mc "Unsupported merge tool '%s'" $tool]
269 return
270 }
271 }
272
273 merge_tool_start $cmdline $MERGED $BACKUP [list $BASE $LOCAL $REMOTE]
274}
275
276proc delete_temp_files {files} {
277 foreach fname $files {
278 file delete $fname
279 }
280}
281
282proc merge_tool_get_stages {target stages} {
283 global merge_stages
284
285 set i 1
286 foreach fname $stages {
287 if {$merge_stages($i) eq {}} {
288 file delete $fname
48c74a58 289 catch { close [open $fname w] }
7e30682c
AG
290 } else {
291 # A hack to support autocrlf properly
292 git checkout-index -f --stage=$i -- $target
293 file rename -force -- $target $fname
294 }
295 incr i
296 }
297}
298
299proc merge_tool_start {cmdline target backup stages} {
300 global merge_stages mtool_target mtool_tmpfiles mtool_fd mtool_mtime
301
302 if {[info exists mtool_fd]} {
303 if {[ask_popup [mc "Merge tool is already running, terminate it?"]] eq {yes}} {
304 catch { kill_file_process $mtool_fd }
305 catch { close $mtool_fd }
306 unset mtool_fd
307
308 set old_backup [lindex $mtool_tmpfiles end]
309 file rename -force -- $old_backup $mtool_target
310 delete_temp_files $mtool_tmpfiles
311 } else {
312 return
313 }
314 }
315
316 # Save the original file
317 file rename -force -- $target $backup
318
319 # Get the blobs; it destroys $target
320 if {[catch {merge_tool_get_stages $target $stages} err]} {
321 file rename -force -- $backup $target
322 delete_temp_files $stages
323 error_popup [mc "Error retrieving versions:\n%s" $err]
324 return
325 }
326
327 # Restore the conflict file
328 file copy -force -- $backup $target
329
330 # Initialize global state
331 set mtool_target $target
332 set mtool_mtime [file mtime $target]
333 set mtool_tmpfiles $stages
334
335 lappend mtool_tmpfiles $backup
336
337 # Force redirection to avoid interpreting output on stderr
338 # as an error, and launch the tool
339 lappend cmdline {2>@1}
340
341 if {[catch { set mtool_fd [_open_stdout_stderr $cmdline] } err]} {
342 delete_temp_files $mtool_tmpfiles
343 error_popup [mc "Could not start the merge tool:\n\n%s" $err]
344 return
345 }
346
347 ui_status [mc "Running merge tool..."]
348
349 fconfigure $mtool_fd -blocking 0 -translation binary -encoding binary
350 fileevent $mtool_fd readable [list read_mtool_output $mtool_fd]
351}
352
353proc read_mtool_output {fd} {
354 global mtool_fd mtool_tmpfiles
355
356 read $fd
357 if {[eof $fd]} {
358 unset mtool_fd
359
360 fconfigure $fd -blocking 1
361 merge_tool_finish $fd
362 }
363}
364
365proc merge_tool_finish {fd} {
366 global mtool_tmpfiles mtool_target mtool_mtime
367
368 set backup [lindex $mtool_tmpfiles end]
369 set failed 0
370
371 # Check the return code
372 if {[catch {close $fd} err]} {
373 set failed 1
374 if {$err ne {child process exited abnormally}} {
375 error_popup [strcat [mc "Merge tool failed."] "\n\n$err"]
376 }
377 }
378
7e30682c
AG
379 # Finish
380 if {$failed} {
381 file rename -force -- $backup $mtool_target
382 delete_temp_files $mtool_tmpfiles
383 ui_status [mc "Merge tool failed."]
384 } else {
fb25092a 385 if {[is_config_true mergetool.keepbackup]} {
7e30682c
AG
386 file rename -force -- $backup "$mtool_target.orig"
387 }
388
389 delete_temp_files $mtool_tmpfiles
390
d3bcf55d 391 reshow_diff
7e30682c
AG
392 }
393}