git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/2] commit/reset: try to clean up sequencer state

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Apr 8, 2019, 15:47 UTC
Message-ID
<9f3ec8b3-f2c4-63b8-3350-a37d42bebd47@gmail.com>
In-Reply-To
<CACsJy8D3tH0K8wNLighuNtjUtv3K3TGNMGgx3T5j5sCxok8hbQ@mail.gmail.com>
On 01/04/2019 11:09, Duy Nguyen wrote:
Show 45 quoted lines
> On Fri, Mar 29, 2019 at 11:32 PM Phillip Wood <phillip.wood123@gmail.com> wrote:
>>
>> From: Phillip Wood <phillip.wood@dunelm.org.uk>
>>
>> When cherry-picking or reverting a sequence of commits and if the final
>> pick/revert has conflicts and the user uses `git commit` to commit the
>> conflict resolution and does not run `git cherry-pick --continue` then
>> the sequencer state is left behind. This can cause problems later. In my
>> case I cherry-picked a sequence of commits the last one of which I
>> committed with `git commit` after resolving some conflicts, then a while
>> later, on a different branch I aborted a revert which rewound my HEAD to
>> the end of the cherry-pick sequence on the previous branch. Avoid this
>> potential problem by removing the sequencer state if we're committing or
>> resetting the final pick in a sequence.
>>
>> Signed-off-by: Phillip Wood <phillip.wood@dunelm.org.uk>
>> ---
>>   branch.c                        |  7 +++++--
>>   builtin/commit.c                |  7 +++++--
>>   sequencer.c                     | 23 +++++++++++++++++++++++
>>   sequencer.h                     |  1 +
>>   t/t3507-cherry-pick-conflict.sh | 19 +++++++++++++++++++
>>   5 files changed, 53 insertions(+), 4 deletions(-)
>>
>> diff --git a/branch.c b/branch.c
>> index 28b81a7e02..9ed60081c1 100644
>> --- a/branch.c
>> +++ b/branch.c
>> @@ -5,6 +5,7 @@
>>   #include "refs.h"
>>   #include "refspec.h"
>>   #include "remote.h"
>> +#include "sequencer.h"
>>   #include "commit.h"
>>   #include "worktree.h"
>>
>> @@ -339,8 +340,10 @@ void create_branch(struct repository *r,
>>
>>   void remove_branch_state(struct repository *r)
> 
> This function is also called in git-am, git-rebase and git-checkout.
> While the first two should not be affected, git-checkout can be
> executed while we're in the middle of a cherry-pick or revert. I guess
> that's ok because git-checkout is basically the same as git-reset in
> this case?
Yes that's right
Show 57 quoted lines
>>   {
>> -       unlink(git_path_cherry_pick_head(r));
>> -       unlink(git_path_revert_head(r));
>> +       if (!unlink(git_path_cherry_pick_head(r)))
>> +               sequencer_post_commit_cleanup();
>> +       if (!unlink(git_path_revert_head(r)))
>> +               sequencer_post_commit_cleanup();
>>          unlink(git_path_merge_head(r));
>>          unlink(git_path_merge_rr(r));
>>          unlink(git_path_merge_msg(r));
>> diff --git a/builtin/commit.c b/builtin/commit.c
>> index 2986553d5f..422b7d62a5 100644
>> --- a/builtin/commit.c
>> +++ b/builtin/commit.c
>> @@ -1657,8 +1657,10 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
>>                  die("%s", err.buf);
>>          }
>>
>> -       unlink(git_path_cherry_pick_head(the_repository));
>> -       unlink(git_path_revert_head(the_repository));
>> +       if (!unlink(git_path_cherry_pick_head(the_repository)))
>> +               sequencer_post_commit_cleanup();
>> +       if (!unlink(git_path_revert_head(the_repository)))
>> +               sequencer_post_commit_cleanup();
>>          unlink(git_path_merge_head(the_repository));
>>          unlink(git_path_merge_msg(the_repository));
>>          unlink(git_path_merge_mode(the_repository));
>> @@ -1678,6 +1680,7 @@ int cmd_commit(int argc, const char **argv, const char *prefix)
>>          if (amend && !no_post_rewrite) {
>>                  commit_post_rewrite(the_repository, current_head, &oid);
>>          }
>> +
>>          if (!quiet) {
>>                  unsigned int flags = 0;
>>
>> diff --git a/sequencer.c b/sequencer.c
>> index 0db410d590..028699209f 100644
>> --- a/sequencer.c
>> +++ b/sequencer.c
>> @@ -2220,6 +2220,29 @@ static ssize_t strbuf_read_file_or_whine(struct strbuf *sb, const char *path)
>>          return len;
>>   }
>>
>> +void sequencer_post_commit_cleanup(void)
>> +{
>> +       struct replay_opts opts = REPLAY_OPTS_INIT;
>> +       struct strbuf buf = STRBUF_INIT;
>> +       const char *eol;
>> +       const char *todo_path = git_path_todo_file();
>> +
>> +       if (strbuf_read_file(&buf, todo_path, 0) < 0) {
>> +               if (errno == ENOENT) {
>> +                       return;
>> +               } else {
>> +                       error_errno("unable to open '%s'", todo_path);
> 
> _() the string to make it translatable.
Well spotted, thanks
Show 10 quoted lines
>> +                       return;
>> +               }
>> +       }
>> +       /* If there is only one line then we are done */
>> +       eol = strchr(buf.buf, '\n');
>> +       if (!eol || !eol[1])
>> +               sequencer_remove_state(&opts);
> 
> Should we say something to let the user know cherry-pick/revert is
> finished? (unless --quiet is specified)

I'd not thought of that, at the moment we don't say anything about removing CHERRY_PICK_HEAD etc when they are removed by reset or checkout, I'm not sure this is much different to those cases - but maybe they should be printing some feedback as well.

Best Wishes
Phillip
Show 7 quoted lines
>> +
>> +       strbuf_release(&buf);
>> +}
>> +
>>   static int read_populate_todo(struct repository *r,
>>                                struct todo_list *todo_list,
>>                                struct replay_opts *opts)
Previous: Duy NguyenNext: Phillip Wood
Message 6 of 15 in “A couple of cherry-pick related fixes”
  1. 0/2 A couple of cherry-pick related fixesPhillip Wood, Mar 29, 2019
  2. 1/2 commit/reset: try to clean up sequencer statePhillip Wood, Mar 29, 2019
  3. Junio C HamanoApr 1, 2019
  4. Phillip WoodApr 8, 2019
  5. Duy NguyenApr 1, 2019
  6. Phillip WoodApr 8, 2019
  7. 2/2 fix cherry-pick/revert status after commitPhillip Wood, Mar 29, 2019
  8. Junio C HamanoApr 1, 2019
  9. Phillip WoodApr 8, 2019
  10. 0/2 A couple of cherry-pick related fixesPhillip Wood, Apr 16, 2019
  11. 2/2 fix cherry-pick/revert status after commitPhillip Wood, Apr 16, 2019
  12. 1/2 commit/reset: try to clean up sequencer statePhillip Wood, Apr 16, 2019
  13. Junio C HamanoApr 17, 2019
  14. Johannes SchindelinApr 17, 2019
  15. Phillip WoodApr 17, 2019

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.