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 0 { # Stage without confirmation, to minimize
9 # disruption of the rerere workflow
10 merge_add_resolution
$current_diff_path
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
}]
38 [mc
"Adding resolution for %s" [short_path
$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
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
{}} {
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]
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"]
123 if {![file exists
$current_diff_path]} {
124 error_popup
[mc
"Conflict file does not exist"]
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
{}} {
135 emerge
{ set merge_tool_path
"emacs" }
136 araxis
{ set merge_tool_path
"compare" }
137 default { set merge_tool_path
$tool }
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
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"]
161 set cmdline
[list "$merge_tool_path" --auto --L1 "$MERGED (Local)" \
162 --L2 "$MERGED (Remote)" -o "$MERGED" "$LOCAL" "$REMOTE"]
166 if {$base_stage ne
{}} {
167 set cmdline
[list "$merge_tool_path" -a "$BASE" -o "$MERGED" "$LOCAL" "$REMOTE"]
169 set cmdline
[list "$merge_tool_path" -o "$MERGED" "$LOCAL" "$REMOTE"]
173 set cmdline
[list "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"]
176 set cmdline
[list "$merge_tool_path" -f "$LOCAL" "$MERGED" "$REMOTE"]
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"]
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"]
194 if {$base_stage ne
{}} {
195 set cmdline
[list "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"]
197 set cmdline
[list "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED"]
201 if {$base_stage ne
{}} {
202 set cmdline
[list "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --default --mode=merge3
--to="$MERGED"]
204 set cmdline
[list "$merge_tool_path" "$LOCAL" "$REMOTE" --default --mode=merge2
--to="$MERGED"]
208 if {$base_stage ne
{}} {
209 set cmdline
[list "$merge_tool_path" -f emerge-files-with-ancestor-command
\
210 "$LOCAL" "$REMOTE" "$BASE" "$basename"]
212 set cmdline
[list "$merge_tool_path" -f emerge-files-command
\
213 "$LOCAL" "$REMOTE" "$basename"]
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"]
222 set cmdline
[list "$merge_tool_path" -e -ub -wl \
223 -dl "Theirs File" -dr "Mine File" "$REMOTE" "$LOCAL" "$MERGED"]
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"]
233 set cmdline
[list "$merge_tool_path" -wait -2 \
234 -title1:"'$MERGED (Local)'" -title2:"'$MERGED (Remote)'" \
235 "$LOCAL" "$REMOTE" "$MERGED"]
239 set cmdline
[list "$merge_tool_path" "$BASE" "$REMOTE" "$LOCAL" "$MERGED"]
242 error_popup
[mc
"Not a GUI merge tool: '%s'" $tool]
246 error_popup
[mc
"Unsupported merge tool '%s'" $tool]
251 merge_tool_start
$cmdline $MERGED $BACKUP [list $BASE $LOCAL $REMOTE]
254 proc delete_temp_files
{files
} {
255 foreach fname
$files {
260 proc merge_tool_get_stages
{target stages
} {
264 foreach fname
$stages {
265 if {$merge_stages($i) eq
{}} {
267 catch { close [open $fname w
] }
269 # A hack to support autocrlf properly
270 git checkout-index
-f --stage=$i -- $target
271 file rename -force -- $target $fname
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 }
286 set old_backup
[lindex $mtool_tmpfiles end
]
287 file rename -force -- $old_backup $mtool_target
288 delete_temp_files
$mtool_tmpfiles
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]
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]
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
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
]
349 # Check the return code
350 if {[catch {close $fd} err
]} {
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
}} {
367 file rename -force -- $backup $mtool_target
368 delete_temp_files
$mtool_tmpfiles
369 ui_status
[mc
"Merge tool failed."]
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