From: Paul Tan <pyokagan@gmail.com>
To: git@vger.kernel.org
Cc: Stefan Beller <sbeller@google.com>,
Johannes Schindelin <johannes.schindelin@gmx.de>,
Paul Tan <pyokagan@gmail.com>
Subject: [PATCH/WIP 3/8] am: implement patch queue mechanism
Date: Wed, 27 May 2015 21:33:33 +0800 [thread overview]
Message-ID: <1432733618-25629-4-git-send-email-pyokagan@gmail.com> (raw)
In-Reply-To: <1432733618-25629-1-git-send-email-pyokagan@gmail.com>
git-am applies a series of patches. If the process terminates
abnormally, we want to be able to resume applying the series of patches.
This requires the session state to be saved in a persistent location.
Implement the mechanism of a "patch queue", represented by 2 integers --
the index of the current patch we are applying and the index of the last
patch, as well as its lifecycle through the following functions:
* am_setup(), which will set up the state directory
$GIT_DIR/rebase-apply. As such, even if the process exits abnormally,
the last-known state will still persist.
* am_state_load(), which is called if there is an am session in
progress, to load the last known state from the state directory so we
can resume applying patches.
* am_run(), which will do the actual patch application. After applying a
patch, it calls am_next() to increment the current patch index. The
logic for applying and committing a patch is not implemented yet.
* am_destroy(), which is finally called when we successfully applied all
the patches in the queue, to clean up by removing the state directory
and its contents.
Signed-off-by: Paul Tan <pyokagan@gmail.com>
---
Makefile | 2 +-
builtin.h | 1 +
builtin/am.c | 167 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
git.c | 1 +
4 files changed, 170 insertions(+), 1 deletion(-)
create mode 100644 builtin/am.c
diff --git a/Makefile b/Makefile
index 323c401..57a7c8c 100644
--- a/Makefile
+++ b/Makefile
@@ -466,7 +466,6 @@ TEST_PROGRAMS_NEED_X =
# interactive shell sessions without exporting it.
unexport CDPATH
-SCRIPT_SH += git-am.sh
SCRIPT_SH += git-bisect.sh
SCRIPT_SH += git-difftool--helper.sh
SCRIPT_SH += git-filter-branch.sh
@@ -812,6 +811,7 @@ LIB_OBJS += xdiff-interface.o
LIB_OBJS += zlib.o
BUILTIN_OBJS += builtin/add.o
+BUILTIN_OBJS += builtin/am.o
BUILTIN_OBJS += builtin/annotate.o
BUILTIN_OBJS += builtin/apply.o
BUILTIN_OBJS += builtin/archive.o
diff --git a/builtin.h b/builtin.h
index b87df70..d50c9d1 100644
--- a/builtin.h
+++ b/builtin.h
@@ -30,6 +30,7 @@ extern int textconv_object(const char *path, unsigned mode, const unsigned char
extern int is_builtin(const char *s);
extern int cmd_add(int argc, const char **argv, const char *prefix);
+extern int cmd_am(int argc, const char **argv, const char *prefix);
extern int cmd_annotate(int argc, const char **argv, const char *prefix);
extern int cmd_apply(int argc, const char **argv, const char *prefix);
extern int cmd_archive(int argc, const char **argv, const char *prefix);
diff --git a/builtin/am.c b/builtin/am.c
new file mode 100644
index 0000000..6c00009
--- /dev/null
+++ b/builtin/am.c
@@ -0,0 +1,167 @@
+/*
+ * Builtin "git am"
+ *
+ * Based on git-am.sh by Junio C Hamano.
+ */
+#include "cache.h"
+#include "parse-options.h"
+#include "dir.h"
+
+struct am_state {
+ struct strbuf dir; /* state directory path */
+ int cur; /* current patch number */
+ int last; /* last patch number */
+};
+
+/**
+ * Initializes am_state with the default values.
+ */
+static void am_state_init(struct am_state *state)
+{
+ memset(state, 0, sizeof(*state));
+
+ strbuf_init(&state->dir, 0);
+}
+
+/**
+ * Release memory allocated by an am_state.
+ */
+static void am_state_release(struct am_state *state)
+{
+ strbuf_release(&state->dir);
+}
+
+/**
+ * Returns path relative to the am_state directory.
+ */
+static inline const char *am_path(const struct am_state *state, const char *path)
+{
+ return mkpath("%s/%s", state->dir.buf, path);
+}
+
+/**
+ * Returns 1 if there is an am session in progress, 0 otherwise.
+ */
+static int am_in_progress(const struct am_state *state)
+{
+ struct stat st;
+
+ if (lstat(state->dir.buf, &st) < 0 || !S_ISDIR(st.st_mode))
+ return 0;
+ if (lstat(am_path(state, "last"), &st) || !S_ISREG(st.st_mode))
+ return 0;
+ if (lstat(am_path(state, "next"), &st) || !S_ISREG(st.st_mode))
+ return 0;
+ return 1;
+}
+
+/**
+ * Reads the contents of `file`. The third argument can be used to give a hint
+ * about the file size, to avoid reallocs. Returns 0 on success, -1 if the file
+ * does not exist.
+ */
+static int read_state_file(struct strbuf *sb, const char *file, size_t hint) {
+ strbuf_reset(sb);
+
+ if (!strbuf_read_file(sb, file, hint))
+ return 0;
+
+ if (errno == ENOENT)
+ return -1;
+
+ die_errno(_("could not read '%s'"), file);
+}
+
+/**
+ * Loads state from disk.
+ */
+static void am_state_load(struct am_state *state)
+{
+ struct strbuf sb = STRBUF_INIT;
+
+ read_state_file(&sb, am_path(state, "next"), 8);
+ state->cur = strtol(sb.buf, NULL, 10);
+
+ read_state_file(&sb, am_path(state, "last"), 8);
+ state->last = strtol(sb.buf, NULL, 10);
+
+ strbuf_release(&sb);
+}
+
+/**
+ * Remove the am_state directory.
+ */
+static void am_destroy(const struct am_state *state)
+{
+ struct strbuf sb = STRBUF_INIT;
+
+ strbuf_addstr(&sb, state->dir.buf);
+ remove_dir_recursively(&sb, 0);
+ strbuf_release(&sb);
+}
+
+/**
+ * Setup a new am session for applying patches
+ */
+static void am_setup(struct am_state *state)
+{
+ if (mkdir(state->dir.buf, 0777) < 0 && errno != EEXIST)
+ die_errno(_("failed to create directory '%s'"), state->dir.buf);
+
+ write_file(am_path(state, "next"), 1, "%d", state->cur);
+
+ write_file(am_path(state, "last"), 1, "%d", state->last);
+}
+
+/**
+ * Increments the patch pointer, and cleans am_state for the application of the
+ * next patch.
+ */
+static void am_next(struct am_state *state)
+{
+ state->cur++;
+ write_file(am_path(state, "next"), 1, "%d", state->cur);
+}
+
+/**
+ * Applies all queued patches.
+ */
+static void am_run(struct am_state *state)
+{
+ while (state->cur <= state->last)
+ am_next(state);
+
+ am_destroy(state);
+}
+
+struct am_state state;
+
+static const char * const am_usage[] = {
+ N_("git am [options] [(<mbox>|<Maildir>)...]"),
+ NULL
+};
+
+static struct option am_options[] = {
+ OPT_END()
+};
+
+int cmd_am(int argc, const char **argv, const char *prefix)
+{
+ git_config(git_default_config, NULL);
+
+ am_state_init(&state);
+ strbuf_addstr(&state.dir, git_path("rebase-apply"));
+
+ argc = parse_options(argc, argv, prefix, am_options, am_usage, 0);
+
+ if (am_in_progress(&state))
+ am_state_load(&state);
+ else
+ am_setup(&state);
+
+ am_run(&state);
+
+ am_state_release(&state);
+
+ return 0;
+}
diff --git a/git.c b/git.c
index 44374b1..42328ed 100644
--- a/git.c
+++ b/git.c
@@ -370,6 +370,7 @@ static int run_builtin(struct cmd_struct *p, int argc, const char **argv)
static struct cmd_struct commands[] = {
{ "add", cmd_add, RUN_SETUP | NEED_WORK_TREE },
+ { "am", cmd_am, RUN_SETUP | NEED_WORK_TREE },
{ "annotate", cmd_annotate, RUN_SETUP },
{ "apply", cmd_apply, RUN_SETUP_GENTLY },
{ "archive", cmd_archive },
--
2.1.4
next prev parent reply other threads:[~2015-05-27 13:34 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-27 13:33 [PATCH/WIP 0/8] Make git-am a builtin Paul Tan
2015-05-27 13:33 ` [PATCH/WIP 1/8] wrapper: implement xopen() Paul Tan
2015-05-27 17:52 ` Stefan Beller
2015-05-27 19:03 ` Torsten Bögershausen
2015-05-27 21:53 ` Jeff King
2015-06-03 8:16 ` Paul Tan
2015-06-04 12:05 ` Paul Tan
2015-05-27 13:33 ` [PATCH/WIP 2/8] wrapper: implement xfopen() Paul Tan
2015-05-27 21:55 ` Jeff King
2015-05-27 13:33 ` Paul Tan [this message]
2015-05-27 20:38 ` [PATCH/WIP 3/8] am: implement patch queue mechanism Junio C Hamano
2015-05-27 13:33 ` [PATCH/WIP 4/8] am: split out mbox/maildir patches with git-mailsplit Paul Tan
2015-05-28 23:05 ` Eric Sunshine
2015-06-02 14:27 ` Paul Tan
2015-05-27 13:33 ` [PATCH/WIP 5/8] am: detect mbox patches Paul Tan
2015-05-31 17:16 ` Eric Sunshine
2015-05-27 13:33 ` [PATCH/WIP 6/8] am: extract patch, message and authorship with git-mailinfo Paul Tan
2015-05-27 20:44 ` Junio C Hamano
2015-05-27 22:13 ` Jeff King
2015-06-03 7:56 ` Paul Tan
2015-05-27 22:13 ` Junio C Hamano
2015-06-03 7:57 ` Paul Tan
2015-05-27 13:33 ` [PATCH/WIP 7/8] am: apply patch with git-apply Paul Tan
2015-05-27 13:33 ` [PATCH/WIP 8/8] am: commit applied patch Paul Tan
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1432733618-25629-4-git-send-email-pyokagan@gmail.com \
--to=pyokagan@gmail.com \
--cc=git@vger.kernel.org \
--cc=johannes.schindelin@gmx.de \
--cc=sbeller@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.