{"thread":{"id":"54200","subject":"[PATCH 0/2] some minor add--interactive fixes","startedAt":"2020-09-07T08:07:31Z","lastAt":"2020-09-09T09:15:26Z","messageCount":7,"participants":["Jeff King","Junio C Hamano","Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"405117","messageId":"20200907080632.GA1261825@coredump.intra.peff.net","threadId":"54200","inReplyTo":null,"subject":"[PATCH 0/2] some minor add--interactive fixes","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2020-09-07T08:06:32Z","receivedAt":"2020-09-07T08:07:31Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"There was a mini-mystery about the add--interactive tests on the\ngit-mentoring list this morning. The second patch here solves the\nmystery. But while digging I also noticed a bug in the new C version,\nwhich is fixed by the first patch.\n\n  [1/2]: add-patch: fix inverted return code of repo_read_index()\n  [2/2]: add--interactive.perl: specify --no-color explicitly\n\n add-patch.c                | 2 +-\n git-add--interactive.perl  | 2 +-\n t/t3701-add-interactive.sh | 8 ++++++++\n 3 files changed, 10 insertions(+), 2 deletions(-)\n\n-Peff\n"},{"id":"405118","messageId":"20200907080853.GA1263923@coredump.intra.peff.net","threadId":"54200","inReplyTo":"20200907080632.GA1261825@coredump.intra.peff.net","subject":"[PATCH 1/2] add-patch: fix inverted return code of repo_read_index()","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2020-09-07T08:08:53Z","receivedAt":"2020-09-07T08:10:25Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"After applying hunks to a file with \"add -p\", the C patch_update_file()\nfunction tries to refresh the index (just like the perl version does).\nWe can only refresh the index if we're able to read it in, so we first\ncheck the return value of repo_read_index(). But unlike many functions,\nwhere \"0\" is success, that function is documented to return the number\nof entries in the index.  Hence we should be checking for success with a\nnon-negative return value.\n\nNeither the tests nor any users seem to have noticed this, probably due\nto a combination of:\n\n  - this affects only the C version, which is not yet the default\n\n  - following it up with any porcelain command like \"git diff\" or \"git\n    commit\" would refresh the index automatically.\n\nBut you can see the problem by running the plumbing \"git diff-files\"\nimmediately after \"add -p\" stages all hunks. Running the new test with\nGIT_TEST_ADD_I_USE_BUILTIN=1 fails without the matching code change.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\n add-patch.c                | 2 +-\n t/t3701-add-interactive.sh | 7 +++++++\n 2 files changed, 8 insertions(+), 1 deletion(-)\n\ndiff --git a/add-patch.c b/add-patch.c\nindex 457b8c550e..9544437247 100644\n--- a/add-patch.c\n+++ b/add-patch.c\n@@ -1641,7 +1641,7 @@ static int patch_update_file(struct add_p_state *s,\n \t\t\t\t\t NULL, 0, NULL, 0))\n \t\t\t\terror(_(\"'git apply' failed\"));\n \t\t}\n-\t\tif (!repo_read_index(s->s.r))\n+\t\tif (repo_read_index(s->s.r) >= 0)\n \t\t\trepo_refresh_and_write_index(s->s.r, REFRESH_QUIET, 0,\n \t\t\t\t\t\t     1, NULL, NULL, NULL);\n \t}\ndiff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh\nindex fb73a847cb..67d1c36cef 100755\n--- a/t/t3701-add-interactive.sh\n+++ b/t/t3701-add-interactive.sh\n@@ -569,6 +569,13 @@ test_expect_success 'patch mode ignores unmerged entries' '\n \tdiff_cmp expected diff\n '\n \n+test_expect_success 'index is refreshed after applying patch' '\n+\tgit reset --hard &&\n+\techo content >test &&\n+\tprintf y | git add -p &&\n+\tgit diff-files --exit-code\n+'\n+\n test_expect_success 'diffs can be colorized' '\n \tgit reset --hard &&\n \n-- \n2.28.0.889.g64d3ad7607\n\n"},{"id":"405119","messageId":"20200907081739.GB1263923@coredump.intra.peff.net","threadId":"54200","inReplyTo":"20200907080632.GA1261825@coredump.intra.peff.net","subject":"[PATCH 2/2] add--interactive.perl: specify --no-color explicitly","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2020-09-07T08:17:39Z","receivedAt":"2020-09-07T08:17:43Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"Our color tests of \"git add -p\" do something a bit different from how a\nnormal user would behave: we pretend there's a pager in use, so that Git\nthinks it's OK to write color to a non-tty stdout.  This comes from\n8539b46534 (t3701: avoid depending on the TTY prerequisite, 2019-12-06),\nwhich allows us to avoid a lot of complicated mock-tty code.\n\nHowever, those environment variables also make their way down to\nsub-processes of add--interactive, including the \"diff-files\" we run to\ngenerate the patches. As a result, it thinks it should output color,\ntoo. So in t3701.50, for example, the machine-readable version of the\ndiff we get unexpectedly has color in it. We fail to parse it as a diff\nand think there are zero hunks.\n\nThe test does still pass, though, because even with zero hunks we'll\ndump the diff header (and we consider those unparseable bits to be part\nof the header!), and so the output still has the expected color codes in\nit. We don't notice that the command was totally broken and failed to\napply anything.\n\nAnd in fact we're not really testing what we think we are about the\ncolor, either. While add--interactive does correctly show the version we\ngot from running \"diff-files --color\", we'd also pass the test if we had\naccidentally shown the machine-readable version, too, since it\n(erroneously) has color codes in it.\n\nOne could argue that the test isn't very realistic; it's setting up this\n\"pretend there's a pager\" situation to get around the tty restrictions\nof the test environment. So one option would be to move back towards\nusing a real tty. But the behavior of add--interactive really is\nuser-visible here. If a user, for whatever reason, did run \"git\n--paginate add --patch\" (perhaps because their pager is really a filter\nor something), the command would totally fail to do anything useful.\n\nSince we know that we don't want color in this output, let's just make\nadd--interactive more defensive, and say \"--no-color\" explicitly. It\ndoesn't hurt anything in the common case, but it fixes this odd case and\nlets our test function properly again.\n\nNote that the C builtin run_add_p() already passes --no-color, so it\ndoesn't need a similar fix. That will eventually replace this perl code\nanyway, but the test change here will be valuable for ensuring that.\n\nSigned-off-by: Jeff King <peff@peff.net>\n---\nCuriously, run_add_p() claims that --no-color is important to override\n\"diff.color = always\", but in a quick test that doesn't seem to make any\ndifference. I didn't dig into whether it's possible to trigger or not\n(either way, defensively using --no-color is a reasonable precaution).\n\n git-add--interactive.perl  | 2 +-\n t/t3701-add-interactive.sh | 1 +\n 2 files changed, 2 insertions(+), 1 deletion(-)\n\ndiff --git a/git-add--interactive.perl b/git-add--interactive.perl\nindex f36c0078ac..b6cdcfef61 100755\n--- a/git-add--interactive.perl\n+++ b/git-add--interactive.perl\n@@ -714,7 +714,7 @@ sub parse_diff {\n \tif (defined $patch_mode_revision) {\n \t\tpush @diff_cmd, get_diff_reference($patch_mode_revision);\n \t}\n-\tmy @diff = run_cmd_pipe(\"git\", @diff_cmd, \"--\", $path);\n+\tmy @diff = run_cmd_pipe(\"git\", @diff_cmd, qw(--no-color --), $path);\n \tmy @colored = ();\n \tif ($diff_use_color) {\n \t\tmy @display_cmd = (\"git\", @diff_cmd, qw(--color --), $path);\ndiff --git a/t/t3701-add-interactive.sh b/t/t3701-add-interactive.sh\nindex 67d1c36cef..1590cf6b98 100755\n--- a/t/t3701-add-interactive.sh\n+++ b/t/t3701-add-interactive.sh\n@@ -582,6 +582,7 @@ test_expect_success 'diffs can be colorized' '\n \techo content >test &&\n \tprintf y >y &&\n \tforce_color git add -p >output 2>&1 <y &&\n+\tgit diff-files --exit-code &&\n \n \t# We do not want to depend on the exact coloring scheme\n \t# git uses for diffs, so just check that we saw some kind of color.\n-- \n2.28.0.889.g64d3ad7607\n"},{"id":"405120","messageId":"20200907082510.GA1264377@coredump.intra.peff.net","threadId":"54200","inReplyTo":"20200907081739.GB1263923@coredump.intra.peff.net","subject":"Re: [PATCH 2/2] add--interactive.perl: specify --no-color explicitly","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2020-09-07T08:25:10Z","receivedAt":"2020-09-07T08:25:15Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Mon, Sep 07, 2020 at 04:17:39AM -0400, Jeff King wrote:\n\n> Our color tests of \"git add -p\" do something a bit different from how a\n> normal user would behave: we pretend there's a pager in use, so that Git\n> thinks it's OK to write color to a non-tty stdout.  This comes from\n> 8539b46534 (t3701: avoid depending on the TTY prerequisite, 2019-12-06),\n> which allows us to avoid a lot of complicated mock-tty code.\n> \n> However, those environment variables also make their way down to\n> sub-processes of add--interactive, including the \"diff-files\" we run to\n> generate the patches. As a result, it thinks it should output color,\n> too. So in t3701.50, for example, the machine-readable version of the\n> diff we get unexpectedly has color in it. We fail to parse it as a diff\n> and think there are zero hunks.\n\nBy the way, this is an instance of a more generic bug, which is that:\n\n  git -p my-script\n\nwill cause any sub-programs of git-my-script to think their stdout is\ngoing to a pager, even if my-script redirects them to a file or another\npipe.\n\nI had a solution long ago in:\n\n  https://lore.kernel.org/git/20150810052353.GB15441@sigill.intra.peff.net/\n\nbut it raises a bunch of interesting portability questions. Since this\ncomes up so rarely, I never really pursued it further.\n\n-Peff\n"},{"id":"405161","messageId":"xmqqo8mgz150.fsf@gitster.c.googlers.com","threadId":"54200","inReplyTo":"20200907082510.GA1264377@coredump.intra.peff.net","subject":"Re: [PATCH 2/2] add--interactive.perl: specify --no-color explicitly","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2020-09-08T16:25:31Z","receivedAt":"2020-09-08T16:55:05Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Jeff King <peff@peff.net> writes:\n\n> By the way, this is an instance of a more generic bug, which is that:\n>\n>   git -p my-script\n>\n> will cause any sub-programs of git-my-script to think their stdout is\n> going to a pager, even if my-script redirects them to a file or another\n> pipe.\n>\n> I had a solution long ago in:\n>\n>   https://lore.kernel.org/git/20150810052353.GB15441@sigill.intra.peff.net/\n>\n> but it raises a bunch of interesting portability questions. Since this\n> comes up so rarely, I never really pursued it further.\n\nThat indeed is from long ago ;-) and I still kind of like it.  Is\nthe issue that some platforms do not even have inum of the \"thing\"\nconnected to a file descriptor?  I think a fallback code for such a\nsystem can just return a constant pipe ID to anybody asks, so that\nan exported GIT_PAGER_PIPE_ID would always match, which gives the\nbehaviour identical to what we currently do with GIT_PAGER_IN_USE,\nand that would be one way to help those capable of giving useful IDs\nwhile not harming others.\n\nThanks.\n"},{"id":"405171","messageId":"nycvar.QRO.7.76.6.2009082047220.54@tvgsbejvaqbjf.bet","threadId":"54200","inReplyTo":"20200907081739.GB1263923@coredump.intra.peff.net","subject":"Re: [PATCH 2/2] add--interactive.perl: specify --no-color explicitly","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2020-09-08T18:51:31Z","receivedAt":"2020-09-08T18:52:05Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Peff,\n\nACK on both patches, just one clarification:\n\nOn Mon, 7 Sep 2020, Jeff King wrote:\n\n> [...]\n>\n> One could argue that the test isn't very realistic; it's setting up this\n> \"pretend there's a pager\" situation to get around the tty restrictions\n> of the test environment. So one option would be to move back towards\n> using a real tty. [...]\n\nThe main reason why we moved away from the \"real\" TTY is that the TTY we\nsimulate in `t/test-terminal.perl` is not actually real at all on Windows.\nInstead, it is the Cygwin/MSYS2-emulated version of a PTY, and as a\nconsequence `git.exe` simply has no way of accessing it.\n\nWhich means that by moving \"back towards using a real tty\" we would lose\nall test coverage on Windows, which is not a thought I like to entertain.\n\nCiao,\nDscho\n"},{"id":"405234","messageId":"20200909091520.GC2496536@coredump.intra.peff.net","threadId":"54200","inReplyTo":"nycvar.QRO.7.76.6.2009082047220.54@tvgsbejvaqbjf.bet","subject":"Re: [PATCH 2/2] add--interactive.perl: specify --no-color explicitly","fromName":"Jeff King","fromEmail":"peff@peff.net","sentAt":"2020-09-09T09:15:20Z","receivedAt":"2020-09-09T09:15:26Z","isPatch":true,"sender":{"key":"peff@peff.net","avatar":"https://avatars.githubusercontent.com/u/45925?v=4"},"body":"On Tue, Sep 08, 2020 at 08:51:31PM +0200, Johannes Schindelin wrote:\n\n> > One could argue that the test isn't very realistic; it's setting up this\n> > \"pretend there's a pager\" situation to get around the tty restrictions\n> > of the test environment. So one option would be to move back towards\n> > using a real tty. [...]\n> \n> The main reason why we moved away from the \"real\" TTY is that the TTY we\n> simulate in `t/test-terminal.perl` is not actually real at all on Windows.\n> Instead, it is the Cygwin/MSYS2-emulated version of a PTY, and as a\n> consequence `git.exe` simply has no way of accessing it.\n> \n> Which means that by moving \"back towards using a real tty\" we would lose\n> all test coverage on Windows, which is not a thought I like to entertain.\n\nYeah, regressing the improvements done by 8539b46534 (t3701: avoid\ndepending on the TTY prerequisite, 2019-12-06) was definitely part of my\nmotivation, but I guess I didn't spell that out completely.\n\n-Peff\n"}]}