git-gui: Refactored diff line display formatting logic.
[gitweb.git] / git-gui
diff --git a/git-gui b/git-gui
index 7126f8d897dc7780c692ea25526a827909775421..a2a76c11dda447b97903c938857a6b6154e07ba1 100755 (executable)
--- a/git-gui
+++ b/git-gui
@@ -369,13 +369,14 @@ proc read_diff_index {fd after} {
                if {$z2 == -1} break
 
                incr c
-               set n [split [string range $buf_rdi $c [expr {$z1 - 2}]] { }]
+               set i [split [string range $buf_rdi $c [expr {$z1 - 2}]] { }]
                merge_state \
                        [string range $buf_rdi $z1 [expr {$z2 - 1}]] \
-                       [lindex $n 4]? \
-                       [list [lindex $n 0] [lindex $n 2]] \
+                       [lindex $i 4]? \
+                       [list [lindex $i 0] [lindex $i 2]] \
                        [list]
                set c $z2
+               incr c
        }
        if {$c < $n} {
                set buf_rdi [string range $buf_rdi $c end]
@@ -400,13 +401,14 @@ proc read_diff_files {fd after} {
                if {$z2 == -1} break
 
                incr c
-               set n [split [string range $buf_rdf $c [expr {$z1 - 2}]] { }]
+               set i [split [string range $buf_rdf $c [expr {$z1 - 2}]] { }]
                merge_state \
                        [string range $buf_rdf $z1 [expr {$z2 - 1}]] \
-                       ?[lindex $n 4] \
+                       ?[lindex $i 4] \
                        [list] \
-                       [list [lindex $n 0] [lindex $n 2]]
+                       [list [lindex $i 0] [lindex $i 2]]
                set c $z2
+               incr c
        }
        if {$c < $n} {
                set buf_rdf [string range $buf_rdf $c end]
@@ -542,7 +544,7 @@ files list, to prevent possible confusion.
 
 proc show_diff {path {w {}} {lno {}}} {
        global file_states file_lists
-       global diff_3way diff_active repo_config
+       global is_3way_diff diff_active repo_config
        global ui_diff current_diff ui_status_value
 
        if {$diff_active || ![lock_index read]} return
@@ -563,7 +565,7 @@ proc show_diff {path {w {}} {lno {}}} {
 
        set s $file_states($path)
        set m [lindex $s 0]
-       set diff_3way 0
+       set is_3way_diff 0
        set diff_active 1
        set current_diff $path
        set ui_status_value "Loading diff of [escape_path $path]..."
@@ -618,48 +620,52 @@ proc show_diff {path {w {}} {lno {}}} {
 }
 
 proc read_diff {fd} {
-       global ui_diff ui_status_value diff_3way diff_active
+       global ui_diff ui_status_value is_3way_diff diff_active
        global repo_config
 
+       $ui_diff conf -state normal
        while {[gets $fd line] >= 0} {
-               if {[string match {diff --git *} $line]} continue
+               # -- Cleanup uninteresting diff header lines.
+               #
+               if {[string match {diff --git *}      $line]} continue
                if {[string match {diff --combined *} $line]} continue
-               if {[string match {--- *} $line]} continue
-               if {[string match {+++ *} $line]} continue
-               if {[string match index* $line]} {
-                       if {[string first , $line] >= 0} {
-                               set diff_3way 1
-                       }
+               if {[string match {--- *}             $line]} continue
+               if {[string match {+++ *}             $line]} continue
+               if {$line eq {deleted file mode 120000}} {
+                       set line "deleted symlink"
                }
 
-               $ui_diff conf -state normal
-               if {!$diff_3way} {
-                       set x [string index $line 0]
-                       switch -- $x {
-                       "@" {set tags da}
-                       "+" {set tags dp}
-                       "-" {set tags dm}
+               # -- Automatically detect if this is a 3 way diff.
+               #
+               if {[string match {@@@ *} $line]} {set is_3way_diff 1}
+
+               # -- Reformat a 3 way diff, 'cause its too weird.
+               #
+               if {$is_3way_diff} {
+                       set op [string range $line 0 1]
+                       switch -- $op {
+                       {@@} {set tags d_@}
+                       {++} {set tags d_+ ; set op { +}}
+                       {--} {set tags d_- ; set op { -}}
+                       { +} {set tags d_++; set op {++}}
+                       { -} {set tags d_--; set op {--}}
+                       {+ } {set tags d_-+; set op {-+}}
+                       {- } {set tags d_+-; set op {+-}}
                        default {set tags {}}
                        }
+                       set line [string replace $line 0 1 $op]
                } else {
-                       set x [string range $line 0 1]
-                       switch -- $x {
-                       default {set tags {}}
-                       "@@" {set tags da}
-                       "++" {set tags dp; set x " +"}
-                       " +" {set tags {di bold}; set x "++"}
-                       "+ " {set tags dni; set x "-+"}
-                       "--" {set tags dm; set x " -"}
-                       " -" {set tags {dm bold}; set x "--"}
-                       "- " {set tags di; set x "+-"}
+                       switch -- [string index $line 0] {
+                       @ {set tags d_@}
+                       + {set tags d_+}
+                       - {set tags d_-}
                        default {set tags {}}
                        }
-                       set line [string replace $line 0 1 $x]
                }
                $ui_diff insert end $line $tags
-               $ui_diff insert end "\n"
-               $ui_diff conf -state disabled
+               $ui_diff insert end "\n" $tags
        }
+       $ui_diff conf -state disabled
 
        if {[eof $fd]} {
                close $fd
@@ -1022,6 +1028,8 @@ proc commit_committree {fd_wt curHEAD msg} {
                set s $file_states($path)
                set m [lindex $s 0]
                switch -glob -- $m {
+               DD -
+               AO {set m __}
                A? -
                M? -
                D? {set m _[string index $m 1]}
@@ -1167,9 +1175,10 @@ proc short_path {path} {
 }
 
 set next_icon_id 0
+set null_sha1 [string repeat 0 40]
 
 proc merge_state {path new_state {head_info {}} {index_info {}}} {
-       global file_states next_icon_id
+       global file_states next_icon_id null_sha1
 
        set s0 [string index $new_state 0]
        set s1 [string index $new_state 1]
@@ -1190,7 +1199,9 @@ proc merge_state {path new_state {head_info {}} {index_info {}}} {
        if     {$s1 eq {?}} {set s1 [string index $state 1]} \
        elseif {$s1 eq {_}} {set s1 _}
 
-       if {$s0 ne {_} && [string index $state 0] eq {_}
+       if {$s0 eq {A} && $s1 eq {_} && $head_info eq {}} {
+               set head_info [list 0 $null_sha1]
+       } elseif {$s0 ne {_} && [string index $state 0] eq {_}
                && $head_info eq {}} {
                set head_info $index_info
        }
@@ -1409,7 +1420,7 @@ proc write_update_index {fd pathList totalCnt batch msg after} {
                switch -glob -- [lindex $file_states($path) 0] {
                AD -
                MD -
-               _D {set new D_}
+               _D {set new DD}
 
                _M -
                MM -
@@ -2414,6 +2425,7 @@ proc toggle_or_diff {w x y} {
                A_ -
                AO -
                M_ -
+               DD -
                D_ {
                        update_indexinfo \
                                "Removing [short_path $path] from commit" \
@@ -2976,12 +2988,17 @@ pack $ui_diff -side left -fill both -expand 1
 pack .vpane.lower.diff.header -side top -fill x
 pack .vpane.lower.diff.body -side bottom -fill both -expand 1
 
-$ui_diff tag conf dm -foreground red
-$ui_diff tag conf dp -foreground blue
-$ui_diff tag conf di -foreground {#00a000}
-$ui_diff tag conf dni -foreground {#a000a0}
-$ui_diff tag conf da -font font_diffbold
-$ui_diff tag conf bold -font font_diffbold
+$ui_diff tag conf d_@ -font font_diffbold
+$ui_diff tag conf d_+  -foreground blue
+$ui_diff tag conf d_-  -foreground red
+$ui_diff tag conf d_++ -foreground {#00a000}
+$ui_diff tag conf d_-- -foreground {#a000a0}
+$ui_diff tag conf d_+- \
+       -foreground red \
+       -background {light goldenrod yellow}
+$ui_diff tag conf d_-+ \
+       -foreground blue \
+       -background azure2
 
 # -- Diff Body Context Menu
 #