2008-08-30 22:55:45 +02:00
|
|
|
# git-gui merge conflict resolution
|
|
|
|
# parts based on git-mergetool (c) 2006 Theodore Y. Ts'o
|
|
|
|
|
|
|
|
proc merge_resolve_one {stage} {
|
|
|
|
global current_diff_path
|
|
|
|
|
|
|
|
switch -- $stage {
|
2008-09-12 11:17:38 +02:00
|
|
|
1 { set targetquestion [mc "Force resolution to the base version?"] }
|
|
|
|
2 { set targetquestion [mc "Force resolution to this branch?"] }
|
|
|
|
3 { set targetquestion [mc "Force resolution to the other branch?"] }
|
2008-08-30 22:55:45 +02:00
|
|
|
}
|
|
|
|
|
2008-09-12 11:17:38 +02:00
|
|
|
set op_question [strcat $targetquestion "\n" \
|
|
|
|
[mc "Note that the diff shows only conflicting changes.
|
2008-08-30 22:55:45 +02:00
|
|
|
|
|
|
|
%s will be overwritten.
|
|
|
|
|
|
|
|
This operation can be undone only by restarting the merge." \
|
2008-09-12 11:17:38 +02:00
|
|
|
[short_path $current_diff_path]]]
|
2008-08-30 22:55:45 +02:00
|
|
|
|
|
|
|
if {[ask_popup $op_question] eq {yes}} {
|
|
|
|
merge_load_stages $current_diff_path [list merge_force_stage $stage]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-30 10:12:16 +02:00
|
|
|
proc merge_stage_workdir {path {lno {}}} {
|
2008-09-20 10:19:18 +02:00
|
|
|
global current_diff_path diff_active
|
2008-09-30 10:12:16 +02:00
|
|
|
global current_diff_side ui_workdir
|
2008-09-20 10:19:18 +02:00
|
|
|
|
|
|
|
if {$diff_active} return
|
|
|
|
|
2008-09-30 10:12:16 +02:00
|
|
|
if {$path ne $current_diff_path || $ui_workdir ne $current_diff_side} {
|
|
|
|
show_diff $path $ui_workdir $lno {} [list do_merge_stage_workdir $path]
|
2008-09-20 10:19:18 +02:00
|
|
|
} else {
|
|
|
|
do_merge_stage_workdir $path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc do_merge_stage_workdir {path} {
|
|
|
|
global current_diff_path is_conflict_diff
|
|
|
|
|
|
|
|
if {$path ne $current_diff_path} return;
|
|
|
|
|
|
|
|
if {$is_conflict_diff} {
|
|
|
|
if {[ask_popup [mc "File %s seems to have unresolved conflicts, still stage?" \
|
|
|
|
[short_path $path]]] ne {yes}} {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
merge_add_resolution $path
|
|
|
|
}
|
|
|
|
|
2008-08-30 22:55:45 +02:00
|
|
|
proc merge_add_resolution {path} {
|
2008-08-30 23:02:56 +02:00
|
|
|
global current_diff_path ui_workdir
|
2008-08-30 22:55:45 +02:00
|
|
|
|
2008-08-30 23:02:56 +02:00
|
|
|
set after [next_diff_after_action $ui_workdir $path {} {^_?U}]
|
2008-08-30 22:55:45 +02:00
|
|
|
|
|
|
|
update_index \
|
|
|
|
[mc "Adding resolution for %s" [short_path $path]] \
|
|
|
|
[list $path] \
|
|
|
|
[concat $after [list ui_ready]]
|
|
|
|
}
|
|
|
|
|
|
|
|
proc merge_force_stage {stage} {
|
|
|
|
global current_diff_path merge_stages
|
|
|
|
|
|
|
|
if {$merge_stages($stage) ne {}} {
|
|
|
|
git checkout-index -f --stage=$stage -- $current_diff_path
|
|
|
|
} else {
|
|
|
|
file delete -- $current_diff_path
|
|
|
|
}
|
|
|
|
|
|
|
|
merge_add_resolution $current_diff_path
|
|
|
|
}
|
|
|
|
|
|
|
|
proc merge_load_stages {path cont} {
|
|
|
|
global merge_stages_fd merge_stages merge_stages_buf
|
|
|
|
|
|
|
|
if {[info exists merge_stages_fd]} {
|
|
|
|
catch { kill_file_process $merge_stages_fd }
|
|
|
|
catch { close $merge_stages_fd }
|
|
|
|
}
|
|
|
|
|
|
|
|
set merge_stages(0) {}
|
|
|
|
set merge_stages(1) {}
|
|
|
|
set merge_stages(2) {}
|
|
|
|
set merge_stages(3) {}
|
|
|
|
set merge_stages_buf {}
|
|
|
|
|
2009-03-20 08:48:55 +01:00
|
|
|
set merge_stages_fd [eval git_read ls-files -u -z -- {$path}]
|
2008-08-30 22:55:45 +02:00
|
|
|
|
|
|
|
fconfigure $merge_stages_fd -blocking 0 -translation binary -encoding binary
|
|
|
|
fileevent $merge_stages_fd readable [list read_merge_stages $merge_stages_fd $cont]
|
|
|
|
}
|
|
|
|
|
|
|
|
proc read_merge_stages {fd cont} {
|
|
|
|
global merge_stages_buf merge_stages_fd merge_stages
|
|
|
|
|
|
|
|
append merge_stages_buf [read $fd]
|
|
|
|
set pck [split $merge_stages_buf "\0"]
|
|
|
|
set merge_stages_buf [lindex $pck end]
|
|
|
|
|
|
|
|
if {[eof $fd] && $merge_stages_buf ne {}} {
|
|
|
|
lappend pck {}
|
|
|
|
set merge_stages_buf {}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach p [lrange $pck 0 end-1] {
|
|
|
|
set fcols [split $p "\t"]
|
|
|
|
set cols [split [lindex $fcols 0] " "]
|
|
|
|
set stage [lindex $cols 2]
|
|
|
|
|
|
|
|
set merge_stages($stage) [lrange $cols 0 1]
|
|
|
|
}
|
|
|
|
|
|
|
|
if {[eof $fd]} {
|
|
|
|
close $fd
|
|
|
|
unset merge_stages_fd
|
|
|
|
eval $cont
|
|
|
|
}
|
|
|
|
}
|
2008-08-30 22:56:51 +02:00
|
|
|
|
|
|
|
proc merge_resolve_tool {} {
|
|
|
|
global current_diff_path
|
|
|
|
|
|
|
|
merge_load_stages $current_diff_path [list merge_resolve_tool2]
|
|
|
|
}
|
|
|
|
|
|
|
|
proc merge_resolve_tool2 {} {
|
|
|
|
global current_diff_path merge_stages
|
|
|
|
|
|
|
|
# Validate the stages
|
|
|
|
if {$merge_stages(2) eq {} ||
|
|
|
|
[lindex $merge_stages(2) 0] eq {120000} ||
|
|
|
|
[lindex $merge_stages(2) 0] eq {160000} ||
|
|
|
|
$merge_stages(3) eq {} ||
|
|
|
|
[lindex $merge_stages(3) 0] eq {120000} ||
|
|
|
|
[lindex $merge_stages(3) 0] eq {160000}
|
|
|
|
} {
|
|
|
|
error_popup [mc "Cannot resolve deletion or link conflicts using a tool"]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if {![file exists $current_diff_path]} {
|
|
|
|
error_popup [mc "Conflict file does not exist"]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
# Determine the tool to use
|
|
|
|
set tool [get_config merge.tool]
|
|
|
|
if {$tool eq {}} { set tool meld }
|
|
|
|
|
|
|
|
set merge_tool_path [get_config "mergetool.$tool.path"]
|
|
|
|
if {$merge_tool_path eq {}} {
|
|
|
|
switch -- $tool {
|
|
|
|
emerge { set merge_tool_path "emacs" }
|
2008-08-30 22:59:47 +02:00
|
|
|
araxis { set merge_tool_path "compare" }
|
2008-08-30 22:56:51 +02:00
|
|
|
default { set merge_tool_path $tool }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Make file names
|
|
|
|
set filebase [file rootname $current_diff_path]
|
|
|
|
set fileext [file extension $current_diff_path]
|
|
|
|
set basename [lindex [file split $current_diff_path] end]
|
|
|
|
|
|
|
|
set MERGED $current_diff_path
|
|
|
|
set BASE "./$MERGED.BASE$fileext"
|
|
|
|
set LOCAL "./$MERGED.LOCAL$fileext"
|
|
|
|
set REMOTE "./$MERGED.REMOTE$fileext"
|
|
|
|
set BACKUP "./$MERGED.BACKUP$fileext"
|
|
|
|
|
|
|
|
set base_stage $merge_stages(1)
|
|
|
|
|
|
|
|
# Build the command line
|
|
|
|
switch -- $tool {
|
2011-02-26 11:51:14 +01:00
|
|
|
araxis {
|
2008-08-30 22:56:51 +02:00
|
|
|
if {$base_stage ne {}} {
|
2011-02-26 11:51:14 +01:00
|
|
|
set cmdline [list "$merge_tool_path" -wait -merge -3 -a1 \
|
|
|
|
-title1:"'$MERGED (Base)'" -title2:"'$MERGED (Local)'" \
|
|
|
|
-title3:"'$MERGED (Remote)'" \
|
|
|
|
"$BASE" "$LOCAL" "$REMOTE" "$MERGED"]
|
2008-08-30 22:56:51 +02:00
|
|
|
} else {
|
2011-02-26 11:51:14 +01:00
|
|
|
set cmdline [list "$merge_tool_path" -wait -2 \
|
|
|
|
-title1:"'$MERGED (Local)'" -title2:"'$MERGED (Remote)'" \
|
|
|
|
"$LOCAL" "$REMOTE" "$MERGED"]
|
2008-08-30 22:56:51 +02:00
|
|
|
}
|
|
|
|
}
|
2011-02-26 11:52:17 +01:00
|
|
|
bc3 {
|
|
|
|
if {$base_stage ne {}} {
|
|
|
|
set cmdline [list "$merge_tool_path" "$LOCAL" "$REMOTE" "$BASE" -mergeoutput="$MERGED"]
|
|
|
|
} else {
|
|
|
|
set cmdline [list "$merge_tool_path" "$LOCAL" "$REMOTE" -mergeoutput="$MERGED"]
|
|
|
|
}
|
|
|
|
}
|
2011-02-26 11:51:14 +01:00
|
|
|
ecmerge {
|
2008-08-30 22:56:51 +02:00
|
|
|
if {$base_stage ne {}} {
|
2011-02-26 11:51:14 +01:00
|
|
|
set cmdline [list "$merge_tool_path" "$BASE" "$LOCAL" "$REMOTE" --default --mode=merge3 --to="$MERGED"]
|
2008-08-30 22:56:51 +02:00
|
|
|
} else {
|
2011-02-26 11:51:14 +01:00
|
|
|
set cmdline [list "$merge_tool_path" "$LOCAL" "$REMOTE" --default --mode=merge2 --to="$MERGED"]
|
2008-08-30 22:56:51 +02:00
|
|
|
}
|
|
|
|
}
|
2011-02-26 11:51:14 +01:00
|
|
|
emerge {
|
|
|
|
if {$base_stage ne {}} {
|
|
|
|
set cmdline [list "$merge_tool_path" -f emerge-files-with-ancestor-command \
|
|
|
|
"$LOCAL" "$REMOTE" "$BASE" "$basename"]
|
|
|
|
} else {
|
|
|
|
set cmdline [list "$merge_tool_path" -f emerge-files-command \
|
|
|
|
"$LOCAL" "$REMOTE" "$basename"]
|
|
|
|
}
|
2008-08-30 22:56:51 +02:00
|
|
|
}
|
|
|
|
gvimdiff {
|
|
|
|
set cmdline [list "$merge_tool_path" -f "$LOCAL" "$MERGED" "$REMOTE"]
|
|
|
|
}
|
2011-02-26 11:51:14 +01:00
|
|
|
kdiff3 {
|
2008-08-30 22:56:51 +02:00
|
|
|
if {$base_stage ne {}} {
|
2011-02-26 11:51:14 +01:00
|
|
|
set cmdline [list "$merge_tool_path" --auto --L1 "$MERGED (Base)" \
|
|
|
|
--L2 "$MERGED (Local)" --L3 "$MERGED (Remote)" -o "$MERGED" "$BASE" "$LOCAL" "$REMOTE"]
|
2008-08-30 22:56:51 +02:00
|
|
|
} else {
|
2011-02-26 11:51:14 +01:00
|
|
|
set cmdline [list "$merge_tool_path" --auto --L1 "$MERGED (Local)" \
|
|
|
|
--L2 "$MERGED (Remote)" -o "$MERGED" "$LOCAL" "$REMOTE"]
|
2008-08-30 22:56:51 +02:00
|
|
|
}
|
|
|
|
}
|
2011-02-26 11:51:14 +01:00
|
|
|
meld {
|
|
|
|
set cmdline [list "$merge_tool_path" "$LOCAL" "$MERGED" "$REMOTE"]
|
|
|
|
}
|
2008-08-30 22:56:51 +02:00
|
|
|
opendiff {
|
|
|
|
if {$base_stage ne {}} {
|
|
|
|
set cmdline [list "$merge_tool_path" "$LOCAL" "$REMOTE" -ancestor "$BASE" -merge "$MERGED"]
|
|
|
|
} else {
|
|
|
|
set cmdline [list "$merge_tool_path" "$LOCAL" "$REMOTE" -merge "$MERGED"]
|
|
|
|
}
|
|
|
|
}
|
2011-02-26 11:51:14 +01:00
|
|
|
p4merge {
|
|
|
|
set cmdline [list "$merge_tool_path" "$BASE" "$REMOTE" "$LOCAL" "$MERGED"]
|
2008-08-30 22:56:51 +02:00
|
|
|
}
|
2011-02-26 11:51:14 +01:00
|
|
|
tkdiff {
|
2008-08-30 22:56:51 +02:00
|
|
|
if {$base_stage ne {}} {
|
2011-02-26 11:51:14 +01:00
|
|
|
set cmdline [list "$merge_tool_path" -a "$BASE" -o "$MERGED" "$LOCAL" "$REMOTE"]
|
2008-08-30 22:56:51 +02:00
|
|
|
} else {
|
2011-02-26 11:51:14 +01:00
|
|
|
set cmdline [list "$merge_tool_path" -o "$MERGED" "$LOCAL" "$REMOTE"]
|
2008-08-30 22:56:51 +02:00
|
|
|
}
|
|
|
|
}
|
2011-02-26 11:51:14 +01:00
|
|
|
vimdiff {
|
|
|
|
error_popup [mc "Not a GUI merge tool: '%s'" $tool]
|
|
|
|
return
|
|
|
|
}
|
2008-08-30 22:59:47 +02:00
|
|
|
winmerge {
|
|
|
|
if {$base_stage ne {}} {
|
|
|
|
# This tool does not support 3-way merges.
|
|
|
|
# Use the 'conflict file' resolution feature instead.
|
|
|
|
set cmdline [list "$merge_tool_path" -e -ub "$MERGED"]
|
|
|
|
} else {
|
|
|
|
set cmdline [list "$merge_tool_path" -e -ub -wl \
|
|
|
|
-dl "Theirs File" -dr "Mine File" "$REMOTE" "$LOCAL" "$MERGED"]
|
|
|
|
}
|
|
|
|
}
|
2011-02-26 11:51:14 +01:00
|
|
|
xxdiff {
|
2008-08-30 22:59:47 +02:00
|
|
|
if {$base_stage ne {}} {
|
2011-02-26 11:51:14 +01:00
|
|
|
set cmdline [list "$merge_tool_path" -X --show-merged-pane \
|
|
|
|
-R {Accel.SaveAsMerged: "Ctrl-S"} \
|
|
|
|
-R {Accel.Search: "Ctrl+F"} \
|
|
|
|
-R {Accel.SearchForward: "Ctrl-G"} \
|
|
|
|
--merged-file "$MERGED" "$LOCAL" "$BASE" "$REMOTE"]
|
2008-08-30 22:59:47 +02:00
|
|
|
} else {
|
2011-02-26 11:51:14 +01:00
|
|
|
set cmdline [list "$merge_tool_path" -X --show-merged-pane \
|
|
|
|
-R {Accel.SaveAsMerged: "Ctrl-S"} \
|
|
|
|
-R {Accel.Search: "Ctrl+F"} \
|
|
|
|
-R {Accel.SearchForward: "Ctrl-G"} \
|
|
|
|
--merged-file "$MERGED" "$LOCAL" "$REMOTE"]
|
2008-08-30 22:59:47 +02:00
|
|
|
}
|
|
|
|
}
|
2008-08-30 22:56:51 +02:00
|
|
|
default {
|
|
|
|
error_popup [mc "Unsupported merge tool '%s'" $tool]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
merge_tool_start $cmdline $MERGED $BACKUP [list $BASE $LOCAL $REMOTE]
|
|
|
|
}
|
|
|
|
|
|
|
|
proc delete_temp_files {files} {
|
|
|
|
foreach fname $files {
|
|
|
|
file delete $fname
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc merge_tool_get_stages {target stages} {
|
|
|
|
global merge_stages
|
|
|
|
|
|
|
|
set i 1
|
|
|
|
foreach fname $stages {
|
|
|
|
if {$merge_stages($i) eq {}} {
|
|
|
|
file delete $fname
|
2008-08-30 22:59:47 +02:00
|
|
|
catch { close [open $fname w] }
|
2008-08-30 22:56:51 +02:00
|
|
|
} else {
|
|
|
|
# A hack to support autocrlf properly
|
|
|
|
git checkout-index -f --stage=$i -- $target
|
|
|
|
file rename -force -- $target $fname
|
|
|
|
}
|
|
|
|
incr i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc merge_tool_start {cmdline target backup stages} {
|
|
|
|
global merge_stages mtool_target mtool_tmpfiles mtool_fd mtool_mtime
|
|
|
|
|
|
|
|
if {[info exists mtool_fd]} {
|
|
|
|
if {[ask_popup [mc "Merge tool is already running, terminate it?"]] eq {yes}} {
|
|
|
|
catch { kill_file_process $mtool_fd }
|
|
|
|
catch { close $mtool_fd }
|
|
|
|
unset mtool_fd
|
|
|
|
|
|
|
|
set old_backup [lindex $mtool_tmpfiles end]
|
|
|
|
file rename -force -- $old_backup $mtool_target
|
|
|
|
delete_temp_files $mtool_tmpfiles
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Save the original file
|
|
|
|
file rename -force -- $target $backup
|
|
|
|
|
|
|
|
# Get the blobs; it destroys $target
|
|
|
|
if {[catch {merge_tool_get_stages $target $stages} err]} {
|
|
|
|
file rename -force -- $backup $target
|
|
|
|
delete_temp_files $stages
|
|
|
|
error_popup [mc "Error retrieving versions:\n%s" $err]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
# Restore the conflict file
|
|
|
|
file copy -force -- $backup $target
|
|
|
|
|
|
|
|
# Initialize global state
|
|
|
|
set mtool_target $target
|
|
|
|
set mtool_mtime [file mtime $target]
|
|
|
|
set mtool_tmpfiles $stages
|
|
|
|
|
|
|
|
lappend mtool_tmpfiles $backup
|
|
|
|
|
|
|
|
# Force redirection to avoid interpreting output on stderr
|
|
|
|
# as an error, and launch the tool
|
|
|
|
lappend cmdline {2>@1}
|
|
|
|
|
|
|
|
if {[catch { set mtool_fd [_open_stdout_stderr $cmdline] } err]} {
|
|
|
|
delete_temp_files $mtool_tmpfiles
|
|
|
|
error_popup [mc "Could not start the merge tool:\n\n%s" $err]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ui_status [mc "Running merge tool..."]
|
|
|
|
|
|
|
|
fconfigure $mtool_fd -blocking 0 -translation binary -encoding binary
|
|
|
|
fileevent $mtool_fd readable [list read_mtool_output $mtool_fd]
|
|
|
|
}
|
|
|
|
|
|
|
|
proc read_mtool_output {fd} {
|
|
|
|
global mtool_fd mtool_tmpfiles
|
|
|
|
|
|
|
|
read $fd
|
|
|
|
if {[eof $fd]} {
|
|
|
|
unset mtool_fd
|
|
|
|
|
|
|
|
fconfigure $fd -blocking 1
|
|
|
|
merge_tool_finish $fd
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc merge_tool_finish {fd} {
|
|
|
|
global mtool_tmpfiles mtool_target mtool_mtime
|
|
|
|
|
|
|
|
set backup [lindex $mtool_tmpfiles end]
|
|
|
|
set failed 0
|
|
|
|
|
|
|
|
# Check the return code
|
|
|
|
if {[catch {close $fd} err]} {
|
|
|
|
set failed 1
|
|
|
|
if {$err ne {child process exited abnormally}} {
|
|
|
|
error_popup [strcat [mc "Merge tool failed."] "\n\n$err"]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
# Finish
|
|
|
|
if {$failed} {
|
|
|
|
file rename -force -- $backup $mtool_target
|
|
|
|
delete_temp_files $mtool_tmpfiles
|
|
|
|
ui_status [mc "Merge tool failed."]
|
|
|
|
} else {
|
2009-04-07 17:33:35 +02:00
|
|
|
if {[is_config_true mergetool.keepbackup]} {
|
2008-08-30 22:56:51 +02:00
|
|
|
file rename -force -- $backup "$mtool_target.orig"
|
|
|
|
}
|
|
|
|
|
|
|
|
delete_temp_files $mtool_tmpfiles
|
|
|
|
|
2008-09-24 21:08:01 +02:00
|
|
|
reshow_diff
|
2008-08-30 22:56:51 +02:00
|
|
|
}
|
|
|
|
}
|