On Thu, Jun 18, 2015 at 4:25 AM, Paul Tan <pyokagan@xxxxxxxxx> wrote: > +/** > + * Reads the contents of `file`. The third argument can be used to give a hint I would avoid `third` here. (I needed to count twice to be sure which argument you were referring to, as I was confused.) Also how do you abstain from giving a hint? (0 or negative or MAX_INT?) So maybe /** * Reads the contents of `file`. Returns number of bytes read on success, * -1 if the file does not exist. If trim is set, trailing whitespace will be removed * from the file contents. If `hint` is non-zero, it is used as a hint for initial * allocation to avoid reallocs. */ > + * about the file size, to avoid reallocs. Returns number of bytes read on > + * success, -1 if the file does not exist. If trim is set, trailing whitespace > + * will be removed from the file contents. > + */ > +static int read_state_file(struct strbuf *sb, const char *file, size_t hint, int trim) > +{ > + strbuf_reset(sb); > + if (strbuf_read_file(sb, file, hint) >= 0) { > + if (trim) > + strbuf_trim(sb); > + > + return sb->len; > + } > + > + if (errno == ENOENT) > + return -1; > + > + die_errno(_("could not read '%s'"), file); > +} > + > +/** > + * Loads state from disk. > + */ > +static void am_load(struct am_state *state) > +{ > + struct strbuf sb = STRBUF_INIT; > + > + read_state_file(&sb, am_path(state, "next"), 8, 1); > + state->cur = strtol(sb.buf, NULL, 10); > + > + read_state_file(&sb, am_path(state, "last"), 8, 1); > + 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) { > + > + /* TODO: Patch application not implemented yet */ > + > + am_next(state); > + } > + > + am_destroy(state); > +} > + > +static 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) > { > @@ -24,5 +176,21 @@ int cmd_am(int argc, const char **argv, const char *prefix) > setup_work_tree(); > } > > + 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_load(&state); > + else > + am_setup(&state); > + > + am_run(&state); > + > + am_state_release(&state); > + > return 0; > } > -- > 2.1.4 > -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html