Code

Merge git://git.bogomips.org/git-svn
[git.git] / git-gui / lib / diff.tcl
index 96ba94906cf8037b58c2ba135654da840f9fff9a..a30c80a935b58ce0c783dfffde196a85eadca9a2 100644 (file)
@@ -19,16 +19,24 @@ proc clear_diff {} {
 proc reshow_diff {} {
        global file_states file_lists
        global current_diff_path current_diff_side
+       global ui_diff
 
        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} {
+       } elseif {$current_diff_side eq {}} {
                clear_diff
+       } elseif {[catch {set s $file_states($p)}]
+               || [lsearch -sorted -exact $file_lists($current_diff_side) $p] == -1} {
+
+               if {[find_next_diff $current_diff_side $p {} {[^O]}]} {
+                       next_diff
+               } else {
+                       clear_diff
+               }
        } else {
-               show_diff $p $current_diff_side
+               set save_pos [lindex [$ui_diff yview] 0]
+               show_diff $p $current_diff_side {} $save_pos
        }
 }
 
@@ -52,11 +60,12 @@ A rescan will be automatically started to find other files which may have the sa
        rescan ui_ready 0
 }
 
-proc show_diff {path w {lno {}}} {
+proc show_diff {path w {lno {}} {scroll_pos {}}} {
        global file_states file_lists
        global is_3way_diff diff_active repo_config
        global ui_diff ui_index ui_workdir
        global current_diff_path current_diff_side current_diff_header
+       global current_diff_queue
 
        if {$diff_active || ![lock_index read]} return
 
@@ -69,17 +78,74 @@ proc show_diff {path w {lno {}}} {
        }
        if {$lno >= 1} {
                $w tag add in_diff $lno.0 [expr {$lno + 1}].0
+               $w see $lno.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 {}
+       set current_diff_queue {}
        ui_status [mc "Loading diff of %s..." [escape_path $path]]
 
+       if {[string first {U} $m] >= 0} {
+               merge_load_stages $path [list show_unmerged_diff $scroll_pos]
+       } elseif {$m eq {_O}} {
+               show_other_diff $path $w $m $scroll_pos
+       } else {
+               start_show_diff $scroll_pos
+       }
+}
+
+proc show_unmerged_diff {scroll_pos} {
+       global current_diff_path current_diff_side
+       global merge_stages ui_diff
+       global current_diff_queue
+
+       if {$merge_stages(2) eq {}} {
+               lappend current_diff_queue \
+                       [list "LOCAL: deleted\nREMOTE:\n" d======= \
+                           [list ":1:$current_diff_path" ":3:$current_diff_path"]]
+       } elseif {$merge_stages(3) eq {}} {
+               lappend current_diff_queue \
+                       [list "REMOTE: deleted\nLOCAL:\n" d======= \
+                           [list ":1:$current_diff_path" ":2:$current_diff_path"]]
+       } elseif {[lindex $merge_stages(1) 0] eq {120000}
+               || [lindex $merge_stages(2) 0] eq {120000}
+               || [lindex $merge_stages(3) 0] eq {120000}} {
+               lappend current_diff_queue \
+                       [list "LOCAL:\n" d======= \
+                           [list ":1:$current_diff_path" ":2:$current_diff_path"]]
+               lappend current_diff_queue \
+                       [list "REMOTE:\n" d======= \
+                           [list ":1:$current_diff_path" ":3:$current_diff_path"]]
+       } else {
+               start_show_diff $scroll_pos
+               return
+       }
+
+       advance_diff_queue $scroll_pos
+}
+
+proc advance_diff_queue {scroll_pos} {
+       global current_diff_queue ui_diff
+
+       set item [lindex $current_diff_queue 0]
+       set current_diff_queue [lrange $current_diff_queue 1 end]
+
+       $ui_diff conf -state normal
+       $ui_diff insert end [lindex $item 0] [lindex $item 1]
+       $ui_diff conf -state disabled
+
+       start_show_diff $scroll_pos [lindex $item 2]
+}
+
+proc show_other_diff {path w m scroll_pos} {
+       global file_states file_lists
+       global is_3way_diff diff_active repo_config
+       global ui_diff ui_index ui_workdir
+       global current_diff_path current_diff_side current_diff_header
+
        # - Git won't give us the diff, there's nothing to compare to!
        #
        if {$m eq {_O}} {
@@ -151,16 +217,36 @@ proc show_diff {path w {lno {}}} {
                $ui_diff conf -state disabled
                set diff_active 0
                unlock_index
+               if {$scroll_pos ne {}} {
+                       update
+                       $ui_diff yview moveto $scroll_pos
+               }
                ui_ready
                return
        }
+}
+
+proc start_show_diff {scroll_pos {add_opts {}}} {
+       global file_states file_lists
+       global is_3way_diff diff_active repo_config
+       global ui_diff ui_index ui_workdir
+       global current_diff_path current_diff_side current_diff_header
+
+       set path $current_diff_path
+       set w $current_diff_side
+
+       set s $file_states($path)
+       set m [lindex $s 0]
+       set is_3way_diff 0
+       set diff_active 1
+       set current_diff_header {}
 
        set cmd [list]
        if {$w eq $ui_index} {
                lappend cmd diff-index
                lappend cmd --cached
        } elseif {$w eq $ui_workdir} {
-               if {[string index $m 0] eq {U}} {
+               if {[string first {U} $m] >= 0} {
                        lappend cmd diff
                } else {
                        lappend cmd diff-files
@@ -169,14 +255,18 @@ proc show_diff {path w {lno {}}} {
 
        lappend cmd -p
        lappend cmd --no-color
-       if {$repo_config(gui.diffcontext) >= 0} {
+       if {$repo_config(gui.diffcontext) >= 1} {
                lappend cmd "-U$repo_config(gui.diffcontext)"
        }
        if {$w eq $ui_index} {
                lappend cmd [PARENT]
        }
-       lappend cmd --
-       lappend cmd $path
+       if {$add_opts ne {}} {
+               eval lappend cmd $add_opts
+       } else {
+               lappend cmd --
+               lappend cmd $path
+       }
 
        if {[catch {set fd [eval git_read --nice $cmd]} err]} {
                set diff_active 0
@@ -186,33 +276,38 @@ proc show_diff {path w {lno {}}} {
                return
        }
 
+       set ::current_diff_inheader 1
        fconfigure $fd \
                -blocking 0 \
                -encoding binary \
                -translation binary
-       fileevent $fd readable [list read_diff $fd]
+       fileevent $fd readable [list read_diff $fd $scroll_pos]
 }
 
-proc read_diff {fd} {
+proc read_diff {fd scroll_pos} {
        global ui_diff diff_active
        global is_3way_diff current_diff_header
+       global current_diff_queue
 
        $ui_diff conf -state normal
        while {[gets $fd line] >= 0} {
                # -- Cleanup uninteresting diff header lines.
                #
-               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
+               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
+                       }
                }
                if {[string match {index *} $line]} continue
                if {$line eq {deleted file mode 120000}} {
                        set line "deleted symlink"
                }
+               set ::current_diff_inheader 0
 
                # -- Automatically detect if this is a 3 way diff.
                #
@@ -280,8 +375,18 @@ proc read_diff {fd} {
 
        if {[eof $fd]} {
                close $fd
+
+               if {$current_diff_queue ne {}} {
+                       advance_diff_queue $scroll_pos
+                       return
+               }
+
                set diff_active 0
                unlock_index
+               if {$scroll_pos ne {}} {
+                       update
+                       $ui_diff yview moveto $scroll_pos
+               }
                ui_ready
 
                if {[$ui_diff index end] eq {2.0}} {
@@ -356,10 +461,9 @@ proc apply_hunk {x y} {
        }
        unlock_index
        display_file $current_diff_path $mi
+       # This should trigger shift to the next changed file
        if {$o eq {_}} {
-               clear_diff
-       } else {
-               set current_diff_path $current_diff_path
+               reshow_diff
        }
 }
 
@@ -411,6 +515,53 @@ proc apply_line {x y} {
        set hh [lindex [split $hh ,] 0]
        set hln [lindex [split $hh -] 1]
 
+       # 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 {}
+
        set n 0
        set i_l [$ui_diff index "$i_l + 1 lines"]
        set patch {}
@@ -422,16 +573,27 @@ proc apply_line {x y} {
                    [$ui_diff compare $the_l < $next_l]} {
                        # the line to stage/unstage
                        set ln [$ui_diff get $i_l $next_l]
-                       set patch "$patch$ln"
+                       if {$c1 eq {-}} {
+                               set n [expr $n+1]
+                               set patch "$patch$pre_context$ln"
+                       } else {
+                               set patch "$patch$ln$pre_context"
+                       }
+                       set pre_context {}
                } elseif {$c1 ne {-} && $c1 ne {+}} {
                        # context line
                        set ln [$ui_diff get $i_l $next_l]
-                       set patch "$patch$ln"
+                       set patch "$patch$pre_context$ln"
                        set n [expr $n+1]
+                       set pre_context {}
                } elseif {$c1 eq $to_context} {
                        # turn change line into context line
                        set ln [$ui_diff get "$i_l + 1 chars" $next_l]
-                       set patch "$patch $ln"
+                       if {$c1 eq {-}} {
+                               set pre_context "$pre_context $ln"
+                       } else {
+                               set patch "$patch $ln"
+                       }
                        set n [expr $n+1]
                }
                set i_l $next_l