{"thread":{"id":"56469","subject":"git diff: print hunk numbers?","startedAt":"2021-09-08T20:45:38Z","lastAt":"2021-09-09T21:36:21Z","messageCount":5,"participants":["Aleksey Midenkov","Jeff King","Junio C Hamano"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"435177","messageId":"CAF8BazDKG03bpBEQyC9EsM8WxV_7WnQ386T=VT49EHNzsZ3PPw@mail.gmail.com","threadId":"56469","inReplyTo":null,"subject":"git diff: print hunk numbers?","fromName":"Aleksey Midenkov","fromEmail":"midenok@gmail.com","sentAt":"2021-09-08T20:40:20Z","receivedAt":"2021-09-08T20:45:38Z","isPatch":false,"sender":{"key":"midenok@gmail.com","avatar":null},"body":"Is that possible/how to print hunk numbers with git diff?\n\nF.ex. instead of:\n\n@@ -106,7 +110,6 @@ while ($r < $statement_count)\n...\n\nTo print something like:\n\n@@ -106,7 +110,6 @@ 4 @@ while ($r < $statement_count)\n...\n\nfilterdiff uses hunk numbers intensively. Work with line-number ranges\nis not so effective.\n"},{"id":"435180","messageId":"YTktpQ9uZkbwtuLO@coredump.intra.peff.net","threadId":"56469","inReplyTo":"CAF8BazDKG03bpBEQyC9EsM8WxV_7WnQ386T=VT49EHNzsZ3PPw@mail.gmail.com","subject":"Re: git diff: print hunk numbers?","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2021-09-08T21:39:49Z","receivedAt":"2021-09-08T21:39:52Z","isPatch":false,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Wed, Sep 08, 2021 at 11:40:20PM +0300, Aleksey Midenkov wrote:\n\n> Is that possible/how to print hunk numbers with git diff?\n> \n> F.ex. instead of:\n> \n> @@ -106,7 +110,6 @@ while ($r < $statement_count)\n> ...\n> \n> To print something like:\n> \n> @@ -106,7 +110,6 @@ 4 @@ while ($r < $statement_count)\n> ...\n> \n> filterdiff uses hunk numbers intensively. Work with line-number ranges\n> is not so effective.\n\nNo, Git doesn't know how to do any annotations on hunk lines (aside from\nfinding and reporting the funcname lines from the source). So you'd have\nto post-process it, like:\n\n  git diff ... |\n  perl -pe 's/^@@.*?@@/join(\" \", $&, ++$i, \"@@\")/e'\n\nbut I'm not sure if that's quite what you're after. If you're using\nfilterdiff to pick out hunks, then piping through \"filterdiff\n--annotate\" does something similar.\n\nIf you want to post-process your diffs all the time, you can do\nsomething like:\n\n  git config pager.color false\n  git config pager.diff 'filterdiff --annotate | less'\n\nto show the annotations anytime the output is going to a terminal.\nThough sadly filterdiff does not handle the colors; other\npost-processors like diff-highlight parse around them.\n\nAnd finally, if your ultimate goal is to use filterdiff to pick out\nhunks, you might find using Git's picking tools like \"checkout -p\"\neasier. Even if you are starting with an actual patch, you can apply it\nand then pick out bits, like:\n\n  git checkout --detach ;# temporary head for applying patch\n  git apply </path/to/patch\n  git commit -m \"temporary commit for patch\"\n  git checkout - ;# back to the original branch\n  git checkout -p HEAD@{1} ;# now selectively grab parts\n\nOf course that only helps if the patch actually applies. If your goal is\nto filter out hunks that don't apply, it won't help. :)\n\n-Peff\n"},{"id":"435182","messageId":"xmqq4kau3evq.fsf@gitster.g","threadId":"56469","inReplyTo":"YTktpQ9uZkbwtuLO@coredump.intra.peff.net","subject":"Re: git diff: print hunk numbers?","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2021-09-08T22:02:17Z","receivedAt":"2021-09-08T22:02:23Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> Of course that only helps if the patch actually applies. If your goal is\n> to filter out hunks that don't apply, it won't help. :)\n\nThat can be done without filterdiff, though, via \"git apply\n--reject\" ;-)\n"},{"id":"435277","messageId":"CAF8BazB+RLrzD7eO_fmsUbLY0DbqFeum+eMeY1hdZSAnWTxn4A@mail.gmail.com","threadId":"56469","inReplyTo":"YTktpQ9uZkbwtuLO@coredump.intra.peff.net","subject":"Re: git diff: print hunk numbers?","fromName":"Aleksey Midenkov","fromEmail":"midenok@gmail.com","sentAt":"2021-09-09T15:45:42Z","receivedAt":"2021-09-09T15:51:01Z","isPatch":false,"sender":{"key":"midenok@gmail.com","avatar":null},"body":"On Thu, Sep 9, 2021 at 12:39 AM Jeff King <peff@peff.net> wrote:\n>\n> On Wed, Sep 08, 2021 at 11:40:20PM +0300, Aleksey Midenkov wrote:\n>\n> > Is that possible/how to print hunk numbers with git diff?\n> >\n> > F.ex. instead of:\n> >\n> > @@ -106,7 +110,6 @@ while ($r < $statement_count)\n> > ...\n> >\n> > To print something like:\n> >\n> > @@ -106,7 +110,6 @@ 4 @@ while ($r < $statement_count)\n> > ...\n> >\n> > filterdiff uses hunk numbers intensively. Work with line-number ranges\n> > is not so effective.\n>\n> No, Git doesn't know how to do any annotations on hunk lines (aside from\n> finding and reporting the funcname lines from the source). So you'd have\n> to post-process it, like:\n>\n>   git diff ... |\n>   perl -pe 's/^@@.*?@@/join(\" \", $&, ++$i, \"@@\")/e'\n>\n> but I'm not sure if that's quite what you're after. If you're using\n> filterdiff to pick out hunks, then piping through \"filterdiff\n> --annotate\" does something similar.\n>\n> If you want to post-process your diffs all the time, you can do\n> something like:\n>\n>   git config pager.color false\n>   git config pager.diff 'filterdiff --annotate | less'\n>\n\nThanks!\n\n git config pager.color false\n git config pager.diff 'filterdiff --annotate | colordiff | less -FRX'\n git config pager.show 'filterdiff --annotate | colordiff | less -FRX'\n\ndid the job for me.\n\n> to show the annotations anytime the output is going to a terminal.\n> Though sadly filterdiff does not handle the colors; other\n> post-processors like diff-highlight parse around them.\n>\n> And finally, if your ultimate goal is to use filterdiff to pick out\n> hunks, you might find using Git's picking tools like \"checkout -p\"\n> easier. Even if you are starting with an actual patch, you can apply it\n> and then pick out bits, like:\n>\n>   git checkout --detach ;# temporary head for applying patch\n>   git apply </path/to/patch\n>   git commit -m \"temporary commit for patch\"\n>   git checkout - ;# back to the original branch\n>   git checkout -p HEAD@{1} ;# now selectively grab parts\n>\n> Of course that only helps if the patch actually applies. If your goal is\n> to filter out hunks that don't apply, it won't help. :)\n>\n> -Peff\n\n-- \n@midenok\n"},{"id":"435337","messageId":"CAF8BazDyALuPtazZ-H5HnsR2HweVsc6yZPbObSDG+y-m+cUF3Q@mail.gmail.com","threadId":"56469","inReplyTo":"CAF8BazB+RLrzD7eO_fmsUbLY0DbqFeum+eMeY1hdZSAnWTxn4A@mail.gmail.com","subject":"Re: git diff: print hunk numbers?","fromName":"Aleksey Midenkov","fromEmail":"midenok@gmail.com","sentAt":"2021-09-09T21:31:00Z","receivedAt":"2021-09-09T21:36:21Z","isPatch":false,"sender":{"key":"midenok@gmail.com","avatar":null},"body":"On Thu, Sep 9, 2021 at 6:45 PM Aleksey Midenkov <midenok@gmail.com> wrote:\n>\n> On Thu, Sep 9, 2021 at 12:39 AM Jeff King <peff@peff.net> wrote:\n> >\n> > On Wed, Sep 08, 2021 at 11:40:20PM +0300, Aleksey Midenkov wrote:\n> >\n> > > Is that possible/how to print hunk numbers with git diff?\n> > >\n> > > F.ex. instead of:\n> > >\n> > > @@ -106,7 +110,6 @@ while ($r < $statement_count)\n> > > ...\n> > >\n> > > To print something like:\n> > >\n> > > @@ -106,7 +110,6 @@ 4 @@ while ($r < $statement_count)\n> > > ...\n> > >\n> > > filterdiff uses hunk numbers intensively. Work with line-number ranges\n> > > is not so effective.\n> >\n> > No, Git doesn't know how to do any annotations on hunk lines (aside from\n> > finding and reporting the funcname lines from the source). So you'd have\n> > to post-process it, like:\n> >\n> >   git diff ... |\n> >   perl -pe 's/^@@.*?@@/join(\" \", $&, ++$i, \"@@\")/e'\n> >\n> > but I'm not sure if that's quite what you're after. If you're using\n> > filterdiff to pick out hunks, then piping through \"filterdiff\n> > --annotate\" does something similar.\n> >\n> > If you want to post-process your diffs all the time, you can do\n> > something like:\n> >\n> >   git config pager.color false\n> >   git config pager.diff 'filterdiff --annotate | less'\n> >\n>\n> Thanks!\n>\n>  git config pager.color false\n>  git config pager.diff 'filterdiff --annotate | colordiff | less -FRX'\n>  git config pager.show 'filterdiff --annotate | colordiff | less -FRX'\n>\n> did the job for me.\n>\n> > to show the annotations anytime the output is going to a terminal.\n> > Though sadly filterdiff does not handle the colors; other\n> > post-processors like diff-highlight parse around them.\n\nWhat's more sadly filterdiff --annotate does not handle non-diff\noutput, so --stat option does not work! I should now detect whether it\nis diff by reading the first line then return it to the pipe and run\nfilterdiff conditionally. That is all mess and the testing construct\njust doesn't work (not sure if the command is bash-friendly at all):\n\ngit config pager.show 'IFS= read -r l; echo $l $l $l; cat'\n\nSo I can only overcome this by introducing different command through\nalias `s` and assign custom pager to `s`:\n\ngit config pager.color true\ngit config --unset pager.show\ngit config pager.s \"sed 's/\\x1b\\[[0-9;]*m//g'|filterdiff\n--annotate|colordiff|less -FRX\"\n\nsed will bite out ANSI escape sequences.\n\n> >\n> > And finally, if your ultimate goal is to use filterdiff to pick out\n> > hunks, you might find using Git's picking tools like \"checkout -p\"\n> > easier. Even if you are starting with an actual patch, you can apply it\n> > and then pick out bits, like:\n> >\n> >   git checkout --detach ;# temporary head for applying patch\n> >   git apply </path/to/patch\n> >   git commit -m \"temporary commit for patch\"\n> >   git checkout - ;# back to the original branch\n> >   git checkout -p HEAD@{1} ;# now selectively grab parts\n> >\n> > Of course that only helps if the patch actually applies. If your goal is\n> > to filter out hunks that don't apply, it won't help. :)\n> >\n> > -Peff\n>\n> --\n> @midenok\n\n\n\n-- \n@midenok\n"}]}