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

Re: [PATCH v6 14/45] builtin-am: implement --abort

From
Stefan Beller <sbeller@google.com>
Date
Jul 31, 2015, 23:42 UTC
Message-ID
<CAGZ79kYnxjcreKR8igyfUvNBGd836ycKhCSo1cm18feTpe1GxQ@mail.gmail.com>
In-Reply-To
<1437322237-29863-15-git-send-email-pyokagan@gmail.com>
On Sun, Jul 19, 2015 at 9:10 AM, Paul Tan <pyokagan@gmail.com> wrote:
Show 149 quoted lines
> Since 3e5057a (git am --abort, 2008-07-16), git-am supported the --abort
> option that will rewind HEAD back to the original commit. Re-implement
> this through am_abort().
>
> Since 7b3b7e3 (am --abort: keep unrelated commits since the last failure
> and warn, 2010-12-21), to prevent commits made since the last failure
> from being lost, git-am will not rewind HEAD back to the original
> commit if HEAD moved since the last failure. Re-implement this through
> safe_to_abort().
>
> Signed-off-by: Paul Tan <pyokagan@gmail.com>
> ---
>  builtin/am.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 99 insertions(+), 3 deletions(-)
>
> diff --git a/builtin/am.c b/builtin/am.c
> index 765844b..8a054ee 100644
> --- a/builtin/am.c
> +++ b/builtin/am.c
> @@ -490,6 +490,8 @@ static int split_mail(struct am_state *state, enum patch_format patch_format,
>  static void am_setup(struct am_state *state, enum patch_format patch_format,
>                         const char **paths)
>  {
> +       unsigned char curr_head[GIT_SHA1_RAWSZ];
> +
>         if (!patch_format)
>                 patch_format = detect_patch_format(paths);
>
> @@ -506,6 +508,14 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
>                 die(_("Failed to split patches."));
>         }
>
> +       if (!get_sha1("HEAD", curr_head)) {
> +               write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(curr_head));
> +               update_ref("am", "ORIG_HEAD", curr_head, NULL, 0, UPDATE_REFS_DIE_ON_ERR);
> +       } else {
> +               write_file(am_path(state, "abort-safety"), 1, "%s", "");
> +               delete_ref("ORIG_HEAD", NULL, 0);
> +       }
> +
>         /*
>          * NOTE: Since the "next" and "last" files determine if an am_state
>          * session is in progress, they should be written last.
> @@ -522,6 +532,8 @@ static void am_setup(struct am_state *state, enum patch_format patch_format,
>   */
>  static void am_next(struct am_state *state)
>  {
> +       unsigned char head[GIT_SHA1_RAWSZ];
> +
>         free(state->author_name);
>         state->author_name = NULL;
>
> @@ -538,6 +550,11 @@ static void am_next(struct am_state *state)
>         unlink(am_path(state, "author-script"));
>         unlink(am_path(state, "final-commit"));
>
> +       if (!get_sha1("HEAD", head))
> +               write_file(am_path(state, "abort-safety"), 1, "%s", sha1_to_hex(head));
> +       else
> +               write_file(am_path(state, "abort-safety"), 1, "%s", "");
> +
>         state->cur++;
>         write_file(am_path(state, "next"), 1, "%d", state->cur);
>  }
> @@ -788,10 +805,14 @@ static void am_run(struct am_state *state, int resume)
>         const char *argv_gc_auto[] = {"gc", "--auto", NULL};
>         struct strbuf sb = STRBUF_INIT;
>
> +       unlink(am_path(state, "dirtyindex"));
> +
>         refresh_and_write_cache();
>
> -       if (index_has_changes(&sb))
> +       if (index_has_changes(&sb)) {
> +               write_file(am_path(state, "dirtyindex"), 1, "t");
>                 die(_("Dirty index: cannot apply patches (dirty: %s)"), sb.buf);
> +       }
>
>         strbuf_release(&sb);
>
> @@ -980,6 +1001,74 @@ static void am_skip(struct am_state *state)
>  }
>
>  /**
> + * Returns true if it is safe to reset HEAD to the ORIG_HEAD, false otherwise.
> + *
> + * It is not safe to reset HEAD when:
> + * 1. git-am previously failed because the index was dirty.
> + * 2. HEAD has moved since git-am previously failed.
> + */
> +static int safe_to_abort(const struct am_state *state)
> +{
> +       struct strbuf sb = STRBUF_INIT;
> +       unsigned char abort_safety[GIT_SHA1_RAWSZ], head[GIT_SHA1_RAWSZ];
> +
> +       if (file_exists(am_path(state, "dirtyindex")))
> +               return 0;
> +
> +       if (read_state_file(&sb, state, "abort-safety", 1) > 0) {
> +               if (get_sha1_hex(sb.buf, abort_safety))
> +                       die(_("could not parse %s"), am_path(state, "abort_safety"));
> +       } else
> +               hashclr(abort_safety);
> +
> +       if (get_sha1("HEAD", head))
> +               hashclr(head);
> +
> +       if (!hashcmp(head, abort_safety))
> +               return 1;
> +
> +       error(_("You seem to have moved HEAD since the last 'am' failure.\n"
> +               "Not rewinding to ORIG_HEAD"));
> +
> +       return 0;
> +}
> +
> +/**
> + * Aborts the current am session if it is safe to do so.
> + */
> +static void am_abort(struct am_state *state)
> +{
> +       unsigned char curr_head[GIT_SHA1_RAWSZ], orig_head[GIT_SHA1_RAWSZ];
> +       int has_curr_head, has_orig_head;
> +       const char *curr_branch;
> +
> +       if (!safe_to_abort(state)) {
> +               am_destroy(state);
> +               return;
> +       }
> +
> +       curr_branch = resolve_refdup("HEAD", 0, curr_head, NULL);
> +       has_curr_head = !is_null_sha1(curr_head);
> +       if (!has_curr_head)
> +               hashcpy(curr_head, EMPTY_TREE_SHA1_BIN);
> +
> +       has_orig_head = !get_sha1("ORIG_HEAD", orig_head);
> +       if (!has_orig_head)
> +               hashcpy(orig_head, EMPTY_TREE_SHA1_BIN);
> +
> +       clean_index(curr_head, orig_head);
> +
> +       if (has_orig_head)
> +               update_ref("am --abort", "HEAD", orig_head,
> +                               has_curr_head ? curr_head : NULL, 0,
> +                               UPDATE_REFS_DIE_ON_ERR);
> +       else if (curr_branch)
> +               delete_ref(curr_branch, NULL, REF_NODEREF);
> +
> +       am_destroy(state);
free(curr_branch) here?
Show 48 quoted lines
> +}
> +
> +/**
>   * parse_options() callback that validates and sets opt->value to the
>   * PATCH_FORMAT_* enum value corresponding to `arg`.
>   */
> @@ -998,7 +1087,8 @@ enum resume_mode {
>         RESUME_FALSE = 0,
>         RESUME_APPLY,
>         RESUME_RESOLVED,
> -       RESUME_SKIP
> +       RESUME_SKIP,
> +       RESUME_ABORT
>  };
>
>  int cmd_am(int argc, const char **argv, const char *prefix)
> @@ -1009,7 +1099,7 @@ int cmd_am(int argc, const char **argv, const char *prefix)
>
>         const char * const usage[] = {
>                 N_("git am [options] [(<mbox>|<Maildir>)...]"),
> -               N_("git am [options] (--continue | --skip)"),
> +               N_("git am [options] (--continue | --skip | --abort)"),
>                 NULL
>         };
>
> @@ -1026,6 +1116,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
>                 OPT_CMDMODE(0, "skip", &resume,
>                         N_("skip the current patch"),
>                         RESUME_SKIP),
> +               OPT_CMDMODE(0, "abort", &resume,
> +                       N_("restore the original branch and abort the patching operation."),
> +                       RESUME_ABORT),
>                 OPT_END()
>         };
>
> @@ -1090,6 +1183,9 @@ int cmd_am(int argc, const char **argv, const char *prefix)
>         case RESUME_SKIP:
>                 am_skip(&state);
>                 break;
> +       case RESUME_ABORT:
> +               am_abort(&state);
> +               break;
>         default:
>                 die("BUG: invalid resume value");
>         }
> --
> 2.5.0.rc2.110.gb39b692
>
Previous: Paul TanNext: Paul Tan
Message 16 of 47 in “Make git-am a builtin”
  1. 00/45 Make git-am a builtinPaul Tan, Jul 19, 2015
  2. 01/45 wrapper: implement xopen()Paul Tan, Jul 19, 2015
  3. 02/45 wrapper: implement xfopen()Paul Tan, Jul 19, 2015
  4. 03/45 builtin-am: implement skeletal builtin amPaul Tan, Jul 19, 2015
  5. 04/45 builtin-am: implement patch queue mechanismPaul Tan, Jul 19, 2015
  6. 05/45 builtin-am: split out mbox/maildir patches with git-mailsplitPaul Tan, Jul 19, 2015
  7. 06/45 builtin-am: auto-detect mbox patchesPaul Tan, Jul 19, 2015
  8. 07/45 builtin-am: extract patch and commit info with git-mailinfoPaul Tan, Jul 19, 2015
  9. 08/45 builtin-am: apply patch with git-applyPaul Tan, Jul 19, 2015
  10. 09/45 builtin-am: implement committing applied patchPaul Tan, Jul 19, 2015
  11. 10/45 builtin-am: refuse to apply patches if index is dirtyPaul Tan, Jul 19, 2015
  12. 11/45 builtin-am: implement --resolved/--continuePaul Tan, Jul 19, 2015
  13. 12/45 builtin-am: don't parse mail when resumingPaul Tan, Jul 19, 2015
  14. 13/45 builtin-am: implement --skipPaul Tan, Jul 19, 2015
  15. 14/45 builtin-am: implement --abortPaul Tan, Jul 19, 2015
  16. Stefan BellerJul 31, 2015
  17. 15/45 builtin-am: reject patches when there's a session in progressPaul Tan, Jul 19, 2015
  18. 16/45 builtin-am: implement -q/--quietPaul Tan, Jul 19, 2015
  19. 17/45 builtin-am: exit with user friendly message on failurePaul Tan, Jul 19, 2015
  20. 18/45 builtin-am: implement -s/--signoffPaul Tan, Jul 19, 2015
  21. 19/45 cache-tree: introduce write_index_as_tree()Paul Tan, Jul 19, 2015
  22. 20/45 builtin-am: implement --3way, am.threeWayPaul Tan, Jul 19, 2015
  23. 21/45 builtin-am: implement --rebasing modePaul Tan, Jul 19, 2015
  24. 22/45 builtin-am: bypass git-mailinfo when --rebasingPaul Tan, Jul 19, 2015
  25. 23/45 builtin-am: handle stray state directoryPaul Tan, Jul 19, 2015
  26. 24/45 builtin-am: implement -u/--utf8Paul Tan, Jul 19, 2015
  27. 25/45 builtin-am: implement -k/--keep, --keep-non-patchPaul Tan, Jul 19, 2015
  28. 26/45 builtin-am: implement --[no-]message-id, am.messageidPaul Tan, Jul 19, 2015
  29. 27/45 builtin-am: support --keep-cr, am.keepcrPaul Tan, Jul 19, 2015
  30. 28/45 builtin-am: implement --[no-]scissorsPaul Tan, Jul 19, 2015
  31. 29/45 builtin-am: pass git-apply's options to git-applyPaul Tan, Jul 19, 2015
  32. 30/45 builtin-am: implement --ignore-datePaul Tan, Jul 19, 2015
  33. 31/45 builtin-am: implement --committer-date-is-author-datePaul Tan, Jul 19, 2015
  34. 32/45 builtin-am: implement -S/--gpg-sign, commit.gpgsignPaul Tan, Jul 19, 2015
  35. 33/45 builtin-am: invoke post-rewrite hookPaul Tan, Jul 19, 2015
  36. 34/45 builtin-am: support automatic notes copyingPaul Tan, Jul 19, 2015
  37. 35/45 builtin-am: invoke applypatch-msg hookPaul Tan, Jul 19, 2015
  38. 36/45 builtin-am: invoke pre-applypatch hookPaul Tan, Jul 19, 2015
  39. 37/45 builtin-am: invoke post-applypatch hookPaul Tan, Jul 19, 2015
  40. 38/45 builtin-am: rerere supportPaul Tan, Jul 19, 2015
  41. 39/45 builtin-am: support and auto-detect StGit patchesPaul Tan, Jul 19, 2015
  42. 40/45 builtin-am: support and auto-detect StGit series filesPaul Tan, Jul 19, 2015
  43. 41/45 builtin-am: support and auto-detect mercurial patchesPaul Tan, Jul 19, 2015
  44. 42/45 builtin-am: implement -i/--interactivePaul Tan, Jul 19, 2015
  45. 43/45 builtin-am: implement legacy -b/--binary optionPaul Tan, Jul 19, 2015
  46. 44/45 builtin-am: check for valid committer identPaul Tan, Jul 19, 2015
  47. 45/45 builtin-am: remove redirection to git-am.shPaul Tan, Jul 19, 2015

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.