]> asedeno.scripts.mit.edu Git - git.git/blobdiff - lib/diff.tcl
git-gui: add a part about format strings in po/README
[git.git] / lib / diff.tcl
index 7e715a6865858e35babf30e624c16314cdc2c2ec..52b79e4a1f476c2ee9b65087f66a352a25ed0903 100644 (file)
@@ -17,8 +17,9 @@ proc clear_diff {} {
 }
 
 proc reshow_diff {} {
-       global ui_status_value file_states file_lists
+       global file_states file_lists
        global current_diff_path current_diff_side
+       global ui_diff
 
        set p $current_diff_path
        if {$p eq {}} {
@@ -28,7 +29,8 @@ proc reshow_diff {} {
                || [lsearch -sorted -exact $file_lists($current_diff_side) $p] == -1} {
                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
        }
 }
 
@@ -39,23 +41,23 @@ proc handle_empty_diff {} {
        set s $file_states($path)
        if {[lindex $s 0] ne {_M}} return
 
-       info_popup "No differences detected.
+       info_popup [mc "No differences detected.
 
-[short_path $path] has no changes.
+%s has no changes.
 
 The modification date of this file was updated by another application, but the content within the file was not changed.
 
-A rescan will be automatically started to find other files which may have the same state."
+A rescan will be automatically started to find other files which may have the same state." [short_path $path]]
 
        clear_diff
        display_file $path __
-       rescan {set ui_status_value {Ready.}} 0
+       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_status_value ui_index ui_workdir
+       global ui_diff ui_index ui_workdir
        global current_diff_path current_diff_side current_diff_header
 
        if {$diff_active || ![lock_index read]} return
@@ -78,26 +80,50 @@ proc show_diff {path w {lno {}}} {
        set current_diff_path $path
        set current_diff_side $w
        set current_diff_header {}
-       set ui_status_value "Loading diff of [escape_path $path]..."
+       ui_status [mc "Loading diff of %s..." [escape_path $path]]
 
        # - Git won't give us the diff, there's nothing to compare to!
        #
        if {$m eq {_O}} {
                set max_sz [expr {128 * 1024}]
+               set type unknown
                if {[catch {
-                               set fd [open $path r]
-                               set content [read $fd $max_sz]
-                               close $fd
-                               set sz [file size $path]
+                               set type [file type $path]
+                               switch -- $type {
+                               directory {
+                                       set type submodule
+                                       set content {}
+                                       set sz 0
+                               }
+                               link {
+                                       set content [file readlink $path]
+                                       set sz [string length $content]
+                               }
+                               file {
+                                       set fd [open $path r]
+                                       fconfigure $fd -eofchar {}
+                                       set content [read $fd $max_sz]
+                                       close $fd
+                                       set sz [file size $path]
+                               }
+                               default {
+                                       error "'$type' not supported"
+                               }
+                               }
                        } err ]} {
                        set diff_active 0
                        unlock_index
-                       set ui_status_value "Unable to display [escape_path $path]"
-                       error_popup "Error loading file:\n\n$err"
+                       ui_status [mc "Unable to display %s" [escape_path $path]]
+                       error_popup [strcat [mc "Error loading file:"] "\n\n$err"]
                        return
                }
                $ui_diff conf -state normal
-               if {![catch {set type [exec file $path]}]} {
+               if {$type eq {submodule}} {
+                       $ui_diff insert end [append \
+                               "* " \
+                               [mc "Git Repository (subproject)"] \
+                               "\n"] d_@
+               } elseif {![catch {set type [exec file $path]}]} {
                        set n [string length $path]
                        if {[string equal -length $n $path $type]} {
                                set type [string range $type $n end]
@@ -107,7 +133,7 @@ proc show_diff {path w {lno {}}} {
                }
                if {[string first "\0" $content] != -1} {
                        $ui_diff insert end \
-                               "* Binary file (not showing content)." \
+                               [mc "* Binary file (not showing content)."] \
                                d_@
                } else {
                        if {$sz > $max_sz} {
@@ -127,11 +153,15 @@ proc show_diff {path w {lno {}}} {
                $ui_diff conf -state disabled
                set diff_active 0
                unlock_index
-               set ui_status_value {Ready.}
+               if {$scroll_pos ne {}} {
+                       update
+                       $ui_diff yview moveto $scroll_pos
+               }
+               ui_ready
                return
        }
 
-       set cmd [list | git]
+       set cmd [list]
        if {$w eq $ui_index} {
                lappend cmd diff-index
                lappend cmd --cached
@@ -145,7 +175,7 @@ proc show_diff {path w {lno {}}} {
 
        lappend cmd -p
        lappend cmd --no-color
-       if {$repo_config(gui.diffcontext) > 0} {
+       if {$repo_config(gui.diffcontext) >= 0} {
                lappend cmd "-U$repo_config(gui.diffcontext)"
        }
        if {$w eq $ui_index} {
@@ -154,11 +184,11 @@ proc show_diff {path w {lno {}}} {
        lappend cmd --
        lappend cmd $path
 
-       if {[catch {set fd [open $cmd r]} err]} {
+       if {[catch {set fd [eval git_read --nice $cmd]} err]} {
                set diff_active 0
                unlock_index
-               set ui_status_value "Unable to display [escape_path $path]"
-               error_popup "Error loading diff:\n\n$err"
+               ui_status [mc "Unable to display %s" [escape_path $path]]
+               error_popup [strcat [mc "Error loading diff:"] "\n\n$err"]
                return
        }
 
@@ -166,11 +196,11 @@ proc show_diff {path w {lno {}}} {
                -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} {
-       global ui_diff ui_status_value diff_active
+proc read_diff {fd scroll_pos} {
+       global ui_diff diff_active
        global is_3way_diff current_diff_header
 
        $ui_diff conf -state normal
@@ -196,7 +226,9 @@ proc read_diff {fd} {
 
                if {[string match {mode *} $line]
                        || [string match {new file *} $line]
+                       || [regexp {^(old|new) mode *} $line]
                        || [string match {deleted file *} $line]
+                       || [string match {deleted symlink} $line]
                        || [string match {Binary files * and * differ} $line]
                        || $line eq {\ No newline at end of file}
                        || [regexp {^\* Unmerged path } $line]} {
@@ -256,7 +288,11 @@ proc read_diff {fd} {
                close $fd
                set diff_active 0
                unlock_index
-               set ui_status_value {Ready.}
+               if {$scroll_pos ne {}} {
+                       update
+                       $ui_diff yview moveto $scroll_pos
+               }
+               ui_ready
 
                if {[$ui_diff index end] eq {2.0}} {
                        handle_empty_diff
@@ -271,17 +307,17 @@ proc apply_hunk {x y} {
        if {$current_diff_path eq {} || $current_diff_header eq {}} return
        if {![lock_index apply_hunk]} return
 
-       set apply_cmd {git apply --cached --whitespace=nowarn}
+       set apply_cmd {apply --cached --whitespace=nowarn}
        set mi [lindex $file_states($current_diff_path) 0]
        if {$current_diff_side eq $ui_index} {
-               set mode unstage
+               set failed_msg [mc "Failed to unstage selected hunk."]
                lappend apply_cmd --reverse
                if {[string index $mi 0] ne {M}} {
                        unlock_index
                        return
                }
        } else {
-               set mode stage
+               set failed_msg [mc "Failed to stage selected hunk."]
                if {[string index $mi 1] ne {M}} {
                        unlock_index
                        return
@@ -301,12 +337,12 @@ proc apply_hunk {x y} {
        }
 
        if {[catch {
-               set p [open "| $apply_cmd" w]
+               set p [eval git_write $apply_cmd]
                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]} {
-               error_popup "Failed to $mode selected hunk.\n\n$err"
+               error_popup [append $failed_msg "\n\n$err"]
                unlock_index
                return
        }
@@ -332,5 +368,152 @@ proc apply_hunk {x y} {
        display_file $current_diff_path $mi
        if {$o eq {_}} {
                clear_diff
+       } else {
+               set current_diff_path $current_diff_path
        }
 }
+
+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]
+
+       # 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 {}
+       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]
+                       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$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]
+                       if {$c1 eq {-}} {
+                               set pre_context "$pre_context $ln"
+                       } else {
+                               set patch "$patch $ln"
+                       }
+                       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
+}