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