{"thread":{"id":"56999","subject":"[PATCH] sequencer: avoid adding exec commands for non-commit creating commands","startedAt":"2021-11-30T03:58:43Z","lastAt":"2021-12-03T22:22:23Z","messageCount":6,"participants":["Elijah Newren via GitGitGadget","Taylor Blau","Ævar Arnfjörð Bjarmason","Phillip Wood","Johannes Schindelin"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"442596","messageId":"pull.1149.git.git.1638244719381.gitgitgadget@gmail.com","threadId":"56999","inReplyTo":null,"subject":"[PATCH] sequencer: avoid adding exec commands for non-commit creating commands","fromName":"Elijah Newren via GitGitGadget","fromEmail":"gitgitgadget@gmail.com","sentAt":"2021-11-30T03:58:39Z","receivedAt":"2021-11-30T03:58:43Z","isPatch":true,"sender":{"key":"newren@gmail.com","avatar":"https://avatars.githubusercontent.com/u/5455730?v=4"},"body":"From: Elijah Newren <newren@gmail.com>\n\nThe `--exec <cmd>` is documented as\n\n    Append \"exec <cmd>\" after each line creating a commit in the final\n    history.\n    ...\n    If --autosquash is used, \"exec\" lines will not be appended for the\n    intermediate commits, and will only appear at the end of each\n    squash/fixup series.\n\nUnfortunately, it would also add exec commands after non-pick\noperations, such as 'no-op', which could be seen for example with\n    git rebase -i --exec true HEAD\n\ntodo_list_add_exec_commands() intent was to insert exec commands after\neach logical pick, while trying to consider a chains of fixup and squash\ncommits to be part of the pick before it.  So it would keep an 'insert'\nboolean tracking if it had seen a pick or merge, but not write the exec\ncommand until it saw the next non-fixup/squash command.  Since that\nwould make it miss the final exec command, it had some code that would\ncheck whether it still needed to insert one at the end, but instead of a\nsimple\n\n    if (insert)\n\nit had a\n\n    if (insert || <condition that is always true>)\n\nThat's buggy; as per the docs, we should only add exec commands for\nlines that create commits, i.e. only if insert is true.  Fix the\nconditional.\n\nThere was one testcase in the testsuite that we tweak for this change;\nit was introduced in 54fd3243da (\"rebase -i: reread the todo list if\n`exec` touched it\", 2017-04-26), and was merely testing that after an\nexec had fired that the todo list would be re-read.  The test at the\ntime would have worked given any revision at all, though it would only\nwork with 'HEAD' as a side-effect of this bug.  Since we're fixing this\nbug, choose something other than 'HEAD' for that test.\n\nFinally, add a testcase that verifies when we have no commits to pick,\nthat we get no exec lines in the generated todo list.\n\nReported-by: Nikita Bobko <nikitabobko@gmail.com>\nSigned-off-by: Elijah Newren <newren@gmail.com>\n---\n    sequencer: avoid adding exec commands for non-commit creating commands\n    \n    Original report over at\n    https://lore.kernel.org/git/YaVzufpKcC0t+q+L@nand.local/T/#m13fbd7b054c06ba1f98ae66e6d1b9fcc51bb875e\n\nPublished-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1149%2Fnewren%2Frebase-exec-empty-bug-v1\nFetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1149/newren/rebase-exec-empty-bug-v1\nPull-Request: https://github.com/git/git/pull/1149\n\n sequencer.c                 | 2 +-\n t/t3429-rebase-edit-todo.sh | 7 ++++++-\n 2 files changed, 7 insertions(+), 2 deletions(-)\n\ndiff --git a/sequencer.c b/sequencer.c\nindex ea96837cde3..aa790f0bba8 100644\n--- a/sequencer.c\n+++ b/sequencer.c\n@@ -5496,7 +5496,7 @@ static void todo_list_add_exec_commands(struct todo_list *todo_list,\n \t}\n \n \t/* insert or append final <commands> */\n-\tif (insert || nr == todo_list->nr) {\n+\tif (insert) {\n \t\tALLOC_GROW(items, nr + commands->nr, alloc);\n \t\tCOPY_ARRAY(items + nr, base_items, commands->nr);\n \t\tnr += commands->nr;\ndiff --git a/t/t3429-rebase-edit-todo.sh b/t/t3429-rebase-edit-todo.sh\nindex 7024d49ae7b..abd66f36021 100755\n--- a/t/t3429-rebase-edit-todo.sh\n+++ b/t/t3429-rebase-edit-todo.sh\n@@ -13,10 +13,15 @@ test_expect_success 'setup' '\n \n test_expect_success 'rebase exec modifies rebase-todo' '\n \ttodo=.git/rebase-merge/git-rebase-todo &&\n-\tgit rebase HEAD -x \"echo exec touch F >>$todo\" &&\n+\tgit rebase HEAD~1 -x \"echo exec touch F >>$todo\" &&\n \ttest -e F\n '\n \n+test_expect_success 'rebase exec with an empty list does not exec anything' '\n+\tgit rebase HEAD -x \"true\" 2>output &&\n+\t! grep \"Executing: true\" output\n+'\n+\n test_expect_success 'loose object cache vs re-reading todo list' '\n \tGIT_REBASE_TODO=.git/rebase-merge/git-rebase-todo &&\n \texport GIT_REBASE_TODO &&\n\nbase-commit: 35151cf0720460a897cde9b8039af364743240e7\n-- \ngitgitgadget\n"},{"id":"442601","messageId":"YaWy3+nXC/6oLb1A@nand.local","threadId":"56999","inReplyTo":"pull.1149.git.git.1638244719381.gitgitgadget@gmail.com","subject":"Re: [PATCH] sequencer: avoid adding exec commands for non-commit creating commands","fromName":"Taylor Blau","fromEmail":"me@ttaylorr.com","sentAt":"2021-11-30T05:13:03Z","receivedAt":"2021-11-30T05:13:07Z","isPatch":true,"sender":{"key":"me@ttaylorr.com","avatar":"https://avatars.githubusercontent.com/u/301000140?v=4"},"body":"On Tue, Nov 30, 2021 at 03:58:39AM +0000, Elijah Newren via GitGitGadget wrote:\n> diff --git a/sequencer.c b/sequencer.c\n> index ea96837cde3..aa790f0bba8 100644\n> --- a/sequencer.c\n> +++ b/sequencer.c\n> @@ -5496,7 +5496,7 @@ static void todo_list_add_exec_commands(struct todo_list *todo_list,\n>  \t}\n>\n>  \t/* insert or append final <commands> */\n> -\tif (insert || nr == todo_list->nr) {\n> +\tif (insert) {\n\nLooks good. My worry after first reading this is that we wouldn't insert\nan `--autosquash` rebase that ends in a fixup, e.g.:\n\n    git commit -m foo\n    git commit --fixup HEAD\n    git rebase -i --autosquash -x true HEAD~2\n\nBut we're OK there, since we set insert to 1 when we see the first pick,\nand leave it because we never saw another fixup. Then we'll still have\nfixup as 1 when we exit the loop, and we correctly insert an exec line\nat the end of the fixup chain.\n\nSo I think that having \"|| nr == todo_list->nr\" part of the conditional\nwas broken to begin with.\n\nAs far as I can tell, this behavior of always sticking an 'exec' line at\nthe end of the todo list has existed since the inception of the `-x`\noption back in c214538416 (rebase -i: teach \"--exec <cmd>\", 2012-06-12).\nSee the unconditional `printf \"%s\" \"$cmd\"` at the end of the sub-shell\nwithin `add_exec_commands()` from that commit.\n\nBut this is broken according to the docs, and I think that your fix and\ntest coverage are sensible. Thanks!\n\nThanks,\nTaylor\n"},{"id":"442667","messageId":"211130.865ys9em75.gmgdl@evledraar.gmail.com","threadId":"56999","inReplyTo":"pull.1149.git.git.1638244719381.gitgitgadget@gmail.com","subject":"Re: [BUG REPORT] `git rebase --exec` shouldn't run the exec command when there is nothing to rebase","fromName":"Ævar Arnfjörð Bjarmason","fromEmail":"avarab@gmail.com","sentAt":"2021-11-30T14:03:57Z","receivedAt":"2021-11-30T14:45:55Z","isPatch":false,"sender":{"key":"avarab@gmail.com","avatar":"https://avatars.githubusercontent.com/u/45301?v=4"},"body":"\nOn Mon, Nov 29 2021, Elijah Newren wrote:\n\n[Moving this between threads, from\nhttps://lore.kernel.org/git/CABPp-BFRE2=Owf15WzkacNfdNKbkd2n4GZh7HqDokKzeviBWRw@mail.gmail.com/\nto the patch]\n\n> On Mon, Nov 29, 2021 at 2:25 PM Ævar Arnfjörð Bjarmason\n> <avarab@gmail.com> wrote:\n>>\n>> On Fri, Nov 26 2021, Nikita Bobko wrote:\n>>\n>> > Steps:\n>> > git rebase HEAD --exec \"echo foo\"\n>> >\n>> > EXPECTED: since 0 commits are going to be rebased then I expect \"foo\"\n>> > NOT to be printed\n>> > ACTUAL:   \"foo\" is printed\n>>\n>> I don't think this is a bug, but explicitly desired behavior.\n>\n> My reading of the docs are such that I'd expect the same as Nikita here:\n>\n>         Append \"exec <cmd>\" after each line creating a commit in the final\n>         history.\n>         ...\n>         If --autosquash is used, \"exec\" lines will not be appended for the\n>         intermediate commits, and will only appear at the end of each\n>         squash/fixup series.\n>\n> There is no line creating a commit in the final history when you do a\n> git rebase -i --exec \"echo foo\" HEAD (there is only a noop line), so\n> there should be no exec line.\n\nMaybe you're right & we can just change it. Keep in mind that those docs\nwere added by a non-native speaker (or rather, I'm assuming so based on\nthe name / E-Mail address).\n\nSee c214538416e (rebase -i: teach \"--exec <cmd>\", 2012-06-12). I agree\nthat the reading you've got of it is the more obvious one.\n\nThe reason I thought it wasn't a bug (some of which I dug more into\nafterwards):\n\n 1. I read that \"commit in the final history\" as referring to the range of\n    commits to be rebased. Having only one commit or zero is perfectly OK,\n    since...\n\n 2. ... with \"exec\" we don't know if the \"commit in the final history\" isn't\n   affected with an argument of HEAD. I.e. yes you can also provide \"HEAD~\", but\n   that's the difference between having a \"pick\" line or not. I don't think the\n   sequencer cares, but maybe third party scripting via the sequence editor does?\n\n   We already have an explicit facility to early abort the rebasing. See\n   ff74126c03a (rebase -i: do not fail when there is no commit to cherry-pick,\n   2008-10-10)\n\n   So the feature that Nikita wants is already possible via GIT_SEQUENCE_EDITOR.\n   Now, that's a painful UI, but perhaps if this patch is implemented as a 1=1\n   mapping to that we'll discover some new edge case that wasn't considered?\n\n 3. This isn't just a theoretical concern. It's *interactive* rebase, e.g. a\n    perfectly fine use for it (which I've occasionally used is):\n\n        # no local commits\n        git checkout master\n        # opens my editor with just a \"noop\" line\n        git rebase -i\n\n    And then adding/copying around *new* commits in the buffer and saving\n    it, i.e. using it as an interactive text-based cherry-pick (this is\n    particularly nice with Emacs's magit mode).\n\nFor #3 we can just say \"well use HEAD~ then and ignore the one 'pick'\"\nline. Sure, I've probably only used this once or twice.\n\nI just worry that we'll break thinsg for other users because we're\nnarrowly focusing on --exec as a way to follow-up interactive rebase\ncommands that we insert, and forgetting that this is a generic\ntemplating language that others are intercepting and modifying.\n\nSo e.g. if you want to cherry-pick new commits and always use the same\n10 \"exec\" lines to build and test those you can just rebase to HEAD with\nthose --exec, then copy/paste them for each new thing you insert.\n\nYou can also do that with HEAD~ and carry forward any \"pick\" line, but\nthat's *different*. I.e. we're forcing whoever relies on the current\nsemantics to change their GIT_SEQUENCE_EDITOR script from (pseudocode):\n\n    if grep ^noop git-rebase-todo\n    then\n        for c in commits\n        do\n            echo \"pick $c\"\n            # get the exec lines for each one, if any\n            cat git-rebase-todo\n        done\n    fi\n\nTo something that'll have to handle a single \"pick\" line.\n\n>> When you do:\n>>\n>>     git rebase -x 'make test' BASE\n>>\n>> You expect to run 'make test' for all of BASE..HEAD inclusive of\n>> \"base\". E.g. for HEAD~1 we'll run 'make test' twice, and you know both\n>> your HEAD~ and HEAD passed tests.\n>\n> This is not true.  Try `git rebase -i --exec HEAD~$N` for various\n> values of N>0.  base is not included.\n\nSorry, I meant \"inclusive of HEAD\". I.e. we'll run \"make test\" for HEAD,\nnot just HEAD~. Likewise with any \"exec\" commands.\n\n>> So why wouldn't doing the same for HEAD make sense?\n>\n> Indeed; HEAD is weirdly inconsistent and should be brought in line\n> with the others.\n\nI mean why shouldn't we run \"make test\" on HEAD, sorry. I agree that\nrunning \"make test\" on \"base\" would make no sense. You can rebase to\nBASE~ if you want that.\n\nBut yes, the result is the same as a rebase to HEAD~, so maybe it's fine\nto change it...\n\n>> That being said perhaps some users would think an option or\n>> configuration to skip the injection of \"exec\" after \"noop\" would make\n>> sense in that case.\n>>\n>> But does this really have anything per-se to do with --exec? Wouldn't\n>> such an option/configuration be the same as rebase in general dying if\n>> there's no work to do?\n>>\n>> And wouldn't such a thing be more useful than a narrow change to make\n>> --exec a NOOP in these cases?\n>>\n>> E.g. if I've got a \"topic\" that has commit \"A\", that's since been\n>> integrated into my upstream and I have a script to \"make test\" on my\n>> topics, won't simply dying (and thus indicating that the topic is\n>> dead/integrated) be better than noop-ing?\n>\n> Why do you suggest \"dying\" rather than early completion with success?\n\nIf you do:\n\n    git rebase -i HEAD\n\nComment out the \"noop\" line, and save you'll get:\n\n    error: nothing to do\n\nAnd an exit code of 1.\n\nMaybe we should silently return 0 there, but it seems to me like this\nbehavior needs to be consistent with whatever \"noop\" is trying to\naccomplish in general (see ff74126c03a above).\n\nThat's why I said \"does this really have anything per-se to do with\n--exec?\". I.e. we already observe this behavior without --exec, we just\nget a noop line, and if we had no line at all we'd error with nothing to\ndo.\n\nIf we're going to make \"git rebase -i HEAD\" do nothing, why would it\nhave behavior different from a TODO list of just a \"noop\" line (which is\nnot the same thing as \"nothing to do\").\n\nThat's partially a matter of consistency, but mostly the general\nparanoia that if we're going to subtly change what's *probably* an\nobscure feature hopefully many aren't relying on, then at least having\nit die instead of silently \"succeed\" would be better. I.e. we'll now\nsilently ignore the \"--exec\" commands, but didn't before.\n\n"},{"id":"442766","messageId":"4fa55654-1e7a-aa8e-7e8d-a8512c55447d@gmail.com","threadId":"56999","inReplyTo":"pull.1149.git.git.1638244719381.gitgitgadget@gmail.com","subject":"Re: [PATCH] sequencer: avoid adding exec commands for non-commit creating commands","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2021-12-01T11:24:26Z","receivedAt":"2021-12-01T11:24:31Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Elijah\n\nOn 30/11/2021 03:58, Elijah Newren via GitGitGadget wrote:\n> From: Elijah Newren <newren@gmail.com>\n> \n> The `--exec <cmd>` is documented as\n> \n>      Append \"exec <cmd>\" after each line creating a commit in the final\n>      history.\n>      ...\n>      If --autosquash is used, \"exec\" lines will not be appended for the\n>      intermediate commits, and will only appear at the end of each\n>      squash/fixup series.\n> \n> Unfortunately, it would also add exec commands after non-pick\n> operations, such as 'no-op', which could be seen for example with\n>      git rebase -i --exec true HEAD\n> \n> todo_list_add_exec_commands() intent was to insert exec commands after\n> each logical pick, while trying to consider a chains of fixup and squash\n> commits to be part of the pick before it.  So it would keep an 'insert'\n> boolean tracking if it had seen a pick or merge, but not write the exec\n> command until it saw the next non-fixup/squash command.  Since that\n> would make it miss the final exec command, it had some code that would\n> check whether it still needed to insert one at the end, but instead of a\n> simple\n> \n>      if (insert)\n> \n> it had a\n> \n>      if (insert || <condition that is always true>)\n> \n> That's buggy; as per the docs, we should only add exec commands for\n> lines that create commits, i.e. only if insert is true.  Fix the\n> conditional.\n> \n> There was one testcase in the testsuite that we tweak for this change;\n> it was introduced in 54fd3243da (\"rebase -i: reread the todo list if\n> `exec` touched it\", 2017-04-26), and was merely testing that after an\n> exec had fired that the todo list would be re-read.  The test at the\n> time would have worked given any revision at all, though it would only\n> work with 'HEAD' as a side-effect of this bug.  Since we're fixing this\n> bug, choose something other than 'HEAD' for that test.\n> \n> Finally, add a testcase that verifies when we have no commits to pick,\n> that we get no exec lines in the generated todo list.\n\nThanks for fixing this, the patch looks good and the commit message is \nexcellent. I did see Ævar's concerns in another thread but I think this \nis the least surprising approach - if we're not actually rebasing \nanything it seems odd to run the exec command.\n\nBest Wishes\n\nPhillip\n\n> Reported-by: Nikita Bobko <nikitabobko@gmail.com>\n> Signed-off-by: Elijah Newren <newren@gmail.com>\n> ---\n>      sequencer: avoid adding exec commands for non-commit creating commands\n>      \n>      Original report over at\n>      https://lore.kernel.org/git/YaVzufpKcC0t+q+L@nand.local/T/#m13fbd7b054c06ba1f98ae66e6d1b9fcc51bb875e\n> \n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1149%2Fnewren%2Frebase-exec-empty-bug-v1\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1149/newren/rebase-exec-empty-bug-v1\n> Pull-Request: https://github.com/git/git/pull/1149\n> \n>   sequencer.c                 | 2 +-\n>   t/t3429-rebase-edit-todo.sh | 7 ++++++-\n>   2 files changed, 7 insertions(+), 2 deletions(-)\n> \n> diff --git a/sequencer.c b/sequencer.c\n> index ea96837cde3..aa790f0bba8 100644\n> --- a/sequencer.c\n> +++ b/sequencer.c\n> @@ -5496,7 +5496,7 @@ static void todo_list_add_exec_commands(struct todo_list *todo_list,\n>   \t}\n>   \n>   \t/* insert or append final <commands> */\n> -\tif (insert || nr == todo_list->nr) {\n> +\tif (insert) {\n>   \t\tALLOC_GROW(items, nr + commands->nr, alloc);\n>   \t\tCOPY_ARRAY(items + nr, base_items, commands->nr);\n>   \t\tnr += commands->nr;\n> diff --git a/t/t3429-rebase-edit-todo.sh b/t/t3429-rebase-edit-todo.sh\n> index 7024d49ae7b..abd66f36021 100755\n> --- a/t/t3429-rebase-edit-todo.sh\n> +++ b/t/t3429-rebase-edit-todo.sh\n> @@ -13,10 +13,15 @@ test_expect_success 'setup' '\n>   \n>   test_expect_success 'rebase exec modifies rebase-todo' '\n>   \ttodo=.git/rebase-merge/git-rebase-todo &&\n> -\tgit rebase HEAD -x \"echo exec touch F >>$todo\" &&\n> +\tgit rebase HEAD~1 -x \"echo exec touch F >>$todo\" &&\n>   \ttest -e F\n>   '\n>   \n> +test_expect_success 'rebase exec with an empty list does not exec anything' '\n> +\tgit rebase HEAD -x \"true\" 2>output &&\n> +\t! grep \"Executing: true\" output\n> +'\n> +\n>   test_expect_success 'loose object cache vs re-reading todo list' '\n>   \tGIT_REBASE_TODO=.git/rebase-merge/git-rebase-todo &&\n>   \texport GIT_REBASE_TODO &&\n> \n> base-commit: 35151cf0720460a897cde9b8039af364743240e7\n> \n\n"},{"id":"442767","messageId":"4a6387a9-ade0-3690-0003-df28081f76a3@gmail.com","threadId":"56999","inReplyTo":"211130.865ys9em75.gmgdl@evledraar.gmail.com","subject":"Re: [BUG REPORT] `git rebase --exec` shouldn't run the exec command when there is nothing to rebase","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2021-12-01T11:45:10Z","receivedAt":"2021-12-01T11:45:16Z","isPatch":false,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 30/11/2021 14:03, Ævar Arnfjörð Bjarmason wrote:\n> \n> On Mon, Nov 29 2021, Elijah Newren wrote:\n> \n> [Moving this between threads, from\n> https://lore.kernel.org/git/CABPp-BFRE2=Owf15WzkacNfdNKbkd2n4GZh7HqDokKzeviBWRw@mail.gmail.com/\n> to the patch]\n> \n>> On Mon, Nov 29, 2021 at 2:25 PM Ævar Arnfjörð Bjarmason\n>> <avarab@gmail.com> wrote:\n>>>\n>>> On Fri, Nov 26 2021, Nikita Bobko wrote:\n>>>\n>>>> Steps:\n>>>> git rebase HEAD --exec \"echo foo\"\n>>>>\n>>>> EXPECTED: since 0 commits are going to be rebased then I expect \"foo\"\n>>>> NOT to be printed\n>>>> ACTUAL:   \"foo\" is printed\n>>>\n>>> I don't think this is a bug, but explicitly desired behavior.\n>>\n>> My reading of the docs are such that I'd expect the same as Nikita here:\n>>\n>>          Append \"exec <cmd>\" after each line creating a commit in the final\n>>          history.\n>>          ...\n>>          If --autosquash is used, \"exec\" lines will not be appended for the\n>>          intermediate commits, and will only appear at the end of each\n>>          squash/fixup series.\n>>\n>> There is no line creating a commit in the final history when you do a\n>> git rebase -i --exec \"echo foo\" HEAD (there is only a noop line), so\n>> there should be no exec line.\n> \n> Maybe you're right & we can just change it. Keep in mind that those docs\n> were added by a non-native speaker (or rather, I'm assuming so based on\n> the name / E-Mail address).\n> \n> See c214538416e (rebase -i: teach \"--exec <cmd>\", 2012-06-12). I agree\n> that the reading you've got of it is the more obvious one.\n> \n> The reason I thought it wasn't a bug (some of which I dug more into\n> afterwards):\n> \n>   1. I read that \"commit in the final history\" as referring to the range of\n>      commits to be rebased. Having only one commit or zero is perfectly OK,\n>      since...\n> \n>   2. ... with \"exec\" we don't know if the \"commit in the final history\" isn't\n>     affected with an argument of HEAD. I.e. yes you can also provide \"HEAD~\", but\n>     that's the difference between having a \"pick\" line or not. I don't think the\n>     sequencer cares, but maybe third party scripting via the sequence editor does?\n> \n>     We already have an explicit facility to early abort the rebasing. See\n>     ff74126c03a (rebase -i: do not fail when there is no commit to cherry-pick,\n>     2008-10-10)\n> \n>     So the feature that Nikita wants is already possible via GIT_SEQUENCE_EDITOR.\n>     Now, that's a painful UI, but perhaps if this patch is implemented as a 1=1\n>     mapping to that we'll discover some new edge case that wasn't considered?\n> \n>   3. This isn't just a theoretical concern. It's *interactive* rebase, e.g. a\n>      perfectly fine use for it (which I've occasionally used is):\n> \n>          # no local commits\n>          git checkout master\n>          # opens my editor with just a \"noop\" line\n>          git rebase -i\n> \n>      And then adding/copying around *new* commits in the buffer and saving\n>      it, i.e. using it as an interactive text-based cherry-pick (this is\n>      particularly nice with Emacs's magit mode).\n> \n> For #3 we can just say \"well use HEAD~ then and ignore the one 'pick'\"\n> line. Sure, I've probably only used this once or twice.\n\nI'm not sure I really follow. For #3 you can just type the exec command \ninto the editor rather than passing it on the command line. You already \nhave to manually add exec commands after any new pick lines anyway.\n\n> I just worry that we'll break thinsg for other users because we're\n> narrowly focusing on --exec as a way to follow-up interactive rebase\n> commands that we insert, and forgetting that this is a generic\n> templating language that others are intercepting and modifying.\n\nI see what you're getting at but I think this is a small enough corner \ncase that we shouldn't worry too much. I think it is simpler to say if \nwe don't pick any commits we don't add any exec commands.\n\n >[...]\n>>> When you do:\n>>>\n>>>      git rebase -x 'make test' BASE\n>>>\n>>> You expect to run 'make test' for all of BASE..HEAD inclusive of\n>>> \"base\". E.g. for HEAD~1 we'll run 'make test' twice, and you know both\n>>> your HEAD~ and HEAD passed tests.\n>>\n>> This is not true.  Try `git rebase -i --exec HEAD~$N` for various\n>> values of N>0.  base is not included.\n> \n> Sorry, I meant \"inclusive of HEAD\". I.e. we'll run \"make test\" for HEAD,\n> not just HEAD~. Likewise with any \"exec\" commands.\n\nWe do not run \"make test\" for HEAD~ when executing \"git rebase -x 'make \ntest' HEAD~1\".\n\n>>> So why wouldn't doing the same for HEAD make sense?\n>>\n>> Indeed; HEAD is weirdly inconsistent and should be brought in line\n>> with the others.\n> \n> I mean why shouldn't we run \"make test\" on HEAD, sorry. I agree that\n> running \"make test\" on \"base\" would make no sense. You can rebase to\n> BASE~ if you want that.\n> \n> But yes, the result is the same as a rebase to HEAD~, so maybe it's fine\n> to change it...\n> \n>>> That being said perhaps some users would think an option or\n>>> configuration to skip the injection of \"exec\" after \"noop\" would make\n>>> sense in that case.\n>>>\n>>> But does this really have anything per-se to do with --exec? Wouldn't\n>>> such an option/configuration be the same as rebase in general dying if\n>>> there's no work to do?\n>>>\n>>> And wouldn't such a thing be more useful than a narrow change to make\n>>> --exec a NOOP in these cases?\n>>>\n>>> E.g. if I've got a \"topic\" that has commit \"A\", that's since been\n>>> integrated into my upstream and I have a script to \"make test\" on my\n>>> topics, won't simply dying (and thus indicating that the topic is\n>>> dead/integrated) be better than noop-ing?\n>>\n>> Why do you suggest \"dying\" rather than early completion with success?\n> \n> If you do:\n> \n>      git rebase -i HEAD\n> \n> Comment out the \"noop\" line, and save you'll get:\n> \n>      error: nothing to do\n> \n> And an exit code of 1.\n> \n> Maybe we should silently return 0 there, but it seems to me like this\n> behavior needs to be consistent with whatever \"noop\" is trying to\n> accomplish in general (see ff74126c03a above).\n> \n> That's why I said \"does this really have anything per-se to do with\n> --exec?\". I.e. we already observe this behavior without --exec, we just\n> get a noop line, and if we had no line at all we'd error with nothing to\n> do.\n> \n> If we're going to make \"git rebase -i HEAD\" do nothing, why would it\n> have behavior different from a TODO list of just a \"noop\" line (which is\n> not the same thing as \"nothing to do\").\n> \n> That's partially a matter of consistency, but mostly the general\n> paranoia that if we're going to subtly change what's *probably* an\n> obscure feature hopefully many aren't relying on, then at least having\n> it die instead of silently \"succeed\" would be better. I.e. we'll now\n> silently ignore the \"--exec\" commands, but didn't before.\n\nI wonder if we could print a warning if the exec command gets ignored. I \nhaven't looked how hard it would be to do in general but certainly for \n'rebase -x cmd HEAD' it should be simple(ish?) to do that. We also pick \nnothing if we're already up to date. If HEAD is an ancestor of \n<upstream> then I think we avoid fast-forwarding when there is an exec \ncommand so we will pick commits in that case.\n\nBest Wishes\n\nPhillip\n\n"},{"id":"443043","messageId":"nycvar.QRO.7.76.6.2112032321530.63@tvgsbejvaqbjf.bet","threadId":"56999","inReplyTo":"pull.1149.git.git.1638244719381.gitgitgadget@gmail.com","subject":"Re: [PATCH] sequencer: avoid adding exec commands for non-commit creating commands","fromName":"Johannes Schindelin","fromEmail":"johannes.schindelin@gmx.de","sentAt":"2021-12-03T22:22:15Z","receivedAt":"2021-12-03T22:22:23Z","isPatch":true,"sender":{"key":"johannes.schindelin@gmx.de","avatar":"https://avatars.githubusercontent.com/u/127790?v=4"},"body":"Hi Elijah,\n\nOn Tue, 30 Nov 2021, Elijah Newren via GitGitGadget wrote:\n\n> From: Elijah Newren <newren@gmail.com>\n>\n> The `--exec <cmd>` is documented as\n>\n>     Append \"exec <cmd>\" after each line creating a commit in the final\n>     history.\n>     ...\n>     If --autosquash is used, \"exec\" lines will not be appended for the\n>     intermediate commits, and will only appear at the end of each\n>     squash/fixup series.\n>\n> Unfortunately, it would also add exec commands after non-pick\n> operations, such as 'no-op', which could be seen for example with\n>     git rebase -i --exec true HEAD\n>\n> todo_list_add_exec_commands() intent was to insert exec commands after\n> each logical pick, while trying to consider a chains of fixup and squash\n> commits to be part of the pick before it.  So it would keep an 'insert'\n> boolean tracking if it had seen a pick or merge, but not write the exec\n> command until it saw the next non-fixup/squash command.  Since that\n> would make it miss the final exec command, it had some code that would\n> check whether it still needed to insert one at the end, but instead of a\n> simple\n>\n>     if (insert)\n>\n> it had a\n>\n>     if (insert || <condition that is always true>)\n>\n> That's buggy; as per the docs, we should only add exec commands for\n> lines that create commits, i.e. only if insert is true.  Fix the\n> conditional.\n>\n> There was one testcase in the testsuite that we tweak for this change;\n> it was introduced in 54fd3243da (\"rebase -i: reread the todo list if\n> `exec` touched it\", 2017-04-26), and was merely testing that after an\n> exec had fired that the todo list would be re-read.  The test at the\n> time would have worked given any revision at all, though it would only\n> work with 'HEAD' as a side-effect of this bug.  Since we're fixing this\n> bug, choose something other than 'HEAD' for that test.\n>\n> Finally, add a testcase that verifies when we have no commits to pick,\n> that we get no exec lines in the generated todo list.\n>\n> Reported-by: Nikita Bobko <nikitabobko@gmail.com>\n> Signed-off-by: Elijah Newren <newren@gmail.com>\n\nThis patch gets my whole-hearted ACK!\n\nThank you,\nDscho\n\n> ---\n>     sequencer: avoid adding exec commands for non-commit creating commands\n>\n>     Original report over at\n>     https://lore.kernel.org/git/YaVzufpKcC0t+q+L@nand.local/T/#m13fbd7b054c06ba1f98ae66e6d1b9fcc51bb875e\n>\n> Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-git-1149%2Fnewren%2Frebase-exec-empty-bug-v1\n> Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-git-1149/newren/rebase-exec-empty-bug-v1\n> Pull-Request: https://github.com/git/git/pull/1149\n>\n>  sequencer.c                 | 2 +-\n>  t/t3429-rebase-edit-todo.sh | 7 ++++++-\n>  2 files changed, 7 insertions(+), 2 deletions(-)\n>\n> diff --git a/sequencer.c b/sequencer.c\n> index ea96837cde3..aa790f0bba8 100644\n> --- a/sequencer.c\n> +++ b/sequencer.c\n> @@ -5496,7 +5496,7 @@ static void todo_list_add_exec_commands(struct todo_list *todo_list,\n>  \t}\n>\n>  \t/* insert or append final <commands> */\n> -\tif (insert || nr == todo_list->nr) {\n> +\tif (insert) {\n>  \t\tALLOC_GROW(items, nr + commands->nr, alloc);\n>  \t\tCOPY_ARRAY(items + nr, base_items, commands->nr);\n>  \t\tnr += commands->nr;\n> diff --git a/t/t3429-rebase-edit-todo.sh b/t/t3429-rebase-edit-todo.sh\n> index 7024d49ae7b..abd66f36021 100755\n> --- a/t/t3429-rebase-edit-todo.sh\n> +++ b/t/t3429-rebase-edit-todo.sh\n> @@ -13,10 +13,15 @@ test_expect_success 'setup' '\n>\n>  test_expect_success 'rebase exec modifies rebase-todo' '\n>  \ttodo=.git/rebase-merge/git-rebase-todo &&\n> -\tgit rebase HEAD -x \"echo exec touch F >>$todo\" &&\n> +\tgit rebase HEAD~1 -x \"echo exec touch F >>$todo\" &&\n>  \ttest -e F\n>  '\n>\n> +test_expect_success 'rebase exec with an empty list does not exec anything' '\n> +\tgit rebase HEAD -x \"true\" 2>output &&\n> +\t! grep \"Executing: true\" output\n> +'\n> +\n>  test_expect_success 'loose object cache vs re-reading todo list' '\n>  \tGIT_REBASE_TODO=.git/rebase-merge/git-rebase-todo &&\n>  \texport GIT_REBASE_TODO &&\n>\n> base-commit: 35151cf0720460a897cde9b8039af364743240e7\n> --\n> gitgitgadget\n>\n"}]}