1 # git-gui merge conflict resolution
2 # parts based on git-mergetool (c) 2006 Theodore Y. Ts'o
4 proc merge_resolve_one {stage} {
5 global current_diff_path
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?"] }
13 set op_question [strcat $targetquestion "\n" \
14 [mc "Note that the diff shows only conflicting changes.
16 %s will be overwritten.
18 This operation can be undone only by restarting the merge." \
19 [short_path $current_diff_path]]]
21 if {[ask_popup $op_question] eq {yes}} {
22 merge_load_stages $current_diff_path [list merge_force_stage $stage]
26 proc merge_stage_workdir {path w lno} {
27 global current_diff_path diff_active
29 if {$diff_active} return
31 if {$path ne $current_diff_path} {
32 show_diff $path $w $lno {} [list do_merge_stage_workdir $path]
34 do_merge_stage_workdir $path
38 proc do_merge_stage_workdir {path} {
39 global current_diff_path is_conflict_diff
41 if {$path ne $current_diff_path} return;
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}} {
50 merge_add_resolution $path
53 proc merge_add_resolution {path} {
54 global current_diff_path ui_workdir
56 set after [next_diff_after_action $ui_workdir $path {} {^_?U}]
59 [mc "Adding resolution for %s" [short_path $path]] \
61 [concat $after [list ui_ready]]
64 proc merge_force_stage {stage} {
65 global current_diff_path merge_stages
67 if {$merge_stages($stage) ne {}} {
68 git checkout-index -f --stage=$stage -- $current_diff_path
70 file delete -- $current_diff_path
73 merge_add_resolution $current_diff_path
76 proc merge_load_stages {path cont} {
77 global merge_stages_fd merge_stages merge_stages_buf
79 if {[info exists merge_stages_fd]} {
80 catch { kill_file_process $merge_stages_fd }
81 catch { close $merge_stages_fd }
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 {}
90 set merge_stages_fd [eval git_read ls-files -u -z -- $path]
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]
96 proc read_merge_stages {fd cont} {
97 global merge_stages_buf merge_stages_fd merge_stages
99 append merge_stages_buf [read $fd]
100 set pck [split $merge_stages_buf "\0"]
101 set merge_stages_buf [lindex $pck end]
103 if {[eof $fd] && $merge_stages_buf ne {}} {
105 set merge_stages_buf {}
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]
113 set merge_stages($stage) [lrange $cols 0 1]
118 unset merge_stages_fd
123 proc merge_resolve_tool {} {
124 global current_diff_path
126 merge_load_stages $current_diff_path [list merge_resolve_tool2]
129 proc merge_resolve_tool2 {} {
130 global current_diff_path merge_stages
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}
140 error_popup [mc "Cannot resolve deletion or link conflicts using a tool"]
144 if {![file exists $current_diff_path]} {
145 error_popup [mc "Conflict file does not exist"]
149 # Determine the tool to use
150 set tool [get_config merge.tool]
151 if {$tool eq {}} { set tool meld }
153 set merge_tool_path [get_config "mergetool.$tool.path"]
154 if {$merge_tool_path eq {}} {
156 emerge { set merge_tool_path "emacs" }
157 araxis { set merge_tool_path "compare" }
158 default { set merge_tool_path $tool }
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]
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"
173 set base_stage $merge_stages(1)
175 # Build the command line
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"]
182 set cmdline [list "$merge_tool_path" --auto --L1 "$MERGED (Local)" \
183 --L2 "$MERGED (Remote)" -o "$MERGED" "$LOCAL" "$REMOTE"]
187 if {$base_stage ne {}} {
188 set cmdline [list "$merge_tool_path" -a "$BASE" -o "$MERGED" "$LOCAL" "$REMOTE"]
190 set cmdline [list "$merge_tool_path" -o "$MERGED" "$LOCAL" "$REMOTE"]
194 set cmdline [list "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"]
197 set cmdline [list "$merge_tool_path" -f "$LOCAL" "$MERGED" "$REMOTE"]
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"]
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"]
215 if {$base_stage ne {}} {
216 set cmdline [list "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"]
218 set cmdline [list "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED"]
222 if {$base_stage ne {}} {
223 set cmdline [list "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --default --mode=merge3 --to="$MERGED"]
225 set cmdline [list "$merge_tool_path" "$LOCAL" "$REMOTE" --default --mode=merge2 --to="$MERGED"]
229 if {$base_stage ne {}} {
230 set cmdline [list "$merge_tool_path" -f emerge-files-with-ancestor-command \
231 "$LOCAL" "$REMOTE" "$BASE" "$basename"]
233 set cmdline [list "$merge_tool_path" -f emerge-files-command \
234 "$LOCAL" "$REMOTE" "$basename"]
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"]
243 set cmdline [list "$merge_tool_path" -e -ub -wl \
244 -dl "Theirs File" -dr "Mine File" "$REMOTE" "$LOCAL" "$MERGED"]
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"]
254 set cmdline [list "$merge_tool_path" -wait -2 \
255 -title1:"'$MERGED (Local)'" -title2:"'$MERGED (Remote)'" \
256 "$LOCAL" "$REMOTE" "$MERGED"]
260 set cmdline [list "$merge_tool_path" "$BASE" "$REMOTE" "$LOCAL" "$MERGED"]
263 error_popup [mc "Not a GUI merge tool: '%s'" $tool]
267 error_popup [mc "Unsupported merge tool '%s'" $tool]
272 merge_tool_start $cmdline $MERGED $BACKUP [list $BASE $LOCAL $REMOTE]
275 proc delete_temp_files {files} {
276 foreach fname $files {
281 proc merge_tool_get_stages {target stages} {
285 foreach fname $stages {
286 if {$merge_stages($i) eq {}} {
288 catch { close [open $fname w] }
290 # A hack to support autocrlf properly
291 git checkout-index -f --stage=$i -- $target
292 file rename -force -- $target $fname
298 proc merge_tool_start {cmdline target backup stages} {
299 global merge_stages mtool_target mtool_tmpfiles mtool_fd mtool_mtime
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 }
307 set old_backup [lindex $mtool_tmpfiles end]
308 file rename -force -- $old_backup $mtool_target
309 delete_temp_files $mtool_tmpfiles
315 # Save the original file
316 file rename -force -- $target $backup
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]
326 # Restore the conflict file
327 file copy -force -- $backup $target
329 # Initialize global state
330 set mtool_target $target
331 set mtool_mtime [file mtime $target]
332 set mtool_tmpfiles $stages
334 lappend mtool_tmpfiles $backup
336 # Force redirection to avoid interpreting output on stderr
337 # as an error, and launch the tool
338 lappend cmdline {2>@1}
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]
346 ui_status [mc "Running merge tool..."]
348 fconfigure $mtool_fd -blocking 0 -translation binary -encoding binary
349 fileevent $mtool_fd readable [list read_mtool_output $mtool_fd]
352 proc read_mtool_output {fd} {
353 global mtool_fd mtool_tmpfiles
359 fconfigure $fd -blocking 1
360 merge_tool_finish $fd
364 proc merge_tool_finish {fd} {
365 global mtool_tmpfiles mtool_target mtool_mtime
367 set backup [lindex $mtool_tmpfiles end]
370 # Check the return code
371 if {[catch {close $fd} err]} {
373 if {$err ne {child process exited abnormally}} {
374 error_popup [strcat [mc "Merge tool failed."] "\n\n$err"]
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}} {
388 file rename -force -- $backup $mtool_target
389 delete_temp_files $mtool_tmpfiles
390 ui_status [mc "Merge tool failed."]
392 if {[is_config_true merge.keepbackup]} {
393 file rename -force -- $backup "$mtool_target.orig"
396 delete_temp_files $mtool_tmpfiles
398 merge_add_resolution $mtool_target