2007-05-08 05:35:48 +02:00
|
|
|
# git-gui diff viewer
|
|
|
|
# Copyright (C) 2006, 2007 Shawn Pearce
|
|
|
|
|
|
|
|
proc clear_diff {} {
|
|
|
|
global ui_diff current_diff_path current_diff_header
|
|
|
|
global ui_index ui_workdir
|
|
|
|
|
|
|
|
$ui_diff conf -state normal
|
|
|
|
$ui_diff delete 0.0 end
|
|
|
|
$ui_diff conf -state disabled
|
|
|
|
|
|
|
|
set current_diff_path {}
|
|
|
|
set current_diff_header {}
|
|
|
|
|
|
|
|
$ui_index tag remove in_diff 0.0 end
|
|
|
|
$ui_workdir tag remove in_diff 0.0 end
|
|
|
|
}
|
|
|
|
|
|
|
|
proc reshow_diff {} {
|
2007-07-06 05:16:13 +02:00
|
|
|
global file_states file_lists
|
2007-05-08 05:35:48 +02:00
|
|
|
global current_diff_path current_diff_side
|
2008-07-27 08:35:38 +02:00
|
|
|
global ui_diff
|
2007-05-08 05:35:48 +02:00
|
|
|
|
|
|
|
set p $current_diff_path
|
|
|
|
if {$p eq {}} {
|
|
|
|
# No diff is being shown.
|
|
|
|
} elseif {$current_diff_side eq {}
|
|
|
|
|| [catch {set s $file_states($p)}]
|
|
|
|
|| [lsearch -sorted -exact $file_lists($current_diff_side) $p] == -1} {
|
|
|
|
clear_diff
|
|
|
|
} else {
|
2008-07-27 08:35:38 +02:00
|
|
|
set save_pos [lindex [$ui_diff yview] 0]
|
|
|
|
show_diff $p $current_diff_side {} $save_pos
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc handle_empty_diff {} {
|
|
|
|
global current_diff_path file_states file_lists
|
|
|
|
|
|
|
|
set path $current_diff_path
|
|
|
|
set s $file_states($path)
|
|
|
|
if {[lindex $s 0] ne {_M}} return
|
|
|
|
|
2007-07-21 14:21:34 +02:00
|
|
|
info_popup [mc "No differences detected.
|
2007-05-08 05:35:48 +02:00
|
|
|
|
2007-07-21 14:21:34 +02:00
|
|
|
%s has no changes.
|
2007-05-08 05:35:48 +02:00
|
|
|
|
|
|
|
The modification date of this file was updated by another application, but the content within the file was not changed.
|
|
|
|
|
2007-07-21 14:21:34 +02:00
|
|
|
A rescan will be automatically started to find other files which may have the same state." [short_path $path]]
|
2007-05-08 05:35:48 +02:00
|
|
|
|
|
|
|
clear_diff
|
|
|
|
display_file $path __
|
2007-07-06 05:16:13 +02:00
|
|
|
rescan ui_ready 0
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
|
|
|
|
2008-07-27 08:35:38 +02:00
|
|
|
proc show_diff {path w {lno {}} {scroll_pos {}}} {
|
2007-05-08 05:35:48 +02:00
|
|
|
global file_states file_lists
|
|
|
|
global is_3way_diff diff_active repo_config
|
2007-07-06 05:16:13 +02:00
|
|
|
global ui_diff ui_index ui_workdir
|
2007-05-08 05:35:48 +02:00
|
|
|
global current_diff_path current_diff_side current_diff_header
|
|
|
|
|
|
|
|
if {$diff_active || ![lock_index read]} return
|
|
|
|
|
|
|
|
clear_diff
|
|
|
|
if {$lno == {}} {
|
|
|
|
set lno [lsearch -sorted -exact $file_lists($w) $path]
|
|
|
|
if {$lno >= 0} {
|
|
|
|
incr lno
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if {$lno >= 1} {
|
|
|
|
$w tag add in_diff $lno.0 [expr {$lno + 1}].0
|
|
|
|
}
|
|
|
|
|
|
|
|
set s $file_states($path)
|
|
|
|
set m [lindex $s 0]
|
|
|
|
set is_3way_diff 0
|
|
|
|
set diff_active 1
|
|
|
|
set current_diff_path $path
|
|
|
|
set current_diff_side $w
|
|
|
|
set current_diff_header {}
|
2007-09-13 15:19:05 +02:00
|
|
|
ui_status [mc "Loading diff of %s..." [escape_path $path]]
|
2007-05-08 05:35:48 +02:00
|
|
|
|
|
|
|
# - Git won't give us the diff, there's nothing to compare to!
|
|
|
|
#
|
|
|
|
if {$m eq {_O}} {
|
|
|
|
set max_sz [expr {128 * 1024}]
|
2007-09-10 02:13:10 +02:00
|
|
|
set type unknown
|
2007-05-08 05:35:48 +02:00
|
|
|
if {[catch {
|
2007-09-10 02:13:10 +02:00
|
|
|
set type [file type $path]
|
|
|
|
switch -- $type {
|
|
|
|
directory {
|
|
|
|
set type submodule
|
|
|
|
set content {}
|
|
|
|
set sz 0
|
|
|
|
}
|
|
|
|
link {
|
2007-09-09 21:04:45 +02:00
|
|
|
set content [file readlink $path]
|
|
|
|
set sz [string length $content]
|
2007-09-10 02:13:10 +02:00
|
|
|
}
|
|
|
|
file {
|
2007-09-09 21:04:45 +02:00
|
|
|
set fd [open $path r]
|
|
|
|
fconfigure $fd -eofchar {}
|
|
|
|
set content [read $fd $max_sz]
|
|
|
|
close $fd
|
|
|
|
set sz [file size $path]
|
|
|
|
}
|
2007-09-10 02:13:10 +02:00
|
|
|
default {
|
|
|
|
error "'$type' not supported"
|
|
|
|
}
|
|
|
|
}
|
2007-05-08 05:35:48 +02:00
|
|
|
} err ]} {
|
|
|
|
set diff_active 0
|
|
|
|
unlock_index
|
2007-09-13 15:19:05 +02:00
|
|
|
ui_status [mc "Unable to display %s" [escape_path $path]]
|
2007-09-14 07:50:09 +02:00
|
|
|
error_popup [strcat [mc "Error loading file:"] "\n\n$err"]
|
2007-05-08 05:35:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
$ui_diff conf -state normal
|
2007-09-10 02:13:10 +02:00
|
|
|
if {$type eq {submodule}} {
|
2007-09-10 02:56:04 +02:00
|
|
|
$ui_diff insert end [append \
|
|
|
|
"* " \
|
|
|
|
[mc "Git Repository (subproject)"] \
|
|
|
|
"\n"] d_@
|
2007-09-10 02:13:10 +02:00
|
|
|
} elseif {![catch {set type [exec file $path]}]} {
|
2007-05-08 05:35:48 +02:00
|
|
|
set n [string length $path]
|
|
|
|
if {[string equal -length $n $path $type]} {
|
|
|
|
set type [string range $type $n end]
|
|
|
|
regsub {^:?\s*} $type {} type
|
|
|
|
}
|
|
|
|
$ui_diff insert end "* $type\n" d_@
|
|
|
|
}
|
|
|
|
if {[string first "\0" $content] != -1} {
|
|
|
|
$ui_diff insert end \
|
2007-09-13 15:19:05 +02:00
|
|
|
[mc "* Binary file (not showing content)."] \
|
2007-05-08 05:35:48 +02:00
|
|
|
d_@
|
|
|
|
} else {
|
|
|
|
if {$sz > $max_sz} {
|
|
|
|
$ui_diff insert end \
|
|
|
|
"* Untracked file is $sz bytes.
|
|
|
|
* Showing only first $max_sz bytes.
|
|
|
|
" d_@
|
|
|
|
}
|
|
|
|
$ui_diff insert end $content
|
|
|
|
if {$sz > $max_sz} {
|
|
|
|
$ui_diff insert end "
|
|
|
|
* Untracked file clipped here by [appname].
|
|
|
|
* To see the entire file, use an external editor.
|
|
|
|
" d_@
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$ui_diff conf -state disabled
|
|
|
|
set diff_active 0
|
|
|
|
unlock_index
|
2008-07-27 08:35:38 +02:00
|
|
|
if {$scroll_pos ne {}} {
|
|
|
|
update
|
|
|
|
$ui_diff yview moveto $scroll_pos
|
|
|
|
}
|
2007-07-06 05:16:13 +02:00
|
|
|
ui_ready
|
2007-05-08 05:35:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2007-07-09 07:17:09 +02:00
|
|
|
set cmd [list]
|
2007-05-08 05:35:48 +02:00
|
|
|
if {$w eq $ui_index} {
|
|
|
|
lappend cmd diff-index
|
|
|
|
lappend cmd --cached
|
|
|
|
} elseif {$w eq $ui_workdir} {
|
|
|
|
if {[string index $m 0] eq {U}} {
|
|
|
|
lappend cmd diff
|
|
|
|
} else {
|
|
|
|
lappend cmd diff-files
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lappend cmd -p
|
|
|
|
lappend cmd --no-color
|
2008-08-30 18:45:27 +02:00
|
|
|
if {$repo_config(gui.diffcontext) >= 1} {
|
2007-05-08 05:35:48 +02:00
|
|
|
lappend cmd "-U$repo_config(gui.diffcontext)"
|
|
|
|
}
|
|
|
|
if {$w eq $ui_index} {
|
|
|
|
lappend cmd [PARENT]
|
|
|
|
}
|
|
|
|
lappend cmd --
|
|
|
|
lappend cmd $path
|
|
|
|
|
2007-07-09 07:17:09 +02:00
|
|
|
if {[catch {set fd [eval git_read --nice $cmd]} err]} {
|
2007-05-08 05:35:48 +02:00
|
|
|
set diff_active 0
|
|
|
|
unlock_index
|
2007-09-13 15:19:05 +02:00
|
|
|
ui_status [mc "Unable to display %s" [escape_path $path]]
|
2007-09-14 07:50:09 +02:00
|
|
|
error_popup [strcat [mc "Error loading diff:"] "\n\n$err"]
|
2007-05-08 05:35:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2008-09-05 06:46:56 +02:00
|
|
|
set ::current_diff_inheader 1
|
2007-05-08 05:35:48 +02:00
|
|
|
fconfigure $fd \
|
|
|
|
-blocking 0 \
|
|
|
|
-encoding binary \
|
|
|
|
-translation binary
|
2008-07-27 08:35:38 +02:00
|
|
|
fileevent $fd readable [list read_diff $fd $scroll_pos]
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
|
|
|
|
2008-07-27 08:35:38 +02:00
|
|
|
proc read_diff {fd scroll_pos} {
|
2007-07-06 05:16:13 +02:00
|
|
|
global ui_diff diff_active
|
2007-05-08 05:35:48 +02:00
|
|
|
global is_3way_diff current_diff_header
|
|
|
|
|
|
|
|
$ui_diff conf -state normal
|
|
|
|
while {[gets $fd line] >= 0} {
|
|
|
|
# -- Cleanup uninteresting diff header lines.
|
|
|
|
#
|
2008-09-05 06:46:56 +02:00
|
|
|
if {$::current_diff_inheader} {
|
|
|
|
if { [string match {diff --git *} $line]
|
|
|
|
|| [string match {diff --cc *} $line]
|
|
|
|
|| [string match {diff --combined *} $line]
|
|
|
|
|| [string match {--- *} $line]
|
|
|
|
|| [string match {+++ *} $line]} {
|
|
|
|
append current_diff_header $line "\n"
|
|
|
|
continue
|
|
|
|
}
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
|
|
|
if {[string match {index *} $line]} continue
|
|
|
|
if {$line eq {deleted file mode 120000}} {
|
|
|
|
set line "deleted symlink"
|
|
|
|
}
|
2008-09-05 06:46:56 +02:00
|
|
|
set ::current_diff_inheader 0
|
2007-05-08 05:35:48 +02:00
|
|
|
|
|
|
|
# -- Automatically detect if this is a 3 way diff.
|
|
|
|
#
|
|
|
|
if {[string match {@@@ *} $line]} {set is_3way_diff 1}
|
|
|
|
|
|
|
|
if {[string match {mode *} $line]
|
|
|
|
|| [string match {new file *} $line]
|
2007-12-14 07:51:22 +01:00
|
|
|
|| [regexp {^(old|new) mode *} $line]
|
2007-05-08 05:35:48 +02:00
|
|
|
|| [string match {deleted file *} $line]
|
2007-09-09 21:09:07 +02:00
|
|
|
|| [string match {deleted symlink} $line]
|
2007-05-08 05:35:48 +02:00
|
|
|
|| [string match {Binary files * and * differ} $line]
|
|
|
|
|| $line eq {\ No newline at end of file}
|
|
|
|
|| [regexp {^\* Unmerged path } $line]} {
|
|
|
|
set tags {}
|
|
|
|
} elseif {$is_3way_diff} {
|
|
|
|
set op [string range $line 0 1]
|
|
|
|
switch -- $op {
|
|
|
|
{ } {set tags {}}
|
|
|
|
{@@} {set tags d_@}
|
|
|
|
{ +} {set tags d_s+}
|
|
|
|
{ -} {set tags d_s-}
|
|
|
|
{+ } {set tags d_+s}
|
|
|
|
{- } {set tags d_-s}
|
|
|
|
{--} {set tags d_--}
|
|
|
|
{++} {
|
|
|
|
if {[regexp {^\+\+([<>]{7} |={7})} $line _g op]} {
|
|
|
|
set line [string replace $line 0 1 { }]
|
|
|
|
set tags d$op
|
|
|
|
} else {
|
|
|
|
set tags d_++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
puts "error: Unhandled 3 way diff marker: {$op}"
|
|
|
|
set tags {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
set op [string index $line 0]
|
|
|
|
switch -- $op {
|
|
|
|
{ } {set tags {}}
|
|
|
|
{@} {set tags d_@}
|
|
|
|
{-} {set tags d_-}
|
|
|
|
{+} {
|
|
|
|
if {[regexp {^\+([<>]{7} |={7})} $line _g op]} {
|
|
|
|
set line [string replace $line 0 0 { }]
|
|
|
|
set tags d$op
|
|
|
|
} else {
|
|
|
|
set tags d_+
|
|
|
|
}
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
puts "error: Unhandled 2 way diff marker: {$op}"
|
|
|
|
set tags {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$ui_diff insert end $line $tags
|
|
|
|
if {[string index $line end] eq "\r"} {
|
|
|
|
$ui_diff tag add d_cr {end - 2c}
|
|
|
|
}
|
|
|
|
$ui_diff insert end "\n" $tags
|
|
|
|
}
|
|
|
|
$ui_diff conf -state disabled
|
|
|
|
|
|
|
|
if {[eof $fd]} {
|
|
|
|
close $fd
|
|
|
|
set diff_active 0
|
|
|
|
unlock_index
|
2008-07-27 08:35:38 +02:00
|
|
|
if {$scroll_pos ne {}} {
|
|
|
|
update
|
|
|
|
$ui_diff yview moveto $scroll_pos
|
|
|
|
}
|
2007-07-06 05:16:13 +02:00
|
|
|
ui_ready
|
2007-05-08 05:35:48 +02:00
|
|
|
|
|
|
|
if {[$ui_diff index end] eq {2.0}} {
|
|
|
|
handle_empty_diff
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
proc apply_hunk {x y} {
|
|
|
|
global current_diff_path current_diff_header current_diff_side
|
|
|
|
global ui_diff ui_index file_states
|
|
|
|
|
|
|
|
if {$current_diff_path eq {} || $current_diff_header eq {}} return
|
|
|
|
if {![lock_index apply_hunk]} return
|
|
|
|
|
2007-07-09 07:17:09 +02:00
|
|
|
set apply_cmd {apply --cached --whitespace=nowarn}
|
2007-05-08 05:35:48 +02:00
|
|
|
set mi [lindex $file_states($current_diff_path) 0]
|
|
|
|
if {$current_diff_side eq $ui_index} {
|
2007-07-21 14:21:34 +02:00
|
|
|
set failed_msg [mc "Failed to unstage selected hunk."]
|
2007-05-08 05:35:48 +02:00
|
|
|
lappend apply_cmd --reverse
|
|
|
|
if {[string index $mi 0] ne {M}} {
|
|
|
|
unlock_index
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
2007-07-21 14:21:34 +02:00
|
|
|
set failed_msg [mc "Failed to stage selected hunk."]
|
2007-05-08 05:35:48 +02:00
|
|
|
if {[string index $mi 1] ne {M}} {
|
|
|
|
unlock_index
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set s_lno [lindex [split [$ui_diff index @$x,$y] .] 0]
|
|
|
|
set s_lno [$ui_diff search -backwards -regexp ^@@ $s_lno.0 0.0]
|
|
|
|
if {$s_lno eq {}} {
|
|
|
|
unlock_index
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
set e_lno [$ui_diff search -forwards -regexp ^@@ "$s_lno + 1 lines" end]
|
|
|
|
if {$e_lno eq {}} {
|
|
|
|
set e_lno end
|
|
|
|
}
|
|
|
|
|
|
|
|
if {[catch {
|
2007-07-09 07:17:09 +02:00
|
|
|
set p [eval git_write $apply_cmd]
|
2007-05-08 05:35:48 +02:00
|
|
|
fconfigure $p -translation binary -encoding binary
|
|
|
|
puts -nonewline $p $current_diff_header
|
|
|
|
puts -nonewline $p [$ui_diff get $s_lno $e_lno]
|
|
|
|
close $p} err]} {
|
2007-07-21 14:21:34 +02:00
|
|
|
error_popup [append $failed_msg "\n\n$err"]
|
2007-05-08 05:35:48 +02:00
|
|
|
unlock_index
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
$ui_diff conf -state normal
|
|
|
|
$ui_diff delete $s_lno $e_lno
|
|
|
|
$ui_diff conf -state disabled
|
|
|
|
|
|
|
|
if {[$ui_diff get 1.0 end] eq "\n"} {
|
|
|
|
set o _
|
|
|
|
} else {
|
|
|
|
set o ?
|
|
|
|
}
|
|
|
|
|
|
|
|
if {$current_diff_side eq $ui_index} {
|
|
|
|
set mi ${o}M
|
|
|
|
} elseif {[string index $mi 0] eq {_}} {
|
|
|
|
set mi M$o
|
|
|
|
} else {
|
|
|
|
set mi ?$o
|
|
|
|
}
|
|
|
|
unlock_index
|
|
|
|
display_file $current_diff_path $mi
|
|
|
|
if {$o eq {_}} {
|
|
|
|
clear_diff
|
2008-01-16 07:29:39 +01:00
|
|
|
} else {
|
|
|
|
set current_diff_path $current_diff_path
|
2007-05-08 05:35:48 +02:00
|
|
|
}
|
|
|
|
}
|
2008-06-27 09:22:01 +02:00
|
|
|
|
|
|
|
proc apply_line {x y} {
|
|
|
|
global current_diff_path current_diff_header current_diff_side
|
|
|
|
global ui_diff ui_index file_states
|
|
|
|
|
|
|
|
if {$current_diff_path eq {} || $current_diff_header eq {}} return
|
|
|
|
if {![lock_index apply_hunk]} return
|
|
|
|
|
|
|
|
set apply_cmd {apply --cached --whitespace=nowarn}
|
|
|
|
set mi [lindex $file_states($current_diff_path) 0]
|
|
|
|
if {$current_diff_side eq $ui_index} {
|
|
|
|
set failed_msg [mc "Failed to unstage selected line."]
|
|
|
|
set to_context {+}
|
|
|
|
lappend apply_cmd --reverse
|
|
|
|
if {[string index $mi 0] ne {M}} {
|
|
|
|
unlock_index
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
set failed_msg [mc "Failed to stage selected line."]
|
|
|
|
set to_context {-}
|
|
|
|
if {[string index $mi 1] ne {M}} {
|
|
|
|
unlock_index
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
set the_l [$ui_diff index @$x,$y]
|
|
|
|
|
|
|
|
# operate only on change lines
|
|
|
|
set c1 [$ui_diff get "$the_l linestart"]
|
|
|
|
if {$c1 ne {+} && $c1 ne {-}} {
|
|
|
|
unlock_index
|
|
|
|
return
|
|
|
|
}
|
|
|
|
set sign $c1
|
|
|
|
|
|
|
|
set i_l [$ui_diff search -backwards -regexp ^@@ $the_l 0.0]
|
|
|
|
if {$i_l eq {}} {
|
|
|
|
unlock_index
|
|
|
|
return
|
|
|
|
}
|
|
|
|
# $i_l is now at the beginning of a line
|
|
|
|
|
|
|
|
# pick start line number from hunk header
|
|
|
|
set hh [$ui_diff get $i_l "$i_l + 1 lines"]
|
|
|
|
set hh [lindex [split $hh ,] 0]
|
|
|
|
set hln [lindex [split $hh -] 1]
|
|
|
|
|
git-gui: "Stage Line": Treat independent changes in adjacent lines better
Assume that we want to commit these states:
Old state == HEAD Intermediate state New state
--------------------------------------------------------
context before context before context before
old 1 new 1 new 1
old 2 old 2 new 2
context after context after context after
that is, want to commit two changes in this order:
1. transform "old 1" into "new 1"
2. transform "old 2" into "new 2"
[This discussion and this patch is about this very case and one other case
as outlined below; any other intermediate states that one could imagine are
not affected by this patch.]
Now assume further, that we have not staged and commited anything, but we
have already changed the working file to the new state. Then we will see
this hunk in the "Unstaged Changes":
@@ -1,4 +1,4 @@
context before
-old 1
-old 2
+new 1
+new 2
context after
The obvious way to stage the intermediate state is to apply "Stage This
Line" to "-old 1" and "+new 1". Unfortunately, this resulted in this
intermediate state:
context before
old 2
new 1
context after
which is not what we wanted. In fact, it was impossible to stage the
intermediate state using "Stage Line". The crux was that if a "+" line was
staged, then the "-" lines were converted to context lines and arranged
*before* the "+" line in the forged hunk that we fed to 'git apply'.
With this patch we now treat "+" lines that are staged differently. In
particular, the "-" lines before the "+" block are moved *after* the
staged "+" line. Now it is possible to get the correct intermediate state
by staging "-old 1" and "+new 1". Problem solved.
But there is a catch.
Noticing that we didn't get the right intermediate state by staging
"-old 1" and "+new 1", we could have had the idea to stage the complete
hunk and to *unstage* "-old 2" and "+new 2". But... the result is the same.
The reason is that there is the exact symmetric problem with unstaging the
last "-" and "+" line that are in adjacent blocks of "-" and "+" lines.
This patch does *not* change the way in which "-" lines are *unstaged*.
Why? Because if we did (i.e. move "+" lines before the "-" line after
converting them to context lines), then it would be impossible to stage
this intermediate state:
context before
old 1
new 2
context after
that is, it would be impossible to stage the two independet changes in the
opposite order.
Let's look at this case a bit further: The obvious way to get this
intermediate state would be to apply "Stage This Line" to "-old 2" and
"+new 2". Before this patch, this worked as expected. With this patch, it
does not work as expected, but it can still be achieved by first staging
the entire hunk, then *unstaging* "-old 1" and "+new 1".
In summary, this patch makes a common case possible, at the expense that
a less common case is made more complicated for the user.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2008-07-17 15:21:51 +02:00
|
|
|
# There is a special situation to take care of. Consider this hunk:
|
|
|
|
#
|
|
|
|
# @@ -10,4 +10,4 @@
|
|
|
|
# context before
|
|
|
|
# -old 1
|
|
|
|
# -old 2
|
|
|
|
# +new 1
|
|
|
|
# +new 2
|
|
|
|
# context after
|
|
|
|
#
|
|
|
|
# We used to keep the context lines in the order they appear in the
|
|
|
|
# hunk. But then it is not possible to correctly stage only
|
|
|
|
# "-old 1" and "+new 1" - it would result in this staged text:
|
|
|
|
#
|
|
|
|
# context before
|
|
|
|
# old 2
|
|
|
|
# new 1
|
|
|
|
# context after
|
|
|
|
#
|
|
|
|
# (By symmetry it is not possible to *un*stage "old 2" and "new 2".)
|
|
|
|
#
|
|
|
|
# We resolve the problem by introducing an asymmetry, namely, when
|
|
|
|
# a "+" line is *staged*, it is moved in front of the context lines
|
|
|
|
# that are generated from the "-" lines that are immediately before
|
|
|
|
# the "+" block. That is, we construct this patch:
|
|
|
|
#
|
|
|
|
# @@ -10,4 +10,5 @@
|
|
|
|
# context before
|
|
|
|
# +new 1
|
|
|
|
# old 1
|
|
|
|
# old 2
|
|
|
|
# context after
|
|
|
|
#
|
|
|
|
# But we do *not* treat "-" lines that are *un*staged in a special
|
|
|
|
# way.
|
|
|
|
#
|
|
|
|
# With this asymmetry it is possible to stage the change
|
|
|
|
# "old 1" -> "new 1" directly, and to stage the change
|
|
|
|
# "old 2" -> "new 2" by first staging the entire hunk and
|
|
|
|
# then unstaging the change "old 1" -> "new 1".
|
|
|
|
|
|
|
|
# This is non-empty if and only if we are _staging_ changes;
|
|
|
|
# then it accumulates the consecutive "-" lines (after converting
|
|
|
|
# them to context lines) in order to be moved after the "+" change
|
|
|
|
# line.
|
|
|
|
set pre_context {}
|
|
|
|
|
2008-06-27 09:22:01 +02:00
|
|
|
set n 0
|
|
|
|
set i_l [$ui_diff index "$i_l + 1 lines"]
|
|
|
|
set patch {}
|
|
|
|
while {[$ui_diff compare $i_l < "end - 1 chars"] &&
|
|
|
|
[$ui_diff get $i_l "$i_l + 2 chars"] ne {@@}} {
|
|
|
|
set next_l [$ui_diff index "$i_l + 1 lines"]
|
|
|
|
set c1 [$ui_diff get $i_l]
|
|
|
|
if {[$ui_diff compare $i_l <= $the_l] &&
|
|
|
|
[$ui_diff compare $the_l < $next_l]} {
|
|
|
|
# the line to stage/unstage
|
|
|
|
set ln [$ui_diff get $i_l $next_l]
|
2008-07-15 23:11:00 +02:00
|
|
|
if {$c1 eq {-}} {
|
|
|
|
set n [expr $n+1]
|
git-gui: "Stage Line": Treat independent changes in adjacent lines better
Assume that we want to commit these states:
Old state == HEAD Intermediate state New state
--------------------------------------------------------
context before context before context before
old 1 new 1 new 1
old 2 old 2 new 2
context after context after context after
that is, want to commit two changes in this order:
1. transform "old 1" into "new 1"
2. transform "old 2" into "new 2"
[This discussion and this patch is about this very case and one other case
as outlined below; any other intermediate states that one could imagine are
not affected by this patch.]
Now assume further, that we have not staged and commited anything, but we
have already changed the working file to the new state. Then we will see
this hunk in the "Unstaged Changes":
@@ -1,4 +1,4 @@
context before
-old 1
-old 2
+new 1
+new 2
context after
The obvious way to stage the intermediate state is to apply "Stage This
Line" to "-old 1" and "+new 1". Unfortunately, this resulted in this
intermediate state:
context before
old 2
new 1
context after
which is not what we wanted. In fact, it was impossible to stage the
intermediate state using "Stage Line". The crux was that if a "+" line was
staged, then the "-" lines were converted to context lines and arranged
*before* the "+" line in the forged hunk that we fed to 'git apply'.
With this patch we now treat "+" lines that are staged differently. In
particular, the "-" lines before the "+" block are moved *after* the
staged "+" line. Now it is possible to get the correct intermediate state
by staging "-old 1" and "+new 1". Problem solved.
But there is a catch.
Noticing that we didn't get the right intermediate state by staging
"-old 1" and "+new 1", we could have had the idea to stage the complete
hunk and to *unstage* "-old 2" and "+new 2". But... the result is the same.
The reason is that there is the exact symmetric problem with unstaging the
last "-" and "+" line that are in adjacent blocks of "-" and "+" lines.
This patch does *not* change the way in which "-" lines are *unstaged*.
Why? Because if we did (i.e. move "+" lines before the "-" line after
converting them to context lines), then it would be impossible to stage
this intermediate state:
context before
old 1
new 2
context after
that is, it would be impossible to stage the two independet changes in the
opposite order.
Let's look at this case a bit further: The obvious way to get this
intermediate state would be to apply "Stage This Line" to "-old 2" and
"+new 2". Before this patch, this worked as expected. With this patch, it
does not work as expected, but it can still be achieved by first staging
the entire hunk, then *unstaging* "-old 1" and "+new 1".
In summary, this patch makes a common case possible, at the expense that
a less common case is made more complicated for the user.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2008-07-17 15:21:51 +02:00
|
|
|
set patch "$patch$pre_context$ln"
|
|
|
|
} else {
|
|
|
|
set patch "$patch$ln$pre_context"
|
2008-07-15 23:11:00 +02:00
|
|
|
}
|
git-gui: "Stage Line": Treat independent changes in adjacent lines better
Assume that we want to commit these states:
Old state == HEAD Intermediate state New state
--------------------------------------------------------
context before context before context before
old 1 new 1 new 1
old 2 old 2 new 2
context after context after context after
that is, want to commit two changes in this order:
1. transform "old 1" into "new 1"
2. transform "old 2" into "new 2"
[This discussion and this patch is about this very case and one other case
as outlined below; any other intermediate states that one could imagine are
not affected by this patch.]
Now assume further, that we have not staged and commited anything, but we
have already changed the working file to the new state. Then we will see
this hunk in the "Unstaged Changes":
@@ -1,4 +1,4 @@
context before
-old 1
-old 2
+new 1
+new 2
context after
The obvious way to stage the intermediate state is to apply "Stage This
Line" to "-old 1" and "+new 1". Unfortunately, this resulted in this
intermediate state:
context before
old 2
new 1
context after
which is not what we wanted. In fact, it was impossible to stage the
intermediate state using "Stage Line". The crux was that if a "+" line was
staged, then the "-" lines were converted to context lines and arranged
*before* the "+" line in the forged hunk that we fed to 'git apply'.
With this patch we now treat "+" lines that are staged differently. In
particular, the "-" lines before the "+" block are moved *after* the
staged "+" line. Now it is possible to get the correct intermediate state
by staging "-old 1" and "+new 1". Problem solved.
But there is a catch.
Noticing that we didn't get the right intermediate state by staging
"-old 1" and "+new 1", we could have had the idea to stage the complete
hunk and to *unstage* "-old 2" and "+new 2". But... the result is the same.
The reason is that there is the exact symmetric problem with unstaging the
last "-" and "+" line that are in adjacent blocks of "-" and "+" lines.
This patch does *not* change the way in which "-" lines are *unstaged*.
Why? Because if we did (i.e. move "+" lines before the "-" line after
converting them to context lines), then it would be impossible to stage
this intermediate state:
context before
old 1
new 2
context after
that is, it would be impossible to stage the two independet changes in the
opposite order.
Let's look at this case a bit further: The obvious way to get this
intermediate state would be to apply "Stage This Line" to "-old 2" and
"+new 2". Before this patch, this worked as expected. With this patch, it
does not work as expected, but it can still be achieved by first staging
the entire hunk, then *unstaging* "-old 1" and "+new 1".
In summary, this patch makes a common case possible, at the expense that
a less common case is made more complicated for the user.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2008-07-17 15:21:51 +02:00
|
|
|
set pre_context {}
|
2008-06-27 09:22:01 +02:00
|
|
|
} elseif {$c1 ne {-} && $c1 ne {+}} {
|
|
|
|
# context line
|
|
|
|
set ln [$ui_diff get $i_l $next_l]
|
git-gui: "Stage Line": Treat independent changes in adjacent lines better
Assume that we want to commit these states:
Old state == HEAD Intermediate state New state
--------------------------------------------------------
context before context before context before
old 1 new 1 new 1
old 2 old 2 new 2
context after context after context after
that is, want to commit two changes in this order:
1. transform "old 1" into "new 1"
2. transform "old 2" into "new 2"
[This discussion and this patch is about this very case and one other case
as outlined below; any other intermediate states that one could imagine are
not affected by this patch.]
Now assume further, that we have not staged and commited anything, but we
have already changed the working file to the new state. Then we will see
this hunk in the "Unstaged Changes":
@@ -1,4 +1,4 @@
context before
-old 1
-old 2
+new 1
+new 2
context after
The obvious way to stage the intermediate state is to apply "Stage This
Line" to "-old 1" and "+new 1". Unfortunately, this resulted in this
intermediate state:
context before
old 2
new 1
context after
which is not what we wanted. In fact, it was impossible to stage the
intermediate state using "Stage Line". The crux was that if a "+" line was
staged, then the "-" lines were converted to context lines and arranged
*before* the "+" line in the forged hunk that we fed to 'git apply'.
With this patch we now treat "+" lines that are staged differently. In
particular, the "-" lines before the "+" block are moved *after* the
staged "+" line. Now it is possible to get the correct intermediate state
by staging "-old 1" and "+new 1". Problem solved.
But there is a catch.
Noticing that we didn't get the right intermediate state by staging
"-old 1" and "+new 1", we could have had the idea to stage the complete
hunk and to *unstage* "-old 2" and "+new 2". But... the result is the same.
The reason is that there is the exact symmetric problem with unstaging the
last "-" and "+" line that are in adjacent blocks of "-" and "+" lines.
This patch does *not* change the way in which "-" lines are *unstaged*.
Why? Because if we did (i.e. move "+" lines before the "-" line after
converting them to context lines), then it would be impossible to stage
this intermediate state:
context before
old 1
new 2
context after
that is, it would be impossible to stage the two independet changes in the
opposite order.
Let's look at this case a bit further: The obvious way to get this
intermediate state would be to apply "Stage This Line" to "-old 2" and
"+new 2". Before this patch, this worked as expected. With this patch, it
does not work as expected, but it can still be achieved by first staging
the entire hunk, then *unstaging* "-old 1" and "+new 1".
In summary, this patch makes a common case possible, at the expense that
a less common case is made more complicated for the user.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2008-07-17 15:21:51 +02:00
|
|
|
set patch "$patch$pre_context$ln"
|
2008-06-27 09:22:01 +02:00
|
|
|
set n [expr $n+1]
|
git-gui: "Stage Line": Treat independent changes in adjacent lines better
Assume that we want to commit these states:
Old state == HEAD Intermediate state New state
--------------------------------------------------------
context before context before context before
old 1 new 1 new 1
old 2 old 2 new 2
context after context after context after
that is, want to commit two changes in this order:
1. transform "old 1" into "new 1"
2. transform "old 2" into "new 2"
[This discussion and this patch is about this very case and one other case
as outlined below; any other intermediate states that one could imagine are
not affected by this patch.]
Now assume further, that we have not staged and commited anything, but we
have already changed the working file to the new state. Then we will see
this hunk in the "Unstaged Changes":
@@ -1,4 +1,4 @@
context before
-old 1
-old 2
+new 1
+new 2
context after
The obvious way to stage the intermediate state is to apply "Stage This
Line" to "-old 1" and "+new 1". Unfortunately, this resulted in this
intermediate state:
context before
old 2
new 1
context after
which is not what we wanted. In fact, it was impossible to stage the
intermediate state using "Stage Line". The crux was that if a "+" line was
staged, then the "-" lines were converted to context lines and arranged
*before* the "+" line in the forged hunk that we fed to 'git apply'.
With this patch we now treat "+" lines that are staged differently. In
particular, the "-" lines before the "+" block are moved *after* the
staged "+" line. Now it is possible to get the correct intermediate state
by staging "-old 1" and "+new 1". Problem solved.
But there is a catch.
Noticing that we didn't get the right intermediate state by staging
"-old 1" and "+new 1", we could have had the idea to stage the complete
hunk and to *unstage* "-old 2" and "+new 2". But... the result is the same.
The reason is that there is the exact symmetric problem with unstaging the
last "-" and "+" line that are in adjacent blocks of "-" and "+" lines.
This patch does *not* change the way in which "-" lines are *unstaged*.
Why? Because if we did (i.e. move "+" lines before the "-" line after
converting them to context lines), then it would be impossible to stage
this intermediate state:
context before
old 1
new 2
context after
that is, it would be impossible to stage the two independet changes in the
opposite order.
Let's look at this case a bit further: The obvious way to get this
intermediate state would be to apply "Stage This Line" to "-old 2" and
"+new 2". Before this patch, this worked as expected. With this patch, it
does not work as expected, but it can still be achieved by first staging
the entire hunk, then *unstaging* "-old 1" and "+new 1".
In summary, this patch makes a common case possible, at the expense that
a less common case is made more complicated for the user.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2008-07-17 15:21:51 +02:00
|
|
|
set pre_context {}
|
2008-06-27 09:22:01 +02:00
|
|
|
} elseif {$c1 eq $to_context} {
|
|
|
|
# turn change line into context line
|
|
|
|
set ln [$ui_diff get "$i_l + 1 chars" $next_l]
|
git-gui: "Stage Line": Treat independent changes in adjacent lines better
Assume that we want to commit these states:
Old state == HEAD Intermediate state New state
--------------------------------------------------------
context before context before context before
old 1 new 1 new 1
old 2 old 2 new 2
context after context after context after
that is, want to commit two changes in this order:
1. transform "old 1" into "new 1"
2. transform "old 2" into "new 2"
[This discussion and this patch is about this very case and one other case
as outlined below; any other intermediate states that one could imagine are
not affected by this patch.]
Now assume further, that we have not staged and commited anything, but we
have already changed the working file to the new state. Then we will see
this hunk in the "Unstaged Changes":
@@ -1,4 +1,4 @@
context before
-old 1
-old 2
+new 1
+new 2
context after
The obvious way to stage the intermediate state is to apply "Stage This
Line" to "-old 1" and "+new 1". Unfortunately, this resulted in this
intermediate state:
context before
old 2
new 1
context after
which is not what we wanted. In fact, it was impossible to stage the
intermediate state using "Stage Line". The crux was that if a "+" line was
staged, then the "-" lines were converted to context lines and arranged
*before* the "+" line in the forged hunk that we fed to 'git apply'.
With this patch we now treat "+" lines that are staged differently. In
particular, the "-" lines before the "+" block are moved *after* the
staged "+" line. Now it is possible to get the correct intermediate state
by staging "-old 1" and "+new 1". Problem solved.
But there is a catch.
Noticing that we didn't get the right intermediate state by staging
"-old 1" and "+new 1", we could have had the idea to stage the complete
hunk and to *unstage* "-old 2" and "+new 2". But... the result is the same.
The reason is that there is the exact symmetric problem with unstaging the
last "-" and "+" line that are in adjacent blocks of "-" and "+" lines.
This patch does *not* change the way in which "-" lines are *unstaged*.
Why? Because if we did (i.e. move "+" lines before the "-" line after
converting them to context lines), then it would be impossible to stage
this intermediate state:
context before
old 1
new 2
context after
that is, it would be impossible to stage the two independet changes in the
opposite order.
Let's look at this case a bit further: The obvious way to get this
intermediate state would be to apply "Stage This Line" to "-old 2" and
"+new 2". Before this patch, this worked as expected. With this patch, it
does not work as expected, but it can still be achieved by first staging
the entire hunk, then *unstaging* "-old 1" and "+new 1".
In summary, this patch makes a common case possible, at the expense that
a less common case is made more complicated for the user.
Signed-off-by: Johannes Sixt <johannes.sixt@telecom.at>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
2008-07-17 15:21:51 +02:00
|
|
|
if {$c1 eq {-}} {
|
|
|
|
set pre_context "$pre_context $ln"
|
|
|
|
} else {
|
|
|
|
set patch "$patch $ln"
|
|
|
|
}
|
2008-06-27 09:22:01 +02:00
|
|
|
set n [expr $n+1]
|
|
|
|
}
|
|
|
|
set i_l $next_l
|
|
|
|
}
|
|
|
|
set patch "@@ -$hln,$n +$hln,[eval expr $n $sign 1] @@\n$patch"
|
|
|
|
|
|
|
|
if {[catch {
|
|
|
|
set p [eval git_write $apply_cmd]
|
|
|
|
fconfigure $p -translation binary -encoding binary
|
|
|
|
puts -nonewline $p $current_diff_header
|
|
|
|
puts -nonewline $p $patch
|
|
|
|
close $p} err]} {
|
|
|
|
error_popup [append $failed_msg "\n\n$err"]
|
|
|
|
}
|
|
|
|
|
|
|
|
unlock_index
|
|
|
|
}
|