A cherry-pick/ revert operation consists of several smaller steps. Later in the series, we would like to be able to resume a failed operation. As a prelude, we first need to persist the current state of operation. Introduce a "head" file to make note of the HEAD when the operation stated (so that the operation can be aborted), a "todo" file to keep the list of the steps to be performed, and a "done" file to keep a list of steps that have completed successfully. The format of these files is similar to the one used by the "rebase -i" process. Helped-by: Christian Couder <chriscool@xxxxxxxxxxxxx> Signed-off-by: Ramkumar Ramachandra <artagnon@xxxxxxxxx> --- Note how I've used the words "operation" and "step" to differentiate between a picking/ reverting a single commit versus the entire operation in the commit message. In one discussion involving Jonathan and Daniel, it was discussed that multiple meta-picking/ reverting levels is a goal that is tangential to this project. I only intend to support two levels: the overall "operation", and the composite "steps". builtin/revert.c | 117 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 files changed, 111 insertions(+), 6 deletions(-) diff --git a/builtin/revert.c b/builtin/revert.c index 0fe87e8..13569c2 100644 --- a/builtin/revert.c +++ b/builtin/revert.c @@ -13,6 +13,7 @@ #include "rerere.h" #include "merge-recursive.h" #include "refs.h" +#include "dir.h" /* * This implements the builtins revert and cherry-pick. @@ -25,6 +26,13 @@ * Copyright (c) 2005 Junio C Hamano */ +#define SEQ_DIR "sequencer" + +#define SEQ_PATH git_path(SEQ_DIR) +#define HEAD_FILE git_path(SEQ_DIR "/head") +#define TODO_FILE git_path(SEQ_DIR "/todo") +#define DONE_FILE git_path(SEQ_DIR "/done") + static const char * const revert_usage[] = { "git revert [options] <commit-ish>", NULL @@ -629,21 +637,118 @@ static int read_and_refresh_cache(struct replay_opts *opts) return 0; } +static int format_todo(struct strbuf *buf, struct commit_list *list, + struct replay_opts *opts) +{ + struct commit_list *cur = NULL; + struct commit_message msg = { NULL, NULL, NULL, NULL, NULL }; + const char *sha1 = NULL; + const char *action; + + action = (opts->action == REVERT ? "revert" : "pick"); + for (cur = list; cur; cur = cur->next) { + sha1 = find_unique_abbrev(cur->item->object.sha1, DEFAULT_ABBREV); + if (get_message(cur->item, cur->item->buffer, &msg)) + return error(_("Cannot get commit message for %s"), sha1); + strbuf_addf(buf, "%s %s %s\n", action, sha1, msg.subject); + } + return 0; +} + +static int persist_initialize(unsigned char *head) +{ + struct strbuf buf = STRBUF_INIT; + int fd; + + if (!file_exists(SEQ_PATH) && mkdir(SEQ_PATH, 0777)) { + int err = errno; + strbuf_release(&buf); + error(_("Could not create sequencer directory '%s': %s"), + SEQ_PATH, strerror(err)); + return -err; + } + + if ((fd = open(HEAD_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { + int err = errno; + strbuf_release(&buf); + error(_("Could not open '%s' for writing: %s"), + HEAD_FILE, strerror(err)); + return -err; + } + + strbuf_addf(&buf, "%s", find_unique_abbrev(head, DEFAULT_ABBREV)); + write_or_whine(fd, buf.buf, buf.len, HEAD_FILE); + close(fd); + strbuf_release(&buf); + return 0; +} + +static int persist_todo_done(int res, struct commit_list *todo_list, + struct commit_list *done_list, struct replay_opts *opts) +{ + struct strbuf buf = STRBUF_INIT; + int fd, res2; + + if (!res) + return 0; + + /* TODO file */ + if ((fd = open(TODO_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { + int err = errno; + strbuf_release(&buf); + error(_("Could not open '%s' for writing: %s"), + TODO_FILE, strerror(err)); + return -err; + } + + if ((res2 = format_todo(&buf, todo_list, opts))) + return res2; + write_or_whine(fd, buf.buf, buf.len, TODO_FILE); + close(fd); + + /* DONE file */ + strbuf_reset(&buf); + if ((fd = open(DONE_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0) { + int err = errno; + strbuf_release(&buf); + error(_("Could not open '%s' for writing: %s"), + DONE_FILE, strerror(err)); + return -err; + } + + if ((res2 = format_todo(&buf, done_list, opts))) + return res2; + write_or_whine(fd, buf.buf, buf.len, DONE_FILE); + close(fd); + strbuf_release(&buf); + return res; +} + static int pick_commits(struct replay_opts *opts) { + struct commit_list *done_list = NULL; struct rev_info revs; struct commit *commit; + unsigned char head[20]; int res; + if (get_sha1("HEAD", head)) + return error(_("You do not have a valid HEAD")); + if ((res = read_and_refresh_cache(opts)) || - (res = prepare_revs(&revs, opts))) + (res = prepare_revs(&revs, opts)) || + (res = persist_initialize(head))) return res; - while ((commit = get_revision(&revs)) && - !(res = do_pick_commit(commit, opts))) - ; - - return res; + while ((commit = get_revision(&revs))) { + if (!(res = do_pick_commit(commit, opts))) + commit_list_insert(commit, &done_list); + else { + commit_list_insert(commit, &revs.commits); + break; + } + } + return persist_todo_done(res, revs.commits, done_list, opts); } int cmd_revert(int argc, const char **argv, const char *prefix) -- 1.7.5.GIT -- 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