git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: Something wrong with diff --color-words=regexp?

From
Johannes Sixt <j6t@kdbg.org>
Date
Feb 20, 2015, 07:49 UTC
Message-ID
<54E6E6F5.2020202@kdbg.org>
In-Reply-To
<20150219235213.GA1291@glandium.org>
Am 20.02.2015 um 00:52 schrieb Mike Hommey:
Show 32 quoted lines
> Hi,
>
> I was trying to use --color-words with a regex to check a diff, and it appears
> it displays things out of order. Am I misunderstanding what my regexp should be
> doing or is there a bug?
>
> $ git diff -U3 HEAD^ dom/base/nsDOMFileReader.cpp
> diff --git a/dom/base/nsDOMFileReader.cpp b/dom/base/nsDOMFileReader.cpp
> index 6267e0e..fa22590 100644
> --- a/dom/base/nsDOMFileReader.cpp
> +++ b/dom/base/nsDOMFileReader.cpp
> @@ -363,7 +363,7 @@ nsDOMFileReader::DoReadData(nsIAsyncInputStream* aStream, uint64_t aCount)
>         return NS_ERROR_OUT_OF_MEMORY;
>       }
>       if (mDataFormat != FILE_AS_ARRAYBUFFER) {
> -      mFileData = (char *) moz_realloc(mFileData, mDataLen + aCount);
> +      mFileData = (char *) realloc(mFileData, mDataLen + aCount);
>         NS_ENSURE_TRUE(mFileData, NS_ERROR_OUT_OF_MEMORY);
>       }
>
> $ git diff -U3 --color-words='[^ ()]' HEAD^ dom/base/nsDOMFileReader.cpp
> diff --git a/dom/base/nsDOMFileReader.cpp b/dom/base/nsDOMFileReader.cpp
> index 6267e0e..fa22590 100644
> --- a/dom/base/nsDOMFileReader.cpp
> +++ b/dom/base/nsDOMFileReader.cpp
> @@ -363,7 +363,7 @@ nsDOMFileReader::DoReadData(nsIAsyncInputStream* aStream, uint64_t aCount)
>        return NS_ERROR_OUT_OF_MEMORY;
>      }
>      if (mDataFormat != FILE_AS_ARRAYBUFFER) {
>        mFileData = (char *moz_) realloc(mFileData, mDataLen + aCount);
>        NS_ENSURE_TRUE(mFileData, NS_ERROR_OUT_OF_MEMORY);
>      }

Your regexp says that every character (with a few exceptions) by itself is a word. Your diff says that it deleted the words 'm', 'o', 'z', and '_'. So, that is not wrong.

Furthermore, your regexp says that space, '(' and ')' are whitespace. Whitespace is *ignored* for computation of the word difference. Nevertheless, --color-word mode helpfully keeps the whitespace of the post-image to produce readable output. In doing so, it has to choose whether to keep the whitespace before or after a word. It chooses to keep it before a word. Hence, you see the whitespace sequence ') ' attached in front of 'r' (of 'realloc') instead of after '*'. So, the procedure is a matter of choice, which sometimes does not match expectations.

Perhaps you meant to say
     --color-words='[^ ()]+'
to split the diff text into longer words.
-- Hannes
Previous: Mike Hommey
Message 2 of 2 in “Something wrong with diff --color-words=regexp?”
  1. Mike HommeyFeb 19, 2015
  2. Johannes SixtFeb 20, 2015

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.