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
{lno
{}}} {
27 global current_diff_path diff_active
28 global current_diff_side ui_workdir
30 if {$diff_active} return
32 if {$path ne
$current_diff_path ||
$ui_workdir ne
$current_diff_side} {
33 show_diff
$path $ui_workdir $lno {} [list do_merge_stage_workdir
$path]
35 do_merge_stage_workdir
$path
39 proc do_merge_stage_workdir
{path
} {
40 global current_diff_path is_conflict_diff
42 if {$path ne
$current_diff_path} return;
44 if {$is_conflict_diff} {
45 if {[ask_popup
[mc
"File %s seems to have unresolved conflicts, still stage?" \
46 [short_path
$path]]] ne
{yes
}} {
51 merge_add_resolution
$path
54 proc merge_add_resolution
{path
} {
55 global current_diff_path ui_workdir
57 set after [next_diff_after_action
$ui_workdir $path {} {^_?U
}]
60 [mc
"Adding resolution for %s" [short_path
$path]] \
62 [concat $after [list ui_ready
]]
65 proc merge_force_stage
{stage
} {
66 global current_diff_path merge_stages
68 if {$merge_stages($stage) ne
{}} {
69 git checkout-index
-f --stage=$stage -- $current_diff_path
71 file delete
-- $current_diff_path
74 merge_add_resolution
$current_diff_path
77 proc merge_load_stages
{path cont
} {
78 global merge_stages_fd merge_stages merge_stages_buf
80 if {[info exists merge_stages_fd
]} {
81 catch { kill_file_process
$merge_stages_fd }
82 catch { close $merge_stages_fd }
85 set merge_stages
(0) {}
86 set merge_stages
(1) {}
87 set merge_stages
(2) {}
88 set merge_stages
(3) {}
89 set merge_stages_buf
{}
91 set merge_stages_fd
[eval git_read ls-files
-u -z -- {$path}]
93 fconfigure $merge_stages_fd -blocking 0 -translation binary -encoding binary
94 fileevent $merge_stages_fd readable
[list read_merge_stages
$merge_stages_fd $cont]
97 proc read_merge_stages
{fd cont
} {
98 global merge_stages_buf merge_stages_fd merge_stages
100 append merge_stages_buf
[read $fd]
101 set pck
[split $merge_stages_buf "\0"]
102 set merge_stages_buf
[lindex $pck end
]
104 if {[eof $fd] && $merge_stages_buf ne
{}} {
106 set merge_stages_buf
{}
109 foreach p
[lrange $pck 0 end-1
] {
110 set fcols
[split $p "\t"]
111 set cols
[split [lindex $fcols 0] " "]
112 set stage
[lindex $cols 2]
114 set merge_stages
($stage) [lrange $cols 0 1]
119 unset merge_stages_fd
124 proc merge_resolve_tool
{} {
125 global current_diff_path
127 merge_load_stages
$current_diff_path [list merge_resolve_tool2
]
130 proc merge_resolve_tool2
{} {
131 global current_diff_path merge_stages
133 # Validate the stages
134 if {$merge_stages(2) eq
{} ||
135 [lindex $merge_stages(2) 0] eq
{120000} ||
136 [lindex $merge_stages(2) 0] eq
{160000} ||
137 $merge_stages(3) eq
{} ||
138 [lindex $merge_stages(3) 0] eq
{120000} ||
139 [lindex $merge_stages(3) 0] eq
{160000}
141 error_popup
[mc
"Cannot resolve deletion or link conflicts using a tool"]
145 if {![file exists
$current_diff_path]} {
146 error_popup
[mc
"Conflict file does not exist"]
150 # Determine the tool to use
151 set tool
[get_config merge.tool
]
152 if {$tool eq
{}} { set tool meld
}
154 set merge_tool_path
[get_config
"mergetool.$tool.path"]
155 if {$merge_tool_path eq
{}} {
157 emerge
{ set merge_tool_path
"emacs" }
158 araxis
{ set merge_tool_path
"compare" }
159 default { set merge_tool_path
$tool }
164 set filebase
[file rootname
$current_diff_path]
165 set fileext
[file extension
$current_diff_path]
166 set basename
[lindex [file split $current_diff_path] end
]
168 set MERGED
$current_diff_path
169 set BASE
"./$MERGED.BASE$fileext"
170 set LOCAL
"./$MERGED.LOCAL$fileext"
171 set REMOTE
"./$MERGED.REMOTE$fileext"
172 set BACKUP
"./$MERGED.BACKUP$fileext"
174 set base_stage
$merge_stages(1)
176 # Build the command line
179 if {$base_stage ne
{}} {
180 set cmdline
[list "$merge_tool_path" -wait -merge -3 -a1 \
181 -title1:"'$MERGED (Base)'" -title2:"'$MERGED (Local)'" \
182 -title3:"'$MERGED (Remote)'" \
183 "$BASE" "$LOCAL" "$REMOTE" "$MERGED"]
185 set cmdline
[list "$merge_tool_path" -wait -2 \
186 -title1:"'$MERGED (Local)'" -title2:"'$MERGED (Remote)'" \
187 "$LOCAL" "$REMOTE" "$MERGED"]
191 if {$base_stage ne
{}} {
192 set cmdline
[list "$merge_tool_path" "$LOCAL" "$REMOTE" "$BASE" "-mergeoutput=$MERGED"]
194 set cmdline
[list "$merge_tool_path" "$LOCAL" "$REMOTE" "-mergeoutput=$MERGED"]
198 if {$base_stage ne
{}} {
199 set cmdline
[list "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --default --mode=merge3
--to="$MERGED"]
201 set cmdline
[list "$merge_tool_path" "$LOCAL" "$REMOTE" --default --mode=merge2
--to="$MERGED"]
205 if {$base_stage ne
{}} {
206 set cmdline
[list "$merge_tool_path" -f emerge-files-with-ancestor-command
\
207 "$LOCAL" "$REMOTE" "$BASE" "$basename"]
209 set cmdline
[list "$merge_tool_path" -f emerge-files-command
\
210 "$LOCAL" "$REMOTE" "$basename"]
214 set cmdline
[list "$merge_tool_path" -f "$LOCAL" "$MERGED" "$REMOTE"]
217 if {$base_stage ne
{}} {
218 set cmdline
[list "$merge_tool_path" --auto --L1 "$MERGED (Base)" \
219 --L2 "$MERGED (Local)" --L3 "$MERGED (Remote)" -o "$MERGED" "$BASE" "$LOCAL" "$REMOTE"]
221 set cmdline
[list "$merge_tool_path" --auto --L1 "$MERGED (Local)" \
222 --L2 "$MERGED (Remote)" -o "$MERGED" "$LOCAL" "$REMOTE"]
226 set cmdline
[list "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"]
229 if {$base_stage ne
{}} {
230 set cmdline
[list "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"]
232 set cmdline
[list "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED"]
236 set cmdline
[list "$merge_tool_path" "$BASE" "$REMOTE" "$LOCAL" "$MERGED"]
239 if {$base_stage ne
{}} {
240 set cmdline
[list "$merge_tool_path" -a "$BASE" -o "$MERGED" "$LOCAL" "$REMOTE"]
242 set cmdline
[list "$merge_tool_path" -o "$MERGED" "$LOCAL" "$REMOTE"]
246 error_popup
[mc
"Not a GUI merge tool: '%s'" $tool]
250 if {$base_stage ne
{}} {
251 # This tool does not support 3-way merges.
252 # Use the 'conflict file' resolution feature instead.
253 set cmdline
[list "$merge_tool_path" -e -ub "$MERGED"]
255 set cmdline
[list "$merge_tool_path" -e -ub -wl \
256 -dl "Theirs File" -dr "Mine File" "$REMOTE" "$LOCAL" "$MERGED"]
260 if {$base_stage ne
{}} {
261 set cmdline
[list "$merge_tool_path" -X --show-merged
-pane
\
262 -R {Accel.SaveAsMerged
: "Ctrl-S"} \
263 -R {Accel.Search
: "Ctrl+F"} \
264 -R {Accel.SearchForward
: "Ctrl-G"} \
265 --merged-file
"$MERGED" "$LOCAL" "$BASE" "$REMOTE"]
267 set cmdline
[list "$merge_tool_path" -X --show-merged
-pane
\
268 -R {Accel.SaveAsMerged
: "Ctrl-S"} \
269 -R {Accel.Search
: "Ctrl+F"} \
270 -R {Accel.SearchForward
: "Ctrl-G"} \
271 --merged-file
"$MERGED" "$LOCAL" "$REMOTE"]
275 error_popup
[mc
"Unsupported merge tool '%s'" $tool]
280 merge_tool_start
$cmdline $MERGED $BACKUP [list $BASE $LOCAL $REMOTE]
283 proc delete_temp_files
{files
} {
284 foreach fname
$files {
289 proc merge_tool_get_stages
{target stages
} {
293 foreach fname
$stages {
294 if {$merge_stages($i) eq
{}} {
296 catch { close [open $fname w
] }
298 # A hack to support autocrlf properly
299 git checkout-index
-f --stage=$i -- $target
300 file rename -force -- $target $fname
306 proc merge_tool_start
{cmdline target backup stages
} {
307 global merge_stages mtool_target mtool_tmpfiles mtool_fd mtool_mtime
309 if {[info exists mtool_fd
]} {
310 if {[ask_popup
[mc
"Merge tool is already running, terminate it?"]] eq
{yes
}} {
311 catch { kill_file_process
$mtool_fd }
312 catch { close $mtool_fd }
315 set old_backup
[lindex $mtool_tmpfiles end
]
316 file rename -force -- $old_backup $mtool_target
317 delete_temp_files
$mtool_tmpfiles
323 # Save the original file
324 file rename -force -- $target $backup
326 # Get the blobs; it destroys $target
327 if {[catch {merge_tool_get_stages
$target $stages} err
]} {
328 file rename -force -- $backup $target
329 delete_temp_files
$stages
330 error_popup
[mc
"Error retrieving versions:\n%s" $err]
334 # Restore the conflict file
335 file copy
-force -- $backup $target
337 # Initialize global state
338 set mtool_target
$target
339 set mtool_mtime
[file mtime
$target]
340 set mtool_tmpfiles
$stages
342 lappend mtool_tmpfiles
$backup
344 # Force redirection to avoid interpreting output on stderr
345 # as an error, and launch the tool
346 lappend cmdline
{2>@1}
348 if {[catch { set mtool_fd
[_open_stdout_stderr
$cmdline] } err
]} {
349 delete_temp_files
$mtool_tmpfiles
350 error_popup
[mc
"Could not start the merge tool:\n\n%s" $err]
354 ui_status
[mc
"Running merge tool..."]
356 fconfigure $mtool_fd -blocking 0 -translation binary -encoding binary
357 fileevent $mtool_fd readable
[list read_mtool_output
$mtool_fd]
360 proc read_mtool_output
{fd
} {
361 global mtool_fd mtool_tmpfiles
367 fconfigure $fd -blocking 1
368 merge_tool_finish
$fd
372 proc merge_tool_finish
{fd
} {
373 global mtool_tmpfiles mtool_target mtool_mtime
375 set backup
[lindex $mtool_tmpfiles end
]
378 # Check the return code
379 if {[catch {close $fd} err
]} {
381 if {$err ne
{child process exited abnormally
}} {
382 error_popup
[strcat
[mc
"Merge tool failed."] "\n\n$err"]
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 mergetool.keepbackup
]} {
393 file rename -force -- $backup "$mtool_target.orig"
396 delete_temp_files
$mtool_tmpfiles