git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[RFC/PATCH 12/18] revert: write TODO and DONE files in case of failure

From
Christian Couder <chriscool@tuxfamily.org>
Date
Nov 25, 2010, 21:20 UTC
Message-ID
<20101125212050.5188.74945.chriscool@tuxfamily.org>
In-Reply-To
<20101125210138.5188.13115.chriscool@tuxfamily.org>
Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin/revert.c                    |   42 ++++++++++++++++++++++++++++++-----
 t/t3508-cherry-pick-many-commits.sh |   22 ++++++++++++++++++
 2 files changed, 58 insertions(+), 6 deletions(-)
diff --git a/builtin/revert.c b/builtin/revert.c
index 7429be2..27e9d6f 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -14,6 +14,7 @@
 #include "rerere.h"
 #include "merge-recursive.h"
 #include "refs.h"
+#include "dir.h"
 
 /*
  * This implements the builtins revert and cherry-pick.
@@ -55,6 +56,11 @@ struct args_info {
 
 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
 
+#define SEQ_DIR		"sequencer"
+#define SEQ_PATH	git_path(SEQ_DIR)
+#define TODO_FILE	git_path(SEQ_DIR "/git-cherry-pick-todo")
+#define DONE_FILE	git_path(SEQ_DIR "/git-cherry-pick-done")
+
 static char *get_encoding(const char *message, const unsigned char *sha1);
 
 static const char * const *revert_or_cherry_pick_usage(struct args_info *info)
@@ -651,7 +657,25 @@ static int ff_incompatible(int val, const char *opt)
 	return val ? error("cherry-pick --ff cannot be used with %s", opt) : 0;
 }
 
-static int pick_commits(struct args_info *infos)
+static int save_todo_and_done(int res, struct args_info *infos,
+			      struct commit *commit,
+			      struct commit_list *todo_list,
+			      struct commit_list **done_list)
+{
+	if (res) {
+		if (!file_exists(SEQ_PATH) && mkdir(SEQ_PATH, 0777))
+			die_errno("Could not create sequencer directory '%s'",
+				  SEQ_PATH);
+		if (commit)
+			commit_list_insert(commit, &todo_list);
+		create_todo_file(TODO_FILE, 0, todo_list, "", infos);
+		*done_list = reverse_commit_list(*done_list);
+		create_todo_file(DONE_FILE, 0, *done_list, "", infos);
+	}
+	return res;
+}
+
+static int pick_commits(struct args_info *infos, struct commit_list **done_list)
 {
 	struct rev_info revs;
 	struct commit *commit;
@@ -662,22 +686,24 @@ static int pick_commits(struct args_info *infos)
 	     (res = ff_incompatible(infos->no_commit, "--no_commit")) ||
 	     (res = ff_incompatible(infos->no_replay, "-x")) ||
 	     (res = ff_incompatible(infos->edit, "--edit"))))
-			return res;
+		return save_todo_and_done(res, infos, NULL, NULL, done_list);
 
 	if ((res = read_and_refresh_cache(me)) ||
 	    (res = prepare_revs(&revs, infos)))
-		return res;
+		return save_todo_and_done(res, infos, NULL, NULL, done_list);
 
 	while ((commit = get_revision(&revs)) &&
 	       !(res = do_pick_commit(infos, commit)))
-		; /* do nothing */
+		commit_list_insert(commit, done_list);
 
-	return res;
+	return save_todo_and_done(res, infos, commit, revs.commits, done_list);
 }
 
 static int revert_or_cherry_pick(int argc, const char **argv, int revert, int edit)
 {
 	struct args_info infos;
+	struct commit_list *done_list = NULL;
+	int res;
 
 	git_config(git_default_config, NULL);
 	me = revert ? "revert" : "cherry-pick";
@@ -686,7 +712,11 @@ static int revert_or_cherry_pick(int argc, const char **argv, int revert, int ed
 	infos.action = revert ? REVERT : CHERRY_PICK;
 	parse_args(argc, argv, &infos);
 
-	return pick_commits(&infos);
+	res = pick_commits(&infos, &done_list);
+
+	free_commit_list(done_list);
+
+	return res;
 }
 
 int cmd_revert(int argc, const char **argv, const char *prefix)
diff --git a/t/t3508-cherry-pick-many-commits.sh b/t/t3508-cherry-pick-many-commits.sh
index 8e09fd0..9213d59 100755
--- a/t/t3508-cherry-pick-many-commits.sh
+++ b/t/t3508-cherry-pick-many-commits.sh
@@ -156,4 +156,26 @@ test_expect_success 'cherry-pick --stdin works' '
 	check_head_differs_from fourth
 '
 
+test_expect_success 'create some files to test --continue' '
+	TODO_FILE="$(git rev-parse --git-dir)/sequencer/git-cherry-pick-todo" &&
+	DONE_FILE="$(git rev-parse --git-dir)/sequencer/git-cherry-pick-done" &&
+
+	cat <<-EOF >expected_todo &&
+	pick  $(git rev-parse --short fourth) # fourth
+	EOF
+
+	cat <<-EOF >expected_done
+	pick  $(git rev-parse --short second) # second
+	EOF
+'
+
+test_expect_success 'failed cherry-pick produces todo and done files' '
+	git checkout -f master &&
+	git reset --hard first &&
+	test_tick &&
+	test_must_fail git cherry-pick fourth~2 fourth &&
+	test_cmp expected_todo "$TODO_FILE" &&
+	test_cmp expected_done "$DONE_FILE"
+'
+
 test_done
-- 
1.7.3.2.504.g59d466
Previous: Christian CouderNext: Christian Couder
Message 20 of 27 in “WIP implement cherry-pick/revert --continue”
  1. 00/18 WIP implement cherry-pick/revert --continueChristian Couder, Nov 25, 2010
  2. 01/18 advice: add error_resolve_conflict() functionChristian Couder, Nov 25, 2010
  3. Jonathan NiederNov 26, 2010
  4. 02/18 revert: change many die() calls into "return error()" callsChristian Couder, Nov 25, 2010
  5. Jonathan NiederNov 26, 2010
  6. 03/18 usage: implement error_errno() the same way as die_errno()Christian Couder, Nov 25, 2010
  7. Jonathan NiederNov 26, 2010
  8. Junio C HamanoNov 26, 2010
  9. 04/18 revert: don't die when write_message() failsChristian Couder, Nov 25, 2010
  10. 05/18 commit: move reverse_commit_list() into commit.{h, c}Christian Couder, Nov 25, 2010
  11. 06/18 revert: remove "commit" global variableChristian Couder, Nov 25, 2010
  12. 07/18 revert: put option information in an option structChristian Couder, Nov 25, 2010
  13. Jonathan NiederNov 26, 2010
  14. Junio C HamanoNov 26, 2010
  15. 08/18 revert: refactor code into a new pick_commits() functionChristian Couder, Nov 25, 2010
  16. Daniel BarkalowNov 27, 2010
  17. 09/18 revert: make pick_commits() return an error on --ff incompatible optionChristian Couder, Nov 25, 2010
  18. 10/18 revert: make read_and_refresh_cache() and prepare_revs() return errorsChristian Couder, Nov 25, 2010
  19. 11/18 revert: add get_todo_content() and create_todo_file()Christian Couder, Nov 25, 2010
  20. 12/18 revert: write TODO and DONE files in case of failureChristian Couder, Nov 25, 2010
  21. 13/18 revert: add option parsing for option --continueChristian Couder, Nov 25, 2010
  22. 14/18 revert: move global variable "me" into "struct args_info"Christian Couder, Nov 25, 2010
  23. 15/18 revert: add NONE action and make parse_args() manage itChristian Couder, Nov 25, 2010
  24. 16/18 revert: implement parsing TODO and DONE filesChristian Couder, Nov 25, 2010
  25. 17/18 revert: add remaining instructions in todo fileChristian Couder, Nov 25, 2010
  26. 18/18 revert: implement --continue processingChristian Couder, Nov 25, 2010
  27. Jonathan NiederNov 26, 2010

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.