All of lore.kernel.org
 help / color / mirror / Atom feed
* A better git diff --word-diff (--word-diff-regex) ?
@ 2012-03-19  9:44 Piotr Krukowiecki
  2012-03-19  9:47 ` Matthieu Moy
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Piotr Krukowiecki @ 2012-03-19  9:44 UTC (permalink / raw)
  To: Git Mailing List

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

^ permalink raw reply related	[flat|nested] 7+ messages in thread

* Re: A better git diff --word-diff (--word-diff-regex) ?
  2012-03-19  9:44 A better git diff --word-diff (--word-diff-regex) ? Piotr Krukowiecki
@ 2012-03-19  9:47 ` Matthieu Moy
  2012-03-19 16:54   ` Piotr Krukowiecki
  2012-03-19  9:50 ` Thomas Rast
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Matthieu Moy @ 2012-03-19  9:47 UTC (permalink / raw)
  To: Piotr Krukowiecki; +Cc: Git Mailing List

Piotr Krukowiecki <piotr.krukowiecki@gmail.com> writes:

> Hi,
>
> is there a way to configure --word-diff to be a more programming
> language friendly?

I often use this:

  git diff --color-words=.

It's rather bad on plain text, since it will try to diff within words,
but works well on source code.

-- 
Matthieu Moy
http://www-verimag.imag.fr/~moy/

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: A better git diff --word-diff (--word-diff-regex) ?
  2012-03-19  9:44 A better git diff --word-diff (--word-diff-regex) ? Piotr Krukowiecki
  2012-03-19  9:47 ` Matthieu Moy
@ 2012-03-19  9:50 ` Thomas Rast
  2012-03-19 17:06   ` Piotr Krukowiecki
  2012-03-19  9:58 ` Johannes Sixt
  2012-03-19 15:10 ` Jeff King
  3 siblings, 1 reply; 7+ messages in thread
From: Thomas Rast @ 2012-03-19  9:50 UTC (permalink / raw)
  To: Piotr Krukowiecki; +Cc: Git Mailing List

Piotr Krukowiecki <piotr.krukowiecki@gmail.com> writes:

> 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.

Umm, what's wrong with

  echo '*.cpp diff=cpp' >>.git/info/attributes

Ok, the funcname patterns aren't so good, but the word regex is designed
to "tokenize" as far as that is feasible.

-- 
Thomas Rast
trast@{inf,student}.ethz.ch

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: A better git diff --word-diff (--word-diff-regex) ?
  2012-03-19  9:44 A better git diff --word-diff (--word-diff-regex) ? Piotr Krukowiecki
  2012-03-19  9:47 ` Matthieu Moy
  2012-03-19  9:50 ` Thomas Rast
@ 2012-03-19  9:58 ` Johannes Sixt
  2012-03-19 15:10 ` Jeff King
  3 siblings, 0 replies; 7+ messages in thread
From: Johannes Sixt @ 2012-03-19  9:58 UTC (permalink / raw)
  To: Piotr Krukowiecki; +Cc: Git Mailing List

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: A better git diff --word-diff (--word-diff-regex) ?
  2012-03-19  9:44 A better git diff --word-diff (--word-diff-regex) ? Piotr Krukowiecki
                   ` (2 preceding siblings ...)
  2012-03-19  9:58 ` Johannes Sixt
@ 2012-03-19 15:10 ` Jeff King
  3 siblings, 0 replies; 7+ messages in thread
From: Jeff King @ 2012-03-19 15:10 UTC (permalink / raw)
  To: Piotr Krukowiecki; +Cc: Git Mailing List

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

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: A better git diff --word-diff (--word-diff-regex) ?
  2012-03-19  9:47 ` Matthieu Moy
@ 2012-03-19 16:54   ` Piotr Krukowiecki
  0 siblings, 0 replies; 7+ messages in thread
From: Piotr Krukowiecki @ 2012-03-19 16:54 UTC (permalink / raw)
  To: Matthieu Moy; +Cc: Git Mailing List

On Mon, Mar 19, 2012 at 10:47 AM, Matthieu Moy
<Matthieu.Moy@grenoble-inp.fr> wrote:
> Piotr Krukowiecki <piotr.krukowiecki@gmail.com> writes:
>
>> Hi,
>>
>> is there a way to configure --word-diff to be a more programming
>> language friendly?
>
> I often use this:
>
>  git diff --color-words=.
>
> It's rather bad on plain text, since it will try to diff within words,
> but works well on source code.

Hi,

thanks for the idea, it's surely useful in many cases, but it can look
confusing on source code too. For example on git b3256eb8b in path.c
one part of the diff is

-                       if (!access(used_path, F_OK)) {
+                       if (!stat(used_path, &st) &&
+                           (S_ISREG(st.st_mode) ||
+                           (S_ISDIR(st.st_mode) &&
is_git_directory(used_path)))) {

and "git diff --word-diff-regex=." shows

                        if (![-acces-]s{+tat+}(used_path, [-F-]{+&st) &&+}
{+                          (S+}_[-OK-]{+ISREG(st.st_mode) ||+}
{+                          (S_ISDIR(st.st_mode) &&
is_git_directory(used_path))+})) {



-- 
Piotr Krukowiecki

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: A better git diff --word-diff (--word-diff-regex) ?
  2012-03-19  9:50 ` Thomas Rast
@ 2012-03-19 17:06   ` Piotr Krukowiecki
  0 siblings, 0 replies; 7+ messages in thread
From: Piotr Krukowiecki @ 2012-03-19 17:06 UTC (permalink / raw)
  To: Thomas Rast; +Cc: Git Mailing List

On Mon, Mar 19, 2012 at 10:50 AM, Thomas Rast <trast@student.ethz.ch> wrote:
> Piotr Krukowiecki <piotr.krukowiecki@gmail.com> writes:
>
>> 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.
>
> Umm, what's wrong with
>
>  echo '*.cpp diff=cpp' >>.git/info/attributes
>
> Ok, the funcname patterns aren't so good, but the word regex is designed
> to "tokenize" as far as that is feasible.

Thanks, that's very good! I've missed it reading the git-diff man page.

-- 
Piotr Krukowiecki

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-03-19 17:06 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-19  9:44 A better git diff --word-diff (--word-diff-regex) ? Piotr Krukowiecki
2012-03-19  9:47 ` Matthieu Moy
2012-03-19 16:54   ` Piotr Krukowiecki
2012-03-19  9:50 ` Thomas Rast
2012-03-19 17:06   ` Piotr Krukowiecki
2012-03-19  9:58 ` Johannes Sixt
2012-03-19 15:10 ` Jeff King

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.