git-gui: Reenable staging unmerged files by clicking the icon.
[git/mjg.git] / lib / mergetool.tcl
blob6ab5701d937b9a719570907e745609b60d1d56a8
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
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?"] }
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]
33 } else {
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}} {
46 return
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}]
58 update_index \
59 [mc "Adding resolution for %s" [short_path $path]] \
60 [list $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
69 } else {
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 {}} {
104 lappend pck {}
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]
116 if {[eof $fd]} {
117 close $fd
118 unset merge_stages_fd
119 eval $cont
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"]
141 return
144 if {![file exists $current_diff_path]} {
145 error_popup [mc "Conflict file does not exist"]
146 return
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 {}} {
155 switch -- $tool {
156 emerge { set merge_tool_path "emacs" }
157 araxis { set merge_tool_path "compare" }
158 default { set merge_tool_path $tool }
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]
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
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"]
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"]
193 meld {
194 set cmdline [list "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"]
196 gvimdiff {
197 set cmdline [list "$merge_tool_path" -f "$LOCAL" "$MERGED" "$REMOTE"]
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"]
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"]
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"]
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"]
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"]
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"]
259 p4merge {
260 set cmdline [list "$merge_tool_path" "$BASE" "$REMOTE" "$LOCAL" "$MERGED"]
262 vimdiff {
263 error_popup [mc "Not a GUI merge tool: '%s'" $tool]
264 return
266 default {
267 error_popup [mc "Unsupported merge tool '%s'" $tool]
268 return
272 merge_tool_start $cmdline $MERGED $BACKUP [list $BASE $LOCAL $REMOTE]
275 proc delete_temp_files {files} {
276 foreach fname $files {
277 file delete $fname
281 proc merge_tool_get_stages {target stages} {
282 global merge_stages
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
294 incr i
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 }
305 unset mtool_fd
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
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]
323 return
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]
343 return
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
355 read $fd
356 if {[eof $fd]} {
357 unset mtool_fd
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]
368 set failed 0
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"]
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
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"
396 delete_temp_files $mtool_tmpfiles
398 merge_add_resolution $mtool_target