Skip to content

Commit ae4e3ff

Browse files
trastpaulusmack
authored andcommitted
gitk: Add the equivalent of diff --color-words
Use the newly added 'diff --word-diff=porcelain' to teach gitk a color-words mode, with two different modes analogous to the --word-diff=plain and --word-diff=color settings. These are selected by a dropdown box. As an extra twist, automatically enable this word-diff support when the user mentions a word-diff related option on the command line. These options were previously ignored because they would break diff parsing. Both of these features are only enabled if we have a version of git that supports --word-diff=porcelain, meaning at least 1.7.2. Signed-off-by: Thomas Rast <[email protected]> Signed-off-by: Paul Mackerras <[email protected]>
1 parent 1cca16e commit ae4e3ff

File tree

1 file changed

+61
-3
lines changed

1 file changed

+61
-3
lines changed

gitk

Lines changed: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@ proc unmerged_files {files} {
131131

132132
proc parseviewargs {n arglist} {
133133
global vdatemode vmergeonly vflags vdflags vrevs vfiltered vorigargs env
134+
global worddiff git_version
134135

135136
set vdatemode($n) 0
136137
set vmergeonly($n) 0
@@ -168,7 +169,7 @@ proc parseviewargs {n arglist} {
168169
lappend diffargs $arg
169170
}
170171
"--raw" - "--patch-with-raw" - "--patch-with-stat" -
171-
"--name-only" - "--name-status" - "--color" - "--color-words" -
172+
"--name-only" - "--name-status" - "--color" -
172173
"--log-size" - "--pretty=*" - "--decorate" - "--abbrev-commit" -
173174
"--cc" - "-z" - "--header" - "--parents" - "--boundary" -
174175
"--no-color" - "-g" - "--walk-reflogs" - "--no-walk" -
@@ -177,6 +178,18 @@ proc parseviewargs {n arglist} {
177178
# These cause our parsing of git log's output to fail, or else
178179
# they're options we want to set ourselves, so ignore them.
179180
}
181+
"--color-words*" - "--word-diff=color" {
182+
# These trigger a word diff in the console interface,
183+
# so help the user by enabling our own support
184+
if {[package vcompare $git_version "1.7.2"] >= 0} {
185+
set worddiff [mc "Color words"]
186+
}
187+
}
188+
"--word-diff*" {
189+
if {[package vcompare $git_version "1.7.2"] >= 0} {
190+
set worddiff [mc "Markup words"]
191+
}
192+
}
180193
"--stat=*" - "--numstat" - "--shortstat" - "--summary" -
181194
"--check" - "--exit-code" - "--quiet" - "--topo-order" -
182195
"--full-history" - "--dense" - "--sparse" -
@@ -1972,6 +1985,8 @@ proc makewindow {} {
19721985
global fprogitem fprogcoord lastprogupdate progupdatepending
19731986
global rprogitem rprogcoord rownumsel numcommits
19741987
global have_tk85 use_ttk NS
1988+
global git_version
1989+
global worddiff
19751990

19761991
# The "mc" arguments here are purely so that xgettext
19771992
# sees the following string as needing to be translated
@@ -2245,6 +2260,15 @@ proc makewindow {} {
22452260
${NS}::checkbutton .bleft.mid.ignspace -text [mc "Ignore space change"] \
22462261
-command changeignorespace -variable ignorespace
22472262
pack .bleft.mid.ignspace -side left -padx 5
2263+
2264+
set worddiff [mc "Line diff"]
2265+
if {[package vcompare $git_version "1.7.2"] >= 0} {
2266+
makedroplist .bleft.mid.worddiff worddiff [mc "Line diff"] \
2267+
[mc "Markup words"] [mc "Color words"]
2268+
trace add variable worddiff write changeworddiff
2269+
pack .bleft.mid.worddiff -side left -padx 5
2270+
}
2271+
22482272
set ctext .bleft.bottom.ctext
22492273
text $ctext -background $bgcolor -foreground $fgcolor \
22502274
-state disabled -font textfont \
@@ -7504,11 +7528,16 @@ proc changeignorespace {} {
75047528
reselectline
75057529
}
75067530

7531+
proc changeworddiff {name ix op} {
7532+
reselectline
7533+
}
7534+
75077535
proc getblobdiffs {ids} {
75087536
global blobdifffd diffids env
75097537
global diffinhdr treediffs
75107538
global diffcontext
75117539
global ignorespace
7540+
global worddiff
75127541
global limitdiffs vfilelimit curview
75137542
global diffencoding targetline diffnparents
75147543
global git_version currdiffsubmod
@@ -7525,6 +7554,9 @@ proc getblobdiffs {ids} {
75257554
if {$ignorespace} {
75267555
append cmd " -w"
75277556
}
7557+
if {$worddiff ne [mc "Line diff"]} {
7558+
append cmd " --word-diff=porcelain"
7559+
}
75287560
if {$limitdiffs && $vfilelimit($curview) ne {}} {
75297561
set cmd [concat $cmd -- $vfilelimit($curview)]
75307562
}
@@ -7610,6 +7642,7 @@ proc getblobdiffline {bdf ids} {
76107642
global ctext_file_names ctext_file_lines
76117643
global diffinhdr treediffs mergemax diffnparents
76127644
global diffencoding jump_to_here targetline diffline currdiffsubmod
7645+
global worddiff
76137646

76147647
set nr 0
76157648
$ctext conf -state normal
@@ -7749,15 +7782,28 @@ proc getblobdiffline {bdf ids} {
77497782
# parse the prefix - one ' ', '-' or '+' for each parent
77507783
set prefix [string range $line 0 [expr {$diffnparents - 1}]]
77517784
set tag [expr {$diffnparents > 1? "m": "d"}]
7785+
set dowords [expr {$worddiff ne [mc "Line diff"] && $diffnparents == 1}]
7786+
set words_pre_markup ""
7787+
set words_post_markup ""
77527788
if {[string trim $prefix " -+"] eq {}} {
77537789
# prefix only has " ", "-" and "+" in it: normal diff line
77547790
set num [string first "-" $prefix]
7791+
if {$dowords} {
7792+
set line [string range $line 1 end]
7793+
}
77557794
if {$num >= 0} {
77567795
# removed line, first parent with line is $num
77577796
if {$num >= $mergemax} {
77587797
set num "max"
77597798
}
7760-
$ctext insert end "$line\n" $tag$num
7799+
if {$dowords && $worddiff eq [mc "Markup words"]} {
7800+
$ctext insert end "\[-$line-\]" $tag$num
7801+
} else {
7802+
$ctext insert end "$line" $tag$num
7803+
}
7804+
if {!$dowords} {
7805+
$ctext insert end "\n" $tag$num
7806+
}
77617807
} else {
77627808
set tags {}
77637809
if {[string first "+" $prefix] >= 0} {
@@ -7772,6 +7818,8 @@ proc getblobdiffline {bdf ids} {
77727818
lappend tags m$num
77737819
}
77747820
}
7821+
set words_pre_markup "{+"
7822+
set words_post_markup "+}"
77757823
}
77767824
if {$targetline ne {}} {
77777825
if {$diffline == $targetline} {
@@ -7781,8 +7829,17 @@ proc getblobdiffline {bdf ids} {
77817829
incr diffline
77827830
}
77837831
}
7784-
$ctext insert end "$line\n" $tags
7832+
if {$dowords && $worddiff eq [mc "Markup words"]} {
7833+
$ctext insert end "$words_pre_markup$line$words_post_markup" $tags
7834+
} else {
7835+
$ctext insert end "$line" $tags
7836+
}
7837+
if {!$dowords} {
7838+
$ctext insert end "\n" $tags
7839+
}
77857840
}
7841+
} elseif {$dowords && $prefix eq "~"} {
7842+
$ctext insert end "\n" {}
77867843
} else {
77877844
# "\ No newline at end of file",
77887845
# or something else we don't recognize
@@ -11393,6 +11450,7 @@ if {[tk windowingsystem] eq "win32"} {
1139311450
set diffcolors {red "#00a000" blue}
1139411451
set diffcontext 3
1139511452
set ignorespace 0
11453+
set worddiff ""
1139611454
set markbgcolor "#e0e0ff"
1139711455

1139811456
set circlecolors {white blue gray blue blue}

0 commit comments

Comments
 (0)