git-gui: I18n fix sentence parts into full sentences for translation again.
[git/mingw/4msysgit/wingit-dll.git] / lib / mergetool.tcl
bloba44a7258bccc5bffea21b470eb297b02f0ad97af
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 0 { # Stage without confirmation, to minimize
9 # disruption of the rerere workflow
10 merge_add_resolution $current_diff_path
11 return
14 1 { set targetquestion [mc "Force resolution to the base version?"] }
15 2 { set targetquestion [mc "Force resolution to this branch?"] }
16 3 { set targetquestion [mc "Force resolution to the other branch?"] }
19 set op_question [strcat $targetquestion "\n" \
20 [mc "Note that the diff shows only conflicting changes.
22 %s will be overwritten.
24 This operation can be undone only by restarting the merge." \
25 [short_path $current_diff_path]]]
27 if {[ask_popup $op_question] eq {yes}} {
28 merge_load_stages $current_diff_path [list merge_force_stage $stage]
32 proc merge_add_resolution {path} {
33 global current_diff_path ui_workdir
35 set after [next_diff_after_action $ui_workdir $path {} {^_?U}]
37 update_index \
38 [mc "Adding resolution for %s" [short_path $path]] \
39 [list $path] \
40 [concat $after [list ui_ready]]
43 proc merge_force_stage {stage} {
44 global current_diff_path merge_stages
46 if {$merge_stages($stage) ne {}} {
47 git checkout-index -f --stage=$stage -- $current_diff_path
48 } else {
49 file delete -- $current_diff_path
52 merge_add_resolution $current_diff_path
55 proc merge_load_stages {path cont} {
56 global merge_stages_fd merge_stages merge_stages_buf
58 if {[info exists merge_stages_fd]} {
59 catch { kill_file_process $merge_stages_fd }
60 catch { close $merge_stages_fd }
63 set merge_stages(0) {}
64 set merge_stages(1) {}
65 set merge_stages(2) {}
66 set merge_stages(3) {}
67 set merge_stages_buf {}
69 set merge_stages_fd [eval git_read ls-files -u -z -- $path]
71 fconfigure $merge_stages_fd -blocking 0 -translation binary -encoding binary
72 fileevent $merge_stages_fd readable [list read_merge_stages $merge_stages_fd $cont]
75 proc read_merge_stages {fd cont} {
76 global merge_stages_buf merge_stages_fd merge_stages
78 append merge_stages_buf [read $fd]
79 set pck [split $merge_stages_buf "\0"]
80 set merge_stages_buf [lindex $pck end]
82 if {[eof $fd] && $merge_stages_buf ne {}} {
83 lappend pck {}
84 set merge_stages_buf {}
87 foreach p [lrange $pck 0 end-1] {
88 set fcols [split $p "\t"]
89 set cols [split [lindex $fcols 0] " "]
90 set stage [lindex $cols 2]
92 set merge_stages($stage) [lrange $cols 0 1]
95 if {[eof $fd]} {
96 close $fd
97 unset merge_stages_fd
98 eval $cont
102 proc merge_resolve_tool {} {
103 global current_diff_path
105 merge_load_stages $current_diff_path [list merge_resolve_tool2]
108 proc merge_resolve_tool2 {} {
109 global current_diff_path merge_stages
111 # Validate the stages
112 if {$merge_stages(2) eq {} ||
113 [lindex $merge_stages(2) 0] eq {120000} ||
114 [lindex $merge_stages(2) 0] eq {160000} ||
115 $merge_stages(3) eq {} ||
116 [lindex $merge_stages(3) 0] eq {120000} ||
117 [lindex $merge_stages(3) 0] eq {160000}
119 error_popup [mc "Cannot resolve deletion or link conflicts using a tool"]
120 return
123 if {![file exists $current_diff_path]} {
124 error_popup [mc "Conflict file does not exist"]
125 return
128 # Determine the tool to use
129 set tool [get_config merge.tool]
130 if {$tool eq {}} { set tool meld }
132 set merge_tool_path [get_config "mergetool.$tool.path"]
133 if {$merge_tool_path eq {}} {
134 switch -- $tool {
135 emerge { set merge_tool_path "emacs" }
136 araxis { set merge_tool_path "compare" }
137 default { set merge_tool_path $tool }
141 # Make file names
142 set filebase [file rootname $current_diff_path]
143 set fileext [file extension $current_diff_path]
144 set basename [lindex [file split $current_diff_path] end]
146 set MERGED $current_diff_path
147 set BASE "./$MERGED.BASE$fileext"
148 set LOCAL "./$MERGED.LOCAL$fileext"
149 set REMOTE "./$MERGED.REMOTE$fileext"
150 set BACKUP "./$MERGED.BACKUP$fileext"
152 set base_stage $merge_stages(1)
154 # Build the command line
155 switch -- $tool {
156 kdiff3 {
157 if {$base_stage ne {}} {
158 set cmdline [list "$merge_tool_path" --auto --L1 "$MERGED (Base)" \
159 --L2 "$MERGED (Local)" --L3 "$MERGED (Remote)" -o "$MERGED" "$BASE" "$LOCAL" "$REMOTE"]
160 } else {
161 set cmdline [list "$merge_tool_path" --auto --L1 "$MERGED (Local)" \
162 --L2 "$MERGED (Remote)" -o "$MERGED" "$LOCAL" "$REMOTE"]
165 tkdiff {
166 if {$base_stage ne {}} {
167 set cmdline [list "$merge_tool_path" -a "$BASE" -o "$MERGED" "$LOCAL" "$REMOTE"]
168 } else {
169 set cmdline [list "$merge_tool_path" -o "$MERGED" "$LOCAL" "$REMOTE"]
172 meld {
173 set cmdline [list "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"]
175 gvimdiff {
176 set cmdline [list "$merge_tool_path" -f "$LOCAL" "$MERGED" "$REMOTE"]
178 xxdiff {
179 if {$base_stage ne {}} {
180 set cmdline [list "$merge_tool_path" -X --show-merged-pane \
181 -R {Accel.SaveAsMerged: "Ctrl-S"} \
182 -R {Accel.Search: "Ctrl+F"} \
183 -R {Accel.SearchForward: "Ctrl-G"} \
184 --merged-file "$MERGED" "$LOCAL" "$BASE" "$REMOTE"]
185 } else {
186 set cmdline [list "$merge_tool_path" -X --show-merged-pane \
187 -R {Accel.SaveAsMerged: "Ctrl-S"} \
188 -R {Accel.Search: "Ctrl+F"} \
189 -R {Accel.SearchForward: "Ctrl-G"} \
190 --merged-file "$MERGED" "$LOCAL" "$REMOTE"]
193 opendiff {
194 if {$base_stage ne {}} {
195 set cmdline [list "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"]
196 } else {
197 set cmdline [list "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED"]
200 ecmerge {
201 if {$base_stage ne {}} {
202 set cmdline [list "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --default --mode=merge3 --to="$MERGED"]
203 } else {
204 set cmdline [list "$merge_tool_path" "$LOCAL" "$REMOTE" --default --mode=merge2 --to="$MERGED"]
207 emerge {
208 if {$base_stage ne {}} {
209 set cmdline [list "$merge_tool_path" -f emerge-files-with-ancestor-command \
210 "$LOCAL" "$REMOTE" "$BASE" "$basename"]
211 } else {
212 set cmdline [list "$merge_tool_path" -f emerge-files-command \
213 "$LOCAL" "$REMOTE" "$basename"]
216 winmerge {
217 if {$base_stage ne {}} {
218 # This tool does not support 3-way merges.
219 # Use the 'conflict file' resolution feature instead.
220 set cmdline [list "$merge_tool_path" -e -ub "$MERGED"]
221 } else {
222 set cmdline [list "$merge_tool_path" -e -ub -wl \
223 -dl "Theirs File" -dr "Mine File" "$REMOTE" "$LOCAL" "$MERGED"]
226 araxis {
227 if {$base_stage ne {}} {
228 set cmdline [list "$merge_tool_path" -wait -merge -3 -a1 \
229 -title1:"'$MERGED (Base)'" -title2:"'$MERGED (Local)'" \
230 -title3:"'$MERGED (Remote)'" \
231 "$BASE" "$LOCAL" "$REMOTE" "$MERGED"]
232 } else {
233 set cmdline [list "$merge_tool_path" -wait -2 \
234 -title1:"'$MERGED (Local)'" -title2:"'$MERGED (Remote)'" \
235 "$LOCAL" "$REMOTE" "$MERGED"]
238 p4merge {
239 set cmdline [list "$merge_tool_path" "$BASE" "$REMOTE" "$LOCAL" "$MERGED"]
241 vimdiff {
242 error_popup [mc "Not a GUI merge tool: '%s'" $tool]
243 return
245 default {
246 error_popup [mc "Unsupported merge tool '%s'" $tool]
247 return
251 merge_tool_start $cmdline $MERGED $BACKUP [list $BASE $LOCAL $REMOTE]
254 proc delete_temp_files {files} {
255 foreach fname $files {
256 file delete $fname
260 proc merge_tool_get_stages {target stages} {
261 global merge_stages
263 set i 1
264 foreach fname $stages {
265 if {$merge_stages($i) eq {}} {
266 file delete $fname
267 catch { close [open $fname w] }
268 } else {
269 # A hack to support autocrlf properly
270 git checkout-index -f --stage=$i -- $target
271 file rename -force -- $target $fname
273 incr i
277 proc merge_tool_start {cmdline target backup stages} {
278 global merge_stages mtool_target mtool_tmpfiles mtool_fd mtool_mtime
280 if {[info exists mtool_fd]} {
281 if {[ask_popup [mc "Merge tool is already running, terminate it?"]] eq {yes}} {
282 catch { kill_file_process $mtool_fd }
283 catch { close $mtool_fd }
284 unset mtool_fd
286 set old_backup [lindex $mtool_tmpfiles end]
287 file rename -force -- $old_backup $mtool_target
288 delete_temp_files $mtool_tmpfiles
289 } else {
290 return
294 # Save the original file
295 file rename -force -- $target $backup
297 # Get the blobs; it destroys $target
298 if {[catch {merge_tool_get_stages $target $stages} err]} {
299 file rename -force -- $backup $target
300 delete_temp_files $stages
301 error_popup [mc "Error retrieving versions:\n%s" $err]
302 return
305 # Restore the conflict file
306 file copy -force -- $backup $target
308 # Initialize global state
309 set mtool_target $target
310 set mtool_mtime [file mtime $target]
311 set mtool_tmpfiles $stages
313 lappend mtool_tmpfiles $backup
315 # Force redirection to avoid interpreting output on stderr
316 # as an error, and launch the tool
317 lappend cmdline {2>@1}
319 if {[catch { set mtool_fd [_open_stdout_stderr $cmdline] } err]} {
320 delete_temp_files $mtool_tmpfiles
321 error_popup [mc "Could not start the merge tool:\n\n%s" $err]
322 return
325 ui_status [mc "Running merge tool..."]
327 fconfigure $mtool_fd -blocking 0 -translation binary -encoding binary
328 fileevent $mtool_fd readable [list read_mtool_output $mtool_fd]
331 proc read_mtool_output {fd} {
332 global mtool_fd mtool_tmpfiles
334 read $fd
335 if {[eof $fd]} {
336 unset mtool_fd
338 fconfigure $fd -blocking 1
339 merge_tool_finish $fd
343 proc merge_tool_finish {fd} {
344 global mtool_tmpfiles mtool_target mtool_mtime
346 set backup [lindex $mtool_tmpfiles end]
347 set failed 0
349 # Check the return code
350 if {[catch {close $fd} err]} {
351 set failed 1
352 if {$err ne {child process exited abnormally}} {
353 error_popup [strcat [mc "Merge tool failed."] "\n\n$err"]
357 # Check the modification time of the target file
358 if {!$failed && [file mtime $mtool_target] eq $mtool_mtime} {
359 if {[ask_popup [mc "File %s unchanged, still accept as resolved?" \
360 [short_path $mtool_target]]] ne {yes}} {
361 set failed 1
365 # Finish
366 if {$failed} {
367 file rename -force -- $backup $mtool_target
368 delete_temp_files $mtool_tmpfiles
369 ui_status [mc "Merge tool failed."]
370 } else {
371 if {[is_config_true merge.keepbackup]} {
372 file rename -force -- $backup "$mtool_target.orig"
375 delete_temp_files $mtool_tmpfiles
377 merge_add_resolution $mtool_target