gitk --color-words
cover gitk --color-words
This little pair of patches teaches gitk a color-words diff view.
To get there, I finally got rid of the assumption that --color-words only ever needs to output ANSI(?) color sequences. Hopefully this will help other people to use --color-words in nice frontends.
It uses an ad-hoc output format where newlines in the output are insignificant (so as to be able to speak about added/removed words) and newlines from the input are output as a line with only a '~'.
RFC among other things because
* I chose ~ more or less randomly. Do any diff implementations out there use it for some other purpose?
* The --color-words code was the only user of color_fwrite_lines() so I tweaked it according to my needs. But that makes it highly diff-specific again, so it's not in color.c any more. Did anyone else intend to use it?
* The Tk part of the patch is 95% voodoo of the cut&paste-from-elsewhere vintage and I have no clue whether it works only by accident
* I still need to do some things: add docs for --porcelain, add tests to the first patch, move the gitk patch to affect only the subtree
* I'm not even burning midnight oil any more
Thomas Rast (2):
Add diff --porcelain option for --color-words
gitk: add the equivalent of diff --color-words
color.c | 28 --------------- color.h | 1 - diff.c | 105 +++++++++++++++++++++++++++++++++++++++++++++++--------- diff.h | 1 + gitk-git/gitk | 29 +++++++++++++++- 5 files changed, 116 insertions(+), 48 deletions(-)
PATCH 1/2 Add diff --porcelain option for --color-words
This teaches the --color-words mode a new option --porcelain that disables color mode again and instead uses an ad-hoc format for the word diff designed for machine reading:
* Newlines in the output are no longer significant
* Each diff unit is prefixed by [-+ ] and terminated by newline as before
* Newlines in the input are output as a line consisting only of a tilde '~'
Note that for backwards compatibility --color-words still implies --color, so the order matters: if you say --porcelain --color-words, the hunk headers will be colored; if you say --color-words --porcelain, they won't.
--- color.c | 28 ----------------- color.h | 1 - diff.c | 105 ++++++++++++++++++++++++++++++++++++++++++++++++++++---------- diff.h | 1 + 4 files changed, 89 insertions(+), 46 deletions(-)
color.c +0 -28
@@ -211,31 +211,3 @@ int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)va_end (args);return r; } - -/* -* This function splits the buffer by newlines and colors the lines individually. -* -* Returns 0 on success. -*/ -int color_fwrite_lines (FILE *fp,const char *color, -size_t count,const char *buf) -{ -if (!*color) -return fwrite (buf, count,1 , fp) !=1 ; -while (count) { -char *p =memchr (buf,'\n' , count); -if (p != buf && (fputs (color, fp) <0 || -fwrite (buf, p ? p - buf : count,1 , fp) !=1 || -fputs (GIT_COLOR_RESET , fp) <0 )) -return -1 ; -if (!p) -return 0 ; -if (fputc ('\n' , fp) <0 ) -return -1 ; - count -= p +1 - buf; - buf = p +1 ; - } -return 0 ; -} - -
color.h +0 -1
@@ -61,6 +61,5 @@int color_fprintf (FILE *fp,const char *color,const char *fmt, ...); __attribute__((format (printf,3 ,4 )))int color_fprintf_ln (FILE *fp,const char *color,const char *fmt, ...); -int color_fwrite_lines (FILE *fp,const char *color,size_t count,const char *buf);#endif /* COLOR_H */
diff.c +88 -17
159 lines not on this page · raw message
diff.h +1 -0
@@ -71,6 +71,7 @@ typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,#define DIFF_OPT_SUBMODULE_LOG (1 << 23)#define DIFF_OPT_DIRTY_SUBMODULES (1 << 24)#define DIFF_OPT_IGNORE_UNTRACKED_IN_SUBMODULES (1 << 25) +#define DIFF_OPT_PORCELAIN (1 << 26)#define DIFF_OPT_TST (opts, flag) ((opts)->flags & DIFF_OPT_##flag)#define DIFF_OPT_SET (opts, flag) ((opts)->flags |= DIFF_OPT_##flag)
Signature · 1 line
1.7.0.3.522.ga3f6f.dirty
PATCH 2/2 gitk: add the equivalent of diff --color-words
In reply to Thomas RastUse the newly added 'diff --color-words --porcelain' to teach gitk a color-words mode, triggered by a checkbox.
--- gitk-git/gitk | 29 +++++++++++++++++++++++++++-- 1 files changed, 27 insertions(+), 2 deletions(-)
gitk-git/gitk +27 -2
@@ -2240,6 +2240,9 @@ proc makewindow {} { ${NS}::checkbutton .bleft.mid.ignspace -text [mc "Ignore space change"] \ -command changeignorespace -variable ignorespace pack .bleft.mid.ignspace -side left -padx 5 + ${NS}::checkbutton .bleft.mid.worddiff -text [mc "Word diff"] \ + -command changeworddiff -variable worddiff + pack .bleft.mid.worddiff -side left -padx 5 set ctext .bleft.bottom.ctext text $ctext -background $bgcolor -foreground $fgcolor \ -state disabled -font textfont \
@@ -7494,11 +7497,16 @@ proc changeignorespace {} { reselectline } +proc changeworddiff {} { + reselectline +} + proc getblobdiffs {ids} { global blobdifffd diffids env global diffinhdr treediffs global diffcontext global ignorespace + global worddiff global limitdiffs vfilelimit curview global diffencoding targetline diffnparents global git_version
@@ -7515,6 +7523,9 @@ proc getblobdiffs {ids} { if {$ignorespace} { append cmd " -w" } + if {$worddiff} { + append cmd " --color-words --porcelain" + } if {$limitdiffs && $vfilelimit($curview) ne {}} { set cmd [concat $cmd -- $vfilelimit($curview)] }
@@ -7599,6 +7610,7 @@ proc getblobdiffline {bdf ids} { global ctext_file_names ctext_file_lines global diffinhdr treediffs mergemax diffnparents global diffencoding jump_to_here targetline diffline + global worddiff set nr 0 $ctext conf -state normal
@@ -7727,15 +7739,22 @@ proc getblobdiffline {bdf ids} { # parse the prefix - one ' ', '-' or '+' for each parent set prefix [string range $line 0 [expr {$diffnparents - 1}]] set tag [expr {$diffnparents > 1? "m": "d"}] + puts "prefix is '$prefix'" if {[string trim $prefix " -+"] eq {}} { # prefix only has " ", "-" and "+" in it: normal diff line set num [string first "-" $prefix] + if {$worddiff} { + set line [string range $line 1 end] + } if {$num >= 0} { # removed line, first parent with line is $num if {$num >= $mergemax} { set num "max" } - $ctext insert end "$line\n" $tag$num + $ctext insert end "$line" $tag$num + if {!$worddiff} { + $ctext insert end "\n" $tag$num + } } else { set tags {} if {[string first "+" $prefix] >= 0} {
@@ -7759,8 +7778,13 @@ proc getblobdiffline {bdf ids} { incr diffline } } - $ctext insert end "$line\n" $tags + $ctext insert end "$line" $tags + if {!$worddiff} { + $ctext insert end "\n" $tags + } } + } elseif {$worddiff && $prefix eq "~"} { + $ctext insert end "\n" {} } else { # "\ No newline at end of file", # or something else we don't recognize
@@ -11379,6 +11403,7 @@ if {[tk windowingsystem] eq "win32"} { set diffcolors {red "#00a000" blue} set diffcontext 3 set ignorespace 0 +set worddiff 0 set markbgcolor "#e0e0ff" set circlecolors {white blue gray blue blue}
Signature · 1 line
1.7.0.3.522.ga3f6f.dirty
Re: [PATCH 2/2] gitk: add the equivalent of diff --color-words
gitk-git/gitk proc getblobdiffline {bdf ids} { · line 7742
Quoted 5 lines · Thomas Rast
gitk-git/gitk +1
-0@@ -7727,15 +7739,22 @@ proc getblobdiffline {bdf ids} { # parse the prefix - one ' ', '-' or '+' for each parent set prefix [string range $line 0 [expr {$diffnparents - 1}]] set tag [expr {$diffnparents > 1? "m": "d"}] + puts "prefix is '$prefix'"
This is a leftover debugging print and should be removed if you want to try it... sorry for the noise.
Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.chv2 cover gitk --color-words
In reply to Thomas RastSo here's a version intended for application.
[1/2] gained tests and docs.
[2/2] now disables the word-diff code for merge commits, since the underlying --color-words also just gives up and shows a combined diff. It also patches /gitk, i.e., it's intended for application inside the subtree.
I wasn't really sure about the last point -- this makes things a bit complicated since Paul will have to apply 2/2, and Junio will have to take care to only pull from Paul once 1/2 is in.
Thomas Rast (2):
Add diff --porcelain option for --color-words
gitk: add the equivalent of diff --color-words
PATCH v2 1/2 Add diff --porcelain option for --color-words
This teaches the --color-words mode a new option --porcelain that disables color mode again and instead uses an ad-hoc format for the word diff designed for machine reading:
* Newlines in the output are no longer significant
* Each diff unit is prefixed by [-+ ] and terminated by newline as before
* Newlines in the input are output as a line consisting only of a tilde '~'
Note that for backwards compatibility --color-words still implies --color, so the order matters: if you say --porcelain --color-words, the hunk headers will be colored; if you say --color-words --porcelain, they won't.
--- Documentation/diff-options.txt | 11 ++++ color.c | 28 ----------- color.h | 1 - diff.c | 105 +++++++++++++++++++++++++++++++++------- diff.h | 1 + t/t4034-diff-words.sh | 27 ++++++++++ 6 files changed, 127 insertions(+), 46 deletions(-)
Documentation/diff-options.txt +11 -0
@@ -142,6 +142,17 @@ The regex can also be set via a diff driver or configuration option, see linkgit:gitattributes[1] or linkgit:git-config[1]. Giving it explicitly overrides any diff driver or configuration setting. Diff drivers override configuration settings. ++ +With `--porcelain`, use a variant of the unified diff format: +Added/removed/unchanged runs of words are printed as a line starting +with `+`/`-`/` ` (respectively) and terminated by a newline. Newlines +in the input are shown as a tilde `~` on a line of its own. + +--porcelain:: + Switch to plumbing mode, designed for consumption by a + porcelain script. Currently, this only affects + `--color-words`, which see. + endif::git-format-patch[] --no-renames::
color.c +0 -28
@@ -211,31 +211,3 @@ int color_fprintf_ln(FILE *fp, const char *color, const char *fmt, ...)va_end (args);return r; } - -/* -* This function splits the buffer by newlines and colors the lines individually. -* -* Returns 0 on success. -*/ -int color_fwrite_lines (FILE *fp,const char *color, -size_t count,const char *buf) -{ -if (!*color) -return fwrite (buf, count,1 , fp) !=1 ; -while (count) { -char *p =memchr (buf,'\n' , count); -if (p != buf && (fputs (color, fp) <0 || -fwrite (buf, p ? p - buf : count,1 , fp) !=1 || -fputs (GIT_COLOR_RESET , fp) <0 )) -return -1 ; -if (!p) -return 0 ; -if (fputc ('\n' , fp) <0 ) -return -1 ; - count -= p +1 - buf; - buf = p +1 ; - } -return 0 ; -} - -
color.h +0 -1
@@ -61,6 +61,5 @@int color_fprintf (FILE *fp,const char *color,const char *fmt, ...); __attribute__((format (printf,3 ,4 )))int color_fprintf_ln (FILE *fp,const char *color,const char *fmt, ...); -int color_fwrite_lines (FILE *fp,const char *color,size_t count,const char *buf);#endif /* COLOR_H */
diff.c +88 -17
159 lines not on this page · raw message
diff.h +1 -0
@@ -71,6 +71,7 @@ typedef void (*diff_format_fn_t)(struct diff_queue_struct *q,#define DIFF_OPT_SUBMODULE_LOG (1 << 23)#define DIFF_OPT_DIRTY_SUBMODULES (1 << 24)#define DIFF_OPT_IGNORE_UNTRACKED_IN_SUBMODULES (1 << 25) +#define DIFF_OPT_PORCELAIN (1 << 26)#define DIFF_OPT_TST (opts, flag) ((opts)->flags & DIFF_OPT_##flag)#define DIFF_OPT_SET (opts, flag) ((opts)->flags |= DIFF_OPT_##flag)
t/t4034-diff-words.sh +27 -0
@@ -55,6 +55,33 @@ test_expect_success 'word diff with runs of whitespace' ' ' +sed 's/#.*$//' > expect <<EOF +diff --git a/pre b/post +index 330b04f..5ed8eff 100644 +--- a/pre ++++ b/post +@@ -1,3 +1,7 @@ +-h(4) ++h(4),hh[44] +~ + # significant space +~ + a = b + c +~ +~ ++aa = a +~ +~ ++aeff = aeff * ( aaa ) +~ +EOF + +test_expect_success '--porcelain' ' + + word_diff --color-words --porcelain + +' + cat > expect <<\EOF <WHITE>diff --git a/pre b/post<RESET> <WHITE>index 330b04f..5ed8eff 100644<RESET>
Signature · 1 line
1.7.0.4.504.g4d2c
Re: [PATCH v2 1/2] Add diff --porcelain option for --color-words
Quoted 3 lines · Thomas Rast
This teaches the --color-words mode a new option --porcelain that disables color mode again and instead uses an ad-hoc format for the word diff designed for machine reading.
How readable can you make this for human consumption while still keeping it machine readable? The answer could be it already is human readble.
Two reasons I ask the above question are that I find the feature quite interesting, and would want to see if it can be also fed to humans, and that the combination of this new option and the existing --color-words is misnamed.
What you are giving "git diff" is "word-level diff" as opposed to the usual "line-level diff", isn't it? The machinery may have already been there, but it had a hardwired representation of the result to color pre- and post- image words, and you are giving the result from the machinery another representation with this patch. If you call this --word-diff, then it would become more clear that --color-words perhaps should have been called --word-diff=color or something.
Besides, --porcelain invites "what does it do without --color-words?", to which you wouldn't have a good answer, as non word-diff output is already machine readable.
Re: [PATCH v2 1/2] Add diff --porcelain option for --color-words
Quoted 7 lines · Junio C Hamano
How readable can you make this for human consumption while still keeping it machine readable? The answer could be it already is human readble.
Two reasons I ask the above question are that I find the feature quite interesting, and would want to see if it can be also fed to humans, and that the combination of this new option and the existing --color-words is misnamed.
There's the format used by the "wdiff" program, which is more like traditional diff output in that it doesn't use color, but is human friendly, and also seems to be somewhat machine-parseable:
$ echo 'This is a test' > /tmp/a
$ echo 'This is funky test' > /tmp/b
$ wdiff /tmp/a /tmp/b
This is [-a-] {+funky+} test[I say "somewhat" because wdiff itself doesn't appear to escape potentially ambiguous content, e.g., if there's actually a "{+" in the file....]
-Miles
Signature · 2 lines
Marriage, n. The state or condition of a community consisting of a master, a mistress and two slaves, making in all, two.
Quoted 3 lines · Thomas Rast
I wasn't really sure about the last point -- this makes things a bit complicated since Paul will have to apply 2/2, and Junio will have to take care to only pull from Paul once 1/2 is in.
You mean that the patch will mean that gitk will break if the underlying git isn't new enough? I have been careful to avoid that sort of dependency as far as possible. Have a look at how this is handled for the --textconv and --submodule options in [getblobdiffs] and do something similar for --color-words, please.
Paul.
Quoted 11 lines · Paul Mackerras
Quoted 3 lines · Thomas Rast
I wasn't really sure about the last point -- this makes things a bit complicated since Paul will have to apply 2/2, and Junio will have to take care to only pull from Paul once 1/2 is in.
You mean that the patch will mean that gitk will break if the underlying git isn't new enough? I have been careful to avoid that sort of dependency as far as possible. Have a look at how this is handled for the --textconv and --submodule options in [getblobdiffs] and do something similar for --color-words, please.
Ok, I'll look into that.
Signature · 2 lines
Thomas Rast
trast@{inf,student}.ethz.chPATCH v2 2/2 gitk: add the equivalent of diff --color-words
In reply to Thomas RastUse the newly added 'diff --color-words --porcelain' to teach gitk a color-words mode, triggered by a checkbox.
--- gitk | 29 +++++++++++++++++++++++++++-- 1 files changed, 27 insertions(+), 2 deletions(-)
gitk +27 -2
@@ -2240,6 +2240,9 @@ proc makewindow {} { ${NS}::checkbutton .bleft.mid.ignspace -text [mc "Ignore space change"] \ -command changeignorespace -variable ignorespace pack .bleft.mid.ignspace -side left -padx 5 + ${NS}::checkbutton .bleft.mid.worddiff -text [mc "Word diff"] \ + -command changeworddiff -variable worddiff + pack .bleft.mid.worddiff -side left -padx 5 set ctext .bleft.bottom.ctext text $ctext -background $bgcolor -foreground $fgcolor \ -state disabled -font textfont \
@@ -7494,11 +7497,16 @@ proc changeignorespace {} { reselectline } +proc changeworddiff {} { + reselectline +} + proc getblobdiffs {ids} { global blobdifffd diffids env global diffinhdr treediffs global diffcontext global ignorespace + global worddiff global limitdiffs vfilelimit curview global diffencoding targetline diffnparents global git_version
@@ -7515,6 +7523,9 @@ proc getblobdiffs {ids} { if {$ignorespace} { append cmd " -w" } + if {$worddiff} { + append cmd " --color-words --porcelain" + } if {$limitdiffs && $vfilelimit($curview) ne {}} { set cmd [concat $cmd -- $vfilelimit($curview)] }
@@ -7599,6 +7610,7 @@ proc getblobdiffline {bdf ids} { global ctext_file_names ctext_file_lines global diffinhdr treediffs mergemax diffnparents global diffencoding jump_to_here targetline diffline + global worddiff set nr 0 $ctext conf -state normal
@@ -7727,15 +7739,22 @@ proc getblobdiffline {bdf ids} { # parse the prefix - one ' ', '-' or '+' for each parent set prefix [string range $line 0 [expr {$diffnparents - 1}]] set tag [expr {$diffnparents > 1? "m": "d"}] + set dowords [expr {$worddiff && $diffnparents == 1}] if {[string trim $prefix " -+"] eq {}} { # prefix only has " ", "-" and "+" in it: normal diff line set num [string first "-" $prefix] + if {$dowords} { + set line [string range $line 1 end] + } if {$num >= 0} { # removed line, first parent with line is $num if {$num >= $mergemax} { set num "max" } - $ctext insert end "$line\n" $tag$num + $ctext insert end "$line" $tag$num + if {!$dowords} { + $ctext insert end "\n" $tag$num + } } else { set tags {} if {[string first "+" $prefix] >= 0} {
@@ -7759,8 +7778,13 @@ proc getblobdiffline {bdf ids} { incr diffline } } - $ctext insert end "$line\n" $tags + $ctext insert end "$line" $tags + if {!$dowords} { + $ctext insert end "\n" $tags + } } + } elseif {$dowords && $prefix eq "~"} { + $ctext insert end "\n" {} } else { # "\ No newline at end of file", # or something else we don't recognize
@@ -11379,6 +11403,7 @@ if {[tk windowingsystem] eq "win32"} { set diffcolors {red "#00a000" blue} set diffcontext 3 set ignorespace 0 +set worddiff 0 set markbgcolor "#e0e0ff" set circlecolors {white blue gray blue blue}
Signature · 1 line
1.7.0.4.504.g4d2c