{"thread":{"id":"19620","subject":"whitespace ignoring during diff -M","startedAt":"2009-05-31T20:28:50Z","lastAt":"2009-06-28T20:02:55Z","messageCount":2,"participants":["Daniel Mierswa","Jeff King"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"115164","messageId":"4A22E882.8020500@impulze.org","threadId":"19620","inReplyTo":null,"subject":"whitespace ignoring during diff -M","fromName":"Daniel Mierswa","fromEmail":"impulze@impulze.org","sentAt":"2009-05-31T20:28:50Z","receivedAt":"2009-05-31T20:28:50Z","isPatch":false,"sender":{"key":"impulze@impulze.org","avatar":null},"body":"Hi list,\nI was told to try it here after visiting #git/Freenode\nI want git to think that the diff of two branches where filenames and\nwhitespace amount differ are the same.\nThe following is a snippet from my terminal with output, is there a\nchance to make git think that those are equal?\n\nimpulze@istari ~/gittest $ git init\nInitialized empty Git repository in /home/impulze/gittest/.git/\nimpulze@istari ~/gittest $ touch initial\nimpulze@istari ~/gittest $ git add initial && git commit initial -m\n'initial commit'\n[master (root-commit) 7b67dcd] initial commit\n 0 files changed, 0 insertions(+), 0 deletions(-)\n create mode 100644 initial\nimpulze@istari ~/gittest $ git checkout -b another\nSwitched to a new branch 'another'\nimpulze@istari ~/gittest $ echo -e \"   abcdef   \\n   ghijkl \" > file.cc\nimpulze@istari ~/gittest $ unix2dos -a -u file.cc\nimpulze@istari ~/gittest $ git add file.cc && git commit file.cc -m\n'another commit'\n[another 37826f4] another commit\n 1 files changed, 2 insertions(+), 0 deletions(-)\n create mode 100644 file.cc\nimpulze@istari ~/gittest $ git checkout master\nSwitched to branch 'master'\nimpulze@istari ~/gittest $ echo -e \"\\t\\tabcdef\\t\\n\\tghijkl\\t\" > file.c\nimpulze@istari ~/gittest $ git add file.c && git commit file.c -m\n'master commit'\n[master f9f0ac5] master commit\n 1 files changed, 2 insertions(+), 0 deletions(-)\n create mode 100644 file.c\nimpulze@istari ~/gittest $ git --no-pager diff another -M -w\ndiff --git a/file.c b/file.c\nnew file mode 100644\nindex 0000000..18364be\n--- /dev/null\n+++ b/file.c\n@@ -0,0 +1,2 @@\n+               abcdef\n+       ghijkl\ndiff --git a/file.cc b/file.cc\ndeleted file mode 100644\nindex 1a303ea..0000000\n--- a/file.cc\n+++ /dev/null\n@@ -1,2 +0,0 @@\n-   abcdef\n-   ghijkl\n\n\n-- \nMierswa, Daniel\n\nIf you still don't like it, that's ok: that's why I'm boss. I simply\nknow better than you do.\n               --- Linus Torvalds, comp.os.linux.advocacy, 1996/07/22\n"},{"id":"117141","messageId":"20090628200254.GA8828@sigio.peff.net","threadId":"19620","inReplyTo":"4A22E882.8020500@impulze.org","subject":"Re: whitespace ignoring during diff -M","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2009-06-28T20:02:55Z","receivedAt":"2009-06-28T20:02:55Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"[this is a bit of an old message, but I am way behind on git mail,\n and nobody else seems to have responded, so...]\n\nOn Sun, May 31, 2009 at 10:28:50PM +0200, Daniel Mierswa wrote:\n\n> I was told to try it here after visiting #git/Freenode\n> I want git to think that the diff of two branches where filenames and\n> whitespace amount differ are the same.\n> The following is a snippet from my terminal with output, is there a\n> chance to make git think that those are equal?\n\nRename detection in git does not respect the \"-w\" option at all. It\nhashes each line of a text file, and then compares the hashes to see how\n\"similar\" the files are.\n\nIt already makes some effort to ignore the CR in a CRLF sequence when\ncalculating the hash. So just running \"unix2dos\" (or vice versa) on a\nfile should still allow it to find renames.\n\nThis could probably be extended fairly trivially to ignore arbitrary\nwhitespace when generating the hash (I'm not sure if the feature should\nbe triggered by \"-w\" or not; it makes sense to me, but I'm not sure if\nthere are cases where people would want diff generation to have\ndifferent rules than rename detection. We maybe would even want to\nignore whitespace in diff generation _always_, as we always do already\nwith CRLF. Somebody would need to check the results of the two\napproaches against a number of cases).\n\nIf you are interested, the relevant code is in hash_chars in\ndiffcore-delta.c. A trivial implementation would probably look something\nlike the patch below. I tested it with:\n\n  git init\n  cp /usr/share/dict/words words && git add words && git commit -m one\n  sed 's/^/  /' <words >munged\n  git add munged && git rm words\n  git diff --cached --summary\n\nwhich curious reports 82% similarity. So maybe there is more\ninvestigation to be done. Anyway, patch below.\n\n---\n\ndiff --git a/diffcore-delta.c b/diffcore-delta.c\nindex e670f85..63704da 100644\n--- a/diffcore-delta.c\n+++ b/diffcore-delta.c\n@@ -145,6 +145,8 @@ static struct spanhash_top *hash_chars(struct diff_filespec *one)\n \t\t/* Ignore CR in CRLF sequence if text */\n \t\tif (is_text && c == '\\r' && sz && *buf == '\\n')\n \t\t\tcontinue;\n+\t\tif (is_text && (c == ' ' || c == '\\t'))\n+\t\t\tcontinue;\n \n \t\taccum1 = (accum1 << 7) ^ (accum2 >> 25);\n \t\taccum2 = (accum2 << 7) ^ (old_1 >> 25);\n"}]}