diff options
author | Paul Tan <pyokagan@gmail.com> | 2015-08-04 21:51:30 +0800 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2015-08-04 22:02:11 -0700 |
commit | c9e8d960b612d5962cd1e952916c2ab6f483e620 (patch) | |
tree | 9892ab84e5c699d607ddb7613971dd6cfeefc425 | |
parent | 38a824fe050c4da3f2e0979a94062119080a77a0 (diff) | |
download | git-c9e8d960b612d5962cd1e952916c2ab6f483e620.tar.gz |
builtin-am: implement committing applied patch
Implement do_commit(), which commits the index which contains the
results of applying the patch, along with the extracted commit message
and authorship information.
Since 29b6754 (am: remove rebase-apply directory before gc, 2010-02-22),
git gc --auto is also invoked to pack the loose objects that are created
from making the commits.
Signed-off-by: Paul Tan <pyokagan@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
-rw-r--r-- | builtin/am.c | 55 |
1 files changed, 51 insertions, 4 deletions
diff --git a/builtin/am.c b/builtin/am.c index 1f198e4f31..a2811b687a 100644 --- a/builtin/am.c +++ b/builtin/am.c @@ -11,6 +11,9 @@ #include "run-command.h" #include "quote.h" #include "lockfile.h" +#include "cache-tree.h" +#include "refs.h" +#include "commit.h" /** * Returns 1 if the file is empty or does not exist, 0 otherwise. @@ -674,10 +677,56 @@ static int run_apply(const struct am_state *state) } /** + * Commits the current index with state->msg as the commit message and + * state->author_name, state->author_email and state->author_date as the author + * information. + */ +static void do_commit(const struct am_state *state) +{ + unsigned char tree[GIT_SHA1_RAWSZ], parent[GIT_SHA1_RAWSZ], + commit[GIT_SHA1_RAWSZ]; + unsigned char *ptr; + struct commit_list *parents = NULL; + const char *reflog_msg, *author; + struct strbuf sb = STRBUF_INIT; + + if (write_cache_as_tree(tree, 0, NULL)) + die(_("git write-tree failed to write a tree")); + + if (!get_sha1_commit("HEAD", parent)) { + ptr = parent; + commit_list_insert(lookup_commit(parent), &parents); + } else { + ptr = NULL; + fprintf_ln(stderr, _("applying to an empty history")); + } + + author = fmt_ident(state->author_name, state->author_email, + state->author_date, IDENT_STRICT); + + if (commit_tree(state->msg, state->msg_len, tree, parents, commit, + author, NULL)) + die(_("failed to write commit object")); + + reflog_msg = getenv("GIT_REFLOG_ACTION"); + if (!reflog_msg) + reflog_msg = "am"; + + strbuf_addf(&sb, "%s: %.*s", reflog_msg, linelen(state->msg), + state->msg); + + update_ref(sb.buf, "HEAD", commit, ptr, 0, UPDATE_REFS_DIE_ON_ERR); + + strbuf_release(&sb); +} + +/** * Applies all queued mail. */ static void am_run(struct am_state *state) { + const char *argv_gc_auto[] = {"gc", "--auto", NULL}; + refresh_and_write_cache(); while (state->cur <= state->last) { @@ -709,16 +758,14 @@ static void am_run(struct am_state *state) exit(128); } - /* - * NEEDSWORK: After the patch has been applied to the index - * with git-apply, we need to make commit as well. - */ + do_commit(state); next: am_next(state); } am_destroy(state); + run_command_v_opt(argv_gc_auto, RUN_GIT_CMD); } /** |