[PATCH 09/11] revert: Write head, todo, done files
Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
builtin/revert.c | 94 ++++++++++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 88 insertions(+), 6 deletions(-)
diff --git a/builtin/revert.c b/builtin/revert.c
index e0352d4..25969a5 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
@@ -643,21 +651,95 @@ static int read_and_refresh_cache(void)
return 0;
}
+static void format_todo(struct strbuf *buf, struct commit_list *list)
+{
+ struct commit_list *cur = NULL;
+ struct commit_message msg = { NULL, NULL, NULL, NULL, NULL };
+ const char *sha1 = NULL;
+ const char *action;
+
+ action = (cmd_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))
+ die("Cannot get commit message for %s", sha1);
+
+ strbuf_addf(buf, "%s %s %s\n", action, sha1, msg.subject);
+ }
+}
+
+static int persist_initialize(unsigned char *head)
+{
+ struct strbuf buf = STRBUF_INIT;
+ int fd;
+
+ if (!file_exists(SEQ_PATH) && mkdir(SEQ_PATH, 0777))
+ return error_errno(_("Could not create sequencer directory '%s'"), SEQ_PATH);
+
+ if ((fd = open(HEAD_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
+ return error_errno(_("Could not open '%s' for writing"), HEAD_FILE);
+
+ 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 strbuf buf = STRBUF_INIT;
+ int fd;
+
+ if (!res)
+ return 0;
+
+ /* TODO file */
+ if ((fd = open(TODO_FILE, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
+ return error_errno(_("Could not open '%s' for writing"), TODO_FILE);
+
+ format_todo(&buf, todo_list);
+ 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)
+ return error_errno(_("Could not open '%s' for writing"), DONE_FILE);
+
+ format_todo(&buf, done_list);
+ write_or_whine(fd, buf.buf, buf.len, DONE_FILE);
+ close(fd);
+ strbuf_release(&buf);
+ return res;
+}
+
static int pick_commits(void)
{
+ 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()) ||
- (res = prepare_revs(&revs)))
+ (res = prepare_revs(&revs)) ||
+ (res = persist_initialize(head)))
return res;
- while ((commit = get_revision(&revs)) &&
- !(res = do_pick_commit(commit)))
- ;
-
- return res;
+ while ((commit = get_revision(&revs))) {
+ if (!(res = do_pick_commit(commit)))
+ commit_list_insert(commit, &done_list);
+ else {
+ commit_list_insert(commit, &revs.commits);
+ break;
+ }
+ }
+ return persist_todo_done(res, revs.commits, done_list);
}
int cmd_revert(int argc, const char **argv, const char *prefix)
--
1.7.4.rc1.7.g2cf08.dirty