{"thread":{"id":"42487","subject":"git add without whitespace","startedAt":"2016-05-30T15:26:44Z","lastAt":"2016-05-31T16:27:21Z","messageCount":8,"participants":["Robert Dailey","Junio C Hamano","Christian Neukirchen","demerphq"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"287829","messageId":"CAHd499Agn=vLBxDpHi2dy1HMy-_58PZGs7VNtFJnBfP5zXatTA@mail.gmail.com","threadId":"42487","inReplyTo":null,"subject":"git add without whitespace","fromName":"Robert Dailey","fromEmail":"rcdailey.lists@gmail.com","sentAt":"2016-05-30T15:26:44Z","receivedAt":"2016-05-30T15:26:44Z","isPatch":false,"sender":{"key":"rcdailey.lists@gmail.com","avatar":null},"body":"I think it would be useful to have a '-w' option for 'git add' that\ncompletely ignores whitespace changes, the same way that 'git diff -w'\ndoes.\n\nReal life scenario:\n\nSometimes developers will use tooling that does not properly strip\ntrailing whitespace in source files. Next time I edit those files for\na simple 1-line code change, my tooling will strip whitespace from the\nwhole file. I *do* want these changes, however I want 2 commits: 1\ncommit with the bugfix, and a supplementary commit with just the\nwhitespace changes.\n\nAt the moment, there is no way for me to conveniently add the source\nfile to the index without whitespace. The only way to accomplish this\ntoday that I'm aware of is via this command:\n\n$ git diff -U0 -w --no-color | git apply --cached --ignore-whitespace\n--unidiff-zero\n\nThis command explicitly leaves out context because it can sometimes\ncause the patch to fail to apply, I think due to whitespace being in\nit, but I'm not completely sure myself.\n\nIt would be useful to be able to do this instead:\n\n$ git add -w\n\nThis would effectively function the same as my workaround command\nshown earlier. It should also be valid to use -w with -i and -p. In\nthe -p case, it just won't show hunks containing whitespace changes.\nFor -i, it would assume '-w' as part of any command run during the\ninteractive session.\n\nDoes this idea sound good? I have some free time on my hands so I\nwouldn't mind implementing this. Maybe there isn't a huge audience for\nthis kind of thing, or maybe I'm just going about this the wrong way.\nThoughts would be much appreciated.\n"},{"id":"287848","messageId":"xmqqlh2r8ixu.fsf@gitster.mtv.corp.google.com","threadId":"42487","inReplyTo":"CAHd499Agn=vLBxDpHi2dy1HMy-_58PZGs7VNtFJnBfP5zXatTA@mail.gmail.com","subject":"Re: git add without whitespace","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-05-30T19:06:05Z","receivedAt":"2016-05-30T19:06:05Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Robert Dailey <rcdailey.lists@gmail.com> writes:\n\n> $ git diff -U0 -w --no-color | git apply --cached --ignore-whitespace\n> --unidiff-zero\n>\n> This command explicitly leaves out context because it can sometimes\n> cause the patch to fail to apply, I think due to whitespace being in\n> it, but I'm not completely sure myself.\n\nI have had this in my ~/.gitconfig for a long time.\n\n[alias]\n\twsadd = \"!sh -c 'git diff -- \\\"$@\\\" | git apply --cached --whitespace=fix;\\\n\t\tgit co -- ${1-.} \\\"$@\\\"' -\"\n\nThat is, \"take what's different from the _index_ and the working\ntree, apply that difference while correcting whitespace errors to\nthe index, and check the result out to the working tree\".  This\nwould _not_ touch existing whitespace-damaged lines that you are not\ntouching, and honours the customized definition of what is\nconsidered whitespace breakage for each paths (which you set up with\nthe attributes system).\n"},{"id":"287851","messageId":"CAHd499Drdt6GoScdksNNWLrsvG3Dex7fTYNhatL-qboCPW5RZQ@mail.gmail.com","threadId":"42487","inReplyTo":"xmqqlh2r8ixu.fsf@gitster.mtv.corp.google.com","subject":"Re: git add without whitespace","fromName":"Robert Dailey","fromEmail":"rcdailey.lists@gmail.com","sentAt":"2016-05-30T19:50:05Z","receivedAt":"2016-05-30T19:50:05Z","isPatch":false,"sender":{"key":"rcdailey.lists@gmail.com","avatar":null},"body":"On Mon, May 30, 2016 at 2:06 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> I have had this in my ~/.gitconfig for a long time.\n>\n> [alias]\n>         wsadd = \"!sh -c 'git diff -- \\\"$@\\\" | git apply --cached --whitespace=fix;\\\n>                 git co -- ${1-.} \\\"$@\\\"' -\"\n>\n> That is, \"take what's different from the _index_ and the working\n> tree, apply that difference while correcting whitespace errors to\n> the index, and check the result out to the working tree\".  This\n> would _not_ touch existing whitespace-damaged lines that you are not\n> touching, and honours the customized definition of what is\n> considered whitespace breakage for each paths (which you set up with\n> the attributes system).\n>\n\nI like your solution better than mine because it utilizes the rules\ndefined in .gitattributes. I think that's a really good idea. But\nother than that, yours is functionally the same as what I'm doing,\nright? I just want to make sure I understand: What ends up in the\nindex/staging area is the code MINUS the trailing whitespace (e.g.\nwhitespace errors)?\n\nWhat does the checkout at the end do? That part confuses me (granted\nI'm not well-versed with bash script).\n\nThanks for the feedback. Looks like this is niche enough that an\nalias/script is probably the best solution.\n"},{"id":"287856","messageId":"xmqq8tyr8aur.fsf@gitster.mtv.corp.google.com","threadId":"42487","inReplyTo":"CAHd499Drdt6GoScdksNNWLrsvG3Dex7fTYNhatL-qboCPW5RZQ@mail.gmail.com","subject":"Re: git add without whitespace","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-05-30T22:00:44Z","receivedAt":"2016-05-30T22:00:44Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Robert Dailey <rcdailey.lists@gmail.com> writes:\n\n> I like your solution better than mine because it utilizes the rules\n> defined in .gitattributes.\n\nA difference that may be more important is that I do not do\ngeneration of a patch or application of it without ignoring\nwhitespaces with things like -w and --ignore-whitespace.  That way,\nif my edit is a correction of existing whitespace breakage (e.g. I\nnoticed a line that is indented by 8 spaces, and I corrected it by\nreplacing them with one tab), that is shown as a change by \"diff\"\nand kept in the result.  I suspect that your \"diff -w | apply --ignore\"\nwill ignore that manual fix?\n\n> What does the checkout at the end do? That part confuses me (granted\n> I'm not well-versed with bash script).\n\nI correct whitespace-broken updates the user (i.e. I) made in her\nworking tree file by adding a corrected version to the index, and\nthen I checkout the result out of the index to the working tree.\n\nThat corrects the breakage in both the index and the working tree,\nso that my further edit to the file will start from a ws-corrected\nversion.\n"},{"id":"287899","messageId":"CAHd499AreoaqfCVGi6XiatiWQMzf+KqL=a+f3D8u53dNkg0tiQ@mail.gmail.com","threadId":"42487","inReplyTo":"xmqq8tyr8aur.fsf@gitster.mtv.corp.google.com","subject":"Re: git add without whitespace","fromName":"Robert Dailey","fromEmail":"rcdailey.lists@gmail.com","sentAt":"2016-05-31T15:03:01Z","receivedAt":"2016-05-31T15:03:01Z","isPatch":false,"sender":{"key":"rcdailey.lists@gmail.com","avatar":null},"body":"On Mon, May 30, 2016 at 5:00 PM, Junio C Hamano <gitster@pobox.com> wrote:\n> Robert Dailey <rcdailey.lists@gmail.com> writes:\n>\n>> I like your solution better than mine because it utilizes the rules\n>> defined in .gitattributes.\n>\n> A difference that may be more important is that I do not do\n> generation of a patch or application of it without ignoring\n> whitespaces with things like -w and --ignore-whitespace.  That way,\n> if my edit is a correction of existing whitespace breakage (e.g. I\n> noticed a line that is indented by 8 spaces, and I corrected it by\n> replacing them with one tab), that is shown as a change by \"diff\"\n> and kept in the result.  I suspect that your \"diff -w | apply --ignore\"\n> will ignore that manual fix?\n>\n>> What does the checkout at the end do? That part confuses me (granted\n>> I'm not well-versed with bash script).\n>\n> I correct whitespace-broken updates the user (i.e. I) made in her\n> working tree file by adding a corrected version to the index, and\n> then I checkout the result out of the index to the working tree.\n>\n> That corrects the breakage in both the index and the working tree,\n> so that my further edit to the file will start from a ws-corrected\n> version.\n\nAh, I think I get it now. I was confused, you and I are apparently\ntrying to accomplish two different things.\n\nMy alias stages changes EXCEPT whitespace fixes. In this case, I've\nalready made the whitespace corrections (by hand) in the working tree.\nI want to stage the changes EXCEPT whitespace. This will allow me to\ndo 1 commit without whitespace, and another with just the whitespace.\nSo yes, my patch is deliberately ignoring whitespace because I do not\nwant it in the index for the first commit.\n\nYours seems to take exactly what is in the working tree and make\nfurther modifications to it as it is added to the index. That is, to\ncorrect whitespace errors as defined by the gitattributes file.\n\nWhen I tested your alias, all changes in my working tree were added to\nthe index, where I expected the index to contain everything except\nwhitespace modifications.\n\nBut honestly your solution is a little better, even if it requires me\nto change my process, because:\n- You don't depend on external tooling to correct whitespace\n- Splitting whitespace & real changes to two commits is probably\nsuperfluous; my original goal was to make diffing the actual changes\neasier, but since 'git diff -w' exists this is moot.\n\nSo I guess it's not bad practice to mix whitespace changes in with\nreal diffs, given the flexibility of Git (in previous VCS such as Git\nand perforce I didn't do it this way). I have a similar habitual\ncarry-over from SVN where I do file moves/renames in separate commits\nbefore I modify file contents. Again with Git, probably not as\nnecessary to do this.\n"},{"id":"287912","messageId":"87pos2tdzi.fsf@gmail.com","threadId":"42487","inReplyTo":"xmqqlh2r8ixu.fsf@gitster.mtv.corp.google.com","subject":"Re: git add without whitespace","fromName":"Christian Neukirchen","fromEmail":"chneukirchen@gmail.com","sentAt":"2016-05-31T15:59:45Z","receivedAt":"2016-05-31T15:59:45Z","isPatch":false,"sender":{"key":"chneukirchen@gmail.com","avatar":"https://avatars.githubusercontent.com/u/139?v=4"},"body":"Junio C Hamano <gitster@pobox.com> writes:\n\n> Robert Dailey <rcdailey.lists@gmail.com> writes:\n>\n>> $ git diff -U0 -w --no-color | git apply --cached --ignore-whitespace\n>> --unidiff-zero\n>>\n>> This command explicitly leaves out context because it can sometimes\n>> cause the patch to fail to apply, I think due to whitespace being in\n>> it, but I'm not completely sure myself.\n>\n> I have had this in my ~/.gitconfig for a long time.\n>\n> [alias]\n> \twsadd = \"!sh -c 'git diff -- \\\"$@\\\" | git apply --cached --whitespace=fix;\\\n> \t\tgit co -- ${1-.} \\\"$@\\\"' -\"\n\nVery useful, thanks for sharing!\n\nI wonder which other gems are in your .gitconfig, is it public?\n\n-- \nChristian Neukirchen  <chneukirchen@gmail.com>  http://chneukirchen.org\n"},{"id":"287914","messageId":"xmqqvb1u5hka.fsf@gitster.mtv.corp.google.com","threadId":"42487","inReplyTo":"CAHd499AreoaqfCVGi6XiatiWQMzf+KqL=a+f3D8u53dNkg0tiQ@mail.gmail.com","subject":"Re: git add without whitespace","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2016-05-31T16:16:21Z","receivedAt":"2016-05-31T16:16:21Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Robert Dailey <rcdailey.lists@gmail.com> writes:\n\n> - Splitting whitespace & real changes to two commits is probably\n> superfluous; my original goal was to make diffing the actual changes\n> easier, but since 'git diff -w' exists this is moot.\n\nDoing \"whitespace clean-up\" in a separate preparatory patch _is_ a\ngood practice.  I do not think either approach makes it harder to\ndo.  After all, if you had a real change on the same line that you\ndropped a broken whitespace, e.g. (end-of-line shown with '$')\n\n    -\tif  (i==1) {  $\n    +   if (j == 2) {$\n\nboth approaches would add the updated line.\n\nThe wsadd is about protecting _me_ from introducing new whitespace\nbreakages, not about helping me when fixing existing whitespace\nbreakages.  So from that point of view, that may not fit well to\nwhat you are trying to do.\n"},{"id":"287916","messageId":"CANgJU+WZHes1-d2GXT9p+Up4e6_aA-Q5r1A8EEmCEOXjsOyuXw@mail.gmail.com","threadId":"42487","inReplyTo":"xmqqlh2r8ixu.fsf@gitster.mtv.corp.google.com","subject":"Re: git add without whitespace","fromName":"demerphq","fromEmail":"demerphq@gmail.com","sentAt":"2016-05-31T16:27:21Z","receivedAt":"2016-05-31T16:27:21Z","isPatch":false,"sender":{"key":"demerphq@gmail.com","avatar":null},"body":"On 30 May 2016 at 21:06, Junio C Hamano <gitster@pobox.com> wrote:\n> Robert Dailey <rcdailey.lists@gmail.com> writes:\n>\n>> $ git diff -U0 -w --no-color | git apply --cached --ignore-whitespace\n>> --unidiff-zero\n>>\n>> This command explicitly leaves out context because it can sometimes\n>> cause the patch to fail to apply, I think due to whitespace being in\n>> it, but I'm not completely sure myself.\n>\n> I have had this in my ~/.gitconfig for a long time.\n>\n> [alias]\n>         wsadd = \"!sh -c 'git diff -- \\\"$@\\\" | git apply --cached --whitespace=fix;\\\n>                 git co -- ${1-.} \\\"$@\\\"' -\"\n>\n> That is, \"take what's different from the _index_ and the working\n> tree, apply that difference while correcting whitespace errors to\n> the index, and check the result out to the working tree\".  This\n> would _not_ touch existing whitespace-damaged lines that you are not\n> touching, and honours the customized definition of what is\n> considered whitespace breakage for each paths (which you set up with\n> the attributes system).\n\n\nThat is very very cool. I have a perl script that does the same thing\nfrom git-blame output. This is MUCH nicer.\n\ncheers,\nYves\n\n\n-- \nperl -Mre=debug -e \"/just|another|perl|hacker/\"\n"}]}