A better git diff --word-diff (--word-diff-regex) ?

Subsystems: the rest

3 messages, 3 authors, 2016-06-15 · open the first message on its own page

A better git diff --word-diff (--word-diff-regex) ?

From: Piotr Krukowiecki <hidden>
Date: 2016-06-15 22:53:20

Hi,

is there a way to configure --word-diff to be a more programming
language friendly? For example if I add one parameter to a function
declaration, I'd like to see only the addition of the parameter as the
change. But currently it shows much more.

For example if
  void foo(int x);
is changed to
  void foo(int x, int y);
I'd like to see only ",int y" as the change, not "x, int y);".

I think I'd like to ignore all white spaces and tokenize text on word
boundaries and see the diff between the tokens. This way if I e.g. add
a missing ";" it'll be shown as the only change.


$ echo 'void foo(int x);' > foo.c && git add foo.c && git commit -m "foo"
[master (root-commit) 5af60cb2] foo
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 foo.c

$ echo 'void foo(int x, int y);' > foo.c && git diff --word-diff
diff --git a/foo.c b/foo.c
index 42a5d4e9..289eb233 100644
--- a/foo.c
+++ b/foo.c
@@ -1 +1 @@
void foo(int [-x);-]{+x, int y);+}


Thanks,
-- 
Piotr Krukowiecki

Re: A better git diff --word-diff (--word-diff-regex) ?

From: Johannes Sixt <hidden>
Date: 2016-06-15 22:53:20

Am 3/19/2012 10:44, schrieb Piotr Krukowiecki:
is there a way to configure --word-diff to be a more programming
language friendly? For example if I add one parameter to a function
declaration, I'd like to see only the addition of the parameter as the
change. But currently it shows much more.

For example if
  void foo(int x);
is changed to
  void foo(int x, int y);
I'd like to see only ",int y" as the change, not "x, int y);".

I think I'd like to ignore all white spaces and tokenize text on word
boundaries and see the diff between the tokens. This way if I e.g. add
a missing ";" it'll be shown as the only change.
Start with

   git config diff.wordregex '[[:alnum:]]+|[^[:space:]]'

Adjust to your likings.

-- Hannes

Re: A better git diff --word-diff (--word-diff-regex) ?

From: Jeff King <hidden>
Date: 2016-06-15 22:53:20

On Mon, Mar 19, 2012 at 10:44:31AM +0100, Piotr Krukowiecki wrote:
is there a way to configure --word-diff to be a more programming
language friendly? For example if I add one parameter to a function
declaration, I'd like to see only the addition of the parameter as the
change. But currently it shows much more.

For example if
  void foo(int x);
is changed to
  void foo(int x, int y);
I'd like to see only ",int y" as the change, not "x, int y);".
This does not directly answer your question, but you may want to look at
the diff-highlight script in contrib/diff-highlight. I wrote it because
I found word-diff often unreadable, but still wanted something to
highlight small changes on a line like this.

On your example, it produces:

  -void foo(int x);
  +void foo(int x{+, int y});

except that the {+...} bit is highlighted by color, which makes it quite
readable.

-Peff
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help