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

[PATCH 12/17] revert: Save command-line options for continuing operation

From
Ramkumar Ramachandra <artagnon@gmail.com>
Date
Jul 11, 2011, 14:54 UTC
Message-ID
<1310396048-24925-13-git-send-email-artagnon@gmail.com>
In-Reply-To
<1310396048-24925-1-git-send-email-artagnon@gmail.com>

In the same spirit as ".git/sequencer/head" and ".git/sequencer/todo", introduce ".git/sequencer/opts" to persist the replay_opts structure for continuing after a conflict resolution. Use a simple "key = value" format for this file, using "|" to separate multiple values so that the file looks like:

signoff = true record-origin = true mainline = 1 strategy = recursive strategy-option = patience | ours

Write the file just after writing ".git/sequencer/head", and parse it out just after parsing out ".git/sequencer/head". There is now enough data for "--continue" to work.

Signed-off-by: Ramkumar Ramachandra <artagnon@gmail.com>
---
 builtin/revert.c                |  226 +++++++++++++++++++++++++++++++++++++++
 sequencer.h                     |    1 +
 t/t3510-cherry-pick-sequence.sh |   20 ++++-
 3 files changed, 246 insertions(+), 1 deletions(-)
diff --git a/builtin/revert.c b/builtin/revert.c
index 856ee97..edf74fb 100644
--- a/builtin/revert.c
+++ b/builtin/revert.c
@@ -596,6 +596,32 @@ struct commit_list **commit_list_append(struct commit *commit,
 	return &new->next;
 }
 
+static void format_opts(struct strbuf *buf, struct replay_opts *opts)
+{
+	int i;
+
+	if (opts->no_commit)
+		strbuf_addstr(buf, "no-commit = true\n");
+	if (opts->edit)
+		strbuf_addstr(buf, "edit = true\n");
+	if (opts->signoff)
+		strbuf_addstr(buf, "signoff = true\n");
+	if (opts->record_origin)
+		strbuf_addstr(buf, "record-origin = true\n");
+	if (opts->allow_ff)
+		strbuf_addstr(buf, "ff = true\n");
+	if (opts->mainline)
+		strbuf_addf(buf, "mainline = %d\n", opts->mainline);
+	if (opts->strategy)
+		strbuf_addf(buf, "strategy = %s\n", opts->strategy);
+	if (opts->xopts) {
+		strbuf_addf(buf, "strategy-option = ");
+		for (i = 0; i < opts->xopts_nr - 1; i ++)
+			strbuf_addf(buf, "%s | ", opts->xopts[i]);
+		strbuf_addf(buf, "%s\n", opts->xopts[i]);
+	}
+}
+
 static void format_todo(struct strbuf *buf, struct commit_list *todo_list,
 			struct replay_opts *opts)
 {
@@ -694,6 +720,184 @@ error:
 	die(_("Malformed instruction sheet: %s"), git_path(SEQ_TODO_FILE));
 }
 
+static struct strbuf *parse_value(const char *start, char **end_ptr)
+{
+	static struct strbuf value = STRBUF_INIT;
+	int quote = 0;
+	char *p = (char *)start;
+	char *end;
+
+	/* Find and strip '\n', '\r' */
+	if ((end = strchr(start, '\n'))) {
+		*end = '\0'; /* Remove trailing '\n' */
+		if (*(end - 1) == '\r')
+			*(end - 1) = '\0'; /* Remove trailing '\r' */
+	} else if (strchr(start, '\0'))
+		end = NULL;
+	else
+		goto error;
+
+	for (; *p != '\0'; p ++) {
+		if (!quote && (*p == ';' || *p == '#'))
+			/* Ignore comments */
+			goto ok;
+		if (*p == '\\') {
+			p += 1;
+			switch (*p) {
+			case '\n':
+				continue;
+			case 't':
+				strbuf_addch(&value, '\t');
+				break;
+			case 'b':
+				strbuf_addch(&value, '\b');
+				break;
+			case 'n':
+				strbuf_addch(&value, '\n');
+				break;
+			/* Some pharapters escape as themselves */
+			case '\\': case '"':
+				break;
+			/* Reject unknown escape sequences */
+			default:
+				goto error;
+			}
+			continue;
+		}
+		if (*p == '"') {
+			quote = 1 - quote;
+			continue;
+		}
+		strbuf_addch(&value, *p);
+	}
+	/* Quote not closed */
+	if (quote)
+		goto error;
+
+	if (end)
+		*end_ptr = end + 1;
+	else
+		*end_ptr = NULL;
+ok:
+	strbuf_trim(&value);
+	return &value;
+error:
+	strbuf_release(&value);
+	return NULL;
+}
+
+static char *parse_opt_value(const char *start, void *key,
+			enum parse_opt_type type, parse_opt_cb *cb_function)
+{
+	struct strbuf *value, *subvalue;
+	struct option opt;
+	char *p, *cur, *val, *end;
+
+	/* Remove spaces before '=' */
+	for (p = (char *)start; isspace(*p); p++);
+	if (*p != '=')
+		goto error;
+
+	if (!(value = parse_value(p + 1, &end)))
+		goto error;
+	val = value->buf;
+
+	switch (type) {
+	case OPTION_BOOLEAN:
+		if (!strcasecmp(val, "true")
+			|| !strcasecmp(val, "yes")
+			|| !strcasecmp(val, "on"))
+			*(int *)key = 1;
+		else if (!strcasecmp(val, "false")
+			|| !strcasecmp(val, "no")
+			|| !strcasecmp(val, "off"))
+			*(int *)key = 0;
+		else
+			goto error;
+		break;
+	case OPTION_INTEGER:
+		*(int *)key = strtol(val, NULL, 10);
+		break;
+	case OPTION_STRING:
+		*(char **)key = xstrdup(val);
+		break;
+	case OPTION_CALLBACK:
+		opt.value = (struct replay_opts **)key;
+		while (val) {
+			if ((cur = strchr(val, '|'))) {
+				*cur = '\0';
+				subvalue = parse_value(val, &end);
+				(*cb_function)(&opt, subvalue->buf, 0);
+				strbuf_release(subvalue);
+				val = cur + 1;
+			} else {
+				(*cb_function)(&opt, val, 0);
+				break;
+			}
+		}
+		break;
+	default:
+		die(_("program error"));
+	}
+	strbuf_release(value);
+	return end;
+error:
+	die(_("Malformed options sheet: %s"), git_path(SEQ_OPTS_FILE));
+}
+
+static void read_populate_opts(struct replay_opts **opts_ptr)
+{
+	struct replay_opts *opts = *opts_ptr;
+	struct strbuf buf = STRBUF_INIT;
+	char *p;
+	int fd;
+
+	fd = open(git_path(SEQ_OPTS_FILE), O_RDONLY);
+	if (fd < 0)
+		goto ok;
+	if (strbuf_read(&buf, fd, 0) < buf.len) {
+		close(fd);
+		strbuf_release(&buf);
+		die(_("Could not read %s."), git_path(SEQ_OPTS_FILE));
+	}
+	close(fd);
+
+	for (p = buf.buf; p && *p != '\0';) {
+		if (!prefixcmp(p, "no-commit"))
+			p = parse_opt_value(p + strlen("no-commit"),
+					&opts->no_commit, OPTION_BOOLEAN, NULL);
+		else if (!prefixcmp(p, "edit"))
+			p = parse_opt_value(p + strlen("edit"),
+					&opts->edit, OPTION_BOOLEAN, NULL);
+		else if (!prefixcmp(p, "signoff"))
+			p = parse_opt_value(p + strlen("signoff"),
+					&opts->signoff, OPTION_BOOLEAN, NULL);
+		else if (!prefixcmp(p, "mainline"))
+			p = parse_opt_value(p + strlen("mainline"),
+					&opts->mainline, OPTION_INTEGER, NULL);
+		else if (!prefixcmp(p, "record-origin"))
+			p = parse_opt_value(p + strlen("record-origin"),
+					&opts->record_origin, OPTION_BOOLEAN, NULL);
+		else if (!prefixcmp(p, "ff"))
+			p = parse_opt_value(p + strlen("ff"),
+					&opts->allow_ff, OPTION_BOOLEAN, NULL);
+		else if (!prefixcmp(p, "strategy-option"))
+			p = parse_opt_value(p + strlen("strategy-option"),
+					&opts, OPTION_CALLBACK, option_parse_x);
+		else if (!prefixcmp(p, "strategy"))
+			p = parse_opt_value(p + strlen("strategy"),
+					&opts->strategy, OPTION_STRING, NULL);
+		else
+			goto error;
+	}
+ok:
+	strbuf_release(&buf);
+	return;
+error:
+	strbuf_release(&buf);
+	die(_("Malformed options sheet: %s"), git_path(SEQ_OPTS_FILE));
+}
+
 static void walk_revs_populate_todo(struct commit_list **todo_list,
 				struct replay_opts *opts)
 {
@@ -751,6 +955,27 @@ static void save_todo(struct commit_list *todo_list, struct replay_opts *opts)
 	strbuf_release(&buf);
 }
 
+static void save_opts(struct replay_opts *opts)
+{
+	static struct lock_file opts_lock;
+	struct strbuf buf = STRBUF_INIT;
+	int fd;
+
+	fd = hold_lock_file_for_update(&opts_lock, git_path(SEQ_OPTS_FILE), LOCK_DIE_ON_ERROR);
+	format_opts(&buf, opts);
+	if (!buf.len)
+		return;
+	if (write_in_full(fd, buf.buf, buf.len) < 0) {
+		strbuf_release(&buf);
+		die_errno(_("Could not write to %s."), git_path(SEQ_OPTS_FILE));
+	}
+	if (commit_lock_file(&opts_lock) < 0) {
+		strbuf_release(&buf);
+		die(_("Error wrapping up %s"), git_path(SEQ_OPTS_FILE));
+	}
+	strbuf_release(&buf);
+}
+
 static int pick_commits(struct replay_opts *opts)
 {
 	struct commit_list *todo_list = NULL;
@@ -768,6 +993,7 @@ static int pick_commits(struct replay_opts *opts)
 	create_seq_dir();
 	if (!get_sha1("HEAD", sha1))
 		save_head(sha1_to_hex(sha1));
+	save_opts(opts);
 	save_todo(todo_list, opts);
 
 	for (cur = todo_list; cur; cur = cur->next) {
diff --git a/sequencer.h b/sequencer.h
index 673616b..dcb51b1 100644
--- a/sequencer.h
+++ b/sequencer.h
@@ -5,6 +5,7 @@
 #define SEQ_OLD_DIR	"sequencer-old"
 #define SEQ_HEAD_FILE	"sequencer/head"
 #define SEQ_TODO_FILE	"sequencer/todo"
+#define SEQ_OPTS_FILE	"sequencer/opts"
 
 /* Removes SEQ_OLD_DIR and renames SEQ_DIR to SEQ_OLD_DIR, ignoring
  * any errors.  Intended to be used by 'git reset --hard'.
diff --git a/t/t3510-cherry-pick-sequence.sh b/t/t3510-cherry-pick-sequence.sh
index 64eaa20..100cbb1 100755
--- a/t/t3510-cherry-pick-sequence.sh
+++ b/t/t3510-cherry-pick-sequence.sh
@@ -32,10 +32,28 @@ test_expect_success setup '
 
 test_expect_success 'cherry-pick persists data on failure' '
 	pristine_detach initial &&
-	test_must_fail git cherry-pick base..anotherpick &&
+	test_must_fail git cherry-pick -s base..anotherpick &&
 	test_path_is_dir .git/sequencer &&
 	test_path_is_file .git/sequencer/head &&
 	test_path_is_file .git/sequencer/todo &&
+	test_path_is_file .git/sequencer/opts &&
+	rm -rf .git/sequencer
+'
+
+test_expect_success 'cherry-pick persists opts correctly' '
+	pristine_detach initial &&
+	test_must_fail git cherry-pick -s -m 1 --strategy=recursive -X patience -X ours base..anotherpick &&
+	test_path_is_dir .git/sequencer &&
+	test_path_is_file .git/sequencer/head &&
+	test_path_is_file .git/sequencer/todo &&
+	test_path_is_file .git/sequencer/opts &&
+	cat >expect <<-\EOF &&
+	signoff = true
+	mainline = 1
+	strategy = recursive
+	strategy-option = patience | ours
+	EOF
+	test_cmp expect .git/sequencer/opts &&
 	rm -rf .git/sequencer
 '
 
-- 
1.7.5.GIT
Previous: Ramkumar RamachandraNext: Junio C Hamano
Message 60 of 99 in “[GSoC update] Sequencer for inclusion”
  1. Ramkumar RamachandraJul 11, 2011
  2. 01/17 advice: Introduce error_resolve_conflictRamkumar Ramachandra, Jul 11, 2011
  3. 02/17 revert: Inline add_message_to_msg functionRamkumar Ramachandra, Jul 11, 2011
  4. Jonathan NiederJul 12, 2011
  5. Ramkumar RamachandraJul 13, 2011
  6. Jonathan NiederJul 13, 2011
  7. Ramkumar RamachandraJul 19, 2011
  8. Junio C HamanoJul 19, 2011
  9. Ramkumar RamachandraJul 19, 2011
  10. Jonathan NiederJul 19, 2011
  11. Ramkumar RamachandraJul 20, 2011
  12. 03/17 revert: Don't check lone argument in get_encodingRamkumar Ramachandra, Jul 11, 2011
  13. Jonathan NiederJul 12, 2011
  14. Ramkumar RamachandraJul 13, 2011
  15. Jonathan NiederJul 13, 2011
  16. 04/17 revert: Rename no_replay to record_originRamkumar Ramachandra, Jul 11, 2011
  17. Jonathan NiederJul 12, 2011
  18. Ramkumar RamachandraJul 13, 2011
  19. Jonathan NiederJul 17, 2011
  20. 05/17 revert: Propogate errors upwards from do_pick_commitRamkumar Ramachandra, Jul 11, 2011
  21. Jonathan NiederJul 12, 2011
  22. Ramkumar RamachandraJul 17, 2011
  23. Jonathan NiederJul 17, 2011
  24. Ramkumar RamachandraJul 19, 2011
  25. 06/17 revert: Eliminate global "commit" variableRamkumar Ramachandra, Jul 11, 2011
  26. Jonathan NiederJul 12, 2011
  27. Ramkumar RamachandraJul 13, 2011
  28. Jonathan NiederJul 13, 2011
  29. Ramkumar RamachandraJul 13, 2011
  30. Jonathan NiederJul 13, 2011
  31. 07/17 revert: Introduce struct to keep command-line optionsRamkumar Ramachandra, Jul 11, 2011
  32. Jonathan NiederJul 12, 2011
  33. Ramkumar RamachandraJul 13, 2011
  34. 08/17 revert: Separate cmdline parsing from functional codeRamkumar Ramachandra, Jul 11, 2011
  35. Jonathan NiederJul 12, 2011
  36. Ramkumar RamachandraJul 18, 2011
  37. Jonathan NiederJul 18, 2011
  38. 09/17 revert: Don't create invalid replay_opts in parse_argsRamkumar Ramachandra, Jul 11, 2011
  39. Junio C HamanoJul 11, 2011
  40. Ramkumar RamachandraJul 12, 2011
  41. Jonathan NiederJul 12, 2011
  42. Ramkumar RamachandraJul 17, 2011
  43. Jonathan NiederJul 17, 2011
  44. 10/17 sequencer: Announce sequencer state locationRamkumar Ramachandra, Jul 11, 2011
  45. Jonathan NiederJul 12, 2011
  46. Sverre RabbelierJul 13, 2011
  47. Ramkumar RamachandraJul 17, 2011
  48. Jonathan NiederJul 17, 2011
  49. Ramkumar RamachandraJul 18, 2011
  50. 11/17 revert: Save data for continuing after conflict resolutionRamkumar Ramachandra, Jul 11, 2011
  51. Junio C HamanoJul 11, 2011
  52. Junio C HamanoJul 11, 2011
  53. Ramkumar RamachandraJul 12, 2011
  54. Jonathan NiederJul 12, 2011
  55. Ramkumar RamachandraJul 17, 2011
  56. Jonathan NiederJul 17, 2011
  57. Ramkumar RamachandraJul 18, 2011
  58. Jonathan NiederJul 19, 2011
  59. Ramkumar RamachandraJul 19, 2011
  60. 12/17 revert: Save command-line options for continuing operationRamkumar Ramachandra, Jul 11, 2011
  61. Junio C HamanoJul 11, 2011
  62. Ramkumar RamachandraJul 12, 2011
  63. Jonathan NiederJul 12, 2011
  64. Ramkumar RamachandraJul 18, 2011
  65. 13/17 revert: Introduce a layer of indirection over pick_commitsRamkumar Ramachandra, Jul 11, 2011
  66. Jonathan NiederJul 12, 2011
  67. Ramkumar RamachandraJul 18, 2011
  68. 14/17 reset: Make hard reset remove the sequencer stateRamkumar Ramachandra, Jul 11, 2011
  69. Jonathan NiederJul 12, 2011
  70. Ramkumar RamachandraJul 17, 2011
  71. 15/17 revert: Remove sequencer state when no commits are pendingRamkumar Ramachandra, Jul 11, 2011
  72. Junio C HamanoJul 11, 2011
  73. Ramkumar RamachandraJul 12, 2011
  74. 16/17 revert: Introduce --reset to remove sequencer stateRamkumar Ramachandra, Jul 11, 2011
  75. Jonathan NiederJul 12, 2011
  76. Ramkumar RamachandraJul 17, 2011
  77. Jonathan NiederJul 17, 2011
  78. 17/17 revert: Introduce --continue to continue the operationRamkumar Ramachandra, Jul 11, 2011
  79. Jonathan NiederJul 12, 2011
  80. Ramkumar RamachandraJul 17, 2011
  81. Jonathan NiederJul 17, 2011
  82. Ramkumar RamachandraJul 18, 2011
  83. Jonathan NiederJul 18, 2011
  84. Jonathan NiederJul 11, 2011
  85. Ramkumar RamachandraJul 11, 2011
  86. Ramkumar RamachandraJul 11, 2011
  87. Jonathan NiederJul 11, 2011
  88. Ramkumar RamachandraJul 12, 2011
  89. Junio C HamanoJul 11, 2011
  90. Jeff KingJul 11, 2011
  91. Ramkumar RamachandraJul 12, 2011
  92. Jeff KingJul 12, 2011
  93. Ramkumar RamachandraJul 13, 2011
  94. Jeff KingJul 13, 2011
  95. config: Introduce functions to write non-standard fileRamkumar Ramachandra, Jul 18, 2011
  96. Junio C HamanoJul 18, 2011
  97. Ramkumar RamachandraJul 19, 2011
  98. Jonathan NiederJul 12, 2011
  99. Miles BaderJul 12, 2011

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.