Merge branch 'bp/amend-toggle-bind'
[gitweb.git] / lib / diff.tcl
index 4eaf7e7aa21d4b867ef42ce29b387d93e238c4d8..96288fcc332e0083a56fcbb9248bc10ac536388e 100644 (file)
@@ -1,6 +1,19 @@
 # git-gui diff viewer
 # Copyright (C) 2006, 2007 Shawn Pearce
 
+proc apply_tab_size {{firsttab {}}} {
+       global have_tk85 repo_config ui_diff
+
+       set w [font measure font_diff "0"]
+       if {$have_tk85 && $firsttab != 0} {
+               $ui_diff configure -tabs [list [expr {$firsttab * $w}] [expr {($firsttab + $repo_config(gui.tabsize)) * $w}]]
+       } elseif {$have_tk85 || $repo_config(gui.tabsize) != 8} {
+               $ui_diff configure -tabs [expr {$repo_config(gui.tabsize) * $w}]
+       } else {
+               $ui_diff configure -tabs {}
+       }
+}
+
 proc clear_diff {} {
        global ui_diff current_diff_path current_diff_header
        global ui_index ui_workdir
@@ -42,7 +55,7 @@ proc reshow_diff {{after {}}} {
 
 proc force_diff_encoding {enc} {
        global current_diff_path
-       
+
        if {$current_diff_path ne {}} {
                force_path_encoding $current_diff_path $enc
                reshow_diff
@@ -105,6 +118,8 @@ proc show_diff {path w {lno {}} {scroll_pos {}} {callback {}}} {
 
        set cont_info [list $scroll_pos $callback]
 
+       apply_tab_size 0
+
        if {[string first {U} $m] >= 0} {
                merge_load_stages $path [list show_unmerged_diff $cont_info]
        } elseif {$m eq {_O}} {
@@ -112,6 +127,9 @@ proc show_diff {path w {lno {}} {scroll_pos {}} {callback {}}} {
        } else {
                start_show_diff $cont_info
        }
+
+       global current_diff_path selected_paths
+       set selected_paths($current_diff_path) 1
 }
 
 proc show_unmerged_diff {cont_info} {
@@ -205,10 +223,9 @@ proc show_other_diff {path w m cont_info} {
                }
                $ui_diff conf -state normal
                if {$type eq {submodule}} {
-                       $ui_diff insert end [append \
-                               "* " \
-                               [mc "Git Repository (subproject)"] \
-                               "\n"] d_info
+                       $ui_diff insert end \
+                               "* [mc "Git Repository (subproject)"]\n" \
+                               d_info
                } elseif {![catch {set type [exec file $path]}]} {
                        set n [string length $path]
                        if {[string equal -length $n $path $type]} {
@@ -287,6 +304,9 @@ proc start_show_diff {cont_info {add_opts {}}} {
        if {$w eq $ui_index} {
                lappend cmd diff-index
                lappend cmd --cached
+               if {[git-version >= "1.7.2"]} {
+                       lappend cmd --ignore-submodules=dirty
+               }
        } elseif {$w eq $ui_workdir} {
                if {[string first {U} $m] >= 0} {
                        lappend cmd diff
@@ -309,6 +329,7 @@ proc start_show_diff {cont_info {add_opts {}}} {
 
        lappend cmd -p
        lappend cmd --color
+       set cmd [concat $cmd $repo_config(gui.diffopts)]
        if {$repo_config(gui.diffcontext) >= 1} {
                lappend cmd "-U$repo_config(gui.diffcontext)"
        }
@@ -397,7 +418,10 @@ proc read_diff {fd conflict_size cont_info} {
 
                # -- Automatically detect if this is a 3 way diff.
                #
-               if {[string match {@@@ *} $line]} {set is_3way_diff 1}
+               if {[string match {@@@ *} $line]} {
+                       set is_3way_diff 1
+                       apply_tab_size 1
+               }
 
                if {$::current_diff_inheader} {
 
@@ -423,14 +447,9 @@ proc read_diff {fd conflict_size cont_info} {
                        # -- Name it symlink, not 120000
                        #    Note, that the original line is in $current_diff_header
                        regsub {^(deleted|new) file mode 120000} $line {\1 symlink} line
-               }
 
-               if {[string match {new file *} $line]
-                       || [regexp {^(old|new) mode *} $line]
-                       || [string match {deleted file *} $line]
-                       || [string match {deleted symlink} $line]
-                       || [string match {new symlink} $line]
-                       || $line eq {\ No newline at end of file}} {
+               } elseif {   $line eq {\ No newline at end of file}} {
+                       # -- Handle some special lines
                } elseif {$is_3way_diff} {
                        set op [string range $line 0 1]
                        switch -- $op {
@@ -507,9 +526,9 @@ proc read_diff {fd conflict_size cont_info} {
 
                foreach {posbegin colbegin posend colend} $markup {
                        set prefix clr
-                       foreach style [split $colbegin ";"] {
+                       foreach style [lsort -integer [split $colbegin ";"]] {
                                if {$style eq "7"} {append prefix i; continue}
-                               if {$style < 30 || $style > 47} {continue}
+                               if {$style != 4 && ($style < 30 || $style > 47)} {continue}
                                set a "$mark linestart + $posbegin chars"
                                set b "$mark linestart + $posend chars"
                                catch {$ui_diff tag add $prefix$style $a $b}
@@ -548,24 +567,31 @@ proc read_diff {fd conflict_size cont_info} {
        }
 }
 
-proc apply_hunk {x y} {
+proc apply_or_revert_hunk {x y revert} {
        global current_diff_path current_diff_header current_diff_side
-       global ui_diff ui_index file_states
+       global ui_diff ui_index file_states last_revert last_revert_enc
 
        if {$current_diff_path eq {} || $current_diff_header eq {}} return
        if {![lock_index apply_hunk]} return
 
-       set apply_cmd {apply --cached --whitespace=nowarn}
+       set apply_cmd {apply --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 hunk."]
-               lappend apply_cmd --reverse
+               lappend apply_cmd --reverse --cached
                if {[string index $mi 0] ne {M}} {
                        unlock_index
                        return
                }
        } else {
-               set failed_msg [mc "Failed to stage selected hunk."]
+               if {$revert} {
+                       set failed_msg [mc "Failed to revert selected hunk."]
+                       lappend apply_cmd --reverse
+               } else {
+                       set failed_msg [mc "Failed to stage selected hunk."]
+                       lappend apply_cmd --cached
+               }
+
                if {[string index $mi 1] ne {M}} {
                        unlock_index
                        return
@@ -584,29 +610,40 @@ proc apply_hunk {x y} {
                set e_lno end
        }
 
+       set wholepatch "$current_diff_header[$ui_diff get $s_lno $e_lno]"
+
        if {[catch {
                set enc [get_path_encoding $current_diff_path]
                set p [eval git_write $apply_cmd]
                fconfigure $p -translation binary -encoding $enc
-               puts -nonewline $p $current_diff_header
-               puts -nonewline $p [$ui_diff get $s_lno $e_lno]
+               puts -nonewline $p $wholepatch
                close $p} err]} {
-               error_popup [append $failed_msg "\n\n$err"]
+               error_popup "$failed_msg\n\n$err"
                unlock_index
                return
        }
 
+       if {$revert} {
+               # Save a copy of this patch for undoing reverts.
+               set last_revert $wholepatch
+               set last_revert_enc $enc
+       }
+
        $ui_diff conf -state normal
        $ui_diff delete $s_lno $e_lno
        $ui_diff conf -state disabled
 
+       # Check if the hunk was the last one in the file.
        if {[$ui_diff get 1.0 end] eq "\n"} {
                set o _
        } else {
                set o ?
        }
 
-       if {$current_diff_side eq $ui_index} {
+       # Update the status flags.
+       if {$revert} {
+               set mi [string index $mi 0]$o
+       } elseif {$current_diff_side eq $ui_index} {
                set mi ${o}M
        } elseif {[string index $mi 0] eq {_}} {
                set mi M$o
@@ -621,9 +658,9 @@ proc apply_hunk {x y} {
        }
 }
 
-proc apply_range_or_line {x y} {
+proc apply_or_revert_range_or_line {x y revert} {
        global current_diff_path current_diff_header current_diff_side
-       global ui_diff ui_index file_states
+       global ui_diff ui_index file_states last_revert
 
        set selected [$ui_diff tag nextrange sel 0.0]
 
@@ -641,19 +678,27 @@ proc apply_range_or_line {x y} {
        if {$current_diff_path eq {} || $current_diff_header eq {}} return
        if {![lock_index apply_hunk]} return
 
-       set apply_cmd {apply --cached --whitespace=nowarn}
+       set apply_cmd {apply --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
+               lappend apply_cmd --reverse --cached
                if {[string index $mi 0] ne {M}} {
                        unlock_index
                        return
                }
        } else {
-               set failed_msg [mc "Failed to stage selected line."]
-               set to_context {-}
+               if {$revert} {
+                       set failed_msg [mc "Failed to revert selected line."]
+                       set to_context {+}
+                       lappend apply_cmd --reverse
+               } else {
+                       set failed_msg [mc "Failed to stage selected line."]
+                       set to_context {-}
+                       lappend apply_cmd --cached
+               }
+
                if {[string index $mi 1] ne {M}} {
                        unlock_index
                        return
@@ -768,8 +813,15 @@ proc apply_range_or_line {x y} {
                                # context line
                                set ln [$ui_diff get $i_l $next_l]
                                set patch "$patch$pre_context$ln"
-                               set n [expr $n+1]
-                               set m [expr $m+1]
+                               # Skip the "\ No newline at end of
+                               # file". Depending on the locale setting
+                               # we don't know what this line looks
+                               # like exactly. The only thing we do
+                               # know is that it starts with "\ "
+                               if {![string match {\\ *} $ln]} {
+                                       set n [expr $n+1]
+                                       set m [expr $m+1]
+                               }
                                set pre_context {}
                        } elseif {$c1 eq $to_context} {
                                # turn change line into context line
@@ -802,8 +854,48 @@ proc apply_range_or_line {x y} {
                puts -nonewline $p $current_diff_header
                puts -nonewline $p $wholepatch
                close $p} err]} {
-               error_popup [append $failed_msg "\n\n$err"]
+               error_popup "$failed_msg\n\n$err"
+               unlock_index
+               return
+       }
+
+       if {$revert} {
+               # Save a copy of this patch for undoing reverts.
+               set last_revert $current_diff_header$wholepatch
+               set last_revert_enc $enc
+       }
+
+       unlock_index
+}
+
+# Undo the last line/hunk reverted. When hunks and lines are reverted, a copy
+# of the diff applied is saved. Re-apply that diff to undo the revert.
+#
+# Right now, we only use a single variable to hold the copy, and not a
+# stack/deque for simplicity, so multiple undos are not possible. Maybe this
+# can be added if the need for something like this is felt in the future.
+proc undo_last_revert {} {
+       global last_revert current_diff_path current_diff_header
+       global last_revert_enc
+
+       if {$last_revert eq {}} return
+       if {![lock_index apply_hunk]} return
+
+       set apply_cmd {apply --whitespace=nowarn}
+       set failed_msg [mc "Failed to undo last revert."]
+
+       if {[catch {
+               set enc $last_revert_enc
+               set p [eval git_write $apply_cmd]
+               fconfigure $p -translation binary -encoding $enc
+               puts -nonewline $p $last_revert
+               close $p} err]} {
+               error_popup "$failed_msg\n\n$err"
+               unlock_index
+               return
        }
 
+       set last_revert {}
+
        unlock_index
 }