Skip to contentSkip to conversation

Prepare the sequencer for the upcoming rebase -i patches

PATCH Superseded Johannes Schindelin 10 participants 352 messages Aug 29, 2016 – Nov 6, 2016 Page 1 of 3

lore
Share & tools
lore
#6 lore raw

PATCH 2/22 sequencer: use memoized sequencer directory path

In reply to Johannes Schindelin
---
 builtin/commit.c |  2 +-
 sequencer.c      | 11 ++++++-----
 sequencer.h      |  5 +----
 3 files changed, 8 insertions(+), 10 deletions(-)
builtin/commit.c +1 -1
index 77e3dc8..0221190 100644
@@ -173,7 +173,7 @@ static void determine_whence(struct wt_status *s)
 		whence = FROM_MERGE;
 	else if (file_exists(git_path_cherry_pick_head())) {
 		whence = FROM_CHERRY_PICK;
-		if (file_exists(git_path(SEQ_DIR)))
+		if (file_exists(git_path_seq_dir()))
 			sequencer_in_use = 1;
 	}
 	else
sequencer.c +6 -5
index b6481bb..4d2b4e3 100644
@@ -21,10 +21,11 @@
 const char sign_off_header[] = "Signed-off-by: ";
 static const char cherry_picked_prefix[] = "(cherry picked from commit ";
 
-static GIT_PATH_FUNC(git_path_todo_file, SEQ_TODO_FILE)
-static GIT_PATH_FUNC(git_path_opts_file, SEQ_OPTS_FILE)
-static GIT_PATH_FUNC(git_path_seq_dir, SEQ_DIR)
-static GIT_PATH_FUNC(git_path_head_file, SEQ_HEAD_FILE)
+GIT_PATH_FUNC(git_path_seq_dir, "sequencer")
+
+static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo")
+static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts")
+static GIT_PATH_FUNC(git_path_head_file, "sequencer/head")
 
 static int is_rfc2822_line(const char *buf, int len)
 {
@@ -112,7 +113,7 @@ static void remove_sequencer_state(void)
 {
 	struct strbuf seq_dir = STRBUF_INIT;
 
-	strbuf_addstr(&seq_dir, git_path(SEQ_DIR));
+	strbuf_addstr(&seq_dir, git_path_seq_dir());
 	remove_dir_recursively(&seq_dir, 0);
 	strbuf_release(&seq_dir);
 }
sequencer.h +1 -4
index 2ca096b..c955594 100644
@@ -1,10 +1,7 @@
 #ifndef SEQUENCER_H
 #define SEQUENCER_H
 
-#define SEQ_DIR		"sequencer"
-#define SEQ_HEAD_FILE	"sequencer/head"
-#define SEQ_TODO_FILE	"sequencer/todo"
-#define SEQ_OPTS_FILE	"sequencer/opts"
+const char *git_path_seq_dir(void);
 
 #define APPEND_SIGNOFF_DEDUP (1u << 0)
 
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#1 lore raw

PATCH 1/22 sequencer: use static initializers for replay_opts

In reply to Johannes Schindelin
---
 builtin/revert.c | 6 ++----
 sequencer.h      | 1 +
 2 files changed, 3 insertions(+), 4 deletions(-)
builtin/revert.c +2 -4
index 4e69380..7365559 100644
@@ -178,10 +178,9 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
 
 int cmd_revert(int argc, const char **argv, const char *prefix)
 {
-	struct replay_opts opts;
+	struct replay_opts opts = REPLAY_OPTS_INIT;
 	int res;
 
-	memset(&opts, 0, sizeof(opts));
 	if (isatty(0))
 		opts.edit = 1;
 	opts.action = REPLAY_REVERT;
@@ -195,10 +194,9 @@ int cmd_revert(int argc, const char **argv, const char *prefix)
 
 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
 {
-	struct replay_opts opts;
+	struct replay_opts opts = REPLAY_OPTS_INIT;
 	int res;
 
-	memset(&opts, 0, sizeof(opts));
 	opts.action = REPLAY_PICK;
 	git_config(git_default_config, NULL);
 	parse_args(argc, argv, &opts);
sequencer.h +1 -0
index 5ed5cb1..2ca096b 100644
@@ -47,6 +47,7 @@ struct replay_opts {
 	/* Only used by REPLAY_NONE */
 	struct rev_info *revs;
 };
+#define REPLAY_OPTS_INIT { -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL }
 
 int sequencer_pick_revisions(struct replay_opts *opts);
 
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#0 lore raw

cover Prepare the sequencer for the upcoming rebase -i patches

This patch series marks the '4' in the countdown to speed up rebase -i by implementing large parts in C. It is based on the libify-sequencer patch series that I submitted last week.

The patches in this series merely prepare the sequencer code for the next patch series that actually teaches the sequencer to run an interactive rebase.

The reason to split these two patch series is simple: to keep them at a sensible size.

The two patch series after that are much smaller: a two-patch "series" that switches rebase -i to use the sequencer (except with --root or --preserve-merges), and a couple of patches to move several pretty expensive script processing steps to C (think: autosquash).

The end game of this patch series is a git-rebase--helper that makes rebase -i 5x faster on Windows (according to t/perf/p3404). Travis says that even MacOSX and Linux benefit (4x and 3x, respectively).

I have been working on this since early February, whenever time allowed, and it is time to put it into the users' hands. To that end, I will most likely submit the remaining three patch series in the next two days, and integrate the whole shebang into Git for Windows 2.10.0.

Therefore I would be most grateful for every in-depth review.

Johannes Schindelin (22):
sequencer: use static initializers for replay_opts
sequencer: use memoized sequencer directory path
sequencer: avoid unnecessary indirection
sequencer: future-proof remove_sequencer_state()
sequencer: allow the sequencer to take custody of malloc()ed data
sequencer: release memory that was allocated when reading options
sequencer: future-proof read_populate_todo()
sequencer: remove overzealous assumption
sequencer: completely revamp the "todo" script parsing
sequencer: avoid completely different messages for different actions
sequencer: get rid of the subcommand field
sequencer: refactor the code to obtain a short commit name
sequencer: remember the onelines when parsing the todo file
sequencer: prepare for rebase -i's commit functionality
sequencer: introduce a helper to read files written by scripts
sequencer: prepare for rebase -i's GPG settings
sequencer: allow editing the commit message on a case-by-case basis
sequencer: support amending commits
sequencer: support cleaning up commit messages
sequencer: remember do_recursive_merge()'s return value
sequencer: left-trim the lines read from the script
sequencer: refactor write_message()

 builtin/commit.c                |   2 +-
 builtin/revert.c                |  42 ++-
 sequencer.c                     | 573 +++++++++++++++++++++++++++-------------
 sequencer.h                     |  27 +-
 t/t3510-cherry-pick-sequence.sh |  11 -
 5 files changed, 428 insertions(+), 227 deletions(-)

Based-On: libify-sequencer at https://github.com/dscho/git
Fetch-Base-Via: git fetch https://github.com/dscho/git libify-sequencer
Published-As: https://github.com/dscho/git/releases/tag/prepare-sequencer-v1
Fetch-It-Via: git fetch https://github.com/dscho/git prepare-sequencer-v1

Signature · 3 lines
2.10.0.rc1.114.g2bd6b38

base-commit: 2d6d71e2a2d410b12d783f0a8edd22791f303c12
#9 lore raw

PATCH 3/22 sequencer: avoid unnecessary indirection

We really do not need the *pointer to a* pointer to the options in the read_populate_opts() function.

---
 sequencer.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
sequencer.c +3 -3
index 4d2b4e3..14ef79b 100644
@@ -809,11 +809,11 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
 	return 0;
 }
 
-static int read_populate_opts(struct replay_opts **opts)
+static int read_populate_opts(struct replay_opts *opts)
 {
 	if (!file_exists(git_path_opts_file()))
 		return 0;
-	if (git_config_from_file(populate_opts_cb, git_path_opts_file(), *opts) < 0)
+	if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0)
 		return error(_("Malformed options sheet: %s"),
 			git_path_opts_file());
 	return 0;
@@ -1038,7 +1038,7 @@ static int sequencer_continue(struct replay_opts *opts)
 
 	if (!file_exists(git_path_todo_file()))
 		return continue_single_pick();
-	if (read_populate_opts(&opts) ||
+	if (read_populate_opts(opts) ||
 			read_populate_todo(&todo_list, opts))
 		return -1;
 
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#12 lore raw

PATCH 4/22 sequencer: future-proof remove_sequencer_state()

In reply to Johannes Schindelin

In a couple of commits, we will teach the sequencer to handle the nitty gritty of the interactive rebase, which keeps its state in a different directory.

---
 sequencer.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)
sequencer.c +16 -8
index 14ef79b..c4b223b 100644
@@ -27,6 +27,11 @@ static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo")
 static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts")
 static GIT_PATH_FUNC(git_path_head_file, "sequencer/head")
 
+static const char *get_dir(const struct replay_opts *opts)
+{
+	return git_path_seq_dir();
+}
+
 static int is_rfc2822_line(const char *buf, int len)
 {
 	int i;
@@ -109,13 +114,13 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
 	return 1;
 }
 
-static void remove_sequencer_state(void)
+static void remove_sequencer_state(const struct replay_opts *opts)
 {
-	struct strbuf seq_dir = STRBUF_INIT;
+	struct strbuf dir = STRBUF_INIT;
 
-	strbuf_addstr(&seq_dir, git_path_seq_dir());
-	remove_dir_recursively(&seq_dir, 0);
-	strbuf_release(&seq_dir);
+	strbuf_addf(&dir, "%s", get_dir(opts));
+	remove_dir_recursively(&dir, 0);
+	strbuf_release(&dir);
 }
 
 static const char *action_name(const struct replay_opts *opts)
@@ -895,6 +900,9 @@ static int sequencer_rollback(struct replay_opts *opts)
 	unsigned char sha1[20];
 	struct strbuf buf = STRBUF_INIT;
 
+	if (read_and_refresh_cache(opts))
+		return -1;
+
 	f = fopen(git_path_head_file(), "r");
 	if (!f && errno == ENOENT) {
 		/*
@@ -924,7 +932,7 @@ static int sequencer_rollback(struct replay_opts *opts)
 	}
 	if (reset_for_rollback(sha1))
 		goto fail;
-	remove_sequencer_state();
+	remove_sequencer_state(opts);
 	strbuf_release(&buf);
 	return 0;
 fail:
@@ -1018,7 +1026,7 @@ static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
 	 * Sequence of picks finished successfully; cleanup by
 	 * removing the .git/sequencer directory
 	 */
-	remove_sequencer_state();
+	remove_sequencer_state(opts);
 	return 0;
 }
 
@@ -1079,7 +1087,7 @@ int sequencer_pick_revisions(struct replay_opts *opts)
 	 * one that is being continued
 	 */
 	if (opts->subcommand == REPLAY_REMOVE_STATE) {
-		remove_sequencer_state();
+		remove_sequencer_state(opts);
 		return 0;
 	}
 	if (opts->subcommand == REPLAY_ROLLBACK)
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#16 lore raw

PATCH 5/22 sequencer: allow the sequencer to take custody of malloc()ed data

In reply to Johannes Schindelin

The sequencer is our attempt to lib-ify cherry-pick. Yet it behaves like a one-shot command when it reads its configuration: memory is allocated and released only when the command exits.

This is kind of okay for git-cherry-pick, which *is* a one-shot command. All the work to make the sequencer its work horse was done to allow using the functionality as a library function, though, including proper clean-up after use.

This patch introduces an API to pass the responsibility of releasing certain memory to the sequencer.

---
 sequencer.c | 13 +++++++++++++
 sequencer.h |  8 +++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)
sequencer.c +13 -0
index c4b223b..b5be0f9 100644
@@ -114,9 +114,22 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
 	return 1;
 }
 
+void *sequencer_entrust(struct replay_opts *opts, void *set_me_free_after_use)
+{
+	ALLOC_GROW(opts->owned, opts->owned_nr + 1, opts->owned_alloc);
+	opts->owned[opts->owned_nr++] = set_me_free_after_use;
+
+	return set_me_free_after_use;
+}
+
 static void remove_sequencer_state(const struct replay_opts *opts)
 {
 	struct strbuf dir = STRBUF_INIT;
+	int i;
+
+	for (i = 0; i < opts->owned_nr; i++)
+		free(opts->owned[i]);
+	free(opts->owned);
 
 	strbuf_addf(&dir, "%s", get_dir(opts));
 	remove_dir_recursively(&dir, 0);
sequencer.h +7 -1
index c955594..20b708a 100644
@@ -43,8 +43,14 @@ struct replay_opts {
 
 	/* Only used by REPLAY_NONE */
 	struct rev_info *revs;
+
+	/* malloc()ed data entrusted to the sequencer */
+	void **owned;
+	int owned_nr, owned_alloc;
 };
-#define REPLAY_OPTS_INIT { -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL }
+#define REPLAY_OPTS_INIT { -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL, NULL, 0, 0 }
+
+void *sequencer_entrust(struct replay_opts *opts, void *set_me_free_after_use);
 
 int sequencer_pick_revisions(struct replay_opts *opts);
 
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#24 lore raw

PATCH 6/22 sequencer: release memory that was allocated when reading options

In reply to Johannes Schindelin

The sequencer reads options from disk and stores them in its struct for use during sequencer's operations.

With this patch, the memory is released afterwards, plugging a memory leak.

---
 sequencer.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
sequencer.c +10 -3
index b5be0f9..8d79091 100644
@@ -131,6 +131,8 @@ static void remove_sequencer_state(const struct replay_opts *opts)
 		free(opts->owned[i]);
 	free(opts->owned);
 
+	free(opts->xopts);
+
 	strbuf_addf(&dir, "%s", get_dir(opts));
 	remove_dir_recursively(&dir, 0);
 	strbuf_release(&dir);
@@ -811,13 +813,18 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
 		opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
 	else if (!strcmp(key, "options.mainline"))
 		opts->mainline = git_config_int(key, value);
-	else if (!strcmp(key, "options.strategy"))
+	else if (!strcmp(key, "options.strategy")) {
 		git_config_string(&opts->strategy, key, value);
-	else if (!strcmp(key, "options.gpg-sign"))
+		sequencer_entrust(opts, (char *) opts->strategy);
+	}
+	else if (!strcmp(key, "options.gpg-sign")) {
 		git_config_string(&opts->gpg_sign, key, value);
+		sequencer_entrust(opts, (char *) opts->gpg_sign);
+	}
 	else if (!strcmp(key, "options.strategy-option")) {
 		ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
-		opts->xopts[opts->xopts_nr++] = xstrdup(value);
+		opts->xopts[opts->xopts_nr++] =
+			sequencer_entrust(opts, xstrdup(value));
 	} else
 		return error(_("Invalid key: %s"), key);
 
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#32 lore raw

PATCH 7/22 sequencer: future-proof read_populate_todo()

In reply to Johannes Schindelin

Over the next commits, we will work on improving the sequencer to the point where it can process the edit script of an interactive rebase. To that end, we will need to teach the sequencer to read interactive rebase's todo file. In preparation, we consolidate all places where that todo file is needed to call a function that we will later extend.

---
 sequencer.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
sequencer.c +11 -7
index 8d79091..982b6e9 100644
@@ -32,6 +32,11 @@ static const char *get_dir(const struct replay_opts *opts)
 	return git_path_seq_dir();
 }
 
+static const char *get_todo_path(const struct replay_opts *opts)
+{
+	return git_path_todo_file();
+}
+
 static int is_rfc2822_line(const char *buf, int len)
 {
 	int i;
@@ -772,25 +777,24 @@ static int parse_insn_buffer(char *buf, struct commit_list **todo_list,
 static int read_populate_todo(struct commit_list **todo_list,
 			struct replay_opts *opts)
 {
+	const char *todo_file = get_todo_path(opts);
 	struct strbuf buf = STRBUF_INIT;
 	int fd, res;
 
-	fd = open(git_path_todo_file(), O_RDONLY);
+	fd = open(todo_file, O_RDONLY);
 	if (fd < 0)
-		return error_errno(_("Could not open %s"),
-				   git_path_todo_file());
+		return error_errno(_("Could not open %s"), todo_file);
 	if (strbuf_read(&buf, fd, 0) < 0) {
 		close(fd);
 		strbuf_release(&buf);
-		return error(_("Could not read %s."), git_path_todo_file());
+		return error(_("Could not read %s."), todo_file);
 	}
 	close(fd);
 
 	res = parse_insn_buffer(buf.buf, todo_list, opts);
 	strbuf_release(&buf);
 	if (res)
-		return error(_("Unusable instruction sheet: %s"),
-			git_path_todo_file());
+		return error(_("Unusable instruction sheet: %s"), todo_file);
 	return 0;
 }
 
@@ -1064,7 +1068,7 @@ static int sequencer_continue(struct replay_opts *opts)
 {
 	struct commit_list *todo_list = NULL;
 
-	if (!file_exists(git_path_todo_file()))
+	if (!file_exists(get_todo_path(opts)))
 		return continue_single_pick();
 	if (read_populate_opts(opts) ||
 			read_populate_todo(&todo_list, opts))
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#35 lore raw

PATCH 8/22 sequencer: remove overzealous assumption

In reply to Johannes Schindelin

The sequencer was introduced to make the cherry-pick and revert functionality available as library function, with the original idea being to extend the sequencer to also implement the rebase -i functionality.

The test to ensure that all of the commands in the script are identical to the overall operation does not mesh well with that.

Therefore let's just get rid of the test that wants to verify that this limitation is still in place, in preparation for the upcoming work to teach the sequencer to do rebase -i's work.

---
 t/t3510-cherry-pick-sequence.sh | 11 -----------
 1 file changed, 11 deletions(-)
t/t3510-cherry-pick-sequence.sh +0 -11
index 7b7a89d..6465edf 100755
@@ -459,17 +459,6 @@ test_expect_success 'malformed instruction sheet 1' '
 	test_expect_code 128 git cherry-pick --continue
 '
 
-test_expect_success 'malformed instruction sheet 2' '
-	pristine_detach initial &&
-	test_expect_code 1 git cherry-pick base..anotherpick &&
-	echo "resolved" >foo &&
-	git add foo &&
-	git commit &&
-	sed "s/pick/revert/" .git/sequencer/todo >new_sheet &&
-	cp new_sheet .git/sequencer/todo &&
-	test_expect_code 128 git cherry-pick --continue
-'
-
 test_expect_success 'empty commit set' '
 	pristine_detach initial &&
 	test_expect_code 128 git cherry-pick base..base
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#43 lore raw

PATCH 9/22 sequencer: completely revamp the "todo" script parsing

In reply to Johannes Schindelin

When we came up with the "sequencer" idea, we really wanted to have kind of a plumbing equivalent of the interactive rebase. Hence the choice of words: the "todo" script, a "pick", etc.

However, when it came time to implement the entire shebang, somehow this idea got lost and the sequencer was used as working horse for cherry-pick and revert instead. So as not to interfere with the interactive rebase, it even uses a separate directory to store its state.

Furthermore, it also is stupidly strict about the "todo" script it
accepts: while it parses commands in a way that was *designed* to be similar to the interactive rebase, it then goes on to *error out* if the commands disagree with the overall action (cherry-pick or revert).

Finally, the sequencer code chose to deviate from the interactive rebase code insofar that it *reformats* the "todo" script instead of just writing the part of the parsed script that were not yet processed. This is not only unnecessary churn, but might well lose information that is valuable to the user (i.e. comments after the commands).

Let's just bite the bullet and rewrite the entire parser; the code now becomes not only more elegant: it allows us to go on and teach the sequencer how to parse *true* "todo" scripts as used by the interactive rebase itself. In a way, the sequencer is about to grow up to do its older brother's job. Better.

While at it, do not stop at the first problem, but list *all* of the problems. This helps the user by allowing to address all issues in one go rather than going back and forth until the todo list is valid.

---
 sequencer.c | 241 +++++++++++++++++++++++++++++++++---------------------------
 1 file changed, 134 insertions(+), 107 deletions(-)
sequencer.c +134 -107
index 982b6e9..cbdce6d 100644
@@ -473,7 +473,26 @@ static int allow_empty(struct replay_opts *opts, struct commit *commit)
 		return 1;
 }
 
-static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
+enum todo_command {
+	TODO_PICK,
+	TODO_REVERT
+};
+
+static const char *todo_command_strings[] = {
+	"pick",
+	"revert"
+};
+
+static const char *command_to_string(const enum todo_command command)
+{
+	if (command < ARRAY_SIZE(todo_command_strings))
+		return todo_command_strings[command];
+	die("Unknown command: %d", command);
+}
+
+
+static int do_pick_commit(enum todo_command command, struct commit *commit,
+		struct replay_opts *opts)
 {
 	unsigned char head[20];
 	struct commit *base, *next, *parent;
@@ -535,7 +554,8 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
 		/* TRANSLATORS: The first %s will be "revert" or
 		   "cherry-pick", the second %s a SHA1 */
 		return error(_("%s: cannot parse parent commit %s"),
-			action_name(opts), oid_to_hex(&parent->object.oid));
+			command_to_string(command),
+			oid_to_hex(&parent->object.oid));
 
 	if (get_message(commit, &msg) != 0)
 		return error(_("Cannot get commit message for %s"),
@@ -548,7 +568,7 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
 	 * reverse of it if we are revert.
 	 */
 
-	if (opts->action == REPLAY_REVERT) {
+	if (command == TODO_REVERT) {
 		base = commit;
 		base_label = msg.label;
 		next = parent;
@@ -589,7 +609,7 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
 		}
 	}
 
-	if (!opts->strategy || !strcmp(opts->strategy, "recursive") || opts->action == REPLAY_REVERT) {
+	if (!opts->strategy || !strcmp(opts->strategy, "recursive") || command == TODO_REVERT) {
 		res = do_recursive_merge(base, next, base_label, next_label,
 					 head, &msgbuf, opts);
 		if (res < 0)
@@ -615,17 +635,17 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
 	 * However, if the merge did not even start, then we don't want to
 	 * write it at all.
 	 */
-	if (opts->action == REPLAY_PICK && !opts->no_commit && (res == 0 || res == 1) &&
+	if (command == TODO_PICK && !opts->no_commit && (res == 0 || res == 1) &&
 	    update_ref(NULL, "CHERRY_PICK_HEAD", commit->object.oid.hash, NULL,
 		       REF_NODEREF, UPDATE_REFS_MSG_ON_ERR))
 		res = -1;
-	if (opts->action == REPLAY_REVERT && ((opts->no_commit && res == 0) || res == 1) &&
+	if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) &&
 	    update_ref(NULL, "REVERT_HEAD", commit->object.oid.hash, NULL,
 		       REF_NODEREF, UPDATE_REFS_MSG_ON_ERR))
 		res = -1;
 
 	if (res) {
-		error(opts->action == REPLAY_REVERT
+		error(command == TODO_REVERT
 		      ? _("could not revert %s... %s")
 		      : _("could not apply %s... %s"),
 		      find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV),
@@ -683,116 +703,107 @@ static int read_and_refresh_cache(struct replay_opts *opts)
 	return 0;
 }
 
-static int format_todo(struct strbuf *buf, struct commit_list *todo_list,
-		struct replay_opts *opts)
+struct todo_item {
+	enum todo_command command;
+	struct commit *commit;
+	size_t offset_in_buf;
+};
+
+struct todo_list {
+	struct strbuf buf;
+	struct todo_item *items;
+	int nr, alloc, current;
+};
+
+#define TODO_LIST_INIT { STRBUF_INIT, NULL, 0, 0, 0 }
+
+static void todo_list_release(struct todo_list *todo_list)
 {
-	struct commit_list *cur = NULL;
-	const char *sha1_abbrev = NULL;
-	const char *action_str = opts->action == REPLAY_REVERT ? "revert" : "pick";
-	const char *subject;
-	int subject_len;
+	strbuf_release(&todo_list->buf);
+	free(todo_list->items);
+	todo_list->items = NULL;
+	todo_list->nr = todo_list->alloc = 0;
+}
 
-	for (cur = todo_list; cur; cur = cur->next) {
-		const char *commit_buffer = get_commit_buffer(cur->item, NULL);
-		sha1_abbrev = find_unique_abbrev(cur->item->object.oid.hash, DEFAULT_ABBREV);
-		subject_len = find_commit_subject(commit_buffer, &subject);
-		strbuf_addf(buf, "%s %s %.*s\n", action_str, sha1_abbrev,
-			subject_len, subject);
-		unuse_commit_buffer(cur->item, commit_buffer);
-	}
-	return 0;
+struct todo_item *append_todo(struct todo_list *todo_list)
+{
+	ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc);
+	return todo_list->items + todo_list->nr++;
 }
 
-static struct commit *parse_insn_line(char *bol, char *eol, struct replay_opts *opts)
+static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)
 {
 	unsigned char commit_sha1[20];
-	enum replay_action action;
 	char *end_of_object_name;
-	int saved, status, padding;
-
-	if (starts_with(bol, "pick")) {
-		action = REPLAY_PICK;
-		bol += strlen("pick");
-	} else if (starts_with(bol, "revert")) {
-		action = REPLAY_REVERT;
-		bol += strlen("revert");
-	} else
-		return NULL;
+	int i, saved, status, padding;
+
+	for (i = 0; i < ARRAY_SIZE(todo_command_strings); i++)
+		if (skip_prefix(bol, todo_command_strings[i], &bol)) {
+			item->command = i;
+			break;
+		}
+	if (i >= ARRAY_SIZE(todo_command_strings))
+		return -1;
 
 	/* Eat up extra spaces/ tabs before object name */
 	padding = strspn(bol, " \t");
 	if (!padding)
-		return NULL;
+		return -1;
 	bol += padding;
 
-	end_of_object_name = bol + strcspn(bol, " \t\n");
+	end_of_object_name = (char *) bol + strcspn(bol, " \t\n");
 	saved = *end_of_object_name;
 	*end_of_object_name = '\0';
 	status = get_sha1(bol, commit_sha1);
 	*end_of_object_name = saved;
 
-	/*
-	 * Verify that the action matches up with the one in
-	 * opts; we don't support arbitrary instructions
-	 */
-	if (action != opts->action) {
-		if (action == REPLAY_REVERT)
-		      error((opts->action == REPLAY_REVERT)
-			    ? _("Cannot revert during another revert.")
-			    : _("Cannot revert during a cherry-pick."));
-		else
-		      error((opts->action == REPLAY_REVERT)
-			    ? _("Cannot cherry-pick during a revert.")
-			    : _("Cannot cherry-pick during another cherry-pick."));
-		return NULL;
-	}
-
 	if (status < 0)
-		return NULL;
+		return -1;
 
-	return lookup_commit_reference(commit_sha1);
+	item->commit = lookup_commit_reference(commit_sha1);
+	return !item->commit;
 }
 
-static int parse_insn_buffer(char *buf, struct commit_list **todo_list,
-			struct replay_opts *opts)
+static int parse_insn_buffer(char *buf, struct todo_list *todo_list)
 {
-	struct commit_list **next = todo_list;
-	struct commit *commit;
+	struct todo_item *item;
 	char *p = buf;
-	int i;
+	int i, res = 0;
 
 	for (i = 1; *p; i++) {
 		char *eol = strchrnul(p, '\n');
-		commit = parse_insn_line(p, eol, opts);
-		if (!commit)
-			return error(_("Could not parse line %d."), i);
-		next = commit_list_append(commit, next);
+
+		item = append_todo(todo_list);
+		item->offset_in_buf = p - todo_list->buf.buf;
+		if (parse_insn_line(item, p, eol)) {
+			error("Invalid line: %.*s", (int)(eol - p), p);
+			res |= error(_("Could not parse line %d."), i);
+			item->command = -1;
+		}
 		p = *eol ? eol + 1 : eol;
 	}
-	if (!*todo_list)
+	if (!todo_list->nr)
 		return error(_("No commits parsed."));
-	return 0;
+	return res;
 }
 
-static int read_populate_todo(struct commit_list **todo_list,
+static int read_populate_todo(struct todo_list *todo_list,
 			struct replay_opts *opts)
 {
 	const char *todo_file = get_todo_path(opts);
-	struct strbuf buf = STRBUF_INIT;
 	int fd, res;
 
+	strbuf_reset(&todo_list->buf);
 	fd = open(todo_file, O_RDONLY);
 	if (fd < 0)
 		return error_errno(_("Could not open %s"), todo_file);
-	if (strbuf_read(&buf, fd, 0) < 0) {
+	if (strbuf_read(&todo_list->buf, fd, 0) < 0) {
 		close(fd);
-		strbuf_release(&buf);
 		return error(_("Could not read %s."), todo_file);
 	}
 	close(fd);
 
-	res = parse_insn_buffer(buf.buf, todo_list, opts);
-	strbuf_release(&buf);
+	res = parse_insn_buffer(todo_list->buf.buf, todo_list);
 	if (res)
 		return error(_("Unusable instruction sheet: %s"), todo_file);
 	return 0;
@@ -848,18 +859,33 @@ static int read_populate_opts(struct replay_opts *opts)
 	return 0;
 }
 
-static int walk_revs_populate_todo(struct commit_list **todo_list,
+static int walk_revs_populate_todo(struct todo_list *todo_list,
 				struct replay_opts *opts)
 {
+	enum todo_command command = opts->action == REPLAY_PICK ?
+		TODO_PICK : TODO_REVERT;
 	struct commit *commit;
-	struct commit_list **next;
 
 	if (prepare_revs(opts))
 		return -1;
 
-	next = todo_list;
-	while ((commit = get_revision(opts->revs)))
-		next = commit_list_append(commit, next);
+	while ((commit = get_revision(opts->revs))) {
+		struct todo_item *item = append_todo(todo_list);
+		const char *commit_buffer = get_commit_buffer(commit, NULL);
+		const char *subject;
+		int subject_len;
+
+		item->command = command;
+		item->commit = commit;
+		item->offset_in_buf = todo_list->buf.len;
+		subject_len = find_commit_subject(commit_buffer, &subject);
+		strbuf_addf(&todo_list->buf, "%s %s %.*s\n",
+			opts->action == REPLAY_PICK ?  "pick" : "revert",
+			find_unique_abbrev(commit->object.oid.hash,
+				DEFAULT_ABBREV),
+			subject_len, subject);
+		unuse_commit_buffer(commit, commit_buffer);
+	}
 	return 0;
 }
 
@@ -964,30 +990,24 @@ static int sequencer_rollback(struct replay_opts *opts)
 	return -1;
 }
 
-static int save_todo(struct commit_list *todo_list, struct replay_opts *opts)
+static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
 {
 	static struct lock_file todo_lock;
-	struct strbuf buf = STRBUF_INIT;
-	int fd;
+	const char *todo_path = get_todo_path(opts);
+	int next = todo_list->current, offset, fd;
 
-	fd = hold_lock_file_for_update(&todo_lock, git_path_todo_file(), 0);
+	fd = hold_lock_file_for_update(&todo_lock, todo_path, 0);
 	if (fd < 0)
 		return error_errno(_("Could not lock '%s'"),
 				   git_path_todo_file());
-	if (format_todo(&buf, todo_list, opts) < 0) {
-		strbuf_release(&buf);
-		return error(_("Could not format %s."), git_path_todo_file());
-	}
-	if (write_in_full(fd, buf.buf, buf.len) < 0) {
-		strbuf_release(&buf);
-		return error_errno(_("Could not write to %s"),
-				   git_path_todo_file());
-	}
-	if (commit_lock_file(&todo_lock) < 0) {
-		strbuf_release(&buf);
-		return error(_("Error wrapping up %s."), git_path_todo_file());
-	}
-	strbuf_release(&buf);
+	offset = next < todo_list->nr ?
+		todo_list->items[next].offset_in_buf : todo_list->buf.len;
+	if (write_in_full(fd, todo_list->buf.buf + offset,
+			todo_list->buf.len - offset) < 0)
+		return error(_("Could not write to %s (%s)"),
+			todo_path, strerror(errno));
+	if (commit_lock_file(&todo_lock) < 0)
+		return error(_("Error wrapping up %s."), todo_path);
 	return 0;
 }
 
@@ -1026,9 +1046,8 @@ static int save_opts(struct replay_opts *opts)
 	return res;
 }
 
-static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
+static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
 {
-	struct commit_list *cur;
 	int res;
 
 	setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
@@ -1038,10 +1057,12 @@ static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
 	if (read_and_refresh_cache(opts))
 		return -1;
 
-	for (cur = todo_list; cur; cur = cur->next) {
-		if (save_todo(cur, opts))
+	while (todo_list->current < todo_list->nr) {
+		struct todo_item *item = todo_list->items + todo_list->current;
+		if (save_todo(todo_list, opts))
 			return -1;
-		res = do_pick_commit(cur->item, opts);
+		res = do_pick_commit(item->command, item->commit, opts);
+		todo_list->current++;
 		if (res)
 			return res;
 	}
@@ -1066,7 +1087,8 @@ static int continue_single_pick(void)
 
 static int sequencer_continue(struct replay_opts *opts)
 {
-	struct commit_list *todo_list = NULL;
+	struct todo_list todo_list = TODO_LIST_INIT;
+	int res;
 
 	if (!file_exists(get_todo_path(opts)))
 		return continue_single_pick();
@@ -1083,21 +1105,24 @@ static int sequencer_continue(struct replay_opts *opts)
 	}
 	if (index_differs_from("HEAD", 0))
 		return error_dirty_index(opts);
-	todo_list = todo_list->next;
-	return pick_commits(todo_list, opts);
+	todo_list.current++;
+	res = pick_commits(&todo_list, opts);
+	todo_list_release(&todo_list);
+	return res;
 }
 
 static int single_pick(struct commit *cmit, struct replay_opts *opts)
 {
 	setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
-	return do_pick_commit(cmit, opts);
+	return do_pick_commit(opts->action == REPLAY_PICK ?
+		TODO_PICK : TODO_REVERT, cmit, opts);
 }
 
 int sequencer_pick_revisions(struct replay_opts *opts)
 {
-	struct commit_list *todo_list = NULL;
+	struct todo_list todo_list = TODO_LIST_INIT;
 	unsigned char sha1[20];
-	int i;
+	int i, res;
 
 	if (opts->subcommand == REPLAY_NONE)
 		assert(opts->revs);
@@ -1171,7 +1196,9 @@ int sequencer_pick_revisions(struct replay_opts *opts)
 	if (save_head(sha1_to_hex(sha1)) ||
 			save_opts(opts))
 		return -1;
-	return pick_commits(todo_list, opts);
+	res = pick_commits(&todo_list, opts);
+	todo_list_release(&todo_list);
+	return res;
 }
 
 void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag)
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#51 lore raw

PATCH 10/22 sequencer: avoid completely different messages for different actions

In reply to Johannes Schindelin
---
 sequencer.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
sequencer.c +2 -5
index cbdce6d..1b65202 100644
@@ -232,11 +232,8 @@ static int error_dirty_index(struct replay_opts *opts)
 	if (read_cache_unmerged())
 		return error_resolve_conflict(action_name(opts));
 
-	/* Different translation strings for cherry-pick and revert */
-	if (opts->action == REPLAY_PICK)
-		error(_("Your local changes would be overwritten by cherry-pick."));
-	else
-		error(_("Your local changes would be overwritten by revert."));
+	error(_("Your local changes would be overwritten by %s."),
+		action_name(opts));
 
 	if (advice_commit_before_merge)
 		advise(_("Commit your changes or stash them to proceed."));
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#62 lore raw

PATCH 13/22 sequencer: remember the onelines when parsing the todo file

In reply to Johannes Schindelin

The git-rebase-todo file contains a list of commands. Most of those commands have the form

	<verb> <sha1> <oneline>

The <oneline> is displayed primarily for the user's convenience, as rebase -i really interprets only the <verb> <sha1> part. However, there are *some* places in interactive rebase where the <oneline> is used to display messages, e.g. for reporting at which commit we stopped.

So let's just remember it when parsing the todo file; we keep a copy of the entire todo file anyway (to write out the new done and git-rebase-todo file just before processing each command), so all we need to do is remember the begin and end offsets.

---
 sequencer.c | 7 +++++++
 1 file changed, 7 insertions(+)
sequencer.c +7 -0
index 06759d4..3398774 100644
@@ -709,6 +709,8 @@ static int read_and_refresh_cache(struct replay_opts *opts)
 struct todo_item {
 	enum todo_command command;
 	struct commit *commit;
+	const char *arg;
+	int arg_len;
 	size_t offset_in_buf;
 };
 
@@ -760,6 +762,9 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)
 	status = get_sha1(bol, commit_sha1);
 	*end_of_object_name = saved;
 
+	item->arg = end_of_object_name + strspn(end_of_object_name, " \t");
+	item->arg_len = (int)(eol - item->arg);
+
 	if (status < 0)
 		return -1;
 
@@ -880,6 +885,8 @@ static int walk_revs_populate_todo(struct todo_list *todo_list,
 
 		item->command = command;
 		item->commit = commit;
+		item->arg = NULL;
+		item->arg_len = 0;
 		item->offset_in_buf = todo_list->buf.len;
 		subject_len = find_commit_subject(commit_buffer, &subject);
 		strbuf_addf(&todo_list->buf, "%s %s %.*s\n",
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#81 lore raw

PATCH 14/22 sequencer: prepare for rebase -i's commit functionality

In reply to Johannes Schindelin

In interactive rebases, we commit a little bit differently than the sequencer did so far: we heed the "author-script", the "message" and the "amend" files in the .git/rebase-merge/ subdirectory.

Likewise, we may want to edit the commit message *even* when providing a file containing the suggested commit message. Therefore we change the code to not even provide a default message when we do not want any, and to call the editor explicitly.

As interactive rebase's GPG settings are configured differently from how cherry-pick (and therefore sequencer) handles them, we will leave support for that to the next commit.

---
 sequencer.c | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++++--------
 sequencer.h |  3 ++
 2 files changed, 83 insertions(+), 12 deletions(-)
sequencer.c +80 -12
index 3398774..b124980 100644
@@ -27,6 +27,16 @@ static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo")
 static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts")
 static GIT_PATH_FUNC(git_path_head_file, "sequencer/head")
 
+/*
+ * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
+ * GIT_AUTHOR_DATE that will be used for the commit that is currently
+ * being rebased.
+ */
+static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script")
+
+/* We will introduce the 'interactive rebase' mode later */
+#define IS_REBASE_I() 0
+
 static const char *get_dir(const struct replay_opts *opts)
 {
 	return git_path_seq_dir();
@@ -377,20 +387,72 @@ static int is_index_unchanged(void)
 	return !hashcmp(active_cache_tree->sha1, head_commit->tree->object.oid.hash);
 }
 
+static char **read_author_script(void)
+{
+	struct strbuf script = STRBUF_INIT;
+	int i, count = 0;
+	char *p, *p2, **env;
+	size_t env_size;
+
+	if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0)
+		return NULL;
+
+	for (p = script.buf; *p; p++)
+		if (skip_prefix(p, "'\\\\''", (const char **)&p2))
+			strbuf_splice(&script, p - script.buf, p2 - p, "'", 1);
+		else if (*p == '\'')
+			strbuf_splice(&script, p-- - script.buf, 1, "", 0);
+		else if (*p == '\n') {
+			*p = '\0';
+			count++;
+		}
+
+	env_size = (count + 1) * sizeof(*env);
+	strbuf_grow(&script, env_size);
+	memmove(script.buf + env_size, script.buf, script.len);
+	p = script.buf + env_size;
+	env = (char **)strbuf_detach(&script, NULL);
+
+	for (i = 0; i < count; i++) {
+		env[i] = p;
+		p += strlen(p) + 1;
+	}
+	env[count] = NULL;
+
+	return env;
+}
+
 /*
  * If we are cherry-pick, and if the merge did not result in
  * hand-editing, we will hit this commit and inherit the original
  * author date and name.
  * If we are revert, or if our cherry-pick results in a hand merge,
- * we had better say that the current user is responsible for that.
+ * we had better say that the current user is responsible for that
+ * (except, of course, while running an interactive rebase).
  */
-static int run_git_commit(const char *defmsg, struct replay_opts *opts,
+int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 			  int allow_empty)
 {
+	char **env = NULL;
 	struct argv_array array;
 	int rc;
 	const char *value;
 
+	if (IS_REBASE_I()) {
+		env = read_author_script();
+		if (!env)
+			return error("You have staged changes in your working "
+				"tree. If these changes are meant to be\n"
+				"squashed into the previous commit, run:\n\n"
+				"  git commit --amend $gpg_sign_opt_quoted\n\n"
+				"If they are meant to go into a new commit, "
+				"run:\n\n"
+				"  git commit $gpg_sign_opt_quoted\n\n"
+				"In both case, once you're done, continue "
+				"with:\n\n"
+				"  git rebase --continue\n");
+	}
+
 	argv_array_init(&array);
 	argv_array_push(&array, "commit");
 	argv_array_push(&array, "-n");
@@ -399,14 +461,13 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
 		argv_array_pushf(&array, "-S%s", opts->gpg_sign);
 	if (opts->signoff)
 		argv_array_push(&array, "-s");
-	if (!opts->edit) {
-		argv_array_push(&array, "-F");
-		argv_array_push(&array, defmsg);
-		if (!opts->signoff &&
-		    !opts->record_origin &&
-		    git_config_get_value("commit.cleanup", &value))
-			argv_array_push(&array, "--cleanup=verbatim");
-	}
+	if (defmsg)
+		argv_array_pushl(&array, "-F", defmsg, NULL);
+	if (opts->edit)
+		argv_array_push(&array, "-e");
+	else if (!opts->signoff && !opts->record_origin &&
+		 git_config_get_value("commit.cleanup", &value))
+		argv_array_push(&array, "--cleanup=verbatim");
 
 	if (allow_empty)
 		argv_array_push(&array, "--allow-empty");
@@ -414,8 +475,11 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
 	if (opts->allow_empty_message)
 		argv_array_push(&array, "--allow-empty-message");
 
-	rc = run_command_v_opt(array.argv, RUN_GIT_CMD);
+	rc = run_command_v_opt_cd_env(array.argv, RUN_GIT_CMD, NULL,
+			(const char *const *)env);
 	argv_array_clear(&array);
+	free(env);
+
 	return rc;
 }
 
@@ -664,7 +728,8 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 		goto leave;
 	}
 	if (!opts->no_commit)
-		res = run_git_commit(git_path_merge_msg(), opts, allow);
+		res = sequencer_commit(opts->edit ? NULL : git_path_merge_msg(),
+			opts, allow);
 
 leave:
 	free_message(commit, &msg);
@@ -859,6 +924,9 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
 
 static int read_populate_opts(struct replay_opts *opts)
 {
+	if (IS_REBASE_I())
+		return 0;
+
 	if (!file_exists(git_path_opts_file()))
 		return 0;
 	if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0)
sequencer.h +3 -0
index 674f11e..9f63c31 100644
@@ -49,6 +49,9 @@ int sequencer_continue(struct replay_opts *opts);
 int sequencer_rollback(struct replay_opts *opts);
 int sequencer_remove_state(struct replay_opts *opts);
 
+int sequencer_commit(const char *defmsg, struct replay_opts *opts,
+			  int allow_empty);
+
 extern const char sign_off_header[];
 
 void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag);
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#91 lore raw

PATCH 16/22 sequencer: prepare for rebase -i's GPG settings

In reply to Johannes Schindelin

The rebase command sports a --gpg-sign option that is heeded by the interactive rebase.

This patch teaches the sequencer that trick, as part of the bigger effort to make the sequencer the work horse of the interactive rebase.

---
 sequencer.c | 48 +++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 43 insertions(+), 5 deletions(-)
sequencer.c +43 -5
index 4204cc8..e094ac2 100644
@@ -15,6 +15,7 @@
 #include "merge-recursive.h"
 #include "refs.h"
 #include "argv-array.h"
+#include "quote.h"
 
 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
 
@@ -33,6 +34,11 @@ static GIT_PATH_FUNC(git_path_head_file, "sequencer/head")
  * being rebased.
  */
 static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script")
+/*
+ * The following files are written by git-rebase just after parsing the
+ * command-line (and are only consumed, not modified, by the sequencer).
+ */
+static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")
 
 /* We will introduce the 'interactive rebase' mode later */
 #define IS_REBASE_I() 0
@@ -129,6 +135,16 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
 	return 1;
 }
 
+static const char *gpg_sign_opt_quoted(struct replay_opts *opts)
+{
+	static struct strbuf buf = STRBUF_INIT;
+
+	strbuf_reset(&buf);
+	if (opts->gpg_sign)
+		sq_quotef(&buf, "-S%s", opts->gpg_sign);
+	return buf.buf;
+}
+
 void *sequencer_entrust(struct replay_opts *opts, void *set_me_free_after_use)
 {
 	ALLOC_GROW(opts->owned, opts->owned_nr + 1, opts->owned_alloc);
@@ -471,17 +487,20 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 
 	if (IS_REBASE_I()) {
 		env = read_author_script();
-		if (!env)
+		if (!env) {
+			const char *gpg_opt = gpg_sign_opt_quoted(opts);
+
 			return error("You have staged changes in your working "
 				"tree. If these changes are meant to be\n"
 				"squashed into the previous commit, run:\n\n"
-				"  git commit --amend $gpg_sign_opt_quoted\n\n"
+				"  git commit --amend %s\n\n"
 				"If they are meant to go into a new commit, "
 				"run:\n\n"
-				"  git commit $gpg_sign_opt_quoted\n\n"
+				"  git commit %s\n\n"
 				"In both case, once you're done, continue "
 				"with:\n\n"
-				"  git rebase --continue\n");
+				"  git rebase --continue\n", gpg_opt, gpg_opt);
+		}
 	}
 
 	argv_array_init(&array);
@@ -955,8 +974,27 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
 
 static int read_populate_opts(struct replay_opts *opts)
 {
-	if (IS_REBASE_I())
+	if (IS_REBASE_I()) {
+		struct strbuf buf = STRBUF_INIT;
+
+		if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), 1)) {
+			if (buf.len && buf.buf[buf.len - 1] == '\n') {
+				if (--buf.len &&
+				    buf.buf[buf.len - 1] == '\r')
+					buf.len--;
+				buf.buf[buf.len] = '\0';
+			}
+
+			if (!starts_with(buf.buf, "-S"))
+				strbuf_reset(&buf);
+			else {
+				opts->gpg_sign = buf.buf + 2;
+				strbuf_detach(&buf, NULL);
+			}
+		}
+
 		return 0;
+	}
 
 	if (!file_exists(git_path_opts_file()))
 		return 0;
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#88 lore raw

PATCH 15/22 sequencer: introduce a helper to read files written by scripts

In reply to Johannes Schindelin

As we are slowly teaching the sequencer to perform the hard work for the interactive rebase, we need to read files that were written by shell scripts.

These files typically contain a single line and are invariably ended by a line feed (and possibly a carriage return before that). Let's use a helper to read such files and to remove the line ending.

---
 sequencer.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
sequencer.c +31 -0
index b124980..4204cc8 100644
@@ -239,6 +239,37 @@ static int write_message(struct strbuf *msgbuf, const char *filename)
 	return 0;
 }
 
+/*
+ * Reads a file that was presumably written by a shell script, i.e.
+ * with an end-of-line marker that needs to be stripped.
+ *
+ * Returns 1 if the file was read, 0 if it could not be read or does not exist.
+ */
+static int read_oneliner(struct strbuf *buf,
+	const char *path, int skip_if_empty)
+{
+	int orig_len = buf->len;
+
+	if (!file_exists(path))
+		return 0;
+
+	if (strbuf_read_file(buf, path, 0) < 0) {
+		warning_errno("could not read '%s'", path);
+		return 0;
+	}
+
+	if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') {
+		if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r')
+			--buf->len;
+		buf->buf[buf->len] = '\0';
+	}
+
+	if (skip_if_empty && buf->len == orig_len)
+		return 0;
+
+	return 1;
+}
+
 static struct tree *empty_tree(void)
 {
 	return lookup_tree(EMPTY_TREE_SHA1_BIN);
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#100 lore raw

PATCH 17/22 sequencer: allow editing the commit message on a case-by-case basis

In reply to Johannes Schindelin

In the upcoming commits, we will implement more and more of rebase -i's functionality. One particular feature of the commands to come is that some of them allow editing the commit message while others don't, i.e. we cannot define in the replay_opts whether the commit message should be edited or not.

---
 sequencer.c | 6 +++---
 sequencer.h | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
sequencer.c +3 -3
index e094ac2..7e17d14 100644
@@ -478,7 +478,7 @@ static char **read_author_script(void)
  * (except, of course, while running an interactive rebase).
  */
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty)
+			  int allow_empty, int edit)
 {
 	char **env = NULL;
 	struct argv_array array;
@@ -513,7 +513,7 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 		argv_array_push(&array, "-s");
 	if (defmsg)
 		argv_array_pushl(&array, "-F", defmsg, NULL);
-	if (opts->edit)
+	if (edit)
 		argv_array_push(&array, "-e");
 	else if (!opts->signoff && !opts->record_origin &&
 		 git_config_get_value("commit.cleanup", &value))
@@ -779,7 +779,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	}
 	if (!opts->no_commit)
 		res = sequencer_commit(opts->edit ? NULL : git_path_merge_msg(),
-			opts, allow);
+			opts, allow, opts->edit);
 
 leave:
 	free_message(commit, &msg);
sequencer.h +1 -1
index 9f63c31..fd02baf 100644
@@ -50,7 +50,7 @@ int sequencer_rollback(struct replay_opts *opts);
 int sequencer_remove_state(struct replay_opts *opts);
 
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty);
+			  int allow_empty, int edit);
 
 extern const char sign_off_header[];
 
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#103 lore raw

PATCH 18/22 sequencer: support amending commits

In reply to Johannes Schindelin

This teaches the sequencer_commit() function to take an argument that will allow us to implement "todo" commands that need to amend the commit messages ("fixup", "squash" and "reword").

---
 sequencer.c | 6 ++++--
 sequencer.h | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)
sequencer.c +4 -2
index 7e17d14..20f7590 100644
@@ -478,7 +478,7 @@ static char **read_author_script(void)
  * (except, of course, while running an interactive rebase).
  */
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty, int edit)
+			  int allow_empty, int edit, int amend)
 {
 	char **env = NULL;
 	struct argv_array array;
@@ -507,6 +507,8 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 	argv_array_push(&array, "commit");
 	argv_array_push(&array, "-n");
 
+	if (amend)
+		argv_array_push(&array, "--amend");
 	if (opts->gpg_sign)
 		argv_array_pushf(&array, "-S%s", opts->gpg_sign);
 	if (opts->signoff)
@@ -779,7 +781,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	}
 	if (!opts->no_commit)
 		res = sequencer_commit(opts->edit ? NULL : git_path_merge_msg(),
-			opts, allow, opts->edit);
+			opts, allow, opts->edit, 0);
 
 leave:
 	free_message(commit, &msg);
sequencer.h +1 -1
index fd02baf..2106c0d 100644
@@ -50,7 +50,7 @@ int sequencer_rollback(struct replay_opts *opts);
 int sequencer_remove_state(struct replay_opts *opts);
 
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty, int edit);
+			  int allow_empty, int edit, int amend);
 
 extern const char sign_off_header[];
 
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#106 lore raw

PATCH 19/22 sequencer: support cleaning up commit messages

In reply to Johannes Schindelin

The sequencer_commit() function already knows how to amend commits, and with this new option, it can also clean up commit messages (i.e. strip out commented lines). This is needed to implement rebase -i's 'fixup' and 'squash' commands as sequencer commands.

---
 sequencer.c | 10 +++++++---
 sequencer.h |  3 ++-
 2 files changed, 9 insertions(+), 4 deletions(-)
sequencer.c +7 -3
index 20f7590..5ec956f 100644
@@ -478,7 +478,8 @@ static char **read_author_script(void)
  * (except, of course, while running an interactive rebase).
  */
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty, int edit, int amend)
+			  int allow_empty, int edit, int amend,
+			  int cleanup_commit_message)
 {
 	char **env = NULL;
 	struct argv_array array;
@@ -515,9 +516,12 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 		argv_array_push(&array, "-s");
 	if (defmsg)
 		argv_array_pushl(&array, "-F", defmsg, NULL);
+	if (cleanup_commit_message)
+		argv_array_push(&array, "--cleanup=strip");
 	if (edit)
 		argv_array_push(&array, "-e");
-	else if (!opts->signoff && !opts->record_origin &&
+	else if (!cleanup_commit_message &&
+		 !opts->signoff && !opts->record_origin &&
 		 git_config_get_value("commit.cleanup", &value))
 		argv_array_push(&array, "--cleanup=verbatim");
 
@@ -781,7 +785,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	}
 	if (!opts->no_commit)
 		res = sequencer_commit(opts->edit ? NULL : git_path_merge_msg(),
-			opts, allow, opts->edit, 0);
+			opts, allow, opts->edit, 0, 0);
 
 leave:
 	free_message(commit, &msg);
sequencer.h +2 -1
index 2106c0d..e272549 100644
@@ -50,7 +50,8 @@ int sequencer_rollback(struct replay_opts *opts);
 int sequencer_remove_state(struct replay_opts *opts);
 
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty, int edit, int amend);
+			  int allow_empty, int edit, int amend,
+			  int cleanup_commit_message);
 
 extern const char sign_off_header[];
 
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#110 lore raw

PATCH 20/22 sequencer: remember do_recursive_merge()'s return value

In reply to Johannes Schindelin

The return value of do_recursive_merge() may be positive (indicating merge conflicts), se let's OR later error conditions so as not to overwrite them with 0.

This is not yet a problem, but preparing for the patches to come: we will teach the sequencer to do rebase -i's job.

---
 sequencer.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
sequencer.c +6 -5
index 5ec956f..0614b90 100644
@@ -623,7 +623,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	const char *base_label, *next_label;
 	struct commit_message msg = { NULL, NULL, NULL, NULL };
 	struct strbuf msgbuf = STRBUF_INIT;
-	int res, unborn = 0, allow;
+	int res = 0, unborn = 0, allow;
 
 	if (opts->no_commit) {
 		/*
@@ -734,7 +734,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	}
 
 	if (!opts->strategy || !strcmp(opts->strategy, "recursive") || command == TODO_REVERT) {
-		res = do_recursive_merge(base, next, base_label, next_label,
+		res |= do_recursive_merge(base, next, base_label, next_label,
 					 head, &msgbuf, opts);
 		if (res < 0)
 			return res;
@@ -743,7 +743,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 		struct commit_list *common = NULL;
 		struct commit_list *remotes = NULL;
 
-		res = write_message(&msgbuf, git_path_merge_msg());
+		res |= write_message(&msgbuf, git_path_merge_msg());
 
 		commit_list_insert(base, &common);
 		commit_list_insert(next, &remotes);
@@ -780,11 +780,12 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 
 	allow = allow_empty(opts, commit);
 	if (allow < 0) {
-		res = allow;
+		res |= allow;
 		goto leave;
 	}
 	if (!opts->no_commit)
-		res = sequencer_commit(opts->edit ? NULL : git_path_merge_msg(),
+		res |= sequencer_commit(opts->edit ?
+				NULL : git_path_merge_msg(),
 			opts, allow, opts->edit, 0, 0);
 
 leave:
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#59 lore raw

PATCH 12/22 sequencer: refactor the code to obtain a short commit name

In reply to Johannes Schindelin
---
 sequencer.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
sequencer.c +8 -6
index ba1fd05..06759d4 100644
@@ -157,13 +157,18 @@ struct commit_message {
 	const char *message;
 };
 
+static const char *short_commit_name(struct commit *commit)
+{
+	return find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV);
+}
+
 static int get_message(struct commit *commit, struct commit_message *out)
 {
 	const char *abbrev, *subject;
 	int subject_len;
 
 	out->message = logmsg_reencode(commit, NULL, get_commit_output_encoding());
-	abbrev = find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV);
+	abbrev = short_commit_name(commit);
 
 	subject_len = find_commit_subject(out->message, &subject);
 
@@ -647,8 +652,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 		error(command == TODO_REVERT
 		      ? _("could not revert %s... %s")
 		      : _("could not apply %s... %s"),
-		      find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV),
-		      msg.subject);
+		      short_commit_name(commit), msg.subject);
 		print_advice(res == 1, opts);
 		rerere(opts->allow_rerere_auto);
 		goto leave;
@@ -880,9 +884,7 @@ static int walk_revs_populate_todo(struct todo_list *todo_list,
 		subject_len = find_commit_subject(commit_buffer, &subject);
 		strbuf_addf(&todo_list->buf, "%s %s %.*s\n",
 			opts->action == REPLAY_PICK ?  "pick" : "revert",
-			find_unique_abbrev(commit->object.oid.hash,
-				DEFAULT_ABBREV),
-			subject_len, subject);
+			short_commit_name(commit), subject_len, subject);
 		unuse_commit_buffer(commit, commit_buffer);
 	}
 	return 0;
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#122 lore raw

PATCH 22/22 sequencer: refactor write_message()

In reply to Johannes Schindelin

The write_message() function safely writes an strbuf to a file. Sometimes this is inconvenient, though: the text to be written may not be stored in a strbuf, or the strbuf should not be released after writing.

Let's allow for such use cases by refactoring write_message() to allow for a convenience function write_file_gently(). As some of the upcoming callers of that new function will want to append a newline character, let's just add a flag for that, too.

---
 sequencer.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
sequencer.c +18 -3
index 5efed2e..f5b5e5e 100644
@@ -239,22 +239,37 @@ static void print_advice(int show_hint, struct replay_opts *opts)
 	}
 }
 
-static int write_message(struct strbuf *msgbuf, const char *filename)
+static int write_with_lock_file(const char *filename,
+				const void *buf, size_t len, int append_eol)
 {
 	static struct lock_file msg_file;
 
 	int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0);
 	if (msg_fd < 0)
 		return error_errno(_("Could not lock '%s'"), filename);
-	if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0)
+	if (write_in_full(msg_fd, buf, len) < 0)
 		return error_errno(_("Could not write to %s"), filename);
-	strbuf_release(msgbuf);
+	if (append_eol && write(msg_fd, "\n", 1) < 0)
+		return error_errno(_("Could not write eol to %s"), filename);
 	if (commit_lock_file(&msg_file) < 0)
 		return error(_("Error wrapping up %s."), filename);
 
 	return 0;
 }
 
+static int write_message(struct strbuf *msgbuf, const char *filename)
+{
+	int res = write_with_lock_file(filename, msgbuf->buf, msgbuf->len, 0);
+	strbuf_release(msgbuf);
+	return res;
+}
+
+static int write_file_gently(const char *filename,
+			     const char *text, int append_eol)
+{
+	return write_with_lock_file(filename, text, strlen(text), append_eol);
+}
+
 /*
  * Reads a file that was presumably written by a shell script, i.e.
  * with an end-of-line marker that needs to be stripped.
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#115 lore raw

PATCH 21/22 sequencer: left-trim the lines read from the script

In reply to Johannes Schindelin

Interactive rebase's scripts may be indented; We need to handle this case, too, now that we prepare the sequencer to process interactive rebases.

---
 sequencer.c | 3 +++
 1 file changed, 3 insertions(+)
sequencer.c +3 -0
index 0614b90..5efed2e 100644
@@ -864,6 +864,9 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)
 	char *end_of_object_name;
 	int i, saved, status, padding;
 
+	/* left-trim */
+	bol += strspn(bol, " \t");
+
 	for (i = 0; i < ARRAY_SIZE(todo_command_strings); i++)
 		if (skip_prefix(bol, todo_command_strings[i], &bol)) {
 			item->command = i;
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#56 lore raw

PATCH 11/22 sequencer: get rid of the subcommand field

In reply to Johannes Schindelin

The subcommands are used exactly once, at the very beginning of sequencer_pick_revisions(), to determine what to do. This is an unnecessary level of indirection: we can simply call the correct function to begin with. So let's do that.

While at it, ensure that the subcommands return an error code so that they do not have to die() all over the place (bad practice for library functions...).

---
 builtin/revert.c | 36 ++++++++++++++++--------------------
 sequencer.c      | 35 +++++++++++------------------------
 sequencer.h      | 13 ++++---------
 3 files changed, 31 insertions(+), 53 deletions(-)
builtin/revert.c +16 -20
index 7365559..c9ae4dc 100644
@@ -71,7 +71,7 @@ static void verify_opt_compatible(const char *me, const char *base_opt, ...)
 		die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
 }
 
-static void parse_args(int argc, const char **argv, struct replay_opts *opts)
+static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
 {
 	const char * const * usage_str = revert_or_cherry_pick_usage(opts);
 	const char *me = action_name(opts);
@@ -115,25 +115,15 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
 	if (opts->keep_redundant_commits)
 		opts->allow_empty = 1;
 
-	/* Set the subcommand */
-	if (cmd == 'q')
-		opts->subcommand = REPLAY_REMOVE_STATE;
-	else if (cmd == 'c')
-		opts->subcommand = REPLAY_CONTINUE;
-	else if (cmd == 'a')
-		opts->subcommand = REPLAY_ROLLBACK;
-	else
-		opts->subcommand = REPLAY_NONE;
-
 	/* Check for incompatible command line arguments */
-	if (opts->subcommand != REPLAY_NONE) {
+	if (cmd) {
 		char *this_operation;
-		if (opts->subcommand == REPLAY_REMOVE_STATE)
+		if (cmd == 'q')
 			this_operation = "--quit";
-		else if (opts->subcommand == REPLAY_CONTINUE)
+		else if (cmd == 'c')
 			this_operation = "--continue";
 		else {
-			assert(opts->subcommand == REPLAY_ROLLBACK);
+			assert(cmd == 'a');
 			this_operation = "--abort";
 		}
 
@@ -156,7 +146,7 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
 				"--edit", opts->edit,
 				NULL);
 
-	if (opts->subcommand != REPLAY_NONE) {
+	if (cmd) {
 		opts->revs = NULL;
 	} else {
 		struct setup_revision_opt s_r_opt;
@@ -174,6 +164,14 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
 
 	if (argc > 1)
 		usage_with_options(usage_str, options);
+
+	if (cmd == 'q')
+		return sequencer_remove_state(opts);
+	if (cmd == 'c')
+		return sequencer_continue(opts);
+	if (cmd == 'a')
+		return sequencer_rollback(opts);
+	return sequencer_pick_revisions(opts);
 }
 
 int cmd_revert(int argc, const char **argv, const char *prefix)
@@ -185,8 +183,7 @@ int cmd_revert(int argc, const char **argv, const char *prefix)
 		opts.edit = 1;
 	opts.action = REPLAY_REVERT;
 	git_config(git_default_config, NULL);
-	parse_args(argc, argv, &opts);
-	res = sequencer_pick_revisions(&opts);
+	res = run_sequencer(argc, argv, &opts);
 	if (res < 0)
 		die(_("revert failed"));
 	return res;
@@ -199,8 +196,7 @@ int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
 
 	opts.action = REPLAY_PICK;
 	git_config(git_default_config, NULL);
-	parse_args(argc, argv, &opts);
-	res = sequencer_pick_revisions(&opts);
+	res = run_sequencer(argc, argv, &opts);
 	if (res < 0)
 		die(_("cherry-pick failed"));
 	return res;
sequencer.c +11 -24
index 1b65202..ba1fd05 100644
@@ -127,7 +127,7 @@ void *sequencer_entrust(struct replay_opts *opts, void *set_me_free_after_use)
 	return set_me_free_after_use;
 }
 
-static void remove_sequencer_state(const struct replay_opts *opts)
+int sequencer_remove_state(struct replay_opts *opts)
 {
 	struct strbuf dir = STRBUF_INIT;
 	int i;
@@ -141,6 +141,8 @@ static void remove_sequencer_state(const struct replay_opts *opts)
 	strbuf_addf(&dir, "%s", get_dir(opts));
 	remove_dir_recursively(&dir, 0);
 	strbuf_release(&dir);
+
+	return 0;
 }
 
 static const char *action_name(const struct replay_opts *opts)
@@ -941,7 +943,7 @@ static int rollback_single_pick(void)
 	return reset_for_rollback(head_sha1);
 }
 
-static int sequencer_rollback(struct replay_opts *opts)
+int sequencer_rollback(struct replay_opts *opts)
 {
 	FILE *f;
 	unsigned char sha1[20];
@@ -979,9 +981,8 @@ static int sequencer_rollback(struct replay_opts *opts)
 	}
 	if (reset_for_rollback(sha1))
 		goto fail;
-	remove_sequencer_state(opts);
 	strbuf_release(&buf);
-	return 0;
+	return sequencer_remove_state(opts);
 fail:
 	strbuf_release(&buf);
 	return -1;
@@ -1068,8 +1069,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
 	 * Sequence of picks finished successfully; cleanup by
 	 * removing the .git/sequencer directory
 	 */
-	remove_sequencer_state(opts);
-	return 0;
+	return sequencer_remove_state(opts);
 }
 
 static int continue_single_pick(void)
@@ -1082,11 +1082,14 @@ static int continue_single_pick(void)
 	return run_command_v_opt(argv, RUN_GIT_CMD);
 }
 
-static int sequencer_continue(struct replay_opts *opts)
+int sequencer_continue(struct replay_opts *opts)
 {
 	struct todo_list todo_list = TODO_LIST_INIT;
 	int res;
 
+	if (read_and_refresh_cache(opts))
+		return -1;
+
 	if (!file_exists(get_todo_path(opts)))
 		return continue_single_pick();
 	if (read_populate_opts(opts) ||
@@ -1121,26 +1124,10 @@ int sequencer_pick_revisions(struct replay_opts *opts)
 	unsigned char sha1[20];
 	int i, res;
 
-	if (opts->subcommand == REPLAY_NONE)
-		assert(opts->revs);
-
+	assert(opts->revs);
 	if (read_and_refresh_cache(opts))
 		return -1;
 
-	/*
-	 * Decide what to do depending on the arguments; a fresh
-	 * cherry-pick should be handled differently from an existing
-	 * one that is being continued
-	 */
-	if (opts->subcommand == REPLAY_REMOVE_STATE) {
-		remove_sequencer_state(opts);
-		return 0;
-	}
-	if (opts->subcommand == REPLAY_ROLLBACK)
-		return sequencer_rollback(opts);
-	if (opts->subcommand == REPLAY_CONTINUE)
-		return sequencer_continue(opts);
-
 	for (i = 0; i < opts->revs->pending.nr; i++) {
 		unsigned char sha1[20];
 		const char *name = opts->revs->pending.objects[i].name;
sequencer.h +4 -9
index 20b708a..674f11e 100644
@@ -10,16 +10,8 @@ enum replay_action {
 	REPLAY_PICK
 };
 
-enum replay_subcommand {
-	REPLAY_NONE,
-	REPLAY_REMOVE_STATE,
-	REPLAY_CONTINUE,
-	REPLAY_ROLLBACK
-};
-
 struct replay_opts {
 	enum replay_action action;
-	enum replay_subcommand subcommand;
 
 	/* Boolean options */
 	int edit;
@@ -48,11 +40,14 @@ struct replay_opts {
 	void **owned;
 	int owned_nr, owned_alloc;
 };
-#define REPLAY_OPTS_INIT { -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL, NULL, 0, 0 }
+#define REPLAY_OPTS_INIT { -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL, NULL, 0, 0 }
 
 void *sequencer_entrust(struct replay_opts *opts, void *set_me_free_after_use);
 
 int sequencer_pick_revisions(struct replay_opts *opts);
+int sequencer_continue(struct replay_opts *opts);
+int sequencer_rollback(struct replay_opts *opts);
+int sequencer_remove_state(struct replay_opts *opts);
 
 extern const char sign_off_header[];
 
Signature · 1 line
2.10.0.rc1.114.g2bd6b38
#2 lore raw

Re: [PATCH 01/22] sequencer: use static initializers for replay_opts

In reply to Johannes Schindelin
Quoted 1 line · Johannes Schindelin

+#define REPLAY_OPTS_INIT { -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL }

This looked off to me, as it replaces memset(..., 0, ...) so is not 100% equivalent. But the changed functions both set opts.action and call parse_args which sets opts.subcommand.

D.

#13 lore raw

Re: [PATCH 04/22] sequencer: future-proof remove_sequencer_state()

In reply to Johannes Schindelin
Quoted 3 lines · Johannes Schindelin

+       if (read_and_refresh_cache(opts))
+               return -1;
+

This doesn't seem to be related to the get_dir changes?

D.

#60 lore raw

Re: [PATCH 12/22] sequencer: refactor the code to obtain a short commit name

In reply to Johannes Schindelin

On ma, 2016-08-29 at 10:05 +0200, Johannes Schindelin wrote:

<snip actual commit>

I fail to see the point of this patch, would you mind enlightening me?

D.

#89 lore raw

Re: [PATCH 15/22] sequencer: introduce a helper to read files written by scripts

In reply to Johannes Schindelin
Quoted 11 lines · Johannes Schindelin

+       if (strbuf_read_file(buf, path, 0) < 0) {
+               warning_errno("could not read '%s'", path); +               return 0;
+       }
+
+       if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') { +               if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r')
+                       --buf->len;
+               buf->buf[buf->len] = '\0';
+       }

Why not use open + strbuf_getline instead of hand-rolling a newline eradicator?

D.

#111 lore raw

Re: [PATCH 20/22] sequencer: remember do_recursive_merge()'s return value

In reply to Johannes Schindelin
Quoted 3 lines · Johannes Schindelin

The return value of do_recursive_merge() may be positive (indicating merge conflicts), se let's OR later error conditions so as not to overwrite them with 0.

s/se/so/?

D.

#3 lore raw

Re: [PATCH 01/22] sequencer: use static initializers for replay_opts

In reply to Dennis Kaarsemaker

Hi Dennis,

Quoted 7 lines · Dennis Kaarsemaker
Quoted 1 line · Johannes Schindelin

+#define REPLAY_OPTS_INIT { -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL }

This looked off to me, as it replaces memset(..., 0, ...) so is not 100% equivalent. But the changed functions both set opts.action and call parse_args which sets opts.subcommand.

Okay... Do you want me to change anything?

Ciao,
Dscho

#14 lore raw

Re: [PATCH 04/22] sequencer: future-proof remove_sequencer_state()

In reply to Dennis Kaarsemaker

Hi Dennis,

Quoted 7 lines · Dennis Kaarsemaker
Quoted 3 lines · Johannes Schindelin

+       if (read_and_refresh_cache(opts))
+               return -1;
+

This doesn't seem to be related to the get_dir changes?

Good eyes.

Let me investigate why I have it here...

Ciao,
Dscho

#61 lore raw

Re: [PATCH 12/22] sequencer: refactor the code to obtain a short commit name

In reply to Dennis Kaarsemaker

Hi Dennis,

Quoted 5 lines · Dennis Kaarsemaker

On ma, 2016-08-29 at 10:05 +0200, Johannes Schindelin wrote:

<snip actual commit>

I fail to see the point of this patch, would you mind enlightening me?

Two reasons:

1) by refactoring it into a function, the code is more DRY (with all the advantages that come with it, such as: only a single point to change if changing the behavior)

2) it is easier to reuse the code in upcoming patches (that would be in the next patch series)

Will amend the commit message.

Ciao,
Dscho

#90 lore raw

Re: [PATCH 15/22] sequencer: introduce a helper to read files written by scripts

In reply to Dennis Kaarsemaker

Hi Dennis,

Quoted 13 lines · Dennis Kaarsemaker
Quoted 11 lines · Johannes Schindelin

+       if (strbuf_read_file(buf, path, 0) < 0) {
+               warning_errno("could not read '%s'", path); +               return 0;
+       }
+
+       if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') { +               if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r')
+                       --buf->len;
+               buf->buf[buf->len] = '\0';
+       }

Quoted 2 lines

Why not use open + strbuf_getline instead of hand-rolling a newline eradicator?

Because strbuf_getline() erases the strbuf instead of appending to it (which is what we sometimes need when converting shell scripts to C).

Ciao,
Dscho

#112 lore raw

Re: [PATCH 20/22] sequencer: remember do_recursive_merge()'s return value

In reply to Dennis Kaarsemaker

Hi Dennis,

Quoted 7 lines · Dennis Kaarsemaker
Quoted 3 lines · Johannes Schindelin

The return value of do_recursive_merge() may be positive (indicating merge conflicts), se let's OR later error conditions so as not to overwrite them with 0.

s/se/so/?

Good eyes.

Fixed,
Dscho

#130 lore raw
In reply to Dennis Kaarsemaker

Hi Dennis,

Quoted 6 lines · Dennis Kaarsemaker
Quoted 1 line · Johannes Schindelin

Therefore I would be most grateful for every in-depth review.

Tried to do that, but could come up only with a few nits. I think the approach is sensible.

Thank you for the review!

Ciao,
Dscho

#15 lore raw

Re: [PATCH 04/22] sequencer: future-proof remove_sequencer_state()

In reply to Johannes Schindelin

Hi Dennis,

Quoted 8 lines · Johannes Schindelin
Quoted 6 lines · Dennis Kaarsemaker
Quoted 3 lines · Johannes Schindelin

+       if (read_and_refresh_cache(opts))
+               return -1;
+

Quoted 5 lines
Quoted 1 line

This doesn't seem to be related to the get_dir changes?

Good eyes.

Let me investigate why I have it here...

Unfortunately my reflogs got corrupted by the git-worktree implementations, so I cannot back that far.

Looking at the code, and after running the tests, I am convinced that it is a leftover of some misguided attempt to implement "git rebase -i --abort" in sequencer_rollback().

I removed this hunk from the patch.

Again, Thank you so much for your review!
Dscho

#4 lore raw

Re: [PATCH 01/22] sequencer: use static initializers for replay_opts

In reply to Dennis Kaarsemaker

W dniu 29.08.2016 o 11:19, Dennis Kaarsemaker pisze:

Quoted 7 lines · Johannes Schindelin
Quoted 2 lines

+#define REPLAY_OPTS_INIT { -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL }

This looked off to me, as it replaces memset(..., 0, ...) so is not 100% equivalent. But the changed functions both set opts.action and call parse_args which sets opts.subcommand.

This information would be nice to have in the commit message.

Signature · 1 line
Jakub Narębski
#7 lore raw

Re: [PATCH 02/22] sequencer: use memoized sequencer directory path

In reply to Johannes Schindelin

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

Quoted 6 lines
---
 builtin/commit.c |  2 +-
 sequencer.c      | 11 ++++++-----
 sequencer.h      |  5 +----
 3 files changed, 8 insertions(+), 10 deletions(-)

Just a sidenote: it would be probably easier to read with *.h before *.c (at least this particular one).

builtin/commit.c static void determine_whence(struct wt_status *s) · line 179

Quoted 13 lines
builtin/commit.c +1 -1
index 77e3dc8..0221190 100644
@@ -173,7 +173,7 @@ static void determine_whence(struct wt_status *s)
 		whence = FROM_MERGE;
 	else if (file_exists(git_path_cherry_pick_head())) {
 		whence = FROM_CHERRY_PICK;
-		if (file_exists(git_path(SEQ_DIR)))
+		if (file_exists(git_path_seq_dir()))
 			sequencer_in_use = 1;
 	}
 	else

So it is more "Use memoized sequencer directory path" rather than "sequencer: use memoized sequencer directory path" - it replaces all occurrences of SEQ_DIR,... that's why it can be removed from 'sequencer.h'.

Though perhaps I misunderstood "sequencer: " prefix there. Don't mind me then.

Quoted 7 lines
sequencer.c +0 -0
index b6481bb..4d2b4e3 100644
@@ -21,10 +21,11 @@
 const char sign_off_header[] = "Signed-off-by: ";
 static const char cherry_picked_prefix[] = "(cherry picked from commit ";

sequencer.c line 28

Quoted 10 lines
sequencer.c +5 -4
 
-static GIT_PATH_FUNC(git_path_todo_file, SEQ_TODO_FILE)
-static GIT_PATH_FUNC(git_path_opts_file, SEQ_OPTS_FILE)
-static GIT_PATH_FUNC(git_path_seq_dir, SEQ_DIR)
-static GIT_PATH_FUNC(git_path_head_file, SEQ_HEAD_FILE)
+GIT_PATH_FUNC(git_path_seq_dir, "sequencer")
+
+static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo")
+static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts")
+static GIT_PATH_FUNC(git_path_head_file, "sequencer/head")

This also makes the ordering of memoized-path variables more sensible. Good work.

Quoted 17 lines
sequencer.c +1 -1
 static int is_rfc2822_line(const char *buf, int len)
 {
@@ -112,7 +113,7 @@ static void remove_sequencer_state(void)
 {
 	struct strbuf seq_dir = STRBUF_INIT;
 
-	strbuf_addstr(&seq_dir, git_path(SEQ_DIR));
+	strbuf_addstr(&seq_dir, git_path_seq_dir());
 	remove_dir_recursively(&seq_dir, 0);
 	strbuf_release(&seq_dir);
 }
sequencer.h +0 -0
index 2ca096b..c955594 100644

sequencer.h line 4

Quoted 8 lines
sequencer.h +1 -4
@@ -1,10 +1,7 @@
 #ifndef SEQUENCER_H
 #define SEQUENCER_H
 
-#define SEQ_DIR		"sequencer"
-#define SEQ_HEAD_FILE	"sequencer/head"
-#define SEQ_TODO_FILE	"sequencer/todo"
-#define SEQ_OPTS_FILE	"sequencer/opts"
+const char *git_path_seq_dir(void);

Right, I see this matches other git_path_*() functions declared in cache.h

Quoted 4 lines
sequencer.h +0 -0
 #define APPEND_SIGNOFF_DEDUP (1u << 0)
 
 
#10 lore raw

Re: [PATCH 03/22] sequencer: avoid unnecessary indirection

In reply to Johannes Schindelin

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

Quoted 2 lines

We really do not need the *pointer to a* pointer to the options in the read_populate_opts() function.

Right.

Quoted 10 lines
---
 sequencer.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
sequencer.c +0 -0
index 4d2b4e3..14ef79b 100644

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 812

Quoted 5 lines
sequencer.c +1 -1
@@ -809,11 +809,11 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
 	return 0;
 }
 
-static int read_populate_opts(struct replay_opts **opts)
+static int read_populate_opts(struct replay_opts *opts)

Especially that other *_populate_*() use 'struct replay_opts *opts':

read_populate_todo(struct commit_list **todo_list, struct replay_opts *opts) walk_revs_populate_todo(struct commit_list **todo_list, struct replay_opts *opts)

Though they use **todo_list, because they modify this list; maybe that was why read_populate_opts was using **opts instead of *opts?

Quoted 18 lines
sequencer.c +2 -2
 {
 	if (!file_exists(git_path_opts_file()))
 		return 0;
-	if (git_config_from_file(populate_opts_cb, git_path_opts_file(), *opts) < 0)
+	if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0)
 		return error(_("Malformed options sheet: %s"),
 			git_path_opts_file());
 	return 0;
@@ -1038,7 +1038,7 @@ static int sequencer_continue(struct replay_opts *opts)
 
 	if (!file_exists(git_path_todo_file()))
 		return continue_single_pick();
-	if (read_populate_opts(&opts) ||
+	if (read_populate_opts(opts) ||
 			read_populate_todo(&todo_list, opts))
 		return -1;
 
#113 lore raw

Re: [PATCH 20/22] sequencer: remember do_recursive_merge()'s return value

In reply to Johannes Schindelin

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

sequencer.c static int do_pick_commit(enum todo_command command, struct commit *commit, · line 626

Quoted 10 lines
sequencer.c +1 -1
index 5ec956f..0614b90 100644
@@ -623,7 +623,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	const char *base_label, *next_label;
 	struct commit_message msg = { NULL, NULL, NULL, NULL };
 	struct strbuf msgbuf = STRBUF_INIT;
-	int res, unborn = 0, allow;
+	int res = 0, unborn = 0, allow;

Not that I am against this part of change, making initialization explicit, but why we are initializing automatic variables with 0, which would be the default value anyway? I thought our coding guidelines discourage initializing with 0 or NULL...

Puzzled,

Signature · 1 line
Jakub Narębski
#114 lore raw

Re: [PATCH 20/22] sequencer: remember do_recursive_merge()'s return value

Quoted 16 lines · Jakub Narębski

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

sequencer.c static int do_pick_commit(enum todo_command command, struct commit *commit, · line 626

Quoted 10 lines
sequencer.c +1 -1
index 5ec956f..0614b90 100644
@@ -623,7 +623,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	const char *base_label, *next_label;
 	struct commit_message msg = { NULL, NULL, NULL, NULL };
 	struct strbuf msgbuf = STRBUF_INIT;
-	int res, unborn = 0, allow;
+	int res = 0, unborn = 0, allow;

Not that I am against this part of change, making initialization explicit, but why we are initializing automatic variables with 0, which would be the default value anyway?

Because an on-stack "auto" begins its life with an undefined value, unlike a file-scope static (and global variables) that can be in BSS segment.

Quoted 2 lines

I thought our coding
guidelines discourage initializing with 0 or NULL...

You are confused between the two, I am afraid.

#82 lore raw

Re: [PATCH 14/22] sequencer: prepare for rebase -i's commit functionality

In reply to Johannes Schindelin

sequencer.c static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo") · line 38

Quoted 2 lines · Johannes Schindelin
sequencer.c +2 -0
+/* We will introduce the 'interactive rebase' mode later */
+#define IS_REBASE_I() 0

I do not see a point in naming this all caps. The use site would be a lot more pleasant to read when the reader does not have to care if this is implemented as a preprocessor macro or a helper function.

Quoted 13 lines
sequencer.c +9 -0
@@ -377,20 +387,72 @@ static int is_index_unchanged(void)
 	return !hashcmp(active_cache_tree->sha1, head_commit->tree->object.oid.hash);
 }
 
+static char **read_author_script(void)
+{
+	struct strbuf script = STRBUF_INIT;
+	int i, count = 0;
+	char *p, *p2, **env;
+	size_t env_size;
+
+	if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0)
+		return NULL;

sequencer.c static int is_index_unchanged(void) · line 408

Quoted 10 lines
sequencer.c +10 -0
+
+	for (p = script.buf; *p; p++)
+		if (skip_prefix(p, "'\\\\''", (const char **)&p2))
+			strbuf_splice(&script, p - script.buf, p2 - p, "'", 1);
+		else if (*p == '\'')
+			strbuf_splice(&script, p-- - script.buf, 1, "", 0);
+		else if (*p == '\n') {
+			*p = '\0';
+			count++;
+		}

Hmph. What is this loop doing? Is it decoding a sq-quoted buffer or something? Don't we have a helper function to do that?

Quoted 14 lines
sequencer.c +14 -0
+	env_size = (count + 1) * sizeof(*env);
+	strbuf_grow(&script, env_size);
+	memmove(script.buf + env_size, script.buf, script.len);
+	p = script.buf + env_size;
+	env = (char **)strbuf_detach(&script, NULL);
+
+	for (i = 0; i < count; i++) {
+		env[i] = p;
+		p += strlen(p) + 1;
+	}
+	env[count] = NULL;
+
+	return env;
+}

sequencer.c static int is_index_unchanged(void) · line 432

Quoted 10 lines
sequencer.c +3 -1
+
 /*
  * If we are cherry-pick, and if the merge did not result in
  * hand-editing, we will hit this commit and inherit the original
  * author date and name.
  * If we are revert, or if our cherry-pick results in a hand merge,
- * we had better say that the current user is responsible for that.
+ * we had better say that the current user is responsible for that
+ * (except, of course, while running an interactive rebase).
  */

The added "(except, ...)" reads as if "even if we are reverting, if that is done as part of an interactive rebase, the authorship rule for a revert does not apply".

If that is not what you meant, i.e. if you did not mean to imply that "rebase -i" doing a revert is a normal thing, this needs to be rephrased to avoid the misinterpretation.

#17 lore raw

Re: [PATCH 05/22] sequencer: allow the sequencer to take custody of malloc()ed data

In reply to Johannes Schindelin

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

Quoted 11 lines

The sequencer is our attempt to lib-ify cherry-pick. Yet it behaves like a one-shot command when it reads its configuration: memory is allocated and released only when the command exits.

This is kind of okay for git-cherry-pick, which *is* a one-shot command. All the work to make the sequencer its work horse was done to allow using the functionality as a library function, though, including proper clean-up after use.

This patch introduces an API to pass the responsibility of releasing certain memory to the sequencer.

So how this API would be / is meant to be used? From the following patches (which I shouldn't have to read to understand this one) it looks like it is about strdup'ed strings from option parsing. Or would there be something more in the future?

Would sequencer as a library function be called multiple times, or only once?

I'm trying to find out how this is solved in other places of Git code, and I have stumbled upon free_util in string_list...

Quoted 12 lines
---
 sequencer.c | 13 +++++++++++++
 sequencer.h |  8 +++++++-
 2 files changed, 20 insertions(+), 1 deletion(-)
sequencer.c +0 -0
index c4b223b..b5be0f9 100644

sequencer.c static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, · line 122

Quoted 9 lines
sequencer.c +6 -0
@@ -114,9 +114,22 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
 	return 1;
 }
 
+void *sequencer_entrust(struct replay_opts *opts, void *set_me_free_after_use)
+{
+	ALLOC_GROW(opts->owned, opts->owned_nr + 1, opts->owned_alloc);
+	opts->owned[opts->owned_nr++] = set_me_free_after_use;
+
+	return set_me_free_after_use;

I was wondering what this 'set_me_free_after_use' parameter is about; wouldn't it be more readable if this parameter was called 'owned_data' or 'owned_ptr'?

sequencer.c static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, · line 131

Quoted 9 lines
sequencer.c +6 -0
+}
+
 static void remove_sequencer_state(const struct replay_opts *opts)
 {
 	struct strbuf dir = STRBUF_INIT;
+	int i;
+
+	for (i = 0; i < opts->owned_nr; i++)
+		free(opts->owned[i]);

I guess you can remove owned data in any order, regardless if you store struct or its members first...

Quoted 9 lines
sequencer.c +1 -0
+	free(opts->owned);
 
 	strbuf_addf(&dir, "%s", get_dir(opts));
 	remove_dir_recursively(&dir, 0);
sequencer.h +0 -0
index c955594..20b708a 100644

sequencer.h struct replay_opts { · line 49

Quoted 7 lines
sequencer.h +4 -0
@@ -43,8 +43,14 @@ struct replay_opts {
 
 	/* Only used by REPLAY_NONE */
 	struct rev_info *revs;
+
+	/* malloc()ed data entrusted to the sequencer */
+	void **owned;
+	int owned_nr, owned_alloc;

I'm not sure about naming conventions for those types of data, but wouldn't 'owned_data' be a better name? I could be wrong here...

sequencer.h struct replay_opts { · line 51

Quoted 3 lines
sequencer.h +1 -1
 };
-#define REPLAY_OPTS_INIT { -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL }
+#define REPLAY_OPTS_INIT { -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL, NULL, 0, 0 }

Nb. it is a pity that we cannot use named initializers for structs, so called designated inits. It would make this macro more readable.

Quoted 6 lines
sequencer.h +2 -0
+
+void *sequencer_entrust(struct replay_opts *opts, void *set_me_free_after_use);
 
 int sequencer_pick_revisions(struct replay_opts *opts);
 
#18 lore raw

Re: [PATCH 05/22] sequencer: allow the sequencer to take custody of malloc()ed data

Am 29.08.2016 um 23:59 schrieb Jakub Narębski:

Quoted 6 lines

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

sequencer.h struct replay_opts { · line 51

Quoted 2 lines
sequencer.h +1 -1
-#define REPLAY_OPTS_INIT { -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL }
+#define REPLAY_OPTS_INIT { -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL, NULL, 0, 0 }

Nb. it is a pity that we cannot use named initializers for structs, so called designated inits. It would make this macro more readable.

It is actually pointless to add the 0's and NULL's here. This should be sufficient:

#define REPLAY_OPTS_INIT { -1, -1 }

because initialization with 0 (or NULL) is the default for any omitted members.

-- Hannes

#5 lore raw

Re: [PATCH 01/22] sequencer: use static initializers for replay_opts

In reply to Jakub Narębski

Hi Kuba,

Quoted 10 lines · Jakub Narębski

W dniu 29.08.2016 o 11:19, Dennis Kaarsemaker pisze:

Quoted 7 lines · Johannes Schindelin
Quoted 2 lines

+#define REPLAY_OPTS_INIT { -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL }

This looked off to me, as it replaces memset(..., 0, ...) so is not 100% equivalent. But the changed functions both set opts.action and call parse_args which sets opts.subcommand.

This information would be nice to have in the commit message.

Clarified in the commit message.

Ciao,
Johannes

#8 lore raw

Re: [PATCH 02/22] sequencer: use memoized sequencer directory path

In reply to Jakub Narębski

Hi Kuba,

Quoted 10 lines · Jakub Narębski

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

Quoted 6 lines
---
 builtin/commit.c |  2 +-
 sequencer.c      | 11 ++++++-----
 sequencer.h      |  5 +----
 3 files changed, 8 insertions(+), 10 deletions(-)

Just a sidenote: it would be probably easier to read with *.h before *.c (at least this particular one).

I agree, but I did not find any way to reorder this without substantial manual work...

Quoted 19 lines

builtin/commit.c static void determine_whence(struct wt_status *s) · line 179

Quoted 13 lines
builtin/commit.c +1 -1
index 77e3dc8..0221190 100644
@@ -173,7 +173,7 @@ static void determine_whence(struct wt_status *s)
 		whence = FROM_MERGE;
 	else if (file_exists(git_path_cherry_pick_head())) {
 		whence = FROM_CHERRY_PICK;
-		if (file_exists(git_path(SEQ_DIR)))
+		if (file_exists(git_path_seq_dir()))
 			sequencer_in_use = 1;
 	}
 	else

So it is more "Use memoized sequencer directory path" rather than "sequencer: use memoized sequencer directory path" - it replaces all occurrences of SEQ_DIR,... that's why it can be removed from 'sequencer.h'.

Quoted 2 lines

Though perhaps I misunderstood "sequencer: " prefix there. Don't mind me then.

The idea is that this path is declared and defined in the sequencer. There are other call sites, too, so they have to be changed at the same time...

I'd really like to keep the "sequencer:" prefix because it is semantically
correct: this change is about the sequencer, not about the other call sites.

Ciao,
Johannes

#11 lore raw

Re: [PATCH 03/22] sequencer: avoid unnecessary indirection

In reply to Jakub Narębski

Hi Kuba,

Quoted 25 lines · Jakub Narębski

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

Quoted 2 lines

We really do not need the *pointer to a* pointer to the options in the read_populate_opts() function.

Right.

Quoted 10 lines
---
 sequencer.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
sequencer.c +0 -0
index 4d2b4e3..14ef79b 100644

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 812

Quoted 5 lines
sequencer.c +1 -1
@@ -809,11 +809,11 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
 	return 0;
 }
 
-static int read_populate_opts(struct replay_opts **opts)
+static int read_populate_opts(struct replay_opts *opts)

Especially that other *_populate_*() use 'struct replay_opts *opts':

Quoted 6 lines

read_populate_todo(struct commit_list **todo_list, struct replay_opts *opts) walk_revs_populate_todo(struct commit_list **todo_list, struct replay_opts *opts)

Though they use **todo_list, because they modify this list; maybe that was why read_populate_opts was using **opts instead of *opts?

I won't speculate about the reasons why it was made so.

About read_populate_todo(): it uses **todo_list, but still only *opts.

In any case, in a later patch, the todo_list parsing is completely revamped anyway, so I did not want to "fix" anything that would get reverted later on.

Ciao,
Johannes

#83 lore raw

Re: [PATCH 14/22] sequencer: prepare for rebase -i's commit functionality

In reply to Junio C Hamano

Hi Junio,

Quoted 6 lines · Junio C Hamano

sequencer.c static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo") · line 38

Quoted 2 lines · Johannes Schindelin
sequencer.c +2 -0
+/* We will introduce the 'interactive rebase' mode later */
+#define IS_REBASE_I() 0

I do not see a point in naming this all caps.

Old habit. Macros are all-caps.

Quoted 3 lines

The use site would be a lot more pleasant to read when the reader does not have to care if this is implemented as a preprocessor macro or a helper function.

I converted this to a helper function.

Quoted 18 lines
Quoted 18 lines
sequencer.c +14 -0
@@ -377,20 +387,72 @@ static int is_index_unchanged(void)
 	return !hashcmp(active_cache_tree->sha1, head_commit->tree->object.oid.hash);
 }
 
+static char **read_author_script(void)
+{
+	struct strbuf script = STRBUF_INIT;
+	int i, count = 0;
+	char *p, *p2, **env;
+	size_t env_size;
+
+	if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0)
+		return NULL;
+
+	for (p = script.buf; *p; p++)
+		if (skip_prefix(p, "'\\\\''", (const char **)&p2))
+			strbuf_splice(&script, p - script.buf, p2 - p, "'", 1);
+		else if (*p == '\'')
Quoted 8 lines

sequencer.c static int is_index_unchanged(void) · line 408

Quoted 5 lines
sequencer.c +5 -0
+			strbuf_splice(&script, p-- - script.buf, 1, "", 0);
+		else if (*p == '\n') {
+			*p = '\0';
+			count++;
+		}

Hmph. What is this loop doing? Is it decoding a sq-quoted buffer or something? Don't we have a helper function to do that?

Well, it is not just decoding an sq-quoted buffer, but several lines with definitions we sq-quoted ourselves, individually.

The quote.[ch] code currently has no code to dequote lines individually.

At this point, I would prefer to keep this code as-is, as I tested it over the course of months and do not want to introduce regressions *now*.

Quoted 29 lines
Quoted 14 lines
sequencer.c +14 -0
+	env_size = (count + 1) * sizeof(*env);
+	strbuf_grow(&script, env_size);
+	memmove(script.buf + env_size, script.buf, script.len);
+	p = script.buf + env_size;
+	env = (char **)strbuf_detach(&script, NULL);
+
+	for (i = 0; i < count; i++) {
+		env[i] = p;
+		p += strlen(p) + 1;
+	}
+	env[count] = NULL;
+
+	return env;
+}

sequencer.c static int is_index_unchanged(void) · line 432

Quoted 10 lines
sequencer.c +3 -1
+
 /*
  * If we are cherry-pick, and if the merge did not result in
  * hand-editing, we will hit this commit and inherit the original
  * author date and name.
  * If we are revert, or if our cherry-pick results in a hand merge,
- * we had better say that the current user is responsible for that.
+ * we had better say that the current user is responsible for that
+ * (except, of course, while running an interactive rebase).
  */

The added "(except, ...)" reads as if "even if we are reverting, if that is done as part of an interactive rebase, the authorship rule for a revert does not apply".

Quoted 3 lines

If that is not what you meant, i.e. if you did not mean to imply that "rebase -i" doing a revert is a normal thing, this needs to be rephrased to avoid the misinterpretation.

I rephrased it.

Ciao,
Dscho

#20 lore raw

Re: [PATCH 05/22] sequencer: allow the sequencer to take custody of malloc()ed data

In reply to Jakub Narębski

Hi Kuba,

Quoted 11 lines · Jakub Narębski

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

Quoted 9 lines

The sequencer is our attempt to lib-ify cherry-pick. Yet it behaves like a one-shot command when it reads its configuration: memory is allocated and released only when the command exits.

This is kind of okay for git-cherry-pick, which *is* a one-shot command. All the work to make the sequencer its work horse was done to allow using the functionality as a library function, though, including proper clean-up after use.

Quoted 4 lines
Quoted 2 lines

This patch introduces an API to pass the responsibility of releasing certain memory to the sequencer.

So how this API would be / is meant to be used?

I added an example to the commit message.

Quoted 2 lines

Would sequencer as a library function be called multiple times, or only once?

The point of a library function is that it should not care.

Quoted 2 lines

I'm trying to find out how this is solved in other places of Git code, and I have stumbled upon free_util in string_list...

I wanted this to be flexible enough to take care of any type of data, not just strings.

And while the string_list has a void *util field, it would be rather silly to add strings to a string list for the sole purpose of free()ing their util fields in the end.

(That was the conclusion I came to after a search of my own.)

Quoted 10 lines

sequencer.c static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, · line 122

Quoted 6 lines
sequencer.c +6 -0
+void *sequencer_entrust(struct replay_opts *opts, void *set_me_free_after_use)
+{
+	ALLOC_GROW(opts->owned, opts->owned_nr + 1, opts->owned_alloc);
+	opts->owned[opts->owned_nr++] = set_me_free_after_use;
+
+	return set_me_free_after_use;

I was wondering what this 'set_me_free_after_use' parameter is about; wouldn't it be more readable if this parameter was called 'owned_data' or 'owned_ptr'?

If I read "owned_ptr" as a function's parameter, I would assume that the associated memory is owned by the caller. So I would be puzzled reading that name.

Quoted 10 lines

sequencer.c static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, · line 131

Quoted 7 lines

static void remove_sequencer_state(const struct replay_opts *opts) {

sequencer.c +4 -0
 	struct strbuf dir = STRBUF_INIT;
+	int i;
+
+	for (i = 0; i < opts->owned_nr; i++)
+		free(opts->owned[i]);

I guess you can remove owned data in any order, regardless if you store struct or its members first...

Indeed, this is not like a C++ destructor. It's free().

Quoted 6 lines
Quoted 6 lines
sequencer.h +0 -0
index c955594..20b708a 100644
@@ -43,8 +43,14 @@ struct replay_opts {
 
Quoted 9 lines

sequencer.h struct replay_opts { · line 49

Quoted 6 lines
sequencer.h +4 -0
 	/* Only used by REPLAY_NONE */
 	struct rev_info *revs;
+
+	/* malloc()ed data entrusted to the sequencer */
+	void **owned;
+	int owned_nr, owned_alloc;

I'm not sure about naming conventions for those types of data, but wouldn't 'owned_data' be a better name? I could be wrong here...

The convention seemed to be "void *X; int X_nr, X_alloc;", so I stuck with it.

Thanks for your review!
Johannes

#19 lore raw

Re: [PATCH 05/22] sequencer: allow the sequencer to take custody of malloc()ed data

In reply to Johannes Sixt

Hi Hannes,

Quoted 10 lines · Johannes Sixt

Am 29.08.2016 um 23:59 schrieb Jakub Narębski:

Quoted 8 lines

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

Quoted 4 lines

-#define REPLAY_OPTS_INIT { -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL }
+#define REPLAY_OPTS_INIT { -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL, NULL, 0, 0 }

Nb. it is a pity that we cannot use named initializers for structs, so called designated inits. It would make this macro more readable.

Quoted 7 lines

It is actually pointless to add the 0's and NULL's here. This should be sufficient:

#define REPLAY_OPTS_INIT { -1, -1 }

because initialization with 0 (or NULL) is the default for any omitted members.

D'oh. You're right. The same applies to TODO_LIST_INIT, of course.

Fixed,
Johannes

#21 lore raw

Re: [PATCH 05/22] sequencer: allow the sequencer to take custody of malloc()ed data

In reply to Johannes Schindelin

Hello Johannes,

W dniu 30.08.2016 o 09:29, Johannes Schindelin pisze:

Quoted 14 lines · Jakub Narębski
Quoted 12 lines

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

sequencer.c static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, · line 122

Quoted 6 lines
sequencer.c +6 -0
+void *sequencer_entrust(struct replay_opts *opts, void *set_me_free_after_use)
+{
+	ALLOC_GROW(opts->owned, opts->owned_nr + 1, opts->owned_alloc);
+	opts->owned[opts->owned_nr++] = set_me_free_after_use;
+
+	return set_me_free_after_use;

I was wondering what this 'set_me_free_after_use' parameter is about; wouldn't it be more readable if this parameter was called 'owned_data' or 'owned_ptr'?

Quoted 3 lines

If I read "owned_ptr" as a function's parameter, I would assume that the associated memory is owned by the caller. So I would be puzzled reading that name.

Right. Well, it is difficult to come up with a good name for this parameter that would make sense both in a declaration as an information for a caller, and in the function itself as information about what it holds.

In my personal opinion 'set_me_free_after_use' is not the best name, but I unfortunately do not have a better proposal. Maybe 'entrust_ptr', or 'entrusted_data' / 'entrusted_ptr' / 'entrusted'?

There are two hard things in computer science: cache invalidation, *naming things*, and off-by-one errors ;-)

P.S. It would be nice to have generic mechanism for taking custody of data to help libification, either at this or at lower level (on the level of xstrdup, etc.), but that can safely wait. It even should wait, so that we can see that this approach is a good one, before trying to generalize it. That should be not a blocker for this series, IMVHO.

Best,

Signature · 1 line
Jakub Narębski
#25 lore raw

Re: [PATCH 06/22] sequencer: release memory that was allocated when reading options

In reply to Johannes Schindelin

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

Quoted 16 lines

The sequencer reads options from disk and stores them in its struct for use during sequencer's operations.

With this patch, the memory is released afterwards, plugging a memory leak.

---
 sequencer.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
sequencer.c +0 -0
index b5be0f9..8d79091 100644

sequencer.c static void remove_sequencer_state(const struct replay_opts *opts) · line 135

Quoted 5 lines
sequencer.c +2 -0
@@ -131,6 +131,8 @@ static void remove_sequencer_state(const struct replay_opts *opts)
 		free(opts->owned[i]);
 	free(opts->owned);
 
+	free(opts->xopts);
+

This looks like independent change, not related to using the sequencer_entrust() to store options read from disk in replay_opts struct to be able to free memory afterwards.

I guess you wanted to avoid one line changes...

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data)

Quoted 4 lines
sequencer.c +0 -0
 	strbuf_addf(&dir, "%s", get_dir(opts));
 	remove_dir_recursively(&dir, 0);
 	strbuf_release(&dir);
@@ -811,13 +813,18 @@ static int populate_opts_cb(const char *key, const char *value, void *data)

Sidenote: this patch would be easier to read if lines were reordered as below, but I don't think any slider heuristics could help achieve that automatically. Also, the patch might be invalid...

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 818

Quoted 7 lines
sequencer.c +2 -1
 		opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
 	else if (!strcmp(key, "options.mainline"))
 		opts->mainline = git_config_int(key, value);
-	else if (!strcmp(key, "options.strategy"))
+	else if (!strcmp(key, "options.strategy")) {
 		git_config_string(&opts->strategy, key, value);
+		sequencer_entrust(opts, (char *) opts->strategy);

I wonder if the ability to free strings dup-ed by git_config_string() be something that is part of replay_opts, or rather remove_sequencer_state(), that is a list of

	free(opts->strategy);
	free(opts->gpg_sign);

And of course

	for (i = 0; i < opts->xopts_nr; i++)
		free(opts->xopts[i]);
	free(opts->xopts);

Though... free(NULL) is nop as per standard, but can we rely on it? If it is a problem, we can create xfree(ptr) being if(ptr)free(ptr);

The *_entrust() mechanism is more generic, but do we use this general-ness? Well, it could be xstrdup or git_config_string doing entrust'ing...

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 827

Quoted 11 lines
sequencer.c +6 -2
+	}
-	else if (!strcmp(key, "options.gpg-sign"))
+	else if (!strcmp(key, "options.gpg-sign")) {
 		git_config_string(&opts->gpg_sign, key, value);
+		sequencer_entrust(opts, (char *) opts->gpg_sign);
+	}
 	else if (!strcmp(key, "options.strategy-option")) {
 		ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
-		opts->xopts[opts->xopts_nr++] = xstrdup(value);
+		opts->xopts[opts->xopts_nr++] =
+			sequencer_entrust(opts, xstrdup(value));

Nice.

Quoted 4 lines
sequencer.c +0 -0
 	} else
 		return error(_("Invalid key: %s"), key);
 
#33 lore raw

Re: [PATCH 07/22] sequencer: future-proof read_populate_todo()

In reply to Johannes Schindelin

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

Quoted 16 lines

Over the next commits, we will work on improving the sequencer to the point where it can process the edit script of an interactive rebase. To that end, we will need to teach the sequencer to read interactive rebase's todo file. In preparation, we consolidate all places where that todo file is needed to call a function that we will later extend.

---
 sequencer.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
sequencer.c +0 -0
index 8d79091..982b6e9 100644

sequencer.c static const char *get_dir(const struct replay_opts *opts) · line 38

Quoted 7 lines
sequencer.c +4 -0
@@ -32,6 +32,11 @@ static const char *get_dir(const struct replay_opts *opts)
 	return git_path_seq_dir();
 }
 
+static const char *get_todo_path(const struct replay_opts *opts)
+{
+	return git_path_todo_file();
+}

I guess that in the future commit the return value of get_todo_path() would change depending on what sequencer is used for, cherry-pick or interactive rebase, that is, contents of replay_opts...

sequencer.c static int parse_insn_buffer(char *buf, struct commit_list **todo_list, · line 780

Quoted 9 lines
sequencer.c +2 -0
+
 static int is_rfc2822_line(const char *buf, int len)
 {
 	int i;
@@ -772,25 +777,24 @@ static int parse_insn_buffer(char *buf, struct commit_list **todo_list,
 static int read_populate_todo(struct commit_list **todo_list,
 			struct replay_opts *opts)
 {
+	const char *todo_file = get_todo_path(opts);

...and that's why you have added this temporary variable here, to not repeat get_todo_path(opts) calculations...

sequencer.c static int parse_insn_buffer(char *buf, struct commit_list **todo_list, · line 786

Quoted 9 lines
sequencer.c +2 -3
 	struct strbuf buf = STRBUF_INIT;
 	int fd, res;
 
-	fd = open(git_path_todo_file(), O_RDONLY);
+	fd = open(todo_file, O_RDONLY);
 	if (fd < 0)
-		return error_errno(_("Could not open %s"),
-				   git_path_todo_file());
+		return error_errno(_("Could not open %s"), todo_file);

... So that's why it is s/git_path_todo_file()/todo_file/ replacement, and not simply...

Quoted 18 lines
sequencer.c +2 -3
 	if (strbuf_read(&buf, fd, 0) < 0) {
 		close(fd);
 		strbuf_release(&buf);
-		return error(_("Could not read %s."), git_path_todo_file());
+		return error(_("Could not read %s."), todo_file);
 	}
 	close(fd);
 
 	res = parse_insn_buffer(buf.buf, todo_list, opts);
 	strbuf_release(&buf);
 	if (res)
-		return error(_("Unusable instruction sheet: %s"),
-			git_path_todo_file());
+		return error(_("Unusable instruction sheet: %s"), todo_file);
 	return 0;
 }
 

sequencer.c static int sequencer_continue(struct replay_opts *opts) · line 1071

Quoted 5 lines
sequencer.c +1 -1
@@ -1064,7 +1068,7 @@ static int sequencer_continue(struct replay_opts *opts)
 {
 	struct commit_list *todo_list = NULL;
 
-	if (!file_exists(git_path_todo_file()))
+	if (!file_exists(get_todo_path(opts)))

...the s/git_path_todo_file()/git_todo_path(opts)/, isn't it?

sequencer.c static int sequencer_continue(struct replay_opts *opts) · line 1074

Quoted 4 lines
sequencer.c +0 -0
 		return continue_single_pick();
 	if (read_populate_opts(opts) ||
 			read_populate_todo(&todo_list, opts))

Looks good; though I have not checked if all calling sites were converted.

Good work,

Signature · 1 line
Jakub Narębski
#84 lore raw

Re: [PATCH 14/22] sequencer: prepare for rebase -i's commit functionality

In reply to Johannes Schindelin
Quoted 23 lines · Johannes Schindelin
Quoted 14 lines
Quoted 14 lines
sequencer.c +14 -0
+static char **read_author_script(void)
+{
+	struct strbuf script = STRBUF_INIT;
+	int i, count = 0;
+	char *p, *p2, **env;
+	size_t env_size;
+
+	if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0)
+		return NULL;
+
+	for (p = script.buf; *p; p++)
+		if (skip_prefix(p, "'\\\\''", (const char **)&p2))
+			strbuf_splice(&script, p - script.buf, p2 - p, "'", 1);
+		else if (*p == '\'')
Quoted 8 lines

sequencer.c static int is_index_unchanged(void) · line 408

Quoted 5 lines
sequencer.c +5 -0
+			strbuf_splice(&script, p-- - script.buf, 1, "", 0);
+		else if (*p == '\n') {
+			*p = '\0';
+			count++;
+		}

Hmph. What is this loop doing? Is it decoding a sq-quoted buffer or something? Don't we have a helper function to do that?

Quoted 4 lines

Well, it is not just decoding an sq-quoted buffer, but several lines with definitions we sq-quoted ourselves, individually.

The quote.[ch] code currently has no code to dequote lines individually.

There is a function with exactly the same name in builtin/am.c and I assume that it is reading from a file with the same format, which uses a helper to read one variable line at a time. Hopefully that can be refactored so that more parsing is shared between the two users.

Two functions with the same name reading from the same format, even when they expect to produce the same result in different internal format, without even being aware of each other is a bad enough "regression" in maintainability of the code. One of them not even using sq_dequote() helper and reinventing is even worse.

#34 lore raw

Re: [PATCH 07/22] sequencer: future-proof read_populate_todo()

In reply to Jakub Narębski

Hi Kuba,

Quoted 21 lines · Jakub Narębski

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

Quoted 19 lines

Over the next commits, we will work on improving the sequencer to the point where it can process the edit script of an interactive rebase. To that end, we will need to teach the sequencer to read interactive rebase's todo file. In preparation, we consolidate all places where that todo file is needed to call a function that we will later extend.

---
 sequencer.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
sequencer.c +0 -0
index 8d79091..982b6e9 100644
@@ -32,6 +32,11 @@ static const char *get_dir(const struct replay_opts *opts)
 	return git_path_seq_dir();
 }
 
Quoted 8 lines

sequencer.c static const char *get_dir(const struct replay_opts *opts) · line 38

Quoted 4 lines
sequencer.c +4 -0
+static const char *get_todo_path(const struct replay_opts *opts)
+{
+	return git_path_todo_file();
+}

I guess that in the future commit the return value of get_todo_path() would change depending on what sequencer is used for, cherry-pick or interactive rebase, that is, contents of replay_opts...

Right.

Quoted 11 lines

sequencer.c static int parse_insn_buffer(char *buf, struct commit_list **todo_list, · line 780

Quoted 8 lines
sequencer.c +1 -0
 static int is_rfc2822_line(const char *buf, int len)
 {
 	int i;
@@ -772,25 +777,24 @@ static int parse_insn_buffer(char *buf, struct commit_list **todo_list,
 static int read_populate_todo(struct commit_list **todo_list,
 			struct replay_opts *opts)
 {
+	const char *todo_file = get_todo_path(opts);

...and that's why you have added this temporary variable here, to not repeat get_todo_path(opts) calculations...

... and to repeat only 9 characters instead of 19...

Quoted 10 lines

sequencer.c static int parse_insn_buffer(char *buf, struct commit_list **todo_list, · line 786

Quoted 6 lines
sequencer.c +2 -3
-	fd = open(git_path_todo_file(), O_RDONLY);
+	fd = open(todo_file, O_RDONLY);
 	if (fd < 0)
-		return error_errno(_("Could not open %s"),
-				   git_path_todo_file());
+		return error_errno(_("Could not open %s"), todo_file);

... So that's why it is s/git_path_todo_file()/todo_file/ replacement, and not simply...

Quoted 8 lines

sequencer.c static int sequencer_continue(struct replay_opts *opts) · line 1071

Quoted 6 lines
sequencer.c +1 -1
@@ -1064,7 +1068,7 @@ static int sequencer_continue(struct replay_opts *opts)
 {
 	struct commit_list *todo_list = NULL;
 
-	if (!file_exists(git_path_todo_file()))
+	if (!file_exists(get_todo_path(opts)))

...the s/git_path_todo_file()/git_todo_path(opts)/, isn't it?

Correct.

Quoted 1 line

Looks good; though I have not checked if all calling sites were converted.

Thanks for the review!
Johannes

#26 lore raw

Re: [PATCH 06/22] sequencer: release memory that was allocated when reading options

In reply to Jakub Narębski

Hi Kuba,

Quoted 21 lines · Jakub Narębski

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

Quoted 19 lines

The sequencer reads options from disk and stores them in its struct for use during sequencer's operations.

With this patch, the memory is released afterwards, plugging a memory leak.

---
 sequencer.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
sequencer.c +0 -0
index b5be0f9..8d79091 100644
@@ -131,6 +131,8 @@ static void remove_sequencer_state(const struct replay_opts *opts)
 		free(opts->owned[i]);
 	free(opts->owned);
 
Quoted 8 lines

sequencer.c static void remove_sequencer_state(const struct replay_opts *opts) · line 135

Quoted 2 lines
sequencer.c +2 -0
+	free(opts->xopts);
+

This looks like independent change, not related to using the sequencer_entrust() to store options read from disk in replay_opts struct to be able to free memory afterwards.

I guess you wanted to avoid one line changes...

Actually, it is not an independent change, but it free()s memory that has been allocated while reading the options, as the commit message says ;-)

Quoted 18 lines

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data)

Quoted 1 line
sequencer.c +0 -0
@@ -811,13 +813,18 @@ static int populate_opts_cb(const char *key, const char *value, void *data)

Sidenote: this patch would be easier to read if lines were reordered as below, but I don't think any slider heuristics could help achieve that automatically. Also, the patch might be invalid...

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 818

Quoted 7 lines
sequencer.c +2 -1
 		opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
 	else if (!strcmp(key, "options.mainline"))
 		opts->mainline = git_config_int(key, value);
-	else if (!strcmp(key, "options.strategy"))
+	else if (!strcmp(key, "options.strategy")) {
 		git_config_string(&opts->strategy, key, value);
+		sequencer_entrust(opts, (char *) opts->strategy);

I wonder if the ability to free strings dup-ed by git_config_string() be something that is part of replay_opts, or rather remove_sequencer_state(), that is a list of

Quoted 2 lines
	free(opts->strategy);
	free(opts->gpg_sign);

That is not necessarily possible because the way sequencer works, the options may have not actually be read from the file, but may be populated by the caller (in which case we do not necessarily want to require strdup()ing the strings just so that the sequencer can clean stuff up afterwards).

Quoted 1 line

Though... free(NULL) is nop as per standard, but can we rely on it?

We can, and we do.

Quoted 2 lines

The *_entrust() mechanism is more generic, but do we use this general-ness? Well, it could be xstrdup or git_config_string doing entrust'ing...

Right, but that is exactly what I wanted to avoid, because it is rather inelegant to strdup() strings just so that we do not have to record what to free() and what not to free().

BTW I have no objection at all to generalize this sequencer_entrust() mechanism further (read: to other, similar use cases), should it withstand the test of time.

Ciao,
Johannes

#85 lore raw

Re: [PATCH 14/22] sequencer: prepare for rebase -i's commit functionality

In reply to Junio C Hamano

Hi Junio,

Quoted 36 lines · Junio C Hamano
Quoted 23 lines · Johannes Schindelin
Quoted 14 lines
Quoted 14 lines
sequencer.c +14 -0
+static char **read_author_script(void)
+{
+	struct strbuf script = STRBUF_INIT;
+	int i, count = 0;
+	char *p, *p2, **env;
+	size_t env_size;
+
+	if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0)
+		return NULL;
+
+	for (p = script.buf; *p; p++)
+		if (skip_prefix(p, "'\\\\''", (const char **)&p2))
+			strbuf_splice(&script, p - script.buf, p2 - p, "'", 1);
+		else if (*p == '\'')
Quoted 8 lines

sequencer.c static int is_index_unchanged(void) · line 408

Quoted 5 lines
sequencer.c +5 -0
+			strbuf_splice(&script, p-- - script.buf, 1, "", 0);
+		else if (*p == '\n') {
+			*p = '\0';
+			count++;
+		}

Hmph. What is this loop doing? Is it decoding a sq-quoted buffer or something? Don't we have a helper function to do that?

Quoted 4 lines

Well, it is not just decoding an sq-quoted buffer, but several lines with definitions we sq-quoted ourselves, individually.

The quote.[ch] code currently has no code to dequote lines individually.

There is a function with exactly the same name in builtin/am.c and I assume that it is reading from a file with the same format, which uses a helper to read one variable line at a time. Hopefully that can be refactored so that more parsing is shared between the two users.

Quoted 5 lines

Two functions with the same name reading from the same format, even when they expect to produce the same result in different internal format, without even being aware of each other is a bad enough "regression" in maintainability of the code. One of them not even using sq_dequote() helper and reinventing is even worse.

First of all, builtin/am's read_author_script() really, really, really only wants to read stuff into the am_state struct.

That alone is already so incompatible that I do not think it can be repaired.

Further, builtin/am's read_author_script() reads *only* the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL and GIT_AUTHOR_DATE lines (opening the file three times for the task). And then it *requires* the corresponding values to be sq-quoted.

I do not have a use case for this myself, but rebase -i explicitly eval's the author script, so it is conceivable that some enterprisey user makes use of this feature to set other environment variables. The thing is that rebase -i cannot necessarily expect all of those files in its state directory to be under tight control -- in stark contrast to git-am.

I could imagine that this could be fixed by abstracting the functionality more, and use your favored sq_dequote() (which may actually fail in case of an enterprisey usage as outlined above), and adapting builtin/am's read_author_script() to make use of that refactored approach.

This is a huge task, make no mistake, in particular because we need to ensure compatibility with non-standard usage. So I do not think I can tackle that any time soon. Certainly not as part of an iteration of this here patch series.

Ciao,
Dscho

#22 lore raw

Re: [PATCH 05/22] sequencer: allow the sequencer to take custody of malloc()ed data

In reply to Jakub Narębski
Quoted 3 lines · Jakub Narębski

In my personal opinion 'set_me_free_after_use' is not the best name, but I unfortunately do not have a better proposal. Maybe 'entrust_ptr', or 'entrusted_data' / 'entrusted_ptr' / 'entrusted'?

Is this to accumulate to-be-freed pointers?

I think we often call a local variable that points at a piece of memory to be freed "to_free", and that is an appropriate name for what this function is trying to do.

It is a bit surprising that the careless memory management in this codepath leaks only the dumb pieces of memory (as opposed to pointers to structures like string list that needs _clear() functions, in which case we cannot get away with list of to-be-freed). I guess we were somewhat lucky ;-)

#30 lore raw

Re: [PATCH 06/22] sequencer: release memory that was allocated when reading options

In reply to Johannes Schindelin
Quoted 11 lines · Johannes Schindelin
sequencer.c +4 -2
@@ -811,13 +813,18 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
 		opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
 	else if (!strcmp(key, "options.mainline"))
 		opts->mainline = git_config_int(key, value);
-	else if (!strcmp(key, "options.strategy"))
+	else if (!strcmp(key, "options.strategy")) {
 		git_config_string(&opts->strategy, key, value);
-	else if (!strcmp(key, "options.gpg-sign"))
+		sequencer_entrust(opts, (char *) opts->strategy);
+	}
+	else if (!strcmp(key, "options.gpg-sign")) {

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 829

Quoted 10 lines
sequencer.c +4 -1
 		git_config_string(&opts->gpg_sign, key, value);
+		sequencer_entrust(opts, (char *) opts->gpg_sign);
+	}
 	else if (!strcmp(key, "options.strategy-option")) {
 		ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
-		opts->xopts[opts->xopts_nr++] = xstrdup(value);
+		opts->xopts[opts->xopts_nr++] =
+			sequencer_entrust(opts, xstrdup(value));
 	} else
 		return error(_("Invalid key: %s"), key);

Hmm.

I would have expected a call to sequencer_opts_clear(&opts) once the machinery is done with the options structure, and among these places where an old value in opts->field is overwritten by a new one would get

	free(opt->field); opt->field = ... new value ...;

Perhaps there was a good reason to do it this way (one valid reason may be that there is _no_ good place to declare that opts is now done and it is safe to call sequencer_opts_clear() on it), but this looks backwards from the way things are usually done.

#27 lore raw

Re: [PATCH 06/22] sequencer: release memory that was allocated when reading options

In reply to Johannes Schindelin

Am 30.08.2016 um 19:52 schrieb Johannes Schindelin:

Quoted 3 lines

Right, but that is exactly what I wanted to avoid, because it is rather inelegant to strdup() strings just so that we do not have to record what to free() and what not to free().

Please, excuse, but when I have to choose what is more "elegant":

1. strdup() sometimes so that I can later free() always
2. use sequencer_entrust()

I would choose 1. at all times.

Particularly in this case: parsing options does not sound like a major drain of resources, neither CPU- nor memory-wise.

-- Hannes

#29 lore raw

Re: [PATCH 06/22] sequencer: release memory that was allocated when reading options

In reply to Johannes Schindelin

W dniu 30.08.2016 o 19:52, Johannes Schindelin pisze:

Quoted 55 lines

Hi Kuba,

Quoted 21 lines · Jakub Narębski

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

Quoted 19 lines

The sequencer reads options from disk and stores them in its struct for use during sequencer's operations.

With this patch, the memory is released afterwards, plugging a memory leak.

---
 sequencer.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
sequencer.c +0 -0
index b5be0f9..8d79091 100644
@@ -131,6 +131,8 @@ static void remove_sequencer_state(const struct replay_opts *opts)
 		free(opts->owned[i]);
 	free(opts->owned);
 
Quoted 8 lines

sequencer.c static void remove_sequencer_state(const struct replay_opts *opts) · line 135

Quoted 2 lines
sequencer.c +2 -0
+	free(opts->xopts);
+

This looks like independent change, not related to using the sequencer_entrust() to store options read from disk in replay_opts struct to be able to free memory afterwards.

I guess you wanted to avoid one line changes...

Actually, it is not an independent change, but it free()s memory that has been allocated while reading the options, as the commit message says ;-)

Quoted 18 lines

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data)

Quoted 1 line
sequencer.c +0 -0
@@ -811,13 +813,18 @@ static int populate_opts_cb(const char *key, const char *value, void *data)

Sidenote: this patch would be easier to read if lines were reordered as below, but I don't think any slider heuristics could help achieve that automatically. Also, the patch might be invalid...

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 818

Quoted 7 lines
sequencer.c +2 -1
 		opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
 	else if (!strcmp(key, "options.mainline"))
 		opts->mainline = git_config_int(key, value);
-	else if (!strcmp(key, "options.strategy"))
+	else if (!strcmp(key, "options.strategy")) {
 		git_config_string(&opts->strategy, key, value);
+		sequencer_entrust(opts, (char *) opts->strategy);

I wonder if the ability to free strings dup-ed by git_config_string() be something that is part of replay_opts, or rather remove_sequencer_state(), that is a list of

Quoted 8 lines
Quoted 2 lines
	free(opts->strategy);
	free(opts->gpg_sign);

That is not necessarily possible because the way sequencer works, the options may have not actually be read from the file, but may be populated by the caller (in which case we do not necessarily want to require strdup()ing the strings just so that the sequencer can clean stuff up afterwards).

I guess from cursory browsing through the Git code that _currently_ they are only read from the config file, where git_config_string() strdup's them, isn't it? And we want to prepare for the future, where the caller would prepare replay_opts, and the caller would be responsible for freeing data if necessary?

Would there be any sane situation where some of data should be owned by caller (and freed by caller), and some of data should be owned by sequencer library API (and freed in remove_sequencer_state())? If not, perhaps *_entrust() mechanism is overthinking it, and we simply need 'is_strdup' boolean flag or something like that...

Quoted 6 lines
Quoted 2 lines

The *_entrust() mechanism is more generic, but do we use this general-ness? Well, it could be xstrdup or git_config_string doing entrust'ing...

Right, but that is exactly what I wanted to avoid, because it is rather inelegant to strdup() strings just so that we do not have to record what to free() and what not to free().

Maybe inelegant, but it might be easier than inventing and implementing *_entrust() mechanism, like Hannes wrote.

Quoted 4 lines

BTW I have no objection at all to generalize this sequencer_entrust() mechanism further (read: to other, similar use cases), should it withstand the test of time.

Yeah, that's my take on it too.

Signature · 1 line
Jakub Narębski
#28 lore raw

Re: [PATCH 06/22] sequencer: release memory that was allocated when reading options

In reply to Johannes Sixt
Quoted 11 lines · Johannes Sixt

Am 30.08.2016 um 19:52 schrieb Johannes Schindelin:

Quoted 3 lines

Right, but that is exactly what I wanted to avoid, because it is rather inelegant to strdup() strings just so that we do not have to record what to free() and what not to free().

Please, excuse, but when I have to choose what is more "elegant":

1. strdup() sometimes so that I can later free() always
2. use sequencer_entrust()

I would choose 1. at all times.

Let's not bring elegance or aesthetics in. It is like asking if garbage collected systems are elegant. I do not think it is a valid question. GC is a good pragmatic compromise after (1) declaring that keeping track of allocation is too cumbersome and programmers' time is better spent elsewhere, and (2) making sure hiccups caused by GC can be minimized to keep the latency down to acceptable levels. There are good compromises and bad ones.

Does the proposed entrust() thing qualify as a good pragmatic compromise like GC does?

Quoted 2 lines

Particularly in this case: parsing options does not sound like a major drain of resources, neither CPU- nor memory-wise.

If entrust() does not have any CPU- or memory-cost, then comparing it with having to strdup() sometimes might be a useful comparison.

But the entrust() thing has the allocation cost in order to track of what needs to be done at runtime, and just like strdup() needs to be done on things that are being borrowed, entrust() needs to be avoided on things that are being borrowed (and the caller needs to be sure not to entrust() the same piece of memory twice), so it does not reduce the cost on programmers' and readers' mental burden--the programmer always has to know which piece of memory is borrowed and which are owned by the subsystem.

So...

#86 lore raw

Re: [PATCH 14/22] sequencer: prepare for rebase -i's commit functionality

In reply to Junio C Hamano
Quoted 5 lines · Junio C Hamano

Two functions with the same name reading from the same format, even when they expect to produce the same result in different internal format, without even being aware of each other is a bad enough "regression" in maintainability of the code. One of them not even using sq_dequote() helper and reinventing is even worse.

So, here is what I did as a lunch-break-hack. The "same result in different internal format" calls for a fairly light-weight mechanism to convey the necessary information that can be shared by callers with different needs, and I chose string-list for that.

Totally untested, but parse_key_value_squoted() would be a good foundation to build on. The caller in "am" deliberately wants to be strict (e.g. it wants to error out when the user mucked with the file, so it insists on the three variables to appear in a known order and rejects input that violates its assumption), but the function does not mind if other callers want to be more lenient.

-- >8 --
From: Junio C Hamano <[email protected]>
Date: Tue, 30 Aug 2016 12:36:42 -0700
Subject: [PATCH] am: refactor read_author_script()

By splitting the part that reads from a file and the part that parses the variable definitions from the contents, make the latter can be more reusable in the future.

---
 builtin/am.c | 103 ++++++++++++++++++++++++++---------------------------------
 1 file changed, 45 insertions(+), 58 deletions(-)
builtin/am.c +45 -58
index 739b34d..b36d1f0 100644
@@ -28,6 +28,7 @@
 #include "rerere.h"
 #include "prompt.h"
 #include "mailinfo.h"
+#include "string-list.h"
 
 /**
  * Returns 1 if the file is empty or does not exist, 0 otherwise.
@@ -258,38 +259,29 @@ static int read_state_file(struct strbuf *sb, const struct am_state *state,
 }
 
 /**
- * Reads a KEY=VALUE shell variable assignment from `fp`, returning the VALUE
- * as a newly-allocated string. VALUE must be a quoted string, and the KEY must
- * match `key`. Returns NULL on failure.
- *
- * This is used by read_author_script() to read the GIT_AUTHOR_* variables from
- * the author-script.
+ * Take a series of KEY='VALUE' lines where VALUE part is
+ * sq-quoted, and append <KEY, VALUE> at the end of the string list
  */
-static char *read_shell_var(FILE *fp, const char *key)
+static int parse_key_value_squoted(char *buf, struct string_list *list)
 {
-	struct strbuf sb = STRBUF_INIT;
-	const char *str;
-
-	if (strbuf_getline_lf(&sb, fp))
-		goto fail;
-
-	if (!skip_prefix(sb.buf, key, &str))
-		goto fail;
-
-	if (!skip_prefix(str, "=", &str))
-		goto fail;
-
-	strbuf_remove(&sb, 0, str - sb.buf);
-
-	str = sq_dequote(sb.buf);
-	if (!str)
-		goto fail;
-
-	return strbuf_detach(&sb, NULL);
-
-fail:
-	strbuf_release(&sb);
-	return NULL;
+	while (*buf) {
+		struct string_list_item *item;
+		char *np;
+		char *cp = strchr(buf, '=');
+		if (!cp)
+			return -1;
+		np = strchrnul(cp, '\n');
+		*cp++ = '\0';
+		item = string_list_append(list, buf);
+
+		buf = np + (*np == '\n');
+		*np = '\0';
+		cp = sq_dequote(cp);
+		if (!cp)
+			return -1;
+		item->util = xstrdup(cp);
+	}
+	return 0;
 }
 
 /**
@@ -311,44 +303,39 @@ static char *read_shell_var(FILE *fp, const char *key)
 static int read_author_script(struct am_state *state)
 {
 	const char *filename = am_path(state, "author-script");
-	FILE *fp;
+	struct strbuf buf = STRBUF_INIT;
+	struct string_list kv = STRING_LIST_INIT_DUP;
+	int retval = -1; /* assume failure */
+	int fd;
 
 	assert(!state->author_name);
 	assert(!state->author_email);
 	assert(!state->author_date);
 
-	fp = fopen(filename, "r");
-	if (!fp) {
+	fd = open(filename, O_RDONLY);
+	if (fd < 0) {
 		if (errno == ENOENT)
 			return 0;
 		die_errno(_("could not open '%s' for reading"), filename);
 	}
+	strbuf_read(&buf, fd, 0);
+	close(fd);
+	if (parse_key_value_squoted(buf.buf, &kv))
+		goto finish;
 
-	state->author_name = read_shell_var(fp, "GIT_AUTHOR_NAME");
-	if (!state->author_name) {
-		fclose(fp);
-		return -1;
-	}
-
-	state->author_email = read_shell_var(fp, "GIT_AUTHOR_EMAIL");
-	if (!state->author_email) {
-		fclose(fp);
-		return -1;
-	}
-
-	state->author_date = read_shell_var(fp, "GIT_AUTHOR_DATE");
-	if (!state->author_date) {
-		fclose(fp);
-		return -1;
-	}
-
-	if (fgetc(fp) != EOF) {
-		fclose(fp);
-		return -1;
-	}
-
-	fclose(fp);
-	return 0;
+	if (kv.nr != 3 ||
+	    strcmp(kv.items[0].string, "GIT_AUTHOR_NAME") ||
+	    strcmp(kv.items[1].string, "GIT_AUTHOR_EMAIL") ||
+	    strcmp(kv.items[2].string, "GIT_AUTHOR_DATE"))
+		goto finish;
+	state->author_name = kv.items[0].util;
+	state->author_email = kv.items[1].util;
+	state->author_date = kv.items[2].util;
+	retval = 0;
+finish:
+	string_list_clear(&kv, !!retval);
+	strbuf_release(&buf);
+	return retval;
 }
 
 /**
Signature · 1 line
2.10.0-rc2-246-g4fd2c60
#31 lore raw

Re: [PATCH 06/22] sequencer: release memory that was allocated when reading options

In reply to Junio C Hamano

Hi Junio,

Quoted 26 lines · Junio C Hamano
Quoted 11 lines · Johannes Schindelin
sequencer.c +4 -2
@@ -811,13 +813,18 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
 		opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
 	else if (!strcmp(key, "options.mainline"))
 		opts->mainline = git_config_int(key, value);
-	else if (!strcmp(key, "options.strategy"))
+	else if (!strcmp(key, "options.strategy")) {
 		git_config_string(&opts->strategy, key, value);
-	else if (!strcmp(key, "options.gpg-sign"))
+		sequencer_entrust(opts, (char *) opts->strategy);
+	}
+	else if (!strcmp(key, "options.gpg-sign")) {

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 829

Quoted 10 lines
sequencer.c +4 -1
 		git_config_string(&opts->gpg_sign, key, value);
+		sequencer_entrust(opts, (char *) opts->gpg_sign);
+	}
 	else if (!strcmp(key, "options.strategy-option")) {
 		ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
-		opts->xopts[opts->xopts_nr++] = xstrdup(value);
+		opts->xopts[opts->xopts_nr++] =
+			sequencer_entrust(opts, xstrdup(value));
 	} else
 		return error(_("Invalid key: %s"), key);

Hmm.

Quoted 2 lines

I would have expected a call to sequencer_opts_clear(&opts) once the machinery is done with the options structure,

It is part of sequencer_remove_state().

Quoted 4 lines

and among these places where an old value in opts->field is overwritten by a new one would get

	free(opt->field); opt->field = ... new value ...;

That is *exactly* what we *cannot* do.

You see, the replay_opts struct is *not necessarily* populated by read_populate_opts(). So we really cannot tell whether we are free to free(opt->field) or whether some other code still wants to reference that memory in the end (or for that matter, whether it was malloc()ed).

That is the *precise* reason for the existence of sequencer_entrust().

Ciao,
Dscho

#87 lore raw

Re: [PATCH 14/22] sequencer: prepare for rebase -i's commit functionality

In reply to Junio C Hamano

Hi Junio,

Quoted 5 lines · Junio C Hamano

From: Junio C Hamano <[email protected]>
Date: Tue, 30 Aug 2016 12:36:42 -0700
Subject: [PATCH] am: refactor read_author_script()

[...]

Thank you so much for that! I will use that as a starting point to refactor the two read_author_script() functions into one.

Just don't look surprised when you see this patch as part of my patch series, Signed-off-by me...

Ciao,
Dscho

#23 lore raw

Re: [PATCH 05/22] sequencer: allow the sequencer to take custody of malloc()ed data

In reply to Junio C Hamano

Hi Junio,

Quoted 7 lines · Junio C Hamano
Quoted 3 lines · Jakub Narębski

In my personal opinion 'set_me_free_after_use' is not the best name, but I unfortunately do not have a better proposal. Maybe 'entrust_ptr', or 'entrusted_data' / 'entrusted_ptr' / 'entrusted'?

Is this to accumulate to-be-freed pointers?

Yes.

Quoted 3 lines

I think we often call a local variable that points at a piece of memory to be freed "to_free", and that is an appropriate name for what this function is trying to do.

I changed it to that.

Quoted 5 lines

It is a bit surprising that the careless memory management in this codepath leaks only the dumb pieces of memory (as opposed to pointers to structures like string list that needs _clear() functions, in which case we cannot get away with list of to-be-freed). I guess we were somewhat lucky ;-)

Yeah, the carelessness is more like a convenience where the config machinery is reused to parse the values.

Ciao,
Dscho

#36 lore raw

Re: [PATCH 08/22] sequencer: remove overzealous assumption

In reply to Johannes Schindelin

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

Quoted 7 lines

The sequencer was introduced to make the cherry-pick and revert functionality available as library function, with the original idea being to extend the sequencer to also implement the rebase -i functionality.

The test to ensure that all of the commands in the script are identical to the overall operation does not mesh well with that.

Actually the question is what does the test that got removed in this commit actually check. Is it high-level sanity check that todo list for git-cherry-pick contains only 'pick', and for git-revert contains only 'revert'? Or does it check that at the low level sequencer fails when instruction sheet includes only identical operations?

Only if it is the latter (we are testing too low level details of how sequencer code works, tying too tightly test with implementation) the test should be removed. I see that earlier test check that sequencer handles correctly invalid instructions in todo.

Quoted 3 lines

Therefore let's just get rid of the test that wants to verify that this limitation is still in place, in preparation for the upcoming work to teach the sequencer to do rebase -i's work.

Is it "upcoming work" as in one of the patches in this series? If so, which patch?

Quoted 11 lines
---
 t/t3510-cherry-pick-sequence.sh | 11 -----------
 1 file changed, 11 deletions(-)
t/t3510-cherry-pick-sequence.sh +0 -0
index 7b7a89d..6465edf 100755

t/t3510-cherry-pick-sequence.sh test_expect_success 'malformed instruction sheet 1' ' · line 462

Quoted 4 lines
t/t3510-cherry-pick-sequence.sh +0 -1
@@ -459,17 +459,6 @@ test_expect_success 'malformed instruction sheet 1' '
 	test_expect_code 128 git cherry-pick --continue
 '
 
-test_expect_success 'malformed instruction sheet 2' '

Hmmm... the description is somewhat lacking (especially compared to the rest of test), anyway.

BTW. we should probably rename 'malformed instruction sheet 2' to 'malformed instruction sheet' if there are no further such tests after this removal, isn't it?

Quoted 14 lines
t/t3510-cherry-pick-sequence.sh +0 -10
-	pristine_detach initial &&
-	test_expect_code 1 git cherry-pick base..anotherpick &&
-	echo "resolved" >foo &&
-	git add foo &&
-	git commit &&
-	sed "s/pick/revert/" .git/sequencer/todo >new_sheet &&
-	cp new_sheet .git/sequencer/todo &&
-	test_expect_code 128 git cherry-pick --continue
-'
-
 test_expect_success 'empty commit set' '
 	pristine_detach initial &&
 	test_expect_code 128 git cherry-pick base..base
#44 lore raw

Re: [PATCH 09/22] sequencer: completely revamp the "todo" script parsing

In reply to Johannes Schindelin

W dniu 29.08.2016 o 10:05, Johannes Schindelin pisze:

Quoted 10 lines

When we came up with the "sequencer" idea, we really wanted to have kind of a plumbing equivalent of the interactive rebase. Hence the choice of words: the "todo" script, a "pick", etc.

However, when it came time to implement the entire shebang, somehow this idea got lost and the sequencer was used as working horse for cherry-pick and revert instead. So as not to interfere with the interactive rebase, it even uses a separate directory to store its state.

Quoted 4 lines

Furthermore, it also is stupidly strict about the "todo" script it
accepts: while it parses commands in a way that was *designed* to be similar to the interactive rebase, it then goes on to *error out* if the commands disagree with the overall action (cherry-pick or revert).

Does this mean that after the change you would be able to continue "git revert" with "git cherry-pick --continue", and vice versa? Or that it would be possible for git-cherry-pick to do reverts (e.g. with ^<rev>)?

That's what we need to decide before becoming more lenient.

BTW. perhaps we would be able to continue with 'git continue', regardless of what we have started with, I wonder...

Quoted 6 lines

Finally, the sequencer code chose to deviate from the interactive rebase code insofar that it *reformats* the "todo" script instead of just writing the part of the parsed script that were not yet processed. This is not only unnecessary churn, but might well lose information that is valuable to the user (i.e. comments after the commands).

That's a very good change.

Quoted 6 lines

Let's just bite the bullet and rewrite the entire parser; the code now becomes not only more elegant: it allows us to go on and teach the sequencer how to parse *true* "todo" scripts as used by the interactive rebase itself. In a way, the sequencer is about to grow up to do its older brother's job. Better.

Sidenote: this is not your fault, but Git doesn't do a good job on changes which are mostly rewrites, trying to match stray '}' and the like in generated diff. I wonder if existing diff heuristic options could help here.

Quoted 4 lines

While at it, do not stop at the first problem, but list *all* of the problems. This helps the user by allowing to address all issues in one go rather than going back and forth until the todo list is valid.

That is also a good change, though I wonder how often users need to worry about this outside interactive rebase case. If it is preparation for rebase -i, where instruction list is written by prone to errors human, it would be nice to have this information in the commit message.

Quoted 5 lines
---
 sequencer.c | 241 +++++++++++++++++++++++++++++++++---------------------------
 1 file changed, 134 insertions(+), 107 deletions(-)

Note: I have moved some lines of diff so that the change is more readable to humans (but it results often in --++-++ chunk).

Quoted 6 lines
sequencer.c +0 -0
index 982b6e9..cbdce6d 100644

sequencer.c static int allow_empty(struct replay_opts *opts, struct commit *commit) · line 479

Quoted 8 lines
sequencer.c +4 -1
@@ -473,7 +473,26 @@ static int allow_empty(struct replay_opts *opts, struct commit *commit)
 		return 1;
 }
 
-static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
+enum todo_command {
+	TODO_PICK,
+	TODO_REVERT
+};

Do we have a naming convention for enums elements? Or are we explicitly making enums and #defines interchangeable? I wonder...

...uh, I see we don't have naming convention, but all caps snake-case names dominate:

  $ git grep -A2 'enum .* {'
  [...]
  diff.h:enum color_diff {
  diff.h- DIFF_RESET = 0,
  diff.h- DIFF_CONTEXT = 1,
  --
  dir.c:enum path_treatment {
  dir.c-  path_none = 0,
  dir.c-  path_recurse,
  --

Shouldn't we say 'TODO_PICK = 0' explicitly, though?

sequencer.c static int allow_empty(struct replay_opts *opts, struct commit *commit) · line 484

Quoted 5 lines
sequencer.c +5 -0
+
+static const char *todo_command_strings[] = {
+	"pick",
+	"revert"
+};

It's a bit pity that we cannot use designated inits, and hanging comma, (from ISO C99 standard). That is:

+static const char *todo_command_strings[] = {
+ [TODO_PICK] = "pick",
+ [TODO_REVERT] = "revert",
+};

Quoted 15 lines
sequencer.c +11 -0
+
+static const char *command_to_string(const enum todo_command command)
+{
+	if (command < ARRAY_SIZE(todo_command_strings))
+		return todo_command_strings[command];
+	die("Unknown command: %d", command);
+}
+
+
+static int do_pick_commit(enum todo_command command, struct commit *commit,
+		struct replay_opts *opts)
 {
 	unsigned char head[20];
 	struct commit *base, *next, *parent;

sequencer.c static int do_pick_commit(struct commit *commit, struct replay_opts *opts) · line 556

Quoted 3 lines
sequencer.c +0 -0
@@ -535,7 +554,8 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
 		/* TRANSLATORS: The first %s will be "revert" or
 		   "cherry-pick", the second %s a SHA1 */
 		return error(_("%s: cannot parse parent commit %s"),

I wonder if we should not change also the error message: it is no longer about command, but about operation in todo list (from what I understand). Though admittedly current message works for both...

sequencer.c static int do_pick_commit(struct commit *commit, struct replay_opts *opts)

Quoted 7 lines
sequencer.c +2 -1
-			action_name(opts), oid_to_hex(&parent->object.oid));
+			command_to_string(command),
+			oid_to_hex(&parent->object.oid));
 
 	if (get_message(commit, &msg) != 0)
 		return error(_("Cannot get commit message for %s"),
@@ -548,7 +568,7 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)

From here on changes are about

s/opts->action == REPLAY_\(PICK\|REVERT\)/command == TODO_\1/

Do we still need opts->action, or it is just needed less, and it is 'todo' instruction that decides about command (as it should)?

Quoted 28 lines
sequencer.c +4 -4
 	 * reverse of it if we are revert.
 	 */
 
-	if (opts->action == REPLAY_REVERT) {
+	if (command == TODO_REVERT) {
 		base = commit;
 		base_label = msg.label;
 		next = parent;
@@ -589,7 +609,7 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
 		}
 	}
 
-	if (!opts->strategy || !strcmp(opts->strategy, "recursive") || opts->action == REPLAY_REVERT) {
+	if (!opts->strategy || !strcmp(opts->strategy, "recursive") || command == TODO_REVERT) {
 		res = do_recursive_merge(base, next, base_label, next_label,
 					 head, &msgbuf, opts);
 		if (res < 0)
@@ -615,17 +635,17 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
 	 * However, if the merge did not even start, then we don't want to
 	 * write it at all.
 	 */
-	if (opts->action == REPLAY_PICK && !opts->no_commit && (res == 0 || res == 1) &&
+	if (command == TODO_PICK && !opts->no_commit && (res == 0 || res == 1) &&
 	    update_ref(NULL, "CHERRY_PICK_HEAD", commit->object.oid.hash, NULL,
 		       REF_NODEREF, UPDATE_REFS_MSG_ON_ERR))
 		res = -1;
-	if (opts->action == REPLAY_REVERT && ((opts->no_commit && res == 0) || res == 1) &&
+	if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) &&

sequencer.c static int do_pick_commit(struct commit *commit, struct replay_opts *opts) · line 651

Quoted 10 lines
sequencer.c +1 -1
 	    update_ref(NULL, "REVERT_HEAD", commit->object.oid.hash, NULL,
 		       REF_NODEREF, UPDATE_REFS_MSG_ON_ERR))
 		res = -1;
 
 	if (res) {
-		error(opts->action == REPLAY_REVERT
+		error(command == TODO_REVERT
 		      ? _("could not revert %s... %s")
 		      : _("could not apply %s... %s"),
 		      find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV),

And here those changes end.

s/opts->action == REPLAY_\(PICK\|REVERT\)/command == TODO_\1/

I wonder if Coccinelle / Undebt would help here; or would simple sed or query-and-replace-regexp be enough...

Quoted 7 lines
sequencer.c +1 -2
@@ -683,116 +703,107 @@ static int read_and_refresh_cache(struct replay_opts *opts)
 	return 0;
 }
 
-static int format_todo(struct strbuf *buf, struct commit_list *todo_list,
-		struct replay_opts *opts)
+struct todo_item {

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 716

Quoted 10 lines
sequencer.c +10 -0
+	enum todo_command command;
+	struct commit *commit;
+	size_t offset_in_buf;
+};
+
+struct todo_list {
+	struct strbuf buf;
+	struct todo_item *items;
+	int nr, alloc, current;
+};

So there should be s/commit_list [*]{1,2}todo_list/todo_list *todo_list/ from here on?

Hmmm... commit_list is, as defined in commit.h, a linked list. Here todo_list uses growable array implementation of list. Which is I guess better on current CPU architecture, with slow memory, limited-size caches, and adjacency prefetching.

I guess using items_nr and items_alloc would be not necessary (and a bit funny / overkill).

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 718

Quoted 2 lines
sequencer.c +2 -0
+
+#define TODO_LIST_INIT { STRBUF_INIT, NULL, 0, 0, 0 }

Same as with other patches in this series, it would be enough to

+#define TODO_LIST_INIT { STRBUF_INIT }

You are consistent.

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 721

Quoted 3 lines
sequencer.c +2 -0
+
+static void todo_list_release(struct todo_list *todo_list)
 {

Grh... stray '{' matched...

Quoted 11 lines
sequencer.c +5 -5
-	struct commit_list *cur = NULL;
-	const char *sha1_abbrev = NULL;
-	const char *action_str = opts->action == REPLAY_REVERT ? "revert" : "pick";
-	const char *subject;
-	int subject_len;
+	strbuf_release(&todo_list->buf);
+	free(todo_list->items);
+	todo_list->items = NULL;
+	todo_list->nr = todo_list->alloc = 0;
+}
 

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 728

Quoted 10 lines
sequencer.c +1 -9
-	for (cur = todo_list; cur; cur = cur->next) {
-		const char *commit_buffer = get_commit_buffer(cur->item, NULL);
-		sha1_abbrev = find_unique_abbrev(cur->item->object.oid.hash, DEFAULT_ABBREV);
-		subject_len = find_commit_subject(commit_buffer, &subject);
-		strbuf_addf(buf, "%s %s %.*s\n", action_str, sha1_abbrev,
-			subject_len, subject);
-		unuse_commit_buffer(cur->item, commit_buffer);
-	}
-	return 0;
+struct todo_item *append_todo(struct todo_list *todo_list)

Errr... I don't quite understand the name of this function. What are you appending here to the todo_list?

Compare string_list_append() and string_list_append_nodup(), where the second parameter is item to append.

I'm not against what this function does (grow array if needed, and return pointer to the new todo_item that is to be filled), but I don't quite agree with the name. Naming is hard... :-( [See later in reply for a proposal.]

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 734

Quoted 7 lines
sequencer.c +4 -1
+{
+	ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc);
+	return todo_list->items + todo_list->nr++;
 }
 
-static struct commit *parse_insn_line(char *bol, char *eol, struct replay_opts *opts)
+static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)

Why the change of return type?

I guess the previous code used only opts->action out of whole replay_opts, and now we use item->command instead; that is why replay_opts is replaced by todo_item.

Why now struct todo_item is first when struct replay_opts was last? Not that I say is was a bad change...

Quoted 5 lines
sequencer.c +0 -2
 {
 	unsigned char commit_sha1[20];
-	enum replay_action action;
 	char *end_of_object_name;
-	int saved, status, padding;

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 738

Quoted 10 lines
sequencer.c +1 -9
-
-	if (starts_with(bol, "pick")) {
-		action = REPLAY_PICK;
-		bol += strlen("pick");
-	} else if (starts_with(bol, "revert")) {
-		action = REPLAY_REVERT;
-		bol += strlen("revert");
-	} else
-		return NULL;
+	int i, saved, status, padding;

int i or enum? Just kidding...

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 741

Quoted 3 lines
sequencer.c +3 -0
+
+	for (i = 0; i < ARRAY_SIZE(todo_command_strings); i++)
+		if (skip_prefix(bol, todo_command_strings[i], &bol)) {

skip_prefix() is such a nice abstraction...

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 744

Quoted 3 lines
sequencer.c +3 -0
+			item->command = i;
+			break;
+		}

Nice. Replacing if-elsif chain with loop.

I guess any hashmap would be serious overkill, as there are and would be only a few actions possible.

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 754

Quoted 12 lines
sequencer.c +4 -2
+	if (i >= ARRAY_SIZE(todo_command_strings))
+		return -1;
 
 	/* Eat up extra spaces/ tabs before object name */
 	padding = strspn(bol, " \t");
 	if (!padding)
-		return NULL;
+		return -1;
 	bol += padding;
 
-	end_of_object_name = bol + strcspn(bol, " \t\n");
+	end_of_object_name = (char *) bol + strcspn(bol, " \t\n");

Why is this cast needed?

And do we say '(char *) bol' or '(char *)bol'?

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 741

Quoted 13 lines
sequencer.c +0 -8
 	saved = *end_of_object_name;
 	*end_of_object_name = '\0';
 	status = get_sha1(bol, commit_sha1);
 	*end_of_object_name = saved;
 
-	/*
-	 * Verify that the action matches up with the one in
-	 * opts; we don't support arbitrary instructions
-	 */
-	if (action != opts->action) {
-		if (action == REPLAY_REVERT)
-		      error((opts->action == REPLAY_REVERT)
-			    ? _("Cannot revert during another revert.")

Errr... could the above ever happen? Namely

  action != opts->action && action == REPLAY_REVERT && opts->action == REPLAY_REVERT

Surely not.

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 748

Quoted 7 lines
sequencer.c +0 -7
-			    : _("Cannot revert during a cherry-pick."));
-		else
-		      error((opts->action == REPLAY_REVERT)
-			    ? _("Cannot cherry-pick during a revert.")
-			    : _("Cannot cherry-pick during another cherry-pick."));
-		return NULL;
-	}

Anyway, while it is / would be a good idea to prevent starting any sequencer-based command (cherry-pick, revert, soon rebase -i) when other command is in progress (cherry-pick, revert, soon rebase -i). That is, if cherry-pick / revert waits for user action, you cannot run another cherry-pick or revert.

Which I guess the above code was not about...

Quoted 19 lines
sequencer.c +5 -8
-
 	if (status < 0)
-		return NULL;
+		return -1;
 
-	return lookup_commit_reference(commit_sha1);
+	item->commit = lookup_commit_reference(commit_sha1);
+	return !item->commit;
 }
 
-static int parse_insn_buffer(char *buf, struct commit_list **todo_list,
-			struct replay_opts *opts)
+static int parse_insn_buffer(char *buf, struct todo_list *todo_list)
 {
-	struct commit_list **next = todo_list;
-	struct commit *commit;
+	struct todo_item *item;
 	char *p = buf;
-	int i;

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 776

Quoted 10 lines
sequencer.c +3 -4
+	int i, res = 0;
 
 	for (i = 1; *p; i++) {
 		char *eol = strchrnul(p, '\n');
-		commit = parse_insn_line(p, eol, opts);
-		if (!commit)
-			return error(_("Could not parse line %d."), i);
-		next = commit_list_append(commit, next);
+
+		item = append_todo(todo_list);

A better name, in my personal option, would be

  +		item = todo_list_next(todo_list);

Or todo_next(todo_list).

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 779

Quoted 3 lines
sequencer.c +3 -0
+		item->offset_in_buf = p - todo_list->buf.buf;
+		if (parse_insn_line(item, p, eol)) {
+			error("Invalid line: %.*s", (int)(eol - p), p);

This error message should, I think, be also translatable:

  +			error(_("Invalid line: %.*s"), (int)(eol - p), p);

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 780

Quoted 1 line
sequencer.c +1 -0
+			res |= error(_("Could not parse line %d."), i);

Wouldn't it make more sense to reverse order of errors, that is first tell which line, and then show it?

BTW. would be we able to show where exactly there was problem parsing, that is at which character in line? Or is it something for the future?

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 787

Quoted 9 lines
sequencer.c +4 -2
+			item->command = -1;
+		}
 		p = *eol ? eol + 1 : eol;
 	}
-	if (!*todo_list)
+	if (!todo_list->nr)
 		return error(_("No commits parsed."));
-	return 0;
+	return res;

Ah, so 'res' is "was there an error" in any of lines. Nice.

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 793

Quoted 7 lines
sequencer.c +1 -1
 }
 
-static int read_populate_todo(struct commit_list **todo_list,
+static int read_populate_todo(struct todo_list *todo_list,
 			struct replay_opts *opts)
 {
 	const char *todo_file = get_todo_path(opts);

If I understand it correctly, replay_opts is used only to find out correct todo_file, isn't it?

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 789

Quoted 11 lines
sequencer.c +2 -3
-	struct strbuf buf = STRBUF_INIT;
 	int fd, res;
 
+	strbuf_reset(&todo_list->buf);
 	fd = open(todo_file, O_RDONLY);
 	if (fd < 0)
 		return error_errno(_("Could not open %s"), todo_file);
-	if (strbuf_read(&buf, fd, 0) < 0) {
+	if (strbuf_read(&todo_list->buf, fd, 0) < 0) {
 		close(fd);
-		strbuf_release(&buf);

A question: when is todo_list->buf released?

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 809

Quoted 10 lines
sequencer.c +1 -2
 		return error(_("Could not read %s."), todo_file);
 	}
 	close(fd);
 
-	res = parse_insn_buffer(buf.buf, todo_list, opts);
+	res = parse_insn_buffer(todo_list->buf.buf, todo_list);
-	strbuf_release(&buf);
 	if (res)
 		return error(_("Unusable instruction sheet: %s"), todo_file);
 	return 0;

Nice.

Quoted 12 lines
sequencer.c +3 -2
@@ -848,18 +859,33 @@ static int read_populate_opts(struct replay_opts *opts)
 	return 0;
 }
 
-static int walk_revs_populate_todo(struct commit_list **todo_list,
+static int walk_revs_populate_todo(struct todo_list *todo_list,
 				struct replay_opts *opts)
 {
+	enum todo_command command = opts->action == REPLAY_PICK ?
+		TODO_PICK : TODO_REVERT;
 	struct commit *commit;
-	struct commit_list **next;

sequencer.c static int read_populate_opts(struct replay_opts *opts) · line 874

Quoted 10 lines
sequencer.c +3 -3
 
 	if (prepare_revs(opts))
 		return -1;
 
-	next = todo_list;
-	while ((commit = get_revision(opts->revs)))
-		next = commit_list_append(commit, next);
+	while ((commit = get_revision(opts->revs))) {
+		struct todo_item *item = append_todo(todo_list);
+		const char *commit_buffer = get_commit_buffer(commit, NULL);

I see that you are creating todo file contents while walking revision list, something that was left for later in current / previous implementation of the sequencer...

[Added: I see it was done by format_todo() called from save_todo()]

sequencer.c static int read_populate_opts(struct replay_opts *opts) · line 883

Quoted 9 lines
sequencer.c +9 -0
+		const char *subject;
+		int subject_len;
+
+		item->command = command;
+		item->commit = commit;
+		item->offset_in_buf = todo_list->buf.len;
+		subject_len = find_commit_subject(commit_buffer, &subject);
+		strbuf_addf(&todo_list->buf, "%s %s %.*s\n",
+			opts->action == REPLAY_PICK ?  "pick" : "revert",

Wouldn't it be simpler to use

+ todo_command_strings[command],

Also, this string does not change during the loop, though I guess compiler should be able to optimize it.

sequencer.c static int read_populate_opts(struct replay_opts *opts) · line 886

Quoted 3 lines
sequencer.c +3 -0
+			find_unique_abbrev(commit->object.oid.hash,
+				DEFAULT_ABBREV),
+			subject_len, subject);

...Did format of the 'todo' file changed? And if yes, was it in backward compatible way, so that "git revert" or "git cherry-pick" started with old version of Git can be continued with new version, and what is also important (for somebody who sometimes uses system-installed Git, and sometimes user-compiled one) the reverse: started with new, continued with old?

Quoted 7 lines
sequencer.c +2 -0
+		unuse_commit_buffer(commit, commit_buffer);
+	}
 	return 0;
 }
 
@@ -964,30 +990,24 @@ static int sequencer_rollback(struct replay_opts *opts)
 	return -1;

sequencer.c static int sequencer_rollback(struct replay_opts *opts) · line 997

Quoted 10 lines
sequencer.c +3 -3
 }
 
-static int save_todo(struct commit_list *todo_list, struct replay_opts *opts)
+static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
 {
 	static struct lock_file todo_lock;
-	struct strbuf buf = STRBUF_INIT;
-	int fd;
+	const char *todo_path = get_todo_path(opts);
+	int next = todo_list->current, offset, fd;

The "next = todo_list->current" looks a bit strange. Also, we do not change todo_list->current, we use it in one place, so it can be used directly without help of temporary / helper variable. But that is just my personal opinion.

Also, from 'next', 'offset' and 'fd', all those are different uses of int: the index (int, rarely size_t), the offset in string (formally ptrdiff_t, or size_t, but usually int), and the file descriptor. I think from those the file descriptor could be kept in separate line; it would help diff to be more readable. But this is fairly marginal nitpicking, and a matter of personal opinion.

sequencer.c static int sequencer_rollback(struct replay_opts *opts) · line 1002

Quoted 6 lines
sequencer.c +1 -1
-	fd = hold_lock_file_for_update(&todo_lock, git_path_todo_file(), 0);
+	fd = hold_lock_file_for_update(&todo_lock, todo_path, 0);
 	if (fd < 0)
 		return error_errno(_("Could not lock '%s'"),
 				   git_path_todo_file());

We should use 'todo_path' here... and this should be done in one of earlier patches, isn't it?

This means that

  +	const char *todo_path = get_todo_path(opts);

should better be moved to earlier patch, too.

Or maybe not. But it looks like missed git_path_todo_file() -> get_todo_path(opts) [-> todo_path ] change. If it was left because of planned whole rewrite, it should be mentioned in the commit message of that earlier commit, isn't it?

sequencer.c static int sequencer_rollback(struct replay_opts *opts) · line 979

Quoted 3 lines
sequencer.c +0 -3
-	if (format_todo(&buf, todo_list, opts) < 0) {
-		strbuf_release(&buf);
-		return error(_("Could not format %s."), git_path_todo_file());

Can we still get this error? Could we get this error anyway, and under what conditions?

sequencer.c static int sequencer_rollback(struct replay_opts *opts) · line 1008

Quoted 12 lines
sequencer.c +6 -6
-	}
-	if (write_in_full(fd, buf.buf, buf.len) < 0) {
-		strbuf_release(&buf);
-		return error_errno(_("Could not write to %s"),
-				   git_path_todo_file());
-	}
+	offset = next < todo_list->nr ?
+		todo_list->items[next].offset_in_buf : todo_list->buf.len;
+	if (write_in_full(fd, todo_list->buf.buf + offset,
+			todo_list->buf.len - offset) < 0)
+		return error(_("Could not write to %s (%s)"),
+			todo_path, strerror(errno));

Ah, so it saves the remaining todo_items on todo_list, not the whole todo_list... the name does not fully show it.

sequencer.c static int sequencer_rollback(struct replay_opts *opts) · line 1010

Quoted 7 lines
sequencer.c +2 -5
-	if (commit_lock_file(&todo_lock) < 0) {
-		strbuf_release(&buf);
-		return error(_("Error wrapping up %s."), git_path_todo_file());
-	}
-	strbuf_release(&buf);
+	if (commit_lock_file(&todo_lock) < 0)
+		return error(_("Error wrapping up %s."), todo_path);

Note: this is unrelated change, but we usually put paths in quotes, like this

  +		return error(_("Error wrapping up '%s'."), todo_path);

(in this and earlier error message), so that paths containing spaces show correctly and readably to the user. Though this possibly is not a problem for this path.

Also, how user is to understand "wrapping up"?

Quoted 15 lines
sequencer.c +1 -2
 	return 0;
 }
 
@@ -1026,9 +1046,8 @@ static int save_opts(struct replay_opts *opts)
 	return res;
 }
 
-static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
+static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
 {
-	struct commit_list *cur;
 	int res;
 
 	setenv(GIT_REFLOG_ACTION, action_name(opts), 0);

sequencer.c static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts) · line 1060

Quoted 5 lines
sequencer.c +1 -1
@@ -1038,10 +1057,12 @@ static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
 	if (read_and_refresh_cache(opts))
 		return -1;
 
-	for (cur = todo_list; cur; cur = cur->next) {
+	while (todo_list->current < todo_list->nr) {

Why replace for loop with while loop? Especially that now it looks more for-y ;-)

  +	for ( ; todo_list->current < todo_list->nr; todo_list->current++) {

Oh... I now see why.

sequencer.c static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts) · line 1064

Quoted 6 lines
sequencer.c +3 -2
+		struct todo_item *item = todo_list->items + todo_list->current;
-		if (save_todo(cur, opts))
+		if (save_todo(todo_list, opts))
 			return -1;
-		res = do_pick_commit(cur->item, opts);
+		res = do_pick_commit(item->command, item->commit, opts);

I don't quite understand what sequencer tried to do here, but the change looks all right.

Quoted 15 lines
sequencer.c +3 -1
+		todo_list->current++;
 		if (res)
 			return res;
 	}
@@ -1066,7 +1087,8 @@ static int continue_single_pick(void)
 
 static int sequencer_continue(struct replay_opts *opts)
 {
-	struct commit_list *todo_list = NULL;
+	struct todo_list todo_list = TODO_LIST_INIT;
+	int res;
 
 	if (!file_exists(get_todo_path(opts)))
 		return continue_single_pick();

sequencer.c static int sequencer_continue(struct replay_opts *opts) · line 1111

Quoted 9 lines
sequencer.c +4 -2
@@ -1083,21 +1105,24 @@ static int sequencer_continue(struct replay_opts *opts)
 	}
 	if (index_differs_from("HEAD", 0))
 		return error_dirty_index(opts);
-	todo_list = todo_list->next;
-	return pick_commits(todo_list, opts);
+	todo_list.current++;
+	res = pick_commits(&todo_list, opts);
+	todo_list_release(&todo_list);
+	return res;

Nice. Looks correct.

sequencer.c static int sequencer_continue(struct replay_opts *opts) · line 1118

Quoted 8 lines
sequencer.c +2 -1
 }
 
 static int single_pick(struct commit *cmit, struct replay_opts *opts)
 {
 	setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
-	return do_pick_commit(cmit, opts);
+	return do_pick_commit(opts->action == REPLAY_PICK ?
+		TODO_PICK : TODO_REVERT, cmit, opts);

The ternary conditional operator here translates one enum to other enum, isn't it?

Quoted 13 lines
sequencer.c +2 -2
 }
 
 int sequencer_pick_revisions(struct replay_opts *opts)
 {
-	struct commit_list *todo_list = NULL;
+	struct todo_list todo_list = TODO_LIST_INIT;
 	unsigned char sha1[20];
-	int i;
+	int i, res;
 
 	if (opts->subcommand == REPLAY_NONE)
 		assert(opts->revs);

sequencer.c int sequencer_pick_revisions(struct replay_opts *opts) · line 1201

Quoted 7 lines
sequencer.c +3 -1
@@ -1171,7 +1196,9 @@ int sequencer_pick_revisions(struct replay_opts *opts)
 	if (save_head(sha1_to_hex(sha1)) ||
 			save_opts(opts))
 		return -1;
-	return pick_commits(todo_list, opts);
+	res = pick_commits(&todo_list, opts);
+	todo_list_release(&todo_list);
+	return res;

Looks correct. And consistent.

Quoted 4 lines
sequencer.c +0 -0
 }
 
 void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag)
#52 lore raw

Re: [PATCH 10/22] sequencer: avoid completely different messages for different actions

In reply to Johannes Schindelin

CC-ed to Jiang Xin, L10N coordinator.

W dniu 29.08.2016 o 10:05, Johannes Schindelin pisze:

Quoted 10 lines
---
 sequencer.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
sequencer.c +0 -0
index cbdce6d..1b65202 100644

sequencer.c static int error_dirty_index(struct replay_opts *opts) · line 236

Quoted 10 lines
sequencer.c +2 -5
@@ -232,11 +232,8 @@ static int error_dirty_index(struct replay_opts *opts)
 	if (read_cache_unmerged())
 		return error_resolve_conflict(action_name(opts));
 
-	/* Different translation strings for cherry-pick and revert */
-	if (opts->action == REPLAY_PICK)
-		error(_("Your local changes would be overwritten by cherry-pick."));
-	else
-		error(_("Your local changes would be overwritten by revert."));
+	error(_("Your local changes would be overwritten by %s."),
+		action_name(opts));

If I understand it correctly, it would make "revert" or "cherry-pick" untranslated part of error message. You would need to use translation on the result with "_(action_name(opts))", you would have to mark todo_command_strings elements for gettext lexicon with N_(...).

I am rather against this change (see also below).

From the first glance I though that there would be no problem with translation legos / jigsaw it introduces, namely that the "revert" and "cherry-pick" would require different rest of text:

po/bg.po:msgid "Your local changes would be overwritten by cherry-pick." po/bg.po-msgstr "Локалните ви промени ще бъдат презаписани при отбирането на подавания." --
po/bg.po:msgid "Your local changes would be overwritten by revert." po/bg.po-msgstr "Локалните ви промени ще бъдат презаписани при отмяната на подавания."

po/de.po:msgid "Your local changes would be overwritten by cherry-pick." po/de.po-msgstr "Ihre lokalen Änderungen würden durch den Cherry-Pick überschrieben werden." --
po/de.po:msgid "Your local changes would be overwritten by revert." po/de.po-msgstr "Ihre lokalen Änderungen würden durch den Revert überschrieben werden."

But it turns out that "revert" and "cherry-pick" can be of different gender:

po/ca.po:msgid "Your local changes would be overwritten by cherry-pick." po/ca.po-msgstr "Els vostres canvis locals se sobreescriurien pel recull de cireres." --
po/ca.po:msgid "Your local changes would be overwritten by revert." po/ca.po-msgstr "Els vostres canvis locals se sobreescriurien per la reversió."

In some cases "revert" and "cherry-pick" are not translated literally (but compare translation for similar language: po/bg.po, without this):

po/ru.po:msgid "Your local changes would be overwritten by cherry-pick." po/ru.po-msgstr "Ваши локальные изменение будут перезаписаны отбором лучшего." --
po/ru.po:msgid "Your local changes would be overwritten by revert." po/ru.po-msgstr "Ваши локальные изменение будут перезаписаны возвратом коммита."

Similar for (but here one side uses untranslated English term...):

po/vi.po:msgid "Your local changes would be overwritten by cherry-pick." po/vi.po-msgstr "Các thay đổi nội bộ của bạn có thể bị ghi đè bởi lệnh cherry-pick." --
po/vi.po:msgid "Your local changes would be overwritten by revert." po/vi.po-msgstr "Các thay đổi nội bộ của bạn có thể bị ghi đè bởi lệnh hoàn nguyên."

For some I don't know which is the case:

po/zh_CN.po:msgid "Your local changes would be overwritten by cherry-pick." po/zh_CN.po-msgstr "您的本地修改将被拣选操作覆盖。"
--
po/zh_CN.po:msgid "Your local changes would be overwritten by revert." po/zh_CN.po-msgstr "您的本地修改将被还原操作覆盖。"

Unless we want to require to use English terms:

po/sv.po:msgid "Your local changes would be overwritten by cherry-pick." po/sv.po-msgstr "Dina lokala ändringar skulle skrivas över av \"cherry-pick\"." --
po/sv.po:msgid "Your local changes would be overwritten by revert." po/sv.po-msgstr "Dina lokala ändringar skulle skrivas över av \"revert\"."

Quoted 4 lines
sequencer.c +0 -0
 	if (advice_commit_before_merge)
 		advise(_("Commit your changes or stash them to proceed."));
 
#57 lore raw

Re: [PATCH 11/22] sequencer: get rid of the subcommand field

In reply to Johannes Schindelin

W dniu 29.08.2016 o 10:05, Johannes Schindelin pisze:

Quoted 4 lines

The subcommands are used exactly once, at the very beginning of sequencer_pick_revisions(), to determine what to do. This is an unnecessary level of indirection: we can simply call the correct function to begin with. So let's do that.

Looks good. Parsing is moved from parse_args(), now unnecessary, to the new run_sequencer(). Which also picked up dispatch from sequencer_pick_revisions() - that sometimes didn't pick revisions :-o.

"All problems in computer science can be solved by another level
of indirection, except of course for the problem of too many indirections." -- David John Wheeler

Quoted 4 lines

While at it, ensure that the subcommands return an error code so that they do not have to die() all over the place (bad practice for library functions...).

This perhaps should be moved to a separate patch, but I guess there is a reason behind "while at it".

Also subcommand functions no longer are local to sequencer.c

Quoted 7 lines
---
 builtin/revert.c | 36 ++++++++++++++++--------------------
 sequencer.c      | 35 +++++++++++------------------------
 sequencer.h      | 13 ++++---------
 3 files changed, 31 insertions(+), 53 deletions(-)

Nice size reduction.

#37 lore raw

Re: [PATCH 08/22] sequencer: remove overzealous assumption

In reply to Jakub Narębski

Hi Kuba,

Quoted 6 lines · Jakub Narębski

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

Quoted 5 lines

The sequencer was introduced to make the cherry-pick and revert functionality available as library function, with the original idea being to extend the sequencer to also implement the rebase -i functionality.

Quoted 7 lines
Quoted 2 lines

The test to ensure that all of the commands in the script are identical to the overall operation does not mesh well with that.

Actually the question is what does the test that got removed in this commit actually check. Is it high-level sanity check that todo list for git-cherry-pick contains only 'pick', and for git-revert contains only 'revert'?

It might have been that at some stage.

But should we really check that? Or should we check the *effects*?

I am of the opinion that overzealous checking of certain implementation details is something to be avoided.

Quoted 6 lines
Quoted 3 lines

Therefore let's just get rid of the test that wants to verify that this limitation is still in place, in preparation for the upcoming work to teach the sequencer to do rebase -i's work.

Is it "upcoming work" as in one of the patches in this series? If so, which patch?

I left this a little vague, didn't I? ;-)

The problem is that the git-rebase-todo most definitely does *not* want to be restricted to a single command.

So if you must have a patch that disagrees with this overzealous check, the "revamp todo parsing" one is probably the first. But it is better to think of this at a higher level than just patches: it is wrong to limit the todo script to contain only identical commands.

Quoted 8 lines
Quoted 8 lines
t/t3510-cherry-pick-sequence.sh +0 -0
index 7b7a89d..6465edf 100755
@@ -459,17 +459,6 @@ test_expect_success 'malformed instruction sheet 1' '
 	test_expect_code 128 git cherry-pick --continue
 '
 
Quoted 8 lines

t/t3510-cherry-pick-sequence.sh test_expect_success 'malformed instruction sheet 1' ' · line 462

Quoted 1 line
t/t3510-cherry-pick-sequence.sh +0 -1
-test_expect_success 'malformed instruction sheet 2' '

Hmmm... the description is somewhat lacking (especially compared to the rest of test), anyway.

BTW. we should probably rename 'malformed instruction sheet 2' to 'malformed instruction sheet' if there are no further such tests after this removal, isn't it?

No, we cannot rename it after this patch because the patch removes it ;-) (It is not a file name but really a label for a test case.)

Thanks for the review,
Johannes

#63 lore raw

Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

In reply to Johannes Schindelin

W dniu 29.08.2016 o 10:05, Johannes Schindelin pisze:

Quoted 10 lines

The git-rebase-todo file contains a list of commands. Most of those commands have the form

	<verb> <sha1> <oneline>

The <oneline> is displayed primarily for the user's convenience, as rebase -i really interprets only the <verb> <sha1> part. However, there are *some* places in interactive rebase where the <oneline> is used to display messages, e.g. for reporting at which commit we stopped.

Quoted 4 lines

So let's just remember it when parsing the todo file; we keep a copy of the entire todo file anyway (to write out the new done and git-rebase-todo file just before processing each command), so all we need to do is remember the begin and end offsets.

Actually what we remember is pointer and length, or begin offset and length, not offset and offset.

Quoted 2 lines

Nice, I'll see how it is used later (and in which commit in series).

Quoted 9 lines
---
 sequencer.c | 7 +++++++
 1 file changed, 7 insertions(+)
sequencer.c +0 -0
index 06759d4..3398774 100644

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 713

Quoted 5 lines
sequencer.c +2 -0
@@ -709,6 +709,8 @@ static int read_and_refresh_cache(struct replay_opts *opts)
 struct todo_item {
 	enum todo_command command;
 	struct commit *commit;
+	const char *arg;
+	int arg_len;

Why 'arg', and not 'oneline', or 'subject'?
I'm not saying it is bad name.

sequencer.c static int parse_insn_line(struct todo_item *item, const char *bol, char *eol) · line 767

Quoted 10 lines
sequencer.c +3 -0
 	size_t offset_in_buf;
 };
 
@@ -760,6 +762,9 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)
 	status = get_sha1(bol, commit_sha1);
 	*end_of_object_name = saved;
 
+	item->arg = end_of_object_name + strspn(end_of_object_name, " \t");
+	item->arg_len = (int)(eol - item->arg);
+

Does it work correctly for line without <oneline>, that is

<verb> <sha1>

I think it does, but I not entirely sure.

Quoted 13 lines
sequencer.c +2 -0
 	if (status < 0)
 		return -1;
 
@@ -880,6 +885,8 @@ static int walk_revs_populate_todo(struct todo_list *todo_list,
 
 		item->command = command;
 		item->commit = commit;
+		item->arg = NULL;
+		item->arg_len = 0;
 		item->offset_in_buf = todo_list->buf.len;
 		subject_len = find_commit_subject(commit_buffer, &subject);
 		strbuf_addf(&todo_list->buf, "%s %s %.*s\n",
#38 lore raw

Re: [PATCH 08/22] sequencer: remove overzealous assumption

In reply to Johannes Schindelin

Hello Johannes,

W dniu 31.08.2016 o 20:36, Johannes Schindelin pisze:

Quoted 5 lines · Jakub Narębski
Quoted 1 line

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

I am of the opinion that overzealous checking of certain implementation details is something to be avoided.

I agree.

Quoted 9 lines
Quoted 6 lines
Quoted 3 lines

Therefore let's just get rid of the test that wants to verify that this limitation is still in place, in preparation for the upcoming work to teach the sequencer to do rebase -i's work.

Is it "upcoming work" as in one of the patches in this series? If so, which patch?

I left this a little vague, didn't I? ;-)

Quoted 7 lines

The problem is that the git-rebase-todo most definitely does *not* want to be restricted to a single command.

So if you must have a patch that disagrees with this overzealous check, the "revamp todo parsing" one is probably the first. But it is better to think of this at a higher level than just patches: it is wrong to limit the todo script to contain only identical commands.

I see. Right.

I wonder: would 'git cherry-pick --continue' be able to finish 'git revert', and vice versa, then? Or 'git sequencer --continue'?

Quoted 13 lines
Quoted 13 lines

t/t3510-cherry-pick-sequence.sh test_expect_success 'malformed instruction sheet 1' ' · line 462

Quoted 9 lines
t/t3510-cherry-pick-sequence.sh +0 -1
index 7b7a89d..6465edf 100755
@@ -459,17 +459,6 @@ test_expect_success 'malformed instruction sheet 1' '
 	test_expect_code 128 git cherry-pick --continue
 '
 
-test_expect_success 'malformed instruction sheet 2' '

Hmmm... the description is somewhat lacking (especially compared to the rest of test), anyway.

Quoted 6 lines
Quoted 3 lines

BTW. we should probably rename 'malformed instruction sheet 2' to 'malformed instruction sheet' if there are no further such tests after this removal, isn't it?

No, we cannot rename it after this patch because the patch removes it ;-) (It is not a file name but really a label for a test case.)

Ooops. What I wanted to say that after removing the test case named 'malformed instruction sheet 2' we should also rename *earlier* test case from 'malformed instruction sheet 1' to 'malformed instruction sheet', as it is now the only 'malformed instruction sheet *' test case.

#41 lore raw

Re: [PATCH 08/22] sequencer: remove overzealous assumption

In reply to Johannes Schindelin
Quoted 2 lines · Johannes Schindelin

The problem is that the git-rebase-todo most definitely does *not* want to be restricted to a single command.

Looking at the test in question, it is about what happens when "git cherry-pick A..B" runs, and the test assumes it gets a sequence of "pick". It would be a bug to see any "revert" in there if you are doing a multi cherry-pick.

And what is checked is if "cherry-pick --continue" notices a malformed instruction that has something other than "pick".

If the final invocation were "sequencer --continue", then I perfectly well understand why it is not a good idea to limit the set of instructions only to "pick" (or "revert"). But I do not think the test performed here is for that general case. The user knows Git stopped in the middle of cherry-pick and expects cherry-pick to continue.

Quoted 4 lines

So if you must have a patch that disagrees with this overzealous check, the "revamp todo parsing" one is probably the first. But it is better to think of this at a higher level than just patches: it is wrong to limit the todo script to contain only identical commands.

So if you think of this at even higher level, the check done in parse_insn_line() that _assumes_ that opts->action must match the actions on each line is _WRONG_, but what this test expects to see is perfectly reasonable, I would think.

It is a different matter if it makes sense to _verify_ that the user didn't make nonsensical change to the generated insn and error out when s/he did. I tend to think it is pointless, and I wouldn't object if this test is removed due to that reason.

#64 lore raw

Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

In reply to Jakub Narębski
Quoted 5 lines · Jakub Narębski
Quoted 5 lines
sequencer.c +0 -0
index 06759d4..3398774 100644
@@ -709,6 +709,8 @@ static int read_and_refresh_cache(struct replay_opts *opts)
Quoted 8 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 713

Quoted 5 lines
sequencer.c +2 -0
 struct todo_item {
 	enum todo_command command;
 	struct commit *commit;
+	const char *arg;
+	int arg_len;

Why 'arg', and not 'oneline', or 'subject'?
I'm not saying it is bad name.

I am not sure what the "commit" field of type "struct commit *" is for. It is not needed until it is the commit's turn to be picked or reverted; if we end up stopping in the middle, parsing the commit object for later steps will end up being wasted effort.

Also, when the sequencer becomes one sequencer to rule them all, the command set may contain something that does not even mention any commit at all (think: exec).

So I am not sure if we want a parsed commit there (I would not object if we kept the texual object name read from the file, though). The "one sequencer to rule them all" may even have to say "now give name ':1' to the result of the previous operation" in one step and in another later step have an instruction "merge ':1'". When that happens, you cannot even pre-populate the commit object when the sequencer reads the file, as the commit has not yet been created at that point.

#65 lore raw

Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

W dniu 31.08.2016 o 21:10, Junio C Hamano pisze:

Quoted 17 lines
Quoted 10 lines · Jakub Narębski

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 713

Quoted 10 lines
sequencer.c +2 -0
index 06759d4..3398774 100644
@@ -709,6 +709,8 @@ static int read_and_refresh_cache(struct replay_opts *opts)
 struct todo_item {
 	enum todo_command command;
 	struct commit *commit;
+	const char *arg;
+	int arg_len;

I am not sure what the "commit" field of type "struct commit *" is for. It is not needed until it is the commit's turn to be picked or reverted; if we end up stopping in the middle, parsing the commit object for later steps will end up being wasted effort.

From what I understand this was what sequencer did before this series, so it is not a regression (I think; the commit parsing was in different function, but I think at the same place in the callchain).

Quoted 4 lines

Also, when the sequencer becomes one sequencer to rule them all, the command set may contain something that does not even mention any commit at all (think: exec).

The "exec" line is a bit of exception, all other rebase -i commands take commit as parameter. It could always use NULL.

Quoted 9 lines

So I am not sure if we want a parsed commit there (I would not object if we kept the texual object name read from the file, though). The "one sequencer to rule them all" may even have to say "now give name ':1' to the result of the previous operation" in one step and in another later step have an instruction "merge ':1'". When that happens, you cannot even pre-populate the commit object when the sequencer reads the file, as the commit has not yet been created at that point.

True, --preserve-merges rebase is well, different.

Best,

Signature · 1 line
Jakub Narebski
#66 lore raw

Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

Quoted 12 lines · Jakub Narębski
Quoted 11 lines
Quoted 6 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 713

Quoted 6 lines
sequencer.c +2 -0
@@ -709,6 +709,8 @@ static int read_and_refresh_cache(struct replay_opts *opts)
 struct todo_item {
 	enum todo_command command;
 	struct commit *commit;
+	const char *arg;
+	int arg_len;

I am not sure what the "commit" field of type "struct commit *" is for. It is not needed until it is the commit's turn to be picked or reverted; if we end up stopping in the middle, parsing the commit object for later steps will end up being wasted effort.

Quoted 4 lines

From what I understand this was what sequencer did before this series, so it is not a regression (I think; the commit parsing was in different function, but I think at the same place in the callchain).

Yes, I agree with you and I didn't mean "this is a new bug" at all. It just is an indication that further refactoring after this step is needed, and it is likely to involve removal or modification of this field.

#92 lore raw

Re: [PATCH 16/22] sequencer: prepare for rebase -i's GPG settings

In reply to Johannes Schindelin

Hello Johannes,

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

Quoted 2 lines

The rebase command sports a --gpg-sign option that is heeded by the interactive rebase.

Should it be "sports" or "supports"?

Quoted 22 lines

This patch teaches the sequencer that trick, as part of the bigger effort to make the sequencer the work horse of the interactive rebase.

---
 sequencer.c | 48 +++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 43 insertions(+), 5 deletions(-)
sequencer.c +1 -0
index 4204cc8..e094ac2 100644
@@ -15,6 +15,7 @@
 #include "merge-recursive.h"
 #include "refs.h"
 #include "argv-array.h"
+#include "quote.h"
 
 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
 

sequencer.c static GIT_PATH_FUNC(git_path_head_file, "sequencer/head") · line 40

Quoted 7 lines
sequencer.c +4 -0
@@ -33,6 +34,11 @@ static GIT_PATH_FUNC(git_path_head_file, "sequencer/head")
  * being rebased.
  */
 static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script")
+/*
+ * The following files are written by git-rebase just after parsing the
+ * command-line (and are only consumed, not modified, by the sequencer).
+ */

It is good to have this comment here.

sequencer.c static GIT_PATH_FUNC(git_path_head_file, "sequencer/head") · line 41

Quoted 1 line
sequencer.c +1 -0
+static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")

I know it is not your fault, but I wonder why this file uses snake_case_name, while all other use kebab-case-names. That is, why it is gpg_sign_opt and not gpg-sign-opt.

Quoted 6 lines
sequencer.c +0 -0
 /* We will introduce the 'interactive rebase' mode later */
 #define IS_REBASE_I() 0
@@ -129,6 +135,16 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
 	return 1;
 }

sequencer.c static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob, · line 146

Quoted 10 lines
sequencer.c +9 -0
 
+static const char *gpg_sign_opt_quoted(struct replay_opts *opts)
+{
+	static struct strbuf buf = STRBUF_INIT;
+
+	strbuf_reset(&buf);
+	if (opts->gpg_sign)
+		sq_quotef(&buf, "-S%s", opts->gpg_sign);
+	return buf.buf;
+}

All right, this function is quite clear.

Sidenote: it's a pity api-quote.txt is just a placeholder for proper documentation (including sq_quotef()). I also wonder why it is not named sq_quotef_buf() or strbuf_addf_sq().

Quoted 6 lines
sequencer.c +1 -0
+
 void *sequencer_entrust(struct replay_opts *opts, void *set_me_free_after_use)
 {
 	ALLOC_GROW(opts->owned, opts->owned_nr + 1, opts->owned_alloc);
@@ -471,17 +487,20 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 

sequencer.c int sequencer_commit(const char *defmsg, struct replay_opts *opts, · line 478

Quoted 10 lines
sequencer.c +3 -2
 	if (IS_REBASE_I()) {
 		env = read_author_script();
-		if (!env)
+		if (!env) {
+			const char *gpg_opt = gpg_sign_opt_quoted(opts);
+
 			return error("You have staged changes in your working "
 				"tree. If these changes are meant to be\n"
 				"squashed into the previous commit, run:\n\n"
-				"  git commit --amend $gpg_sign_opt_quoted\n\n"

How did this get expanded by error(), and why we want to replace it if it works?

sequencer.c int sequencer_commit(const char *defmsg, struct replay_opts *opts, · line 502

Quoted 9 lines
sequencer.c +3 -2
+				"  git commit --amend %s\n\n"
 				"If they are meant to go into a new commit, "
 				"run:\n\n"
-				"  git commit $gpg_sign_opt_quoted\n\n"
+				"  git commit %s\n\n"
 				"In both case, once you're done, continue "
 				"with:\n\n"
-				"  git rebase --continue\n");
+				"  git rebase --continue\n", gpg_opt, gpg_opt);

Instead of passing option twice, why not make use of %1$s (arg reordering), that is

+ " git commit --amend %1$s\n\n"
[...]
+ " git commit %1$s\n\n"

sequencer.c int sequencer_commit(const char *defmsg, struct replay_opts *opts, · line 503

Quoted 1 line
sequencer.c +1 -0
+		}

So shell quoting is required only for error output.

Quoted 8 lines
sequencer.c +0 -1
 	}
 
 	argv_array_init(&array);
@@ -955,8 +974,27 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
 
 static int read_populate_opts(struct replay_opts *opts)
 {
-	if (IS_REBASE_I())

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 986

Quoted 10 lines
sequencer.c +10 -0
+	if (IS_REBASE_I()) {
+		struct strbuf buf = STRBUF_INIT;
+
+		if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), 1)) {
+			if (buf.len && buf.buf[buf.len - 1] == '\n') {
+				if (--buf.len &&
+				    buf.buf[buf.len - 1] == '\r')
+					buf.len--;
+				buf.buf[buf.len] = '\0';
+			}

Isn't there some strbuf_chomp() / strbuf_strip_eof() function? Though as strbuf_getline() uses something similar...

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 989

Quoted 3 lines
sequencer.c +3 -0
+
+			if (!starts_with(buf.buf, "-S"))
+				strbuf_reset(&buf);

Should we signal that there was problem with a file contents?

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 992

Quoted 3 lines
sequencer.c +3 -0
+			else {
+				opts->gpg_sign = buf.buf + 2;
+				strbuf_detach(&buf, NULL);

Wouldn't we leak 2 characters that got skipped? Maybe xstrdup would be better (if it is leaked, and not reattached)?

Quoted 9 lines
sequencer.c +4 -0
+			}
+		}
+
 		return 0;
+	}
 
 	if (!file_exists(git_path_opts_file()))
 		return 0;
Signature · 1 line
Jakub Narębski
#93 lore raw

Re: [PATCH 16/22] sequencer: prepare for rebase -i's GPG settings

Quoted 6 lines · Jakub Narębski

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 992

Quoted 3 lines
sequencer.c +3 -0
+			else {
+				opts->gpg_sign = buf.buf + 2;
+				strbuf_detach(&buf, NULL);

Wouldn't we leak 2 characters that got skipped? Maybe xstrdup would be better (if it is leaked, and not reattached)?

An attempt to avoid leaking by calling free(opts->gpg_sign) would make it crash, which would be even worse ;-).

#94 lore raw

Re: [PATCH 16/22] sequencer: prepare for rebase -i's GPG settings

W dniu 31.08.2016 o 22:12, Junio C Hamano pisze:

Quoted 10 lines
Quoted 8 lines · Jakub Narębski

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 992

Quoted 3 lines · Johannes Schindelin
sequencer.c +3 -0
+			else {
+				opts->gpg_sign = buf.buf + 2;
+				strbuf_detach(&buf, NULL);

Wouldn't we leak 2 characters that got skipped? Maybe xstrdup would be better (if it is leaked, and not reattached)?

Quoted 2 lines

An attempt to avoid leaking by calling free(opts->gpg_sign) would make it crash, which would be even worse ;-).

Actually, from C standard:

"If ptr [in free(ptr)] does not point to a block of memory allocated
with the above functions [malloc(), etc.], it causes undefined behavior."

                                                      ^^^^^^^^^

Which is even worse if it does not lead to crash.

Note that if the last line was

    +				sequencer_entrust(opts, strbuf_detach(&buf, NULL));

we can make it not leak. A bit tricksy, though.

Though xstrdup(buf.buf + 2) followed by strbuf_release(&buf) would make free(opts->gpg_sign) possible without crash. That is we can work without *_entrust() mechanism at the cost of strdups.

Signature · 1 line
Jakub Narębski
#95 lore raw

Re: [PATCH 16/22] sequencer: prepare for rebase -i's GPG settings

Quoted 3 lines · Jakub Narębski

Though xstrdup(buf.buf + 2) followed by strbuf_release(&buf) would make free(opts->gpg_sign) possible without crash. That is we can work without *_entrust() mechanism at the cost of strdups.

Absolutely.

It is not like entrust() thing is free of allocation cost (it needs to allocate an array of pointers to keep track of what to free) or programmer's mental burden (you need to be careful what to entrust() and what not to), so "at the cost of strdup(3)" is reasonable cost of doing business in the way normal people expect the code to work.

#101 lore raw

Re: [PATCH 17/22] sequencer: allow editing the commit message on a case-by-case basis

In reply to Johannes Schindelin

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

Quoted 5 lines

In the upcoming commits, we will implement more and more of rebase -i's functionality. One particular feature of the commands to come is that some of them allow editing the commit message while others don't, i.e. we cannot define in the replay_opts whether the commit message should be edited or not.

It's a nice, pretty and self contained refactoring step. Small enough that it is easy to review.

I would like to have in the commit message that it is sequencer_commit() function that needs to rely on new parameter, instead of on a property of command (of its replay_opts). And that currently it simply passes the buck to caller, which uses opts->edit, but in the future the caller that is rebase -i would use todo_item and replay_opts based expression.

Quoted 52 lines
---
 sequencer.c | 6 +++---
 sequencer.h | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
sequencer.c +3 -3
index e094ac2..7e17d14 100644
@@ -478,7 +478,7 @@ static char **read_author_script(void)
  * (except, of course, while running an interactive rebase).
  */
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty)
+			  int allow_empty, int edit)
 {
 	char **env = NULL;
 	struct argv_array array;
@@ -513,7 +513,7 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 		argv_array_push(&array, "-s");
 	if (defmsg)
 		argv_array_pushl(&array, "-F", defmsg, NULL);
-	if (opts->edit)
+	if (edit)
 		argv_array_push(&array, "-e");
 	else if (!opts->signoff && !opts->record_origin &&
 		 git_config_get_value("commit.cleanup", &value))
@@ -779,7 +779,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	}
 	if (!opts->no_commit)
 		res = sequencer_commit(opts->edit ? NULL : git_path_merge_msg(),
-			opts, allow);
+			opts, allow, opts->edit);
 
 leave:
 	free_message(commit, &msg);
sequencer.h +1 -1
index 9f63c31..fd02baf 100644
@@ -50,7 +50,7 @@ int sequencer_rollback(struct replay_opts *opts);
 int sequencer_remove_state(struct replay_opts *opts);
 
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty);
+			  int allow_empty, int edit);
 
 extern const char sign_off_header[];
 
#104 lore raw

Re: [PATCH 18/22] sequencer: support amending commits

In reply to Johannes Schindelin

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

Quoted 9 lines

This teaches the sequencer_commit() function to take an argument that will allow us to implement "todo" commands that need to amend the commit messages ("fixup", "squash" and "reword").

---
 sequencer.c | 6 ++++--
 sequencer.h | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)

Nice and small addition of a new feature, a scaffolding for implementing rebase -i using the sequencer.

sequencer.c static char **read_author_script(void) · line 481

Quoted 11 lines
sequencer.c +1 -1
index 7e17d14..20f7590 100644
@@ -478,7 +478,7 @@ static char **read_author_script(void)
  * (except, of course, while running an interactive rebase).
  */
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty, int edit)
+			  int allow_empty, int edit, int amend)

I guess we won't get much more parameters; it would get unwieldy (and hard to remember). Five is all right.

Quoted 13 lines
sequencer.c +2 -0
 {
 	char **env = NULL;
 	struct argv_array array;
@@ -507,6 +507,8 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 	argv_array_push(&array, "commit");
 	argv_array_push(&array, "-n");
 
+	if (amend)
+		argv_array_push(&array, "--amend");
 	if (opts->gpg_sign)
 		argv_array_pushf(&array, "-S%s", opts->gpg_sign);
 	if (opts->signoff)

sequencer.c static int do_pick_commit(enum todo_command command, struct commit *commit, · line 784

Quoted 5 lines
sequencer.c +1 -1
@@ -779,7 +781,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	}
 	if (!opts->no_commit)
 		res = sequencer_commit(opts->edit ? NULL : git_path_merge_msg(),
-			opts, allow, opts->edit);
+			opts, allow, opts->edit, 0);

... even of this makes one need to check the calling convention, what does this 0 mean.

Quoted 17 lines
sequencer.c +0 -0
 leave:
 	free_message(commit, &msg);
sequencer.h +1 -1
index fd02baf..2106c0d 100644
@@ -50,7 +50,7 @@ int sequencer_rollback(struct replay_opts *opts);
 int sequencer_remove_state(struct replay_opts *opts);
 
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty, int edit);
+			  int allow_empty, int edit, int amend);
 
 extern const char sign_off_header[];
 
#45 lore raw

Re: [PATCH 09/22] sequencer: completely revamp the "todo" script parsing

In reply to Jakub Narębski
Quoted 4 lines · Jakub Narębski

BTW. perhaps we would be able to continue with 'git continue', regardless of what we have started with, I wonder...

git continue as a shorthand for git <relevant-cmd> --continue sounds great.

If we were to introduce that, I think we would need to unify the rest as well then, e.g.
Both revert as well as cherry-pick have --quit as well as --abort, but rebase doesn't have --quit documented, but instead an additional --skip and --edit-todo

Would we pull these all up as a top level command? (That sounds not so great to me)

#46 lore raw

Re: [PATCH 09/22] sequencer: completely revamp the "todo" script parsing

Hi Kuba and Stefan,

Quoted 8 lines · Stefan Beller
Quoted 4 lines · Jakub Narębski

BTW. perhaps we would be able to continue with 'git continue', regardless of what we have started with, I wonder...

git continue as a shorthand for git <relevant-cmd> --continue sounds great.

Before we get ahead of ourselves:

1) this has nothing to do with the patch series at hand, and

2) if we were to introduce git continue, we would need to think long and hard about the following issues:

I) are there potentially ambiguous <relevant-cmd>s that the user may want to continue?

II) what about options? You can say `git rebase --continue
--no-ff, for example, but not git cherry-pick --continue --no-ff`...

III) Would it not be confusing to have a subcommand continue
that does *not* serve a *single* purpose? It's kinda flying into the face of the Unix philosophy.

Ciao,
Dscho

#48 lore raw

Re: [PATCH 09/22] sequencer: completely revamp the "todo" script parsing

In reply to Jakub Narębski

Hi Kuba,

Quoted 12 lines · Jakub Narębski

W dniu 29.08.2016 o 10:05, Johannes Schindelin pisze:

Quoted 10 lines

When we came up with the "sequencer" idea, we really wanted to have kind of a plumbing equivalent of the interactive rebase. Hence the choice of words: the "todo" script, a "pick", etc.

However, when it came time to implement the entire shebang, somehow this idea got lost and the sequencer was used as working horse for cherry-pick and revert instead. So as not to interfere with the interactive rebase, it even uses a separate directory to store its state.

Quoted 8 lines
Quoted 4 lines

Furthermore, it also is stupidly strict about the "todo" script it
accepts: while it parses commands in a way that was *designed* to be similar to the interactive rebase, it then goes on to *error out* if the commands disagree with the overall action (cherry-pick or revert).

Does this mean that after the change you would be able to continue "git revert" with "git cherry-pick --continue", and vice versa? Or that it would be possible for git-cherry-pick to do reverts (e.g. with ^<rev>)?

I guess that I allow that now. Is it harmful? I dunno.

Quoted 10 lines
Quoted 5 lines

Let's just bite the bullet and rewrite the entire parser; the code now becomes not only more elegant: it allows us to go on and teach the sequencer how to parse *true* "todo" scripts as used by the interactive rebase itself. In a way, the sequencer is about to grow up to do its older brother's job. Better.

Sidenote: this is not your fault, but Git doesn't do a good job on changes which are mostly rewrites, trying to match stray '}' and the like in generated diff. I wonder if existing diff heuristic options could help here.

I guess --patience would have helped. Or Michael's upcoming diff-heuristics.

Quoted 9 lines
Quoted 3 lines

While at it, do not stop at the first problem, but list *all* of the problems. This helps the user by allowing to address all issues in one go rather than going back and forth until the todo list is valid.

That is also a good change, though I wonder how often users need to worry about this outside interactive rebase case. If it is preparation for rebase -i, where instruction list is written by prone to errors human, it would be nice to have this information in the commit message.

Okay.

Quoted 31 lines

sequencer.c static int allow_empty(struct replay_opts *opts, struct commit *commit) · line 479

Quoted 13 lines
sequencer.c +4 -1
index 982b6e9..cbdce6d 100644
@@ -473,7 +473,26 @@ static int allow_empty(struct replay_opts *opts, struct commit *commit)
 		return 1;
 }
 
-static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
+enum todo_command {
+	TODO_PICK,
+	TODO_REVERT
+};

Do we have a naming convention for enums elements? Or are we explicitly making enums and #defines interchangeable? I wonder...

...uh, I see we don't have naming convention, but all caps snake-case names dominate:

  $ git grep -A2 'enum .* {'
  [...]
  diff.h:enum color_diff {
  diff.h- DIFF_RESET = 0,
  diff.h- DIFF_CONTEXT = 1,
  --
  dir.c:enum path_treatment {
  dir.c-  path_none = 0,
  dir.c-  path_recurse,
  --
Quoted 1 line

Shouldn't we say 'TODO_PICK = 0' explicitly, though?

Sure.

Quoted 8 lines

sequencer.c static int allow_empty(struct replay_opts *opts, struct commit *commit) · line 484

Quoted 4 lines
sequencer.c +4 -0
+static const char *todo_command_strings[] = {
+	"pick",
+	"revert"
+};

It's a bit pity that we cannot use designated inits, and hanging comma, (from ISO C99 standard). That is:

Quoted 4 lines

+static const char *todo_command_strings[] = {
+ [TODO_PICK] = "pick",
+ [TODO_REVERT] = "revert",
+};

I agree, it is a pity. I could do something like I did in fsck.c:

	#define FOREACH_TODO_COMMAND(FUNC) \
		FUNC(PICK, "pick") \
		FUNC(REVERT, "revert")

	#define COMMAND_ID(id, string) TODO_##id,
	enum todo_command {
		FOREACH_TODO_COMMAND(COMMAND_ID)
		TODO_END
	};
	#undef COMMAND_ID

	#define COMMAND_ID(id, string) string,
	static const char *todo_command_string[] = {
		FOREACH_TODO_COMMAND(COMMAND_ID)
		NULL
	};
	#undef COMMAND_ID

However, this is not even readable, let alone any other type of an improvement. So I won't.

Quoted 9 lines

sequencer.c static int do_pick_commit(struct commit *commit, struct replay_opts *opts)

Quoted 1 line
sequencer.c +0 -0
@@ -548,7 +568,7 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)

From here on changes are about

s/opts->action == REPLAY_\(PICK\|REVERT\)/command == TODO_\1/

Do we still need opts->action, or it is just needed less, and it is 'todo' instruction that decides about command (as it should)?

We need opts->action. For example, the state directory changes depending on it: REPLAY_INTERACTIVE_REBASE stores its stuff in git_path("rebase-merge").

There is lots more behavior that also changes depending on opts->action.

Quoted 10 lines

sequencer.c static int do_pick_commit(struct commit *commit, struct replay_opts *opts) · line 651

Quoted 7 lines

[...]

sequencer.c +1 -1
 	if (res) {
-		error(opts->action == REPLAY_REVERT
+		error(command == TODO_REVERT
 		      ? _("could not revert %s... %s")
 		      : _("could not apply %s... %s"),
 		      find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV),

And here those changes end.

Quoted 4 lines

s/opts->action == REPLAY_\(PICK\|REVERT\)/command == TODO_\1/

I wonder if Coccinelle / Undebt would help here; or would simple sed or query-and-replace-regexp be enough...

I did this by hand, to verify that I did nothing idiotic.

Quoted 4 lines
Quoted 4 lines
sequencer.c +0 -0
@@ -683,116 +703,107 @@ static int read_and_refresh_cache(struct replay_opts *opts)
 	return 0;
 }
 
Quoted 16 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 716

Quoted 13 lines
sequencer.c +11 -2
-static int format_todo(struct strbuf *buf, struct commit_list *todo_list,
-		struct replay_opts *opts)
+struct todo_item {
+	enum todo_command command;
+	struct commit *commit;
+	size_t offset_in_buf;
+};
+
+struct todo_list {
+	struct strbuf buf;
+	struct todo_item *items;
+	int nr, alloc, current;
+};

So there should be s/commit_list [*]{1,2}todo_list/todo_list *todo_list/ from here on?

Almost, but not quite.

Quoted 1 line

Hmmm... commit_list is, as defined in commit.h, a linked list.

That is the most prominent reason why the rest is not a mindless conversion from commit_list to todo_list.

And we need todo_list as an array, because we need to be able to peek, or even move, backwards from the current command.

Quoted 3 lines

Here todo_list uses growable array implementation of list. Which is I guess better on current CPU architecture, with slow memory, limited-size caches, and adjacency prefetching.

That is not the reason that an array is used here. The array allows us much more flexibility.

One of the major performance improvements will come at the very end, for
example: the reordering of the fixup!/squash! lines. And that would be a *major* pain to do if the todo_list were still a linked list.

Quoted 5 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 718

Quoted 1 line
sequencer.c +1 -0
+#define TODO_LIST_INIT { STRBUF_INIT, NULL, 0, 0, 0 }

Same as with other patches in this series, it would be enough to

+#define TODO_LIST_INIT { STRBUF_INIT }

As it happens, after Hannes' comment about REPLAY_OPTIONS_INIT, I already had changed TODO_LIST_INIT as indicated. I just had no time to send out another iteration (besides, I wanted to give the sequencer-i patch series more visibility).

Quoted 1 line

You are consistent.

Thank you!

Quoted 11 lines
Quoted 11 lines
sequencer.c +5 -5
-	struct commit_list *cur = NULL;
-	const char *sha1_abbrev = NULL;
-	const char *action_str = opts->action == REPLAY_REVERT ? "revert" : "pick";
-	const char *subject;
-	int subject_len;
+	strbuf_release(&todo_list->buf);
+	free(todo_list->items);
+	todo_list->items = NULL;
+	todo_list->nr = todo_list->alloc = 0;
+}
 
Quoted 13 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 728

Quoted 10 lines
sequencer.c +1 -9
-	for (cur = todo_list; cur; cur = cur->next) {
-		const char *commit_buffer = get_commit_buffer(cur->item, NULL);
-		sha1_abbrev = find_unique_abbrev(cur->item->object.oid.hash, DEFAULT_ABBREV);
-		subject_len = find_commit_subject(commit_buffer, &subject);
-		strbuf_addf(buf, "%s %s %.*s\n", action_str, sha1_abbrev,
-			subject_len, subject);
-		unuse_commit_buffer(cur->item, commit_buffer);
-	}
-	return 0;
+struct todo_item *append_todo(struct todo_list *todo_list)

Errr... I don't quite understand the name of this function. What are you appending here to the todo_list?

A new item.

Quoted 2 lines

Compare string_list_append() and string_list_append_nodup(), where the second parameter is item to append.

Yes, that is correct. In the case of a todo_item, things are a lot more complicated, though. Some of the values have to be determined tediously (such as the offset and length of the oneline after the "pick <oid>" command). I just put those values directly into the newly allocated item, is all.

Quoted 8 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 734

Quoted 6 lines
sequencer.c +3 -1
+	ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc);
+	return todo_list->items + todo_list->nr++;
 }
 
-static struct commit *parse_insn_line(char *bol, char *eol, struct replay_opts *opts)
+static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)

Why the change of return type?

Because it makes no sense to return a commit here because not all commands are about commits (think rebase -i's exec). It makes tons of sense to return an error condition, though.

Quoted 1 line

Why now struct todo_item is first when struct replay_opts was last?

Those play very, very different roles.

The opts parameter used to provide parse_insn_line() with enough information to complain loudly when the overall command was not identical to the parsed command.

The item parameter is a receptacle for the parsed data. It will contain the pointer to the commit that was previously returned, if any. But it will also contain much more information, such as the command, the oneline, the offset in the buffer, etc etc

So "opts" was an "in" parameter while "item" is an "out" one. Apples and oranges.

Quoted 5 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 741

Quoted 2 lines
sequencer.c +2 -0
+	for (i = 0; i < ARRAY_SIZE(todo_command_strings); i++)
+		if (skip_prefix(bol, todo_command_strings[i], &bol)) {

skip_prefix() is such a nice abstraction...

Quoted 8 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 744

Quoted 3 lines
sequencer.c +3 -0
+			item->command = i;
+			break;
+		}

Nice. Replacing if-elsif chain with loop.

I guess any hashmap would be serious overkill, as there are and would be only a few actions possible.

If at all, we should use a trie here. But as you said: overkill to the max.

Quoted 10 lines
Quoted 10 lines
sequencer.c +3 -1
+	if (i >= ARRAY_SIZE(todo_command_strings))
+		return -1;
 
 	/* Eat up extra spaces/ tabs before object name */
 	padding = strspn(bol, " \t");
 	if (!padding)
-		return NULL;
+		return -1;
 	bol += padding;
 
Quoted 4 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 754

Quoted 2 lines
sequencer.c +1 -1
-	end_of_object_name = bol + strcspn(bol, " \t\n");
+	end_of_object_name = (char *) bol + strcspn(bol, " \t\n");

Why is this cast needed?

Because bol is a "const char *" and we need to put "NUL" temporarily to *end_of_object_name:

Quoted 4 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 758

Quoted 4 lines
sequencer.c +0 -0
 	saved = *end_of_object_name;
 	*end_of_object_name = '\0';
 	status = get_sha1(bol, commit_sha1);
 	*end_of_object_name = saved;

Technically, this would have made a fine excuse to teach get_sha1() a mode where it expects a length parameter instead of relying on a NUL-terminated string.

Practically, such fine excuses cost me months in this rebase--helper project already, and I need to protect my time better.

Quoted 11 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 741

Quoted 8 lines
sequencer.c +0 -8
-	/*
-	 * Verify that the action matches up with the one in
-	 * opts; we don't support arbitrary instructions
-	 */
-	if (action != opts->action) {
-		if (action == REPLAY_REVERT)
-		      error((opts->action == REPLAY_REVERT)
-			    ? _("Cannot revert during another revert.")

Errr... could the above ever happen? Namely

Quoted 3 lines
  action != opts->action && action == REPLAY_REVERT && opts->action == REPLAY_REVERT

Surely not.

Your reply pointed to the very circumstance when this may happen: git cherry-pick --continue after an interrupted git revert.

But then, I remove that code here, so I should not try to defend it.

Quoted 14 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 748

Quoted 7 lines
sequencer.c +0 -7
-			    : _("Cannot revert during a cherry-pick."));
-		else
-		      error((opts->action == REPLAY_REVERT)
-			    ? _("Cannot cherry-pick during a revert.")
-			    : _("Cannot cherry-pick during another cherry-pick."));
-		return NULL;
-	}

Anyway, while it is / would be a good idea to prevent starting any sequencer-based command (cherry-pick, revert, soon rebase -i) when other command is in progress (cherry-pick, revert, soon rebase -i). That is, if cherry-pick / revert waits for user action, you cannot run another cherry-pick or revert.

Quoted 1 line

Which I guess the above code was not about...

It was about that, though.

It went about it in a pretty round-about way: opts->action comes from the name of the command ("was I called as git revert or git cherry-pick?") and action comes from the todo script, which was assumed to be written by a previous run of the sequencer, using the then-current value of opts->action.

So it wrote that command into *every single line* of the todo script, *for the sole purpose* of verifying that it was the same action when running via --continue.

As I said earlier, I would not complain at all if an interrupted git revert could be continued via git cherry-pick --continue.

If that is not desirable, I can reintroduce that overzealous check, but that will have to wait until after v2.10.0. And it would require an argument that convinces me.

Quoted 7 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 776

Quoted 1 line
sequencer.c +1 -0
+		item = append_todo(todo_list);

A better name, in my personal option, would be

  +		item = todo_list_next(todo_list);

Or todo_next(todo_list).

That sounds more like a function that performs the next command in the todo_list.

While I agree that naming is hard, I still think that append_todo() with the todo_list as single parameter and returning a todo_item is pretty much
self-explanatory: it appends a new item to the todo_list and returns a pointer to it.

Quoted 9 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 779

Quoted 3 lines
sequencer.c +3 -0
+		item->offset_in_buf = p - todo_list->buf.buf;
+		if (parse_insn_line(item, p, eol)) {
+			error("Invalid line: %.*s", (int)(eol - p), p);

This error message should, I think, be also translatable:

  +			error(_("Invalid line: %.*s"), (int)(eol - p), p);

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 780

Quoted 1 line
sequencer.c +1 -0
+			res |= error(_("Could not parse line %d."), i);

Sure. In the meantime, I consolidated those two error()s into one, and now I also marked it translatable.

Quoted 2 lines

BTW. would be we able to show where exactly there was problem parsing, that is at which character in line? Or is it something for the future?

Maybe for the future.

Quoted 8 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 793

Quoted 5 lines
sequencer.c +1 -1
-static int read_populate_todo(struct commit_list **todo_list,
+static int read_populate_todo(struct todo_list *todo_list,
 			struct replay_opts *opts)
 {
 	const char *todo_file = get_todo_path(opts);

If I understand it correctly, replay_opts is used only to find out correct todo_file, isn't it?

Probably. Maybe also to make certain code paths conditional on rebase -i mode. Maybe also to figure out whether we run in verbose mode in the future. Or something.

Think of this read_populate_todo() function more as if it were a method of the "replay class", and the "opts" parameter is kind of "self" or "this" or whatever it is called in your favorite object-oriented language.

Quoted 6 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 789

Quoted 4 lines
sequencer.c +1 -2
-	if (strbuf_read(&buf, fd, 0) < 0) {
+	if (strbuf_read(&todo_list->buf, fd, 0) < 0) {
 		close(fd);
-		strbuf_release(&buf);

A question: when is todo_list->buf released?

Why, I am glad you asked! It is released in todo_list_release(), called at the end e.g. of sequencer_continue().

Quoted 12 lines
Quoted 12 lines
sequencer.c +3 -2
-static int walk_revs_populate_todo(struct commit_list **todo_list,
+static int walk_revs_populate_todo(struct todo_list *todo_list,
 				struct replay_opts *opts)
 {
+	enum todo_command command = opts->action == REPLAY_PICK ?
+		TODO_PICK : TODO_REVERT;
 	struct commit *commit;
-	struct commit_list **next;
 
 	if (prepare_revs(opts))
 		return -1;
 
Quoted 10 lines

sequencer.c static int read_populate_opts(struct replay_opts *opts) · line 874

Quoted 6 lines
sequencer.c +3 -3
-	next = todo_list;
-	while ((commit = get_revision(opts->revs)))
-		next = commit_list_append(commit, next);
+	while ((commit = get_revision(opts->revs))) {
+		struct todo_item *item = append_todo(todo_list);
+		const char *commit_buffer = get_commit_buffer(commit, NULL);

I see that you are creating todo file contents while walking revision list, something that was left for later in current / previous implementation of the sequencer...

Not really. This function was always about generating a todo_list. It just did not format it yet.

With the change of keeping the original formatting of the todo script instead of re-formatting it in save_todo(), this function now has to format the todo_list itself.

Quoted 12 lines

sequencer.c static int read_populate_opts(struct replay_opts *opts) · line 883

Quoted 9 lines
sequencer.c +9 -0
+		const char *subject;
+		int subject_len;
+
+		item->command = command;
+		item->commit = commit;
+		item->offset_in_buf = todo_list->buf.len;
+		subject_len = find_commit_subject(commit_buffer, &subject);
+		strbuf_addf(&todo_list->buf, "%s %s %.*s\n",
+			opts->action == REPLAY_PICK ?  "pick" : "revert",

Wouldn't it be simpler to use

Quoted 4 lines

+ todo_command_strings[command],

Also, this string does not change during the loop, though I guess compiler should be able to optimize it.

Sure!

Quoted 10 lines

sequencer.c static int read_populate_opts(struct replay_opts *opts) · line 886

Quoted 3 lines
sequencer.c +3 -0
+			find_unique_abbrev(commit->object.oid.hash,
+				DEFAULT_ABBREV),
+			subject_len, subject);

...Did format of the 'todo' file changed? And if yes, was it in backward compatible way, so that "git revert" or "git cherry-pick" started with old version of Git can be continued with new version, and what is also important (for somebody who sometimes uses system-installed Git, and sometimes user-compiled one) the reverse: started with new, continued with old?

The old format and the new format are compatible. In fact, sequencer's format was based on rebase -i's format (which makes it all the more surprising how much the processing deviated).

Quoted 4 lines
Quoted 4 lines
sequencer.c +0 -0
@@ -964,30 +990,24 @@ static int sequencer_rollback(struct replay_opts *opts)
 	return -1;
 }
 
Quoted 10 lines

sequencer.c static int sequencer_rollback(struct replay_opts *opts) · line 997

Quoted 8 lines
sequencer.c +3 -3
-static int save_todo(struct commit_list *todo_list, struct replay_opts *opts)
+static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
 {
 	static struct lock_file todo_lock;
-	struct strbuf buf = STRBUF_INIT;
-	int fd;
+	const char *todo_path = get_todo_path(opts);
+	int next = todo_list->current, offset, fd;

The "next = todo_list->current" looks a bit strange.

Depending whether we need rebase -i processing or revert/cherry-pick's slightly different one, the "current" position points to the next one already...

Quoted 3 lines

Also, we do not change todo_list->current, we use it in one place, so it can be used directly without help of temporary / helper variable. But that is just my personal opinion.

No, it has nothing to do with opinion. It prepares the code to keep it readable even when REPLAY_INTERACTIVE_REBASE is introduced.

Quoted 6 lines

Also, from 'next', 'offset' and 'fd', all those are different uses of
int: the index (int, rarely size_t), the offset in string (formally ptrdiff_t, or size_t, but usually int), and the file descriptor. I think from those the file descriptor could be kept in separate line; it would help diff to be more readable. But this is fairly marginal nitpicking, and a matter of personal opinion.

Right. At this point, I am really much more concerned about correctness of code than discussing personal preferences.

Quoted 7 lines

sequencer.c static int sequencer_rollback(struct replay_opts *opts) · line 1002

Quoted 5 lines
sequencer.c +1 -1
-	fd = hold_lock_file_for_update(&todo_lock, git_path_todo_file(), 0);
+	fd = hold_lock_file_for_update(&todo_lock, todo_path, 0);
 	if (fd < 0)
 		return error_errno(_("Could not lock '%s'"),
 				   git_path_todo_file());

We should use 'todo_path' here...

True.

Quoted 1 line

and this should be done in one of earlier patches, isn't it?

No. I deliberately skipped save_todo() from "future-proofing" as I planned to rewrite it anyway. There is no point in future-proofing something you are going to toss in a minute.

Quoted 6 lines

sequencer.c static int sequencer_rollback(struct replay_opts *opts) · line 979

Quoted 3 lines
sequencer.c +0 -3
-	if (format_todo(&buf, todo_list, opts) < 0) {
-		strbuf_release(&buf);
-		return error(_("Could not format %s."), git_path_todo_file());

Can we still get this error? Could we get this error anyway, and under what conditions?

No. We keep the original formatting. Keeping it cannot possibly result in a formatting error.

Quoted 7 lines
Quoted 7 lines
sequencer.c +1 -6
-	}
-	if (write_in_full(fd, buf.buf, buf.len) < 0) {
-		strbuf_release(&buf);
-		return error_errno(_("Could not write to %s"),
-				   git_path_todo_file());
-	}
+	offset = next < todo_list->nr ?
Quoted 8 lines

sequencer.c static int sequencer_rollback(struct replay_opts *opts) · line 1008

Quoted 5 lines
sequencer.c +5 -0
+		todo_list->items[next].offset_in_buf : todo_list->buf.len;
+	if (write_in_full(fd, todo_list->buf.buf + offset,
+			todo_list->buf.len - offset) < 0)
+		return error(_("Could not write to %s (%s)"),
+			todo_path, strerror(errno));

Ah, so it saves the remaining todo_items on todo_list, not the whole todo_list... the name does not fully show it.

The name also does not fully show that it will write a "done" file after the sequencer-i patch series.

Quoted 11 lines

sequencer.c static int sequencer_rollback(struct replay_opts *opts) · line 1010

Quoted 8 lines
sequencer.c +2 -5
-	if (commit_lock_file(&todo_lock) < 0) {
-		strbuf_release(&buf);
-		return error(_("Error wrapping up %s."), git_path_todo_file());
-	}
-	strbuf_release(&buf);
+	if (commit_lock_file(&todo_lock) < 0)
+		return error(_("Error wrapping up %s."), todo_path);

Note: this is unrelated change, but we usually put paths in quotes, like this

Quoted 5 lines
  +		return error(_("Error wrapping up '%s'."), todo_path);

(in this and earlier error message), so that paths containing spaces show correctly and readably to the user. Though this possibly is not a problem for this path.

Right.

Quoted 1 line

Also, how user is to understand "wrapping up"?

The same as before: the removed lines already had the error message, missing the quotes, too.

Don't get me wrong: I am a big fan of consistency, and I wish that Git's source code had more of it. So I would love to see a patch series that makes all error messages consistently reporting paths enclosed in single quotes.

I am also a big fan of the separation of concerns, though. And this patch series' concern is consistency *with the existing code*.

So I won't change the error message that I inherited at this point.

Quoted 9 lines

sequencer.c static int sequencer_continue(struct replay_opts *opts) · line 1118

Quoted 6 lines
sequencer.c +2 -1
 static int single_pick(struct commit *cmit, struct replay_opts *opts)
 {
 	setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
-	return do_pick_commit(cmit, opts);
+	return do_pick_commit(opts->action == REPLAY_PICK ?
+		TODO_PICK : TODO_REVERT, cmit, opts);

The ternary conditional operator here translates one enum to other enum, isn't it?

Well, almost. Please note that the enum will receive a new value in the sequencer-i patch series. And there is no equivalent todo_command for REPLAY_INTERACTIVE_REBASE.

Thanks for the review!
Johannes

#53 lore raw

Re: [PATCH 10/22] sequencer: avoid completely different messages for different actions

In reply to Jakub Narębski

Hi Kuba,

Quoted 30 lines · Jakub Narębski

CC-ed to Jiang Xin, L10N coordinator.

W dniu 29.08.2016 o 10:05, Johannes Schindelin pisze:

Quoted 10 lines
---
 sequencer.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
sequencer.c +0 -0
index cbdce6d..1b65202 100644

sequencer.c static int error_dirty_index(struct replay_opts *opts) · line 236

Quoted 10 lines
sequencer.c +2 -5
@@ -232,11 +232,8 @@ static int error_dirty_index(struct replay_opts *opts)
 	if (read_cache_unmerged())
 		return error_resolve_conflict(action_name(opts));
 
-	/* Different translation strings for cherry-pick and revert */
-	if (opts->action == REPLAY_PICK)
-		error(_("Your local changes would be overwritten by cherry-pick."));
-	else
-		error(_("Your local changes would be overwritten by revert."));
+	error(_("Your local changes would be overwritten by %s."),
+		action_name(opts));

If I understand it correctly, it would make "revert" or "cherry-pick" untranslated part of error message. You would need to use translation on the result with "_(action_name(opts))", you would have to mark todo_command_strings elements for gettext lexicon with N_(...).

Quoted 1 line

I am rather against this change (see also below).

Okay.

Unfortunately, I have to focus on the correctness of the code at the moment (and Git for Windows does ship *without* translations for the time being anyway, mostly to save on space, but also because users complained).

So I will take care of this after v2.10.0.

For the record, how is this supposed to be handled, in particular when I introduce a new action whose action_name(opts) will be "rebase -i"? Do I really need to repeat myself three times?

Ciao,
Dscho

#58 lore raw

Re: [PATCH 11/22] sequencer: get rid of the subcommand field

In reply to Jakub Narębski

Hi Kuba,

Quoted 8 lines · Jakub Narębski

W dniu 29.08.2016 o 10:05, Johannes Schindelin pisze:

Quoted 3 lines

While at it, ensure that the subcommands return an error code so that they do not have to die() all over the place (bad practice for library functions...).

This perhaps should be moved to a separate patch, but I guess there is a reason behind "while at it".

Yes. It seemed like the logical thing to do: I already introduce a new function, why should I shlep over a paradigm I do not want in the end?

Quoted 1 line

Also subcommand functions no longer are local to sequencer.c

They never were. All you had to do was to set a field and run the global function.

The real problem there was that the different local functions needed different parameters, and the round-about way to set those parameters as fields in a struct and then call a global function with that struct just makes it impossible to have compile-time safety.

Ciao,
Dscho

#39 lore raw

Re: [PATCH 08/22] sequencer: remove overzealous assumption

In reply to Jakub Narębski

Hi Kuba,

Quoted 4 lines · Jakub Narębski

W dniu 31.08.2016 o 20:36, Johannes Schindelin pisze:

I wonder: would 'git cherry-pick --continue' be able to finish 'git revert', and vice versa, then? Or 'git sequencer --continue'?

I just tested this, via

	diff --git a/t/t3510-cherry-pick-sequence.sh
	b/t/t3510-cherry-pick-sequence.sh
	index 96c7640..085d8bc 100755
	--- a/t/t3510-cherry-pick-sequence.sh
	+++ b/t/t3510-cherry-pick-sequence.sh
	@@ -55,7 +55,7 @@ test_expect_success 'cherry-pick
	mid-cherry-pick-sequence' '
		git checkout HEAD foo &&
		git cherry-pick base &&
		git cherry-pick picked &&
	-       git cherry-pick --continue &&
	+       git revert --continue &&
		git diff --exit-code anotherpick

(Danger! Whitespace corrupted!!!)

It appears that this passes now.

Probably git sequencer --continue would work, too, if there was a git sequencer. :0)

Quoted 20 lines
Quoted 20 lines · Jakub Narębski
Quoted 18 lines

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

t/t3510-cherry-pick-sequence.sh test_expect_success 'malformed instruction sheet 1' ' · line 462

Quoted 9 lines
t/t3510-cherry-pick-sequence.sh +0 -1
index 7b7a89d..6465edf 100755
@@ -459,17 +459,6 @@ test_expect_success 'malformed instruction sheet 1' '
 	test_expect_code 128 git cherry-pick --continue
 '
 
-test_expect_success 'malformed instruction sheet 2' '

Hmmm... the description is somewhat lacking (especially compared to the rest of test), anyway.

BTW. we should probably rename 'malformed instruction sheet 2' to 'malformed instruction sheet' if there are no further such tests after this removal, isn't it?

Quoted 7 lines
Quoted 2 lines

No, we cannot rename it after this patch because the patch removes it ;-) (It is not a file name but really a label for a test case.)

Ooops. What I wanted to say that after removing the test case named 'malformed instruction sheet 2' we should also rename *earlier* test case from 'malformed instruction sheet 1' to 'malformed instruction sheet', as it is now the only 'malformed instruction sheet *' test case.

Actually, you know, I completely missed the fact that there was a "malformed instruction sheet 3". I renumbered it.

Thanks,
Dscho

#42 lore raw

Re: [PATCH 08/22] sequencer: remove overzealous assumption

In reply to Junio C Hamano

Hi Junio,

Quoted 12 lines · Junio C Hamano
Quoted 4 lines · Johannes Schindelin

So if you must have a patch that disagrees with this overzealous check, the "revamp todo parsing" one is probably the first. But it is better to think of this at a higher level than just patches: it is wrong to limit the todo script to contain only identical commands.

So if you think of this at even higher level, the check done in parse_insn_line() that _assumes_ that opts->action must match the actions on each line is _WRONG_, but what this test expects to see is perfectly reasonable, I would think.

Quoted 4 lines

It is a different matter if it makes sense to _verify_ that the user didn't make nonsensical change to the generated insn and error out when s/he did. I tend to think it is pointless, and I wouldn't object if this test is removed due to that reason.

Fine. I will reintroduce that check. I guess I have the time ;-)

Ciao,
Dscho

#80 lore raw

Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

In reply to Jakub Narębski

Hi Kuba,

Quoted 12 lines · Jakub Narębski

W dniu 29.08.2016 o 10:05, Johannes Schindelin pisze:

Quoted 10 lines

The git-rebase-todo file contains a list of commands. Most of those commands have the form

	<verb> <sha1> <oneline>

The <oneline> is displayed primarily for the user's convenience, as rebase -i really interprets only the <verb> <sha1> part. However, there are *some* places in interactive rebase where the <oneline> is used to display messages, e.g. for reporting at which commit we stopped.

Quoted 7 lines
Quoted 4 lines

So let's just remember it when parsing the todo file; we keep a copy of the entire todo file anyway (to write out the new done and git-rebase-todo file just before processing each command), so all we need to do is remember the begin and end offsets.

Actually what we remember is pointer and length, or begin offset and length, not offset and offset.

Right. Fixed.

Quoted 5 lines
Quoted 5 lines
sequencer.c +0 -0
index 06759d4..3398774 100644
@@ -709,6 +709,8 @@ static int read_and_refresh_cache(struct replay_opts *opts)
Quoted 8 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 713

Quoted 5 lines
sequencer.c +2 -0
 struct todo_item {
 	enum todo_command command;
 	struct commit *commit;
+	const char *arg;
+	int arg_len;

Why 'arg', and not 'oneline', or 'subject'?
I'm not saying it is bad name.

Because we will use it for exec commands' args, too. Clarified in the commit message.

Quoted 10 lines

sequencer.c static int parse_insn_line(struct todo_item *item, const char *bol, char *eol) · line 767

Quoted 7 lines
sequencer.c +3 -0
@@ -760,6 +762,9 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)
 	status = get_sha1(bol, commit_sha1);
 	*end_of_object_name = saved;
 
+	item->arg = end_of_object_name + strspn(end_of_object_name, " \t");
+	item->arg_len = (int)(eol - item->arg);
+

Does it work correctly for line without <oneline>, that is

Quoted 3 lines

<verb> <sha1>

I think it does, but I not entirely sure.

It does work correctly: in the example, *end_of_object_name would be '\n', and strspn(end_of_object_name, " \t") would return 0.

Thanks for the review!
Dscho

#70 lore raw

Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

In reply to Junio C Hamano

Hi Junio,

Quoted 16 lines · Junio C Hamano
Quoted 5 lines · Jakub Narębski
Quoted 5 lines
sequencer.c +0 -0
index 06759d4..3398774 100644
@@ -709,6 +709,8 @@ static int read_and_refresh_cache(struct replay_opts *opts)
Quoted 8 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 713

Quoted 5 lines
sequencer.c +2 -0
 struct todo_item {
 	enum todo_command command;
 	struct commit *commit;
+	const char *arg;
+	int arg_len;

Why 'arg', and not 'oneline', or 'subject'?
I'm not saying it is bad name.

Quoted 4 lines

I am not sure what the "commit" field of type "struct commit *" is for. It is not needed until it is the commit's turn to be picked or reverted; if we end up stopping in the middle, parsing the commit object for later steps will end up being wasted effort.

No, it won't be wasted effort, as we *validate* the todo script this way. And since we may very well need the info later (most rebases do not fail in the middle), we store it, too.

Quoted 3 lines

Also, when the sequencer becomes one sequencer to rule them all, the command set may contain something that does not even mention any commit at all (think: exec).

Right, in which case the "commit" field will have the value... *drum roll*... NULL!

Quoted 8 lines

So I am not sure if we want a parsed commit there (I would not object if we kept the texual object name read from the file, though). The "one sequencer to rule them all" may even have to say "now give name ':1' to the result of the previous operation" in one step and in another later step have an instruction "merge ':1'". When that happens, you cannot even pre-populate the commit object when the sequencer reads the file, as the commit has not yet been created at that point.

These considerations are pretty hypothetical. I would even place a bet that we will *never* have ":1" as names, not if I have anything to say... ;-)

What is not so hypothetical is that after parsing the todo_list, we will have to act on the information contained therein. For example we will have to cherry-pick some of the indicated commits (requiring a struct commit * for use in do_pick_commit()). Another example: we may need to determine the oneline for use in fixup!/squash! reordering.

So: keeping *that* aspect of the previous todo_list parsing, i.e. store a pointer to the already-parsed commit, is the right thing to do.

Ciao,
Dscho

#107 lore raw

Re: [PATCH 19/22] sequencer: support cleaning up commit messages

In reply to Johannes Schindelin

Hello Johannes,

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

Quoted 10 lines

The sequencer_commit() function already knows how to amend commits, and with this new option, it can also clean up commit messages (i.e. strip out commented lines). This is needed to implement rebase -i's 'fixup' and 'squash' commands as sequencer commands.

---
 sequencer.c | 10 +++++++---
 sequencer.h |  3 ++-
 2 files changed, 9 insertions(+), 4 deletions(-)

This looks like nice little piece of enhancement, building scaffolding for sequencer-izing interactive rebase bit by bit.

sequencer.c static char **read_author_script(void) · line 482

Quoted 12 lines
sequencer.c +2 -1
index 20f7590..5ec956f 100644
@@ -478,7 +478,8 @@ static char **read_author_script(void)
  * (except, of course, while running an interactive rebase).
  */
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty, int edit, int amend)
+			  int allow_empty, int edit, int amend,
+			  int cleanup_commit_message)

All right, though it slowly begins coming close to the threshold where using bitfield flags would be sensible.

sequencer.c int sequencer_commit(const char *defmsg, struct replay_opts *opts, · line 520

Quoted 9 lines
sequencer.c +2 -0
 {
 	char **env = NULL;
 	struct argv_array array;
@@ -515,9 +516,12 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 		argv_array_push(&array, "-s");
 	if (defmsg)
 		argv_array_pushl(&array, "-F", defmsg, NULL);
+	if (cleanup_commit_message)
+		argv_array_push(&array, "--cleanup=strip");

Good.

sequencer.c int sequencer_commit(const char *defmsg, struct replay_opts *opts, · line 523

Quoted 4 lines
sequencer.c +1 -1
 	if (edit)
 		argv_array_push(&array, "-e");
-	else if (!opts->signoff && !opts->record_origin &&
+	else if (!cleanup_commit_message &&

All right, explicit cleanup=strip overrides "commit.cleanup" config, and turns off passing commit verbatim (incompatible with stripping)...

sequencer.c int sequencer_commit(const char *defmsg, struct replay_opts *opts, · line 524

Quoted 1 line
sequencer.c +1 -0
+		 !opts->signoff && !opts->record_origin &&

...adding signoff and recording origin requires not passing commit verbatim,...

sequencer.c int sequencer_commit(const char *defmsg, struct replay_opts *opts, · line 525

Quoted 1 line
sequencer.c +0 -0
 		 git_config_get_value("commit.cleanup", &value))

..., and in other cases are check the "commit.cleanup"...

sequencer.c int sequencer_commit(const char *defmsg, struct replay_opts *opts, · line 526

Quoted 1 line
sequencer.c +0 -0
 		argv_array_push(&array, "--cleanup=verbatim");

... and pass commit verbatim if it is not set.

Ah, well, the change you made looks good.

sequencer.c static int do_pick_commit(enum todo_command command, struct commit *commit, · line 788

Quoted 7 lines
sequencer.c +1 -1
@@ -781,7 +785,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	}
 	if (!opts->no_commit)
 		res = sequencer_commit(opts->edit ? NULL : git_path_merge_msg(),
-			opts, allow, opts->edit, 0);
+			opts, allow, opts->edit, 0, 0);

The calling convention begins to look unwieldy, but we have only a single such callsite, and there are quite a bit callsites in Git code that have similar API ("git grep ', 0, 0' -- '*.c'"). So we don't need to think about alternatives. Yet.

It's a pity that emulation of named parameters in C requires relying on designated inits from C99

  typedef struct {
    double pressure, moles, temp;
  } ideal_struct;

  #define ideal_pressure(...) ideal_pressure_base((ideal_struct){.pressure=1,   \
                                        .moles=1, .temp=273.15, __VA_ARGS__})

  double ideal_pressure_base(ideal_struct in)
  {
    return 8.314 * in.moles*in.temp/in.pressure;
  }

... ideal_pressure(.moles=2, .temp=373.15) ...

Quoted 18 lines
sequencer.c +0 -0
 leave:
 	free_message(commit, &msg);
sequencer.h +2 -1
index 2106c0d..e272549 100644
@@ -50,7 +50,8 @@ int sequencer_rollback(struct replay_opts *opts);
 int sequencer_remove_state(struct replay_opts *opts);
 
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty, int edit, int amend);
+			  int allow_empty, int edit, int amend,
+			  int cleanup_commit_message);
 
 extern const char sign_off_header[];
 
#116 lore raw

Re: [PATCH 21/22] sequencer: left-trim the lines read from the script

In reply to Johannes Schindelin

Hello Johannes,

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

Subject: [PATCH 21/22] sequencer: left-trim the lines read from the script

In the subject, it should probably be without "the", as "lines" are plural.

s/left-trim the lines/left-trim lines/

Quoted 3 lines

Interactive rebase's scripts may be indented; We need to handle this case, too, now that we prepare the sequencer to process interactive rebases.

s/; We need/; we need/

Nice little bit of scaffolding for sequencer-izing rebase -i.

Quoted 5 lines
---
 sequencer.c | 3 +++
 1 file changed, 3 insertions(+)

Small change, easy to review.

sequencer.c static int parse_insn_line(struct todo_item *item, const char *bol, char *eol) · line 869

Quoted 12 lines
sequencer.c +3 -0
index 0614b90..5efed2e 100644
@@ -864,6 +864,9 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)
 	char *end_of_object_name;
 	int i, saved, status, padding;
 
+	/* left-trim */
+	bol += strspn(bol, " \t");
+

Nice. Thanks for the comment. "left-trim" is better than "de-indent".

'bol' is beginning-of-line, isn't it (a complement to eol)?

Quoted 4 lines
sequencer.c +0 -0
 	for (i = 0; i < ARRAY_SIZE(todo_command_strings); i++)
 		if (skip_prefix(bol, todo_command_strings[i], &bol)) {
 			item->command = i;
Signature · 1 line
Jakub Narębski
#123 lore raw

Re: [PATCH 22/22] sequencer: refactor write_message()

In reply to Johannes Schindelin

Hello Johannes,

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

Quoted 4 lines

The write_message() function safely writes an strbuf to a file. Sometimes this is inconvenient, though: the text to be written may not be stored in a strbuf, or the strbuf should not be released after writing.

By "this" you mean "using strbuf", isn't it? It is not very obvious, and I think it would be better to say it explicitly.

Quoted 5 lines

Let's allow for such use cases by refactoring write_message() to allow for a convenience function write_file_gently(). As some of the upcoming callers of that new function will want to append a newline character, let's just add a flag for that, too.

This paragraph feels a bit convoluted.

As I understand it, you refactor "safely writing string to a file" into write_with_lock_file(), and make write_message() use it. The new function makes it easy to create new convenience function write_file_gently(); as some of the upcoming callers of this new function would want to append a newline character, add a flag for it in write_file_gently(), and thus in write_with_lock_file().

Isn't it better / easier to understand?

Quoted 16 lines
---
 sequencer.c | 21 ++++++++++++++++++---
 1 file changed, 18 insertions(+), 3 deletions(-)
sequencer.c +1 -1
index 5efed2e..f5b5e5e 100644
@@ -239,22 +239,37 @@ static void print_advice(int show_hint, struct replay_opts *opts)
 	}
 }
 
-static int write_message(struct strbuf *msgbuf, const char *filename)
+static int write_with_lock_file(const char *filename,

sequencer.c static void print_advice(int show_hint, struct replay_opts *opts) · line 251

Quoted 10 lines
sequencer.c +2 -1
+				const void *buf, size_t len, int append_eol)
 {
 	static struct lock_file msg_file;
 
 	int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0);
 	if (msg_fd < 0)
 		return error_errno(_("Could not lock '%s'"), filename);
-	if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0)
+	if (write_in_full(msg_fd, buf, len) < 0)
 		return error_errno(_("Could not write to %s"), filename);

You could have, for consistency, add quotes around filename (see previous error_errno callsite), *while at it*:

return error_errno(_("Could not write to '%s'"), filename);

sequencer.c static void print_advice(int show_hint, struct replay_opts *opts) · line 253

Quoted 3 lines
sequencer.c +2 -1
-	strbuf_release(msgbuf);
+	if (append_eol && write(msg_fd, "\n", 1) < 0)
+		return error_errno(_("Could not write eol to %s"), filename);

Same here, and it wouldn't even be 'while at it'

+ return error_errno(_("Could not write eol to '%s'"), filename);

sequencer.c static void print_advice(int show_hint, struct replay_opts *opts) · line 255

Quoted 2 lines
sequencer.c +0 -0
 	if (commit_lock_file(&msg_file) < 0)
 		return error(_("Error wrapping up %s."), filename);

Another "while at it"... though the one that can be safely postponed (well, the make message easier to understand part, not the quote filename part):

return error(_("Error wrapping up writing to '%s'."), filename);

sequencer.c static void print_advice(int show_hint, struct replay_opts *opts) · line 265

Quoted 10 lines
sequencer.c +6 -0
 	return 0;
 }
 
+static int write_message(struct strbuf *msgbuf, const char *filename)
+{
+	int res = write_with_lock_file(filename, msgbuf->buf, msgbuf->len, 0);
+	strbuf_release(msgbuf);
+	return res;
+}

Nice.

sequencer.c static void print_advice(int show_hint, struct replay_opts *opts) · line 271

Quoted 6 lines
sequencer.c +6 -0
+
+static int write_file_gently(const char *filename,
+			     const char *text, int append_eol)
+{
+	return write_with_lock_file(filename, text, strlen(text), append_eol);
+}

Nice. And it is static function, so we don't need to come up with a better function name (to describe its function better).

sequencer.c static void print_advice(int show_hint, struct replay_opts *opts)

Quoted 5 lines
sequencer.c +1 -0
+
 /*
  * Reads a file that was presumably written by a shell script, i.e.
  * with an end-of-line marker that needs to be stripped.
 

And thus we got to the last patch in this series. I have skipped patches that already got reviewed; are there some that you would like to have second review of? Is there patch series that needs to be applied earlier that needs a review?

P.S. I'll try to respond to your comments later today.

Regards,

Signature · 1 line
Jakub Narębski
#68 lore raw

Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

In reply to Jakub Narębski

Hi Kuba & Junio,

Quoted 19 lines · Jakub Narębski

W dniu 31.08.2016 o 21:10, Junio C Hamano pisze:

Quoted 17 lines
Quoted 10 lines · Jakub Narębski

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 713

Quoted 10 lines
sequencer.c +2 -0
index 06759d4..3398774 100644
@@ -709,6 +709,8 @@ static int read_and_refresh_cache(struct replay_opts *opts)
 struct todo_item {
 	enum todo_command command;
 	struct commit *commit;
+	const char *arg;
+	int arg_len;

I am not sure what the "commit" field of type "struct commit *" is for. It is not needed until it is the commit's turn to be picked or reverted; if we end up stopping in the middle, parsing the commit object for later steps will end up being wasted effort.

Quoted 4 lines

From what I understand this was what sequencer did before this series, so it is not a regression (I think; the commit parsing was in different function, but I think at the same place in the callchain).

Exactly.

Quoted 6 lines
Quoted 3 lines

Also, when the sequencer becomes one sequencer to rule them all, the command set may contain something that does not even mention any commit at all (think: exec).

The "exec" line is a bit of exception, all other rebase -i commands take commit as parameter. It could always use NULL.

There is also "noop".

Quoted 10 lines
Quoted 8 lines

So I am not sure if we want a parsed commit there (I would not object if we kept the texual object name read from the file, though). The "one sequencer to rule them all" may even have to say "now give name ':1' to the result of the previous operation" in one step and in another later step have an instruction "merge ':1'". When that happens, you cannot even pre-populate the commit object when the sequencer reads the file, as the commit has not yet been created at that point.

True, --preserve-merges rebase is well, different.

It is mis-designed. And I can be that harsh because it was my design.

In the meantime I came up with a much better design, and implemented it as a shell script on top of rebase -i. Since shell scripts run like slow molasses, even more so on Windows, I have a loose plan to implement its functionality as a new --recreate-merges option, and to deprecate --preserve-merges when that new option works.

It needs to be a new option (not a --preserve-merges=v2) because it is a totally different beast. For starters, it does not need its own code path that overrides pick_one, as --preserve-merges does.

But I get way ahead of myself. First we need to get these last few bits and pieces in place to accelerate (non --preserve-merges) rebase -i.

Ciao,
Dscho

#67 lore raw

Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

In reply to Junio C Hamano

Hi Junio,

Quoted 19 lines · Junio C Hamano
Quoted 12 lines · Jakub Narębski
Quoted 11 lines
Quoted 6 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 713

Quoted 6 lines
sequencer.c +2 -0
@@ -709,6 +709,8 @@ static int read_and_refresh_cache(struct replay_opts *opts)
 struct todo_item {
 	enum todo_command command;
 	struct commit *commit;
+	const char *arg;
+	int arg_len;

I am not sure what the "commit" field of type "struct commit *" is for. It is not needed until it is the commit's turn to be picked or reverted; if we end up stopping in the middle, parsing the commit object for later steps will end up being wasted effort.

Quoted 4 lines

From what I understand this was what sequencer did before this series, so it is not a regression (I think; the commit parsing was in different function, but I think at the same place in the callchain).

Quoted 4 lines

Yes, I agree with you and I didn't mean "this is a new bug" at all. It just is an indication that further refactoring after this step is needed, and it is likely to involve removal or modification of this field.

Not so. After you review the sequencer-i patches, you will see that it makes tons of sense to have that "commit" field: "pick" is by far the most common case, and it would not make sense at all to validate the todo script, only to throw away the information we got, only to re-gain it later in the sequencer.

Read: I strongly disagree with your cursory verdict that the "commit" field should go.

Ciao,
Dscho

#98 lore raw

Re: [PATCH 16/22] sequencer: prepare for rebase -i's GPG settings

In reply to Jakub Narębski

Hi Kuba,

Quoted 6 lines · Jakub Narębski

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

Quoted 2 lines

The rebase command sports a --gpg-sign option that is heeded by the interactive rebase.

Should it be "sports" or "supports"?

Funny. I got a PR last week that wanted to fix a similar expression.

I really meant "to sport", as in "To display; to have as a notable feature.". See https://en.wiktionary.org/wiki/sport#Verb

Quoted 5 lines
Quoted 1 line

+static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")

I know it is not your fault, but I wonder why this file uses snake_case_name, while all other use kebab-case-names. That is, why it is gpg_sign_opt and not gpg-sign-opt.

Yes, you are correct: it is not my fault ;-)

Quoted 3 lines

Sidenote: it's a pity api-quote.txt is just a placeholder for proper documentation (including sq_quotef()). I also wonder why it is not named sq_quotef_buf() or strbuf_addf_sq().

Heh. I did not even bother to check the documentation, it is my long-time habit to dive right into the code.

Quoted 15 lines

sequencer.c int sequencer_commit(const char *defmsg, struct replay_opts *opts, · line 478

Quoted 12 lines
sequencer.c +3 -2
@@ -471,17 +487,20 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 
 	if (IS_REBASE_I()) {
 		env = read_author_script();
-		if (!env)
+		if (!env) {
+			const char *gpg_opt = gpg_sign_opt_quoted(opts);
+
 			return error("You have staged changes in your working "
 				"tree. If these changes are meant to be\n"
 				"squashed into the previous commit, run:\n\n"
-				"  git commit --amend $gpg_sign_opt_quoted\n\n"

How did this get expanded by error(), and why we want to replace it if it works?

It did not work. It was a place-holder waiting for this patch ;-)

Quoted 14 lines

sequencer.c int sequencer_commit(const char *defmsg, struct replay_opts *opts, · line 502

Quoted 10 lines
sequencer.c +3 -2
+				"  git commit --amend %s\n\n"
 				"If they are meant to go into a new commit, "
 				"run:\n\n"
-				"  git commit $gpg_sign_opt_quoted\n\n"
+				"  git commit %s\n\n"
 				"In both case, once you're done, continue "
 				"with:\n\n"
-				"  git rebase --continue\n");
+				"  git rebase --continue\n", gpg_opt, gpg_opt);

Instead of passing option twice, why not make use of %1$s (arg reordering), that is

Quoted 3 lines

+ " git commit --amend %1$s\n\n"
[...]
+ " git commit %1$s\n\n"

Cute. But would this not drive the l10ners insane?

Quoted 1 line

So shell quoting is required only for error output.

Indeed.

Quoted 18 lines
Quoted 5 lines
sequencer.c +0 -1
@@ -955,8 +974,27 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
 
 static int read_populate_opts(struct replay_opts *opts)
 {
-	if (IS_REBASE_I())

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 986

Quoted 10 lines
sequencer.c +10 -0
+	if (IS_REBASE_I()) {
+		struct strbuf buf = STRBUF_INIT;
+
+		if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), 1)) {
+			if (buf.len && buf.buf[buf.len - 1] == '\n') {
+				if (--buf.len &&
+				    buf.buf[buf.len - 1] == '\r')
+					buf.len--;
+				buf.buf[buf.len] = '\0';
+			}

Isn't there some strbuf_chomp() / strbuf_strip_eof() function? Though as strbuf_getline() uses something similar...

Even worse. read_oneliner() *already* does that. I just forgot to delete this code when I introduced and used read_oneliner().

Thanks.

Quoted 4 lines

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 989

Quoted 2 lines
sequencer.c +2 -0
+			if (!starts_with(buf.buf, "-S"))
+				strbuf_reset(&buf);

Should we signal that there was problem with a file contents?

Maybe. But probably not: this file is written by git-rebase itself. I merely safe-guarded against empty files here.

Quoted 6 lines

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 992

Quoted 3 lines
sequencer.c +3 -0
+			else {
+				opts->gpg_sign = buf.buf + 2;
+				strbuf_detach(&buf, NULL);

Wouldn't we leak 2 characters that got skipped? Maybe xstrdup would be better (if it is leaked, and not reattached)?

We do not leak anything because I changed the code locally already to use sequencer_entrust() (I guess in response to an earlier of your comments).

Ciao,
Dscho

#97 lore raw

Re: [PATCH 16/22] sequencer: prepare for rebase -i's GPG settings

In reply to Junio C Hamano

Hi Junio,

Quoted 11 lines · Junio C Hamano
Quoted 6 lines · Jakub Narębski

sequencer.c static int populate_opts_cb(const char *key, const char *value, void *data) · line 992

Quoted 3 lines
sequencer.c +3 -0
+			else {
+				opts->gpg_sign = buf.buf + 2;
+				strbuf_detach(&buf, NULL);

Wouldn't we leak 2 characters that got skipped? Maybe xstrdup would be better (if it is leaked, and not reattached)?

An attempt to avoid leaking by calling free(opts->gpg_sign) would make it crash, which would be even worse ;-).

As I pointed out in a couple of replies yesterday: we cannot assume that gpg_sign is free()able. That's the entire reason behind the sequencer_entrust() dance.

Ciao,
Dscho

#96 lore raw

Re: [PATCH 16/22] sequencer: prepare for rebase -i's GPG settings

In reply to Jakub Narębski

Hi Kuba,

Quoted 5 lines · Jakub Narębski

Note that if the last line was

    +				sequencer_entrust(opts, strbuf_detach(&buf, NULL));

we can make it not leak. A bit tricksy, though.

Heh... I made it that tricksy, then.

Ciao,
Dscho

#102 lore raw

Re: [PATCH 17/22] sequencer: allow editing the commit message on a case-by-case basis

In reply to Jakub Narębski

Hi Kuba,

Quoted 11 lines · Jakub Narębski

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

Quoted 5 lines

In the upcoming commits, we will implement more and more of rebase -i's functionality. One particular feature of the commands to come is that some of them allow editing the commit message while others don't, i.e. we cannot define in the replay_opts whether the commit message should be edited or not.

It's a nice, pretty and self contained refactoring step. Small enough that it is easy to review.

Quoted 6 lines

I would like to have in the commit message that it is sequencer_commit() function that needs to rely on new parameter, instead of on a property of command (of its replay_opts). And that currently it simply passes the buck to caller, which uses opts->edit, but in the future the caller that is rebase -i would use todo_item and replay_opts based expression.

I enhanced the commit message.

Thanks for the review,
Dscho

#105 lore raw

Re: [PATCH 18/22] sequencer: support amending commits

In reply to Jakub Narębski

Hi Kuba,

Quoted 15 lines · Jakub Narębski

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

sequencer.c static char **read_author_script(void) · line 481

Quoted 10 lines
sequencer.c +1 -1
index 7e17d14..20f7590 100644
@@ -478,7 +478,7 @@ static char **read_author_script(void)
  * (except, of course, while running an interactive rebase).
  */
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty, int edit)
+			  int allow_empty, int edit, int amend)

I guess we won't get much more parameters; it would get unwieldy (and hard to remember). Five is all right.

It will eventually get a sixth, cleanup_commit_message.

Quoted 7 lines
Quoted 7 lines
sequencer.c +0 -0
 {
 	char **env = NULL;
 	struct argv_array array;
@@ -507,6 +507,8 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 	argv_array_push(&array, "commit");
 	argv_array_push(&array, "-n");
 
Quoted 14 lines

sequencer.c static int do_pick_commit(enum todo_command command, struct commit *commit, · line 784

Quoted 11 lines
sequencer.c +3 -1
+	if (amend)
+		argv_array_push(&array, "--amend");
 	if (opts->gpg_sign)
 		argv_array_pushf(&array, "-S%s", opts->gpg_sign);
 	if (opts->signoff)
@@ -779,7 +781,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	}
 	if (!opts->no_commit)
 		res = sequencer_commit(opts->edit ? NULL : git_path_merge_msg(),
-			opts, allow, opts->edit);
+			opts, allow, opts->edit, 0);

... even of this makes one need to check the calling convention, what does this 0 mean.

Yeah, but it will not remain "0", but be replaced by a variable named "amend"...

Thanks for the review,
Dscho

#108 lore raw

Re: [PATCH 19/22] sequencer: support cleaning up commit messages

In reply to Jakub Narębski

Hi Kuba,

Quoted 13 lines · Jakub Narębski

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

sequencer.c static int do_pick_commit(enum todo_command command, struct commit *commit, · line 788

Quoted 6 lines
sequencer.c +1 -1
@@ -781,7 +785,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	}
 	if (!opts->no_commit)
 		res = sequencer_commit(opts->edit ? NULL : git_path_merge_msg(),
-			opts, allow, opts->edit, 0);
+			opts, allow, opts->edit, 0, 0);

The calling convention begins to look unwieldy, but we have only a single such callsite, and there are quite a bit callsites in Git code that have similar API ("git grep ', 0, 0' -- '*.c'"). So we don't need to think about alternatives. Yet.

Right.

Please note that it will make much more sense in the end, too, as the 0s will be replaced by appropriate variables.

Quoted 15 lines

It's a pity that emulation of named parameters in C requires relying on designated inits from C99

  typedef struct {
    double pressure, moles, temp;
  } ideal_struct;

  #define ideal_pressure(...) ideal_pressure_base((ideal_struct){.pressure=1,   \
                                        .moles=1, .temp=273.15, __VA_ARGS__})

  double ideal_pressure_base(ideal_struct in)
  {
    return 8.314 * in.moles*in.temp/in.pressure;
  }
Quoted 1 line

... ideal_pressure(.moles=2, .temp=373.15) ...

Yeah, that looks unwieldy ;-)

Thanks for the review,
Dscho

#117 lore raw

Re: [PATCH 21/22] sequencer: left-trim the lines read from the script

In reply to Jakub Narębski

Hi Kuba,

Quoted 8 lines · Jakub Narębski

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

Subject: [PATCH 21/22] sequencer: left-trim the lines read from the script

In the subject, it should probably be without "the", as "lines" are plural.

s/left-trim the lines/left-trim lines/

I am happy that we stepped outside of the "code correctness" land into "grammar fix" land, as it surely means that you are convinced the code is correct? ;-)

Fixed.

Quoted 5 lines
Quoted 3 lines

Interactive rebase's scripts may be indented; We need to handle this case, too, now that we prepare the sequencer to process interactive rebases.

s/; We need/; we need/

Hrmpf. From http://grammar.ccc.commnet.edu/grammar/marks/colon.htm:

There is some disagreement among writing reference manuals about when you should capitalize an independent clause following a colon. Most of the manuals advise that when you have more than one sentence in your explanation or when your sentence(s) is a formal quotation, a capital is a good idea. The NYPL Writer's Guide urges consistency within a document; the Chicago Manual of Style says you may begin an independent clause with a lowercase letter unless it's one of those two things (a quotation or more than one sentence). The APA Publication Manual is the most extreme: it advises us to always capitalize an independent clause following a colon. The advice given above is consistent with the Gregg Reference Manual.

Based on that, I think that a capital is the correct case here.

Quoted 1 line

'bol' is beginning-of-line, isn't it (a complement to eol)?

Yep. How did you guess? :-)

Ciao,
Dscho

#124 lore raw

Re: [PATCH 22/22] sequencer: refactor write_message()

In reply to Jakub Narębski

Hi Kuba,

Quoted 9 lines · Jakub Narębski

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

Quoted 4 lines

The write_message() function safely writes an strbuf to a file. Sometimes this is inconvenient, though: the text to be written may not be stored in a strbuf, or the strbuf should not be released after writing.

By "this" you mean "using strbuf", isn't it? It is not very obvious, and I think it would be better to say it explicitly.

Rephrased.

Quoted 7 lines
Quoted 4 lines

Let's allow for such use cases by refactoring write_message() to allow for a convenience function write_file_gently(). As some of the upcoming callers of that new function will want to append a newline character, let's just add a flag for that, too.

This paragraph feels a bit convoluted.

Quoted 8 lines

As I understand it, you refactor "safely writing string to a file" into write_with_lock_file(), and make write_message() use it. The new function makes it easy to create new convenience function write_file_gently(); as some of the upcoming callers of this new function would want to append a newline character, add a flag for it in write_file_gently(), and thus in write_with_lock_file().

Isn't it better / easier to understand?

I don't know, but I took it.

Quoted 24 lines
Quoted 10 lines
sequencer.c +1 -1
index 5efed2e..f5b5e5e 100644
@@ -239,22 +239,37 @@ static void print_advice(int show_hint, struct replay_opts *opts)
 	}
 }
 
-static int write_message(struct strbuf *msgbuf, const char *filename)
+static int write_with_lock_file(const char *filename,

sequencer.c static void print_advice(int show_hint, struct replay_opts *opts) · line 251

Quoted 10 lines
sequencer.c +2 -1
+				const void *buf, size_t len, int append_eol)
 {
 	static struct lock_file msg_file;
 
 	int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0);
 	if (msg_fd < 0)
 		return error_errno(_("Could not lock '%s'"), filename);
-	if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0)
+	if (write_in_full(msg_fd, buf, len) < 0)
 		return error_errno(_("Could not write to %s"), filename);

You could have, for consistency, add quotes around filename (see previous error_errno callsite), *while at it*:

Quoted 1 line

return error_errno(_("Could not write to '%s'"), filename);

Done.

Quoted 5 lines

sequencer.c static void print_advice(int show_hint, struct replay_opts *opts) · line 253

Quoted 3 lines
sequencer.c +2 -1
-	strbuf_release(msgbuf);
+	if (append_eol && write(msg_fd, "\n", 1) < 0)
+		return error_errno(_("Could not write eol to %s"), filename);

Same here, and it wouldn't even be 'while at it'

Done.

Quoted 11 lines

+ return error_errno(_("Could not write eol to '%s'"), filename);

sequencer.c static void print_advice(int show_hint, struct replay_opts *opts) · line 255

Quoted 2 lines
sequencer.c +0 -0
 	if (commit_lock_file(&msg_file) < 0)
 		return error(_("Error wrapping up %s."), filename);

Another "while at it"... though the one that can be safely postponed (well, the make message easier to understand part, not the quote filename part):

return error(_("Error wrapping up writing to '%s'."), filename);

As I inherited this message, I'll keep it.

Quoted 4 lines

And thus we got to the last patch in this series. I have skipped patches that already got reviewed; are there some that you would like to have second review of? Is there patch series that needs to be applied earlier that needs a review?

Thank you for your review!
Dscho

#47 lore raw

Re: [PATCH 09/22] sequencer: completely revamp the "todo" script parsing

In reply to Johannes Schindelin
Quoted 12 lines · Johannes Schindelin
Quoted 1 line

git continue as a shorthand for git <relevant-cmd> --continue sounds great.

Before we get ahead of ourselves:

1) this has nothing to do with the patch series at hand, and

2) if we were to introduce git continue, we would need to think long and hard about the following issues:

I) are there potentially ambiguous <relevant-cmd>s that the user may want to continue?

Quoted 7 lines

II) what about options? You can say `git rebase --continue
--no-ff, for example, but not git cherry-pick --continue --no-ff`...

III) Would it not be confusing to have a subcommand continue
that does *not* serve a *single* purpose? It's kinda flying into the face of the Unix philosophy.

The above reasoning applies equally to "git abort". I do not think "git continue" would help.

If it were that anything you can do with Git can be --continue'ed the same way (e.g. all uses one sequencer to rule them all), it might be achievable, but I do not think it isn't, and will never be.

"git commit" may say "You haven't added anything yet" and refuse to do anything. Should "git continue" do "git commit -a" by noticing that the last thing you tried to do was "git commit" and guess that it is likely you wanted to commit all changes? I think not.

#118 lore raw

Re: [PATCH 21/22] sequencer: left-trim the lines read from the script

In reply to Johannes Schindelin
Quoted 20 lines · Johannes Schindelin
Quoted 5 lines
Quoted 3 lines

Interactive rebase's scripts may be indented; We need to handle this case, too, now that we prepare the sequencer to process interactive rebases.

s/; We need/; we need/

Hrmpf. From http://grammar.ccc.commnet.edu/grammar/marks/colon.htm:

There is some disagreement among writing reference manuals about when you should capitalize an independent clause following a colon. Most of the manuals advise that when you have more than one sentence in your explanation or when your sentence(s) is a formal quotation, a capital is a good idea. The NYPL Writer's Guide urges consistency within a document; the Chicago Manual of Style says you may begin an independent clause with a lowercase letter unless it's one of those two things (a quotation or more than one sentence). The APA Publication Manual is the most extreme: it advises us to always capitalize an independent clause following a colon. The advice given above is consistent with the Gregg Reference Manual.

Quoted 1 line

Based on that, I think that a capital is the correct case here.

Does that manual have anything to say about semicolons, which is a different thing?

#40 lore raw

Re: [PATCH 08/22] sequencer: remove overzealous assumption

In reply to Johannes Schindelin

Hello Johannes,

W dniu 01.09.2016 o 10:01, Johannes Schindelin pisze:

Quoted 22 lines · Jakub Narębski
Quoted 4 lines

W dniu 31.08.2016 o 20:36, Johannes Schindelin pisze:

I wonder: would 'git cherry-pick --continue' be able to finish 'git revert', and vice versa, then? Or 'git sequencer --continue'?

I just tested this, via

	diff --git a/t/t3510-cherry-pick-sequence.sh
	b/t/t3510-cherry-pick-sequence.sh
	index 96c7640..085d8bc 100755
	--- a/t/t3510-cherry-pick-sequence.sh
	+++ b/t/t3510-cherry-pick-sequence.sh
	@@ -55,7 +55,7 @@ test_expect_success 'cherry-pick
	mid-cherry-pick-sequence' '
		git checkout HEAD foo &&
		git cherry-pick base &&
		git cherry-pick picked &&
	-       git cherry-pick --continue &&
	+       git revert --continue &&
		git diff --exit-code anotherpick
Quoted 3 lines

(Danger! Whitespace corrupted!!!)

It appears that this passes now.

I'm now not sure if it is such a great idea. As was said somewhere else in this thread, different sequencer-based commands sports different options, and you can add options to the "git <command> --continue". For example you can say "git cherry-pick --continue -x", but you cannot say "git revert --continue -x", as '-x' is a cherry-pick only option. Or you can, theoretically, use "git am --continue --no-3way".

One option is to temporarily relax the test (test_expect_failure), then fix it at the end.

BTW. how git-am uses sequencer? I have seen "revert" etc., and "pick" etc., but no git-am related constants or strings...

Quoted 2 lines

Probably git sequencer --continue would work, too, if there was a git sequencer. :0)

Right.

Quoted 24 lines
Quoted 24 lines
Quoted 23 lines · Jakub Narębski
Quoted 10 lines

W dniu 29.08.2016 o 10:04, Johannes Schindelin pisze:

Quoted 8 lines
t/t3510-cherry-pick-sequence.sh +0 -0
index 7b7a89d..6465edf 100755
@@ -459,17 +459,6 @@ test_expect_success 'malformed instruction sheet 1' '
 	test_expect_code 128 git cherry-pick --continue
 '
 
Quoted 8 lines

t/t3510-cherry-pick-sequence.sh test_expect_success 'malformed instruction sheet 1' ' · line 462

Quoted 1 line
t/t3510-cherry-pick-sequence.sh +0 -1
-test_expect_success 'malformed instruction sheet 2' '

Hmmm... the description is somewhat lacking (especially compared to the rest of test), anyway.

BTW. we should probably rename 'malformed instruction sheet 2' to 'malformed instruction sheet' if there are no further such tests after this removal, isn't it?

No, we cannot rename it after this patch because the patch removes it ;-) (It is not a file name but really a label for a test case.)

Quoted 7 lines
Quoted 4 lines

Ooops. What I wanted to say that after removing the test case named 'malformed instruction sheet 2' we should also rename *earlier* test case from 'malformed instruction sheet 1' to 'malformed instruction sheet', as it is now the only 'malformed instruction sheet *' test case.

Actually, you know, I completely missed the fact that there was a "malformed instruction sheet 3". I renumbered it.

Ooops. I have missed it too, having looked only at the test after the one removed (which is not about malformed instruction sheet).

Best,

Signature · 1 line
Jakub Narębski
#71 lore raw

Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

In reply to Johannes Schindelin
Quoted 10 lines · Johannes Schindelin
Quoted 6 lines

though). The "one sequencer to rule them all" may even have to say "now give name ':1' to the result of the previous operation" in one step and in another later step have an instruction "merge ':1'". When that happens, you cannot even pre-populate the commit object when the sequencer reads the file, as the commit has not yet been created at that point.

These considerations are pretty hypothetical. I would even place a bet that we will *never* have ":1" as names, not if I have anything to say... ;-)

If you can always work with pre-existing commit, then you can validate all object references that appear in the instructions upfront.

I was sort of expecting that, when you do the preserve-merges mode of "rebase -i", you would need to jump around, doing "we have reconstructed the side branch on a new 'onto', let's give the result this temporary name ':1', and then switch to the trunk (which would call for 'reset <commit>' instruction) and merge that thing (which would be 'merge :1' or perhaps called 'pick :1')", and at that point you no longer validate the object references upfront.

If you do not have to have such a "mark this point" and a "refer to that point we previously marked", then I agree that you should be able to pre-validate and keep the result in the structure.

#49 lore raw

Re: [PATCH 09/22] sequencer: completely revamp the "todo" script parsing

In reply to Johannes Schindelin

Hello Johannes,

W dniu 01.09.2016 o 09:49, Johannes Schindelin pisze:

Quoted 2 lines · Jakub Narębski
Quoted 1 line

W dniu 29.08.2016 o 10:05, Johannes Schindelin pisze:

[...]

Quoted 8 lines
Quoted 1 line

Hmmm... commit_list is, as defined in commit.h, a linked list.

That is the most prominent reason why the rest is not a mindless conversion from commit_list to todo_list.

And we need todo_list as an array, because we need to be able to peek, or even move, backwards from the current command.

Quoted 6 lines
Quoted 3 lines

Here todo_list uses growable array implementation of list. Which is I guess better on current CPU architecture, with slow memory, limited-size caches, and adjacency prefetching.

That is not the reason that an array is used here. The array allows us much more flexibility.

It would be nice if this reasoning (behind the change from linked list to growable array) was mentioned in appropriate commit message, and perhaps also in the cover letter for the series. It is IMVHO quite important information (that you thought obvious).

Quoted 4 lines

One of the major performance improvements will come at the very end, for
example: the reordering of the fixup!/squash! lines. And that would be a *major* pain to do if the todo_list were still a linked list.

Actually deletion from and insertion into single linked list are not that hard, and O(1) after finding place, O(N) with finding included. Moving elements in array is O(N),... and arguably a bit simpler - but at high level, with appropriate primitives, they are about the same.

Yes, array is easier for permutation and reordering.

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

+struct todo_item *append_todo(struct todo_list *todo_list)

Errr... I don't quite understand the name of this function. What are you appending here to the todo_list?

A new item.

Quoted 8 lines
Quoted 2 lines

Compare string_list_append() and string_list_append_nodup(), where the second parameter is item to append.

Yes, that is correct. In the case of a todo_item, things are a lot more complicated, though. Some of the values have to be determined tediously (such as the offset and length of the oneline after the "pick <oid>" command). I just put those values directly into the newly allocated item, is all.

I would expect sth_append command to take a list (or other collection), an element, and return [modified] collection with the new element added. Such API would require temporary variable in caller and memcopy in the sth_append() function.

This is not it. It creates a new element, expanding a list (a collection), and then expose this element. Which spares us memcopy... on non-critical path.

I don't know how to name operation "grow list and return new element". But "append" it is not.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
sequencer.c +2 -0
+	ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc);
+	return todo_list->items + todo_list->nr++;
 }
Quoted 8 lines
Quoted 4 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 734

Quoted 2 lines
sequencer.c +1 -1
-static struct commit *parse_insn_line(char *bol, char *eol, struct replay_opts *opts)
+static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)

Why the change of return type?

Because it makes no sense to return a commit here because not all commands are about commits (think rebase -i's exec). It makes tons of sense to return an error condition, though.

All right.

I have not checked / not remember what caller of parse_insn_line() used it's return value for. I guess that we save it into todo_item, moving that operation from caller to callee.

Quoted 8 lines
Quoted 1 line

Why now struct todo_item is first when struct replay_opts was last?

Those play very, very different roles.

The opts parameter used to provide parse_insn_line() with enough information to complain loudly when the overall command was not identical to the parsed command.

Quoted 7 lines

The item parameter is a receptacle for the parsed data. It will contain the pointer to the commit that was previously returned, if any. But it will also contain much more information, such as the command, the oneline, the offset in the buffer, etc etc

So "opts" was an "in" parameter while "item" is an "out" one. Apples and oranges.

All right. Good explanation. And the one that is too low-level detail to put in the commit message, I think.

Quoted 7 lines
Quoted 4 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 754

Quoted 2 lines
sequencer.c +1 -1
-	end_of_object_name = bol + strcspn(bol, " \t\n");
+	end_of_object_name = (char *) bol + strcspn(bol, " \t\n");

Why is this cast needed?

Because bol is a "const char *" and we need to put "NUL" temporarily to *end_of_object_name:

Would compiler complain without this const'ness-stripping cast?

Quoted 6 lines
Quoted 5 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 758

Quoted 5 lines
sequencer.c +0 -0
 	saved = *end_of_object_name;
 	*end_of_object_name = '\0';
 	status = get_sha1(bol, commit_sha1);
 	*end_of_object_name = saved;
Quoted 6 lines

Technically, this would have made a fine excuse to teach get_sha1() a mode where it expects a length parameter instead of relying on a NUL-terminated string.

Practically, such fine excuses cost me months in this rebase--helper project already, and I need to protect my time better.

Put it in TODO list (and perhaps add a TODO comment) ;-).

BTW. open-source produces better software, and development bandwidth is good, but the latency :-((((

Quoted 12 lines
Quoted 12 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 741

Quoted 9 lines
sequencer.c +0 -8
-	/*
-	 * Verify that the action matches up with the one in
-	 * opts; we don't support arbitrary instructions
-	 */
-	if (action != opts->action) {
-		if (action == REPLAY_REVERT)
-		      error((opts->action == REPLAY_REVERT)
-			    ? _("Cannot revert during another revert.")

Errr... could the above ever happen? Namely

Quoted 6 lines
Quoted 3 lines
  action != opts->action && action == REPLAY_REVERT && opts->action == REPLAY_REVERT

Surely not.

Your reply pointed to the very circumstance when this may happen: git cherry-pick --continue after an interrupted git revert.

I was talking about "Cannot revert during another revert." and "Cannot cherry-pick during another cherry-pick." errors, which can never happen because of the outermost if.

   x == A && y == A && x != Y

can never happen, because equality is transitive^*

   x == A && y == A  =>  x == y

*) except NaN (NaN != NaN), but we don't deal with floating point here.

From those error messages it looks like they were originally intended to prevent from starting another revert or cherry-pick if sequencer based operation is already in progress (to not stomp on the latter internal state). But for that it would need to examine the opts->subcommand if it is REPLAY_NONE (or equivalent), check that sequencer file already exists, and read it to fond which operation is in progress.

This is about checking that command in todo-list agrees with the git command used (we probably checked that it is --continue, or maybe --skip). But the error message does not spell that; it is misleading.

[Nb. git-blame shows Vasco Almeida as author of those lines]

Quoted 3 lines

But then, I remove that code here, so I should not try to defend it.

But that was / is another reason for removing code: it is slightly wrong.

Quoted 14 lines
Quoted 14 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 748

Quoted 7 lines
sequencer.c +0 -7
-			    : _("Cannot revert during a cherry-pick."));
-		else
-		      error((opts->action == REPLAY_REVERT)
-			    ? _("Cannot cherry-pick during a revert.")
-			    : _("Cannot cherry-pick during another cherry-pick."));
-		return NULL;
-	}

Anyway, while it is / would be a good idea to prevent starting any sequencer-based command (cherry-pick, revert, soon rebase -i) when other command is in progress (cherry-pick, revert, soon rebase -i). That is, if cherry-pick / revert waits for user action, you cannot run another cherry-pick or revert.

Quoted 3 lines
Quoted 1 line

Which I guess the above code was not about...

It was about that, though.

No it was not, see the reasoning above.

Quoted 6 lines

It went about it in a pretty round-about way: opts->action comes from the name of the command ("was I called as git revert or git cherry-pick?") and action comes from the todo script, which was assumed to be written by a previous run of the sequencer, using the then-current value of opts->action.

It did different check, that we continue with the same command as we began with.

Quoted 11 lines

So it wrote that command into *every single line* of the todo script, *for the sole purpose* of verifying that it was the same action when running via --continue.

As I said earlier, I would not complain at all if an interrupted git revert could be continued via git cherry-pick --continue.

If that is not desirable, I can reintroduce that overzealous check, but that will have to wait until after v2.10.0. And it would require an argument that convinces me.

One argument is that you can add options to --continue, and among options for cherry-pick, revert and rebase -i, there are options that apply only to some of them. So we need at least decide what to do if we started rebase, and try to continue with cherry-pick:
do we accept rebase-only options? What we do with cherry-pick-only options, that rebase does not understand?

It is easier to just check that revert is continued with revert (by checking command in todo file), cherry-pick with cherry-pick (same), rebase -i with rebase (by checking another file), and am with am.

Quoted 9 lines
Quoted 8 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 776

Quoted 2 lines
sequencer.c +1 -0
+		item = append_todo(todo_list);

A better name, in my personal option, would be

  +		item = todo_list_next(todo_list);

Or todo_next(todo_list).

Quoted 7 lines

That sounds more like a function that performs the next command in the todo_list.

While I agree that naming is hard, I still think that append_todo() with the todo_list as single parameter and returning a todo_item is pretty much
self-explanatory: it appends a new item to the todo_list and returns a pointer to it.

It creates new item in the todo_list at the end, or it grows todo_list, but the function does not append anything...

[...]

Quoted 9 lines
Quoted 6 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 789

Quoted 4 lines
sequencer.c +1 -2
-	if (strbuf_read(&buf, fd, 0) < 0) {
+	if (strbuf_read(&todo_list->buf, fd, 0) < 0) {
 		close(fd);
-		strbuf_release(&buf);

A question: when is todo_list->buf released?

Why, I am glad you asked! It is released in todo_list_release(), called at the end e.g. of sequencer_continue().

All right. I could have guessed that.

Quoted 23 lines
Quoted 12 lines
Quoted 12 lines
sequencer.c +3 -2
-static int walk_revs_populate_todo(struct commit_list **todo_list,
+static int walk_revs_populate_todo(struct todo_list *todo_list,
 				struct replay_opts *opts)
 {
+	enum todo_command command = opts->action == REPLAY_PICK ?
+		TODO_PICK : TODO_REVERT;
 	struct commit *commit;
-	struct commit_list **next;
 
 	if (prepare_revs(opts))
 		return -1;
Quoted 10 lines

sequencer.c static int read_populate_opts(struct replay_opts *opts) · line 874

Quoted 6 lines
sequencer.c +3 -3
-	next = todo_list;
-	while ((commit = get_revision(opts->revs)))
-		next = commit_list_append(commit, next);
+	while ((commit = get_revision(opts->revs))) {
+		struct todo_item *item = append_todo(todo_list);
+		const char *commit_buffer = get_commit_buffer(commit, NULL);

I see that you are creating todo file contents while walking revision list, something that was left for later in current / previous implementation of the sequencer...

Quoted 6 lines

Not really. This function was always about generating a todo_list. It just did not format it yet.

With the change of keeping the original formatting of the todo script instead of re-formatting it in save_todo(), this function now has to format the todo_list itself.

So the commit parsing was moved from save_todo(), which contrary to the name also re-generated todo list, to walk_revs_populate_todo(). I guess it is the same callchain.

Additional question, answer to which should address Junio's complaint about early/eager commit parsing (if I understand it correctly):
do information from parsing commit is needed for creating a new commit, i.e. doing a pick or revert? If not, then perhaps parsing only those commits that are left, at the time of saving todo file (with probably less commit that what we have started with), would be better?

[...]

Quoted 15 lines
Quoted 10 lines

sequencer.c static int sequencer_rollback(struct replay_opts *opts) · line 997

Quoted 8 lines
sequencer.c +3 -3
-static int save_todo(struct commit_list *todo_list, struct replay_opts *opts)
+static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
 {
 	static struct lock_file todo_lock;
-	struct strbuf buf = STRBUF_INIT;
-	int fd;
+	const char *todo_path = get_todo_path(opts);
+	int next = todo_list->current, offset, fd;

The "next = todo_list->current" looks a bit strange.

Depending whether we need rebase -i processing or revert/cherry-pick's slightly different one, the "current" position points to the next one already...

Quoted 6 lines
Quoted 3 lines

Also, we do not change todo_list->current, we use it in one place, so it can be used directly without help of temporary / helper variable. But that is just my personal opinion.

No, it has nothing to do with opinion. It prepares the code to keep it readable even when REPLAY_INTERACTIVE_REBASE is introduced.

Ah, all right.

[...]

Quoted 6 lines
Quoted 1 line

and this should be done in one of earlier patches, isn't it?

No. I deliberately skipped save_todo() from "future-proofing" as I planned to rewrite it anyway. There is no point in future-proofing something you are going to toss in a minute.

All right. Though I wonder if it should not be mentioned in the commit message of said previous patch (though I have missed that not all sites were "future-proofed" in review; I'm sorry).

Quoted 28 lines
Quoted 10 lines

sequencer.c static int sequencer_rollback(struct replay_opts *opts) · line 1010

Quoted 7 lines
sequencer.c +2 -5
-	if (commit_lock_file(&todo_lock) < 0) {
-		strbuf_release(&buf);
-		return error(_("Error wrapping up %s."), git_path_todo_file());
-	}
-	strbuf_release(&buf);
+	if (commit_lock_file(&todo_lock) < 0)
+		return error(_("Error wrapping up %s."), todo_path);

Note: this is unrelated change, but we usually put paths in quotes, like this

Quoted 5 lines
  +		return error(_("Error wrapping up '%s'."), todo_path);

(in this and earlier error message), so that paths containing spaces show correctly and readably to the user. Though this possibly is not a problem for this path.

Right.

Quoted 1 line

Also, how user is to understand "wrapping up"?

The same as before: the removed lines already had the error message, missing the quotes, too.

Don't get me wrong: I am a big fan of consistency, and I wish that Git's source code had more of it. So I would love to see a patch series that makes all error messages consistently reporting paths enclosed in single quotes.

Quoted 4 lines

I am also a big fan of the separation of concerns, though. And this patch series' concern is consistency *with the existing code*.

So I won't change the error message that I inherited at this point.

All right, I can understand that. But I think it won't cost much to do _while at it_ adding of quotes around pathnames in error messages, where you notice this problem.

Quoted 10 lines
Quoted 9 lines

sequencer.c static int sequencer_continue(struct replay_opts *opts) · line 1118

Quoted 6 lines

static int single_pick(struct commit *cmit, struct replay_opts *opts) {

sequencer.c +2 -1
 	setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
-	return do_pick_commit(cmit, opts);
+	return do_pick_commit(opts->action == REPLAY_PICK ?
+		TODO_PICK : TODO_REVERT, cmit, opts);

The ternary conditional operator here translates one enum to other enum, isn't it?

Quoted 3 lines

Well, almost. Please note that the enum will receive a new value in the sequencer-i patch series. And there is no equivalent todo_command for REPLAY_INTERACTIVE_REBASE.

All right. And casting one enum to other, relying on the same order, is tricky to the extreme and brittle.

Quoted 2 lines

Thanks for the review!

You are welcome.

Best,

Signature · 1 line
Jakub Narębski
#54 lore raw

Re: [PATCH 10/22] sequencer: avoid completely different messages for different actions

In reply to Johannes Schindelin

Hello Johannes

W dniu 01.09.2016 o 09:52, Johannes Schindelin pisze:

Quoted 3 lines · Jakub Narębski
Quoted 2 lines

CC-ed to Jiang Xin, L10N coordinator.
W dniu 29.08.2016 o 10:05, Johannes Schindelin pisze:

[...]

Quoted 13 lines
Quoted 13 lines

sequencer.c static int error_dirty_index(struct replay_opts *opts) · line 236

Quoted 7 lines
sequencer.c +2 -5
-	/* Different translation strings for cherry-pick and revert */
-	if (opts->action == REPLAY_PICK)
-		error(_("Your local changes would be overwritten by cherry-pick."));
-	else
-		error(_("Your local changes would be overwritten by revert."));
+	error(_("Your local changes would be overwritten by %s."),
+		action_name(opts));

If I understand it correctly, it would make "revert" or "cherry-pick" untranslated part of error message. You would need to use translation on the result with "_(action_name(opts))", you would have to mark todo_command_strings elements for gettext lexicon with N_(...).

Quoted 7 lines
Quoted 1 line

I am rather against this change (see also below).

Okay.

Unfortunately, I have to focus on the correctness of the code at the moment (and Git for Windows does ship *without* translations for the time being anyway, mostly to save on space, but also because users complained).

Users complained about having translations, or not having easy way to switch them or switch them off?

Quoted 6 lines

So I will take care of this after v2.10.0.

For the record, how is this supposed to be handled, in particular when I introduce a new action whose action_name(opts) will be "rebase -i"? Do I really need to repeat myself three times?

I think you should be able to mark strings to be translated, without translating them at the time of definition,

  static const char *todo_command_strings[] = {
  	N_("pick"),
  	N_("revert")
  };

then translate at the point of use

error(_("Your local changes would be overwritten by %s."),

		_(action_name(opts)));

I assume that action_name(opts) returns one of todo_command_strings. If not, there should be array with possible actions.

Assuming that such lego l10n is preferable to multiple translations, more free-formt.

Signature · 1 line
Jakub Narębski
#77 lore raw

Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

In reply to Johannes Schindelin

W dniu 01.09.2016 o 11:37, Johannes Schindelin pisze:

Quoted 17 lines · Junio C Hamano
Quoted 16 lines
Quoted 13 lines · Jakub Narębski

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 713

Quoted 10 lines
sequencer.c +2 -0
index 06759d4..3398774 100644
@@ -709,6 +709,8 @@ static int read_and_refresh_cache(struct replay_opts *opts)
 struct todo_item {
 	enum todo_command command;
 	struct commit *commit;
+	const char *arg;
+	int arg_len;

Why 'arg', and not 'oneline', or 'subject'?
I'm not saying it is bad name.

Quoted 8 lines
Quoted 4 lines

I am not sure what the "commit" field of type "struct commit *" is for. It is not needed until it is the commit's turn to be picked or reverted; if we end up stopping in the middle, parsing the commit object for later steps will end up being wasted effort.

No, it won't be wasted effort, as we *validate* the todo script this way. And since we may very well need the info later (most rebases do not fail in the middle), we store it, too.

The question was (I think) whether we should do eager parsing of commits, or whether we can do lazy parsing by postponing full parsing "until it is the commit's turn to be picked or reverted", and possibly when saving todo file.

I wonder how probable is situation where we save instruction sheet for interactive rebase, with shortened SHA-1, and during rebase shortened SHA-1 stops being unambiguous...

Quoted 8 lines

[...] after parsing the todo_list, we will
have to act on the information contained therein. For example we will have to cherry-pick some of the indicated commits (requiring a struct commit * for use in do_pick_commit()). Another example: we may need to determine the oneline for use in fixup!/squash! reordering.

So: keeping *that* aspect of the previous todo_list parsing, i.e. store a pointer to the already-parsed commit, is the right thing to do.

The above probably means that eager eval is better

Best,

Signature · 1 line
Jakub Narębski
#69 lore raw

Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

In reply to Johannes Schindelin

Hello,

W dniu 01.09.2016 o 15:12, Johannes Schindelin pisze:

Quoted 22 lines · Jakub Narębski
Quoted 11 lines

W dniu 31.08.2016 o 21:10, Junio C Hamano pisze:

Quoted 8 lines

So I am not sure if we want a parsed commit there (I would not object if we kept the texual object name read from the file, though). The "one sequencer to rule them all" may even have to say "now give name ':1' to the result of the previous operation" in one step and in another later step have an instruction "merge ':1'". When that happens, you cannot even pre-populate the commit object when the sequencer reads the file, as the commit has not yet been created at that point.

Quoted 1 line

True, --preserve-merges rebase is well, different.

It is mis-designed. And I can be that harsh because it was my design.

In the meantime I came up with a much better design, and implemented it as a shell script on top of rebase -i. Since shell scripts run like slow molasses, even more so on Windows, I have a loose plan to implement its functionality as a new --recreate-merges option, and to deprecate --preserve-merges when that new option works.

Quoted 3 lines

It needs to be a new option (not a --preserve-merges=v2) because it is a totally different beast. For starters, it does not need its own code path that overrides pick_one, as --preserve-merges does.

Better preserving for merges (with cleanly defined sematics) would be certainly nice to have.

Quoted 2 lines

But I get way ahead of myself. First we need to get these last few bits and pieces in place to accelerate (non --preserve-merges) rebase -i.

But it can wait, right.

Signature · 1 line
Jakub Narębski
#78 lore raw

Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

In reply to Jakub Narębski
Quoted 3 lines · Jakub Narębski

I wonder how probable is situation where we save instruction sheet for interactive rebase, with shortened SHA-1, and during rebase shortened SHA-1 stops being unambiguous...

It is my understanding that the shortened ones are only for end-user consumption. The insn sheet internally uses fully expanded form for this exact reason, and then abbreviated back at each step before the updated one is presented to the end-user. Uniqueness guarantee is enforced with new objects created during each step taken into account by doing it this way.

#99 lore raw

Re: [PATCH 16/22] sequencer: prepare for rebase -i's GPG settings

In reply to Johannes Schindelin

Hello Johannes,

W dniu 01.09.2016 o 15:33, Johannes Schindelin pisze:

Quoted 5 lines · Jakub Narębski
Quoted 4 lines

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

Quoted 2 lines
(quoted diff) +0 -0
@@ -471,17 +487,20 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 
Quoted 13 lines
Quoted 5 lines
Quoted 5 lines
sequencer.c +2 -1
 	if (IS_REBASE_I()) {
 		env = read_author_script();
-		if (!env)
+		if (!env) {
+			const char *gpg_opt = gpg_sign_opt_quoted(opts);
Quoted 8 lines

sequencer.c int sequencer_commit(const char *defmsg, struct replay_opts *opts, · line 478

Quoted 5 lines
sequencer.c +1 -1
+
 			return error("You have staged changes in your working "
 				"tree. If these changes are meant to be\n"
 				"squashed into the previous commit, run:\n\n"
-				"  git commit --amend $gpg_sign_opt_quoted\n\n"

How did this get expanded by error(), and why we want to replace it if it works?

After writing this email, I got an idea on how it could work:
git-rebase script calls some C helper, which outputs above, and output of this helper is eval'ed by script (with gpg_sign_opt_quoted variable present in the environment)...

Quoted 3 lines

It did not work. It was a place-holder waiting for this patch ;-)

... but it might have been simply copy'n'pasted from shell script to C, literally.

Quoted 14 lines
Quoted 14 lines

sequencer.c int sequencer_commit(const char *defmsg, struct replay_opts *opts, · line 502

Quoted 10 lines
sequencer.c +3 -2
+				"  git commit --amend %s\n\n"
 				"If they are meant to go into a new commit, "
 				"run:\n\n"
-				"  git commit $gpg_sign_opt_quoted\n\n"
+				"  git commit %s\n\n"
 				"In both case, once you're done, continue "
 				"with:\n\n"
-				"  git rebase --continue\n");
+				"  git rebase --continue\n", gpg_opt, gpg_opt);

Instead of passing option twice, why not make use of %1$s (arg reordering), that is

Quoted 6 lines
Quoted 3 lines

+ " git commit --amend %1$s\n\n"
[...]
+ " git commit %1$s\n\n"

Cute. But would this not drive the l10ners insane?

Shouldn't, as l10ners need to deal with arg reordering, because in different languages the order of words might be different: %s %s in English may be %2$s %1$s in other language, see example in
https://www.gnu.org/software/gettext/manual/gettext.html#c_002dformat-Flag

Best,

Signature · 1 line
Jakub Narębski
#109 lore raw

Re: [PATCH 19/22] sequencer: support cleaning up commit messages

In reply to Johannes Schindelin

Hello Johannes,

W dniu 01.09.2016 o 15:56, Johannes Schindelin pisze:

Quoted 12 lines · Jakub Narębski
Quoted 11 lines

It's a pity that emulation of named parameters in C requires relying on designated inits from C99

  typedef struct {
    double pressure, moles, temp;
  } ideal_struct;

  #define ideal_pressure(...) ideal_pressure_base((ideal_struct){.pressure=1,   \
                                        .moles=1, .temp=273.15, __VA_ARGS__})
Quoted 6 lines
Quoted 6 lines
  double ideal_pressure_base(ideal_struct in)
  {
    return 8.314 * in.moles*in.temp/in.pressure;
  }

... ideal_pressure(.moles=2, .temp=373.15) ...

Forgot to add citation:

[1] Ben Klemens "21st Century C: C Tips from the New School", 2nd Ed. (2014), O'Reilly Media, chapter 10. "Better Structures", subsection "Optional and Named Arguments"

Quoted 3 lines

Yeah, that looks unwieldy ;-)

Declaration needs some trickery, but use is much, much more readable (if we cannot use sensibly named variables for passing arguments):

ideal_pressure()
ideal_pressure(.temp=373.15)
ideal_pressure(.moles=2)
ideal_pressure(.moles=2, .temp=373.15)

It is even better if there are large amount of parameters:

res = amortization(.amount=200000, .inflation=3,

                     .show_table=0, .extra_payoff=100)

vs

double amortize(double amt, double rate, double inflation, int months,
int selloff_month, double extra_payoff, int verbose, double *interest_pv, double *duration, double *monthly_payment);

But we can't use it in Git, anyway

Signature · 1 line
Jakub Narębski
#120 lore raw

Re: [PATCH 21/22] sequencer: left-trim the lines read from the script

In reply to Johannes Schindelin

Hello Johannes,

W dniu 01.09.2016 o 16:13, Johannes Schindelin pisze:

Quoted 5 lines · Jakub Narębski
Quoted 2 lines

'bol' is beginning-of-line, isn't it (a complement to eol)?

Yep. How did you guess? :-)

Wouldn't 'beg' and 'end' instead of 'bol' and 'eol' be easier to understand, thus more readable?

Signature · 1 line
Jakub Narębski
#125 lore raw

Re: [PATCH 22/22] sequencer: refactor write_message()

In reply to Johannes Schindelin

Hello Johannes,

W dniu 01.09.2016 o 16:20, Johannes Schindelin pisze:

Quoted 6 lines · Jakub Narębski
Quoted 5 lines

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

Quoted 2 lines
sequencer.c +0 -0
 	if (commit_lock_file(&msg_file) < 0)
 		return error(_("Error wrapping up %s."), filename);
Quoted 7 lines
Quoted 5 lines

Another "while at it"... though the one that can be safely postponed (well, the make message easier to understand part, not the quote filename part):

return error(_("Error wrapping up writing to '%s'."), filename);

As I inherited this message, I'll keep it.

Well, please then add quotes while at it, at least, for consistency

  		return error(_("Error wrapping up '%s'."), filename);

Best,

Signature · 1 line
Jakub Narębski
#131 lore raw
In reply to Johannes Schindelin

W dniu 29.08.2016 o 10:03, Johannes Schindelin pisze:

Quoted 3 lines

This patch series marks the '4' in the countdown to speed up rebase -i by implementing large parts in C. It is based on the libify-sequencer patch series that I submitted last week.

Which of those got reviewed (and perhaps accepted), and which of those needs review still? What is subject of their cover letter?

Quoted 7 lines

The patches in this series merely prepare the sequencer code for the next patch series that actually teaches the sequencer to run an interactive rebase.

The reason to split these two patch series is simple: to keep them at a sensible size.

That's good.

Quoted 5 lines

The two patch series after that are much smaller: a two-patch "series" that switches rebase -i to use the sequencer (except with --root or --preserve-merges), and a couple of patches to move several pretty expensive script processing steps to C (think: autosquash).

I can understand --preserve-merges, but what is the problem with --root?

Quoted 4 lines

The end game of this patch series is a git-rebase--helper that makes rebase -i 5x faster on Windows (according to t/perf/p3404). Travis says that even MacOSX and Linux benefit (4x and 3x, respectively).

So do I understand correctly that end goal for *this* series is to move most of processing to git-rebase--helper, but full builtin-ification (and retiring git-rebase.sh to contrib/examples/) would have to wait for later?

[...]

I'd like here to summarize the discussion (my review, Dennis review, Johannes Sixt and Junio comments).

If there are no comments, it means no problems or minor changes.

Quoted 2 lines

Johannes Schindelin (22):
sequencer: use static initializers for replay_opts

There is no need for putting zeros in static initializer. Commit message expanded.

Quoted 3 lines

sequencer: use memoized sequencer directory path
sequencer: avoid unnecessary indirection
sequencer: future-proof remove_sequencer_state()

Leftover unrelated chunk removed.

Quoted 1 line

sequencer: allow the sequencer to take custody of malloc()ed data

Is introducing new *_entrust() mechanism (which needs docs, at least as comments) worth it, instead of just strdup everything and free? If it is: naming of function parameter + example in commit message.

Quoted 1 line

sequencer: release memory that was allocated when reading options

See above.

Quoted 1 line

sequencer: future-proof read_populate_todo()

Possibly mention which functions were not future-proofed because of planned for the subsequent patch full rewrite.

Quoted 1 line

sequencer: remove overzealous assumption

Overzealous assumptions, or a worthy check? Perhaps just remove check for rebase -i in future commit, and keep test. Perhaps remove test temporarily.

Quoted 1 line

sequencer: completely revamp the "todo" script parsing

This removes check; it should return if it was worthy. Some discussion about eager versus lazy parsing of commits, but IMHO it should be left for later, if considered worth it.

Quoted 1 line

sequencer: avoid completely different messages for different actions

Fix l10n or drop (and not introduce lego translation).

Quoted 2 lines

sequencer: get rid of the subcommand field
sequencer: refactor the code to obtain a short commit name

Explain reason behind this change in the commit mesage.

Quoted 1 line

sequencer: remember the onelines when parsing the todo file

Lazy or eager again; "exec", "noop" and --preserve-merges.

Quoted 1 line

sequencer: prepare for rebase -i's commit functionality

Add helper function, possibly extract helper function. Rephrase block comment.

"[PATCH] am: refactor read_author_script()" from Junio.

Quoted 1 line

sequencer: introduce a helper to read files written by scripts

Perhaps add why not use open + strbuf_getline to commit message...

Quoted 1 line

sequencer: prepare for rebase -i's GPG settings

Possibly fixes bug. Use *_entrust() or strdup to not leak memory (and to not crash when freeing memory).

Quoted 1 line

sequencer: allow editing the commit message on a case-by-case basis

Enhance the commit message.

Quoted 5 lines

sequencer: support amending commits
sequencer: support cleaning up commit messages
sequencer: remember do_recursive_merge()'s return value
sequencer: left-trim the lines read from the script
sequencer: refactor write_message()

Enhance the commit message. Quote path in messages while at it.

HTH

Quoted 8 lines
 builtin/commit.c                |   2 +-
 builtin/revert.c                |  42 ++-
 sequencer.c                     | 573 +++++++++++++++++++++++++++-------------
 sequencer.h                     |  27 +-
 t/t3510-cherry-pick-sequence.sh |  11 -
 5 files changed, 428 insertions(+), 227 deletions(-)
Quoted 4 lines

Based-On: libify-sequencer at https://github.com/dscho/git
Fetch-Base-Via: git fetch https://github.com/dscho/git libify-sequencer
Published-As: https://github.com/dscho/git/releases/tag/prepare-sequencer-v1
Fetch-It-Via: git fetch https://github.com/dscho/git prepare-sequencer-v1

An unrelated question: Dscho, how are you generating above lines?

Signature · 1 line
Jakub Narębski
#132 lore raw

Hi Kuba,

Quoted 8 lines · Jakub Narębski

W dniu 29.08.2016 o 10:03, Johannes Schindelin pisze:

Quoted 3 lines

This patch series marks the '4' in the countdown to speed up rebase -i by implementing large parts in C. It is based on the libify-sequencer patch series that I submitted last week.

Which of those got reviewed (and perhaps accepted), and which of those needs review still? What is subject of their cover letter?

Most of the patch series I sent before last week got accepted. Only one got rejected, IIRC, and replaced by a better solution (3727318 (Merge branch 'jk/test-send-sh-x-trace-elsewhere', 2016-05-17)).

The patch series I submitted as part of my rebase--helper work that were accepted:

b232439 (Merge branch 'js/t3404-typofix', 2016-05-17)
7b02771 (Merge branch 'js/perf-rebase-i', 2016-05-23)
3437017 (Merge branch 'js/perf-on-apple', 2016-07-06)
62e5e83 (Merge branch 'js/find-commit-subject-ignore-leading-blanks', 2016-07-11) c510926 (Merge branch 'js/sign-empty-commit-fix', 2016-07-13) 6c35952 (Merge branch 'js/t3404-grammo-fix', 2016-07-13)
63641fb (Merge branch 'js/log-to-diffopt-file', 2016-07-19)
3d55eea (Merge branch 'js/am-call-theirs-theirs-in-fallback-3way', 2016-07-19) c97268c (Merge branch 'js/rebase-i-tests', 2016-07-28)
1a5f1a3 (Merge branch 'js/am-3-merge-recursive-direct', 2016-08-10)

You will note that I broke out a couple of patch series that do not strictly have anything to do with the rebase--helper, such as perf-on-apple. Nevertheless, they were part of a 99-strong patch series that was my initial working rebase--helper, which I have used ever since to perform all of my interactive rebases.

There are still a couple of patch series in flight. Let me list them by the tags created by my mail-patch-series.sh script:

https://github.com/dscho/git/releases/tag/libify-sequencer-v2
https://github.com/dscho/git/releases/tag/require-clean-work-tree-v1
https://github.com/dscho/git/releases/tag/prepare-sequencer-v1
https://github.com/dscho/git/releases/tag/sequencer-i-v1
https://github.com/dscho/git/releases/tag/rebase--helper-v1

These tags all contain links to the cover letter as stored on public-inbox.org, identified by the Message-ID.

Please note that the first four of this batch of five already saw substantial work-after-review, thanks in part to your helpful comments. You may appreciate the fact that a link of the form

https://github.com/dscho/git/compare/libify-sequencer-v2...libify-sequencer

shows you where I am at, although it cannot give you a real interdiff because I rebased to a newer version of upstream/master in the meantime.

Finally, there is one last patch series that I did not yet submit: the 'rebase-i-extra' patch series. However, as I continuously update the overall 'interactive-rebase' branch thicket (and have done so since the very beginning of my work on the rebase--helper), it is relatively easy to see what is left:

https://github.com/dscho/git/compare/rebase--helper...interactive-rebase

BTW thanks for making me dig out all of this information (it did take a while to uncover it...), as I am so totally going to use that in a blog post.

Quoted 4 lines
Quoted 2 lines

The reason to split these two patch series is simple: to keep them at a sensible size.

That's good.

Thanks. I really try to be sensible with other people's time.

Even more so after being so offended by the talk at the most recent Git Merge that stated that some people deliberately waste contributors' time because they value their own time so much more. I am *really* offended by that.

As a maintainer of Git for Windows, I do everything in my power to strike a sensible balance between how much time I spend on improving the software and how much time I ask others to do so.

Quoted 6 lines
Quoted 4 lines

The two patch series after that are much smaller: a two-patch "series" that switches rebase -i to use the sequencer (except with --root or --preserve-merges), and a couple of patches to move several pretty expensive script processing steps to C (think: autosquash).

I can understand --preserve-merges, but what is the problem with --root?

The problem with --root is that it *creates* an initial commit. It is empty, and will be amended. It would most likely not be a lot of work, but I really wanted this work to be incremental, focusing on the most important aspects first.

In fact, I do hope that somebody with the need for --root will take the baton and run with it.

Quoted 8 lines
Quoted 3 lines

The end game of this patch series is a git-rebase--helper that makes rebase -i 5x faster on Windows (according to t/perf/p3404). Travis says that even MacOSX and Linux benefit (4x and 3x, respectively).

So do I understand correctly that end goal for *this* series is to move most of processing to git-rebase--helper, but full builtin-ification (and retiring git-rebase.sh to contrib/examples/) would have to wait for later?

Oh yes!

Retiring git-rebase.sh is *far, far, far* in the future. We really missed the boat a *looooong* time ago to turn this from a hacky shell script into a proper C builtin.

There is so much more to do before git-rebase.sh can be retired.

For starters, git-rebase.sh is actually just a glorified command-line option parser and front-end to git-rebase--am.sh, git-rebase--interactive.sh and git-rebase--merge.sh.

To retire it, those three shell scripts need to be *completely* built-in first.

(Actually, for the --preserve-merges case, I could imagine that we simply refactor it into its own shell script and call that from a builtin git-rebase, until we retire --preserve-merges, but that's a couple of years down the road.)

So the first goal would be to retire git-rebase--interactive.sh. For that to happen, --root needs to be supported first. Then the --preserve-merges stuff needs to be refactored into its own shell script. And then the command-line option parsing needs to be moved to rebase--helper, too. And *then* git-rebase--interactive.sh can be retired.

As I stated earlier, my hope is that the rebase--helper work is only an initial step, opening the door for other contributors to tackle independent parts of making git-rebase a builtin.

Quoted 6 lines

[...]

I'd like here to summarize the discussion (my review, Dennis review, Johannes Sixt and Junio comments).

If there are no comments, it means no problems or minor changes.

Please keep in mind that my current state (local, and pushed to my GitHub repository) has advanced substantially. I am reluctant to send it out yet because I still need to send out rebase-i-extra first, so that it gets *some* visibility before v2.10.0.

Quoted 15 lines
Quoted 2 lines

Johannes Schindelin (22):
sequencer: use static initializers for replay_opts

There is no need for putting zeros in static initializer. Commit message expanded.

Quoted 3 lines

sequencer: use memoized sequencer directory path
sequencer: avoid unnecessary indirection
sequencer: future-proof remove_sequencer_state()

Leftover unrelated chunk removed.

Quoted 1 line

sequencer: allow the sequencer to take custody of malloc()ed data

Is introducing new *_entrust() mechanism (which needs docs, at least as comments) worth it, instead of just strdup everything and free? If it is: naming of function parameter + example in commit message.

Quoted 6 lines
Quoted 1 line

sequencer: release memory that was allocated when reading options

See above.

Quoted 1 line

sequencer: future-proof read_populate_todo()

Possibly mention which functions were not future-proofed because of planned for the subsequent patch full rewrite.

Note that this commit is about read_populate_todo(), not about save_todo(). So I do not think that we should mention anything in this commit's message about other functions that may be rewritten instead of being future-proofed..

Quoted 4 lines
Quoted 1 line

sequencer: remove overzealous assumption

Overzealous assumptions, or a worthy check? Perhaps just remove check for rebase -i in future commit, and keep test. Perhaps remove test temporarily.

As mentioned earlier, I bit the bullet and reimplemented that logic. Mostly to fend off more comments in this direction.

Quoted 4 lines
Quoted 1 line

sequencer: completely revamp the "todo" script parsing

This removes check; it should return if it was worthy. Some discussion about eager versus lazy parsing of commits, but IMHO it should be left for later, if considered worth it.

Again, it was reintroduced. The test to check for the overzealous assumption was not removed, and it passes, to prove that I did it right.

Quoted 24 lines
Quoted 1 line

sequencer: avoid completely different messages for different actions

Fix l10n or drop (and not introduce lego translation).

Quoted 2 lines

sequencer: get rid of the subcommand field
sequencer: refactor the code to obtain a short commit name

Explain reason behind this change in the commit mesage.

Quoted 1 line

sequencer: remember the onelines when parsing the todo file

Lazy or eager again; "exec", "noop" and --preserve-merges.

Quoted 1 line

sequencer: prepare for rebase -i's commit functionality

Add helper function, possibly extract helper function. Rephrase block comment.

"[PATCH] am: refactor read_author_script()" from Junio.

Quoted 1 line

sequencer: introduce a helper to read files written by scripts

Perhaps add why not use open + strbuf_getline to commit message...

Quoted 1 line

sequencer: prepare for rebase -i's GPG settings

Possibly fixes bug. Use *_entrust() or strdup to not leak memory (and to not crash when freeing memory).

Quoted 1 line

sequencer: allow editing the commit message on a case-by-case basis

Quoted 8 lines

Enhance the commit message.

Quoted 5 lines

sequencer: support amending commits
sequencer: support cleaning up commit messages
sequencer: remember do_recursive_merge()'s return value
sequencer: left-trim the lines read from the script
sequencer: refactor write_message()

Enhance the commit message. Quote path in messages while at it.

Apart from the l10n issues, I think I addressed them all locally, and pushed the result out to my GitHub repository, although I plan to send out additional iterations only after releasing Git for Windows v2.10.0.

Quoted 6 lines
Quoted 4 lines

Based-On: libify-sequencer at https://github.com/dscho/git
Fetch-Base-Via: git fetch https://github.com/dscho/git libify-sequencer
Published-As: https://github.com/dscho/git/releases/tag/prepare-sequencer-v1
Fetch-It-Via: git fetch https://github.com/dscho/git prepare-sequencer-v1

An unrelated question: Dscho, how are you generating above lines?

It's the mail-patch-series.sh script, in conjunction with setting the config variable mail.publishremoteto to point to my GitHub remote. You can find the mail-patch-series.sh script here:

https://github.com/dscho/mail-patch-series

I probably forgot to adjust the README to reflect the most recent changes (such as the --basedon feature)... PRs welcome [*1*].

Ciao,
Dscho

Footnote *1*:
https://raw.githubusercontent.com/dscho/images/master/i-can-haz-pull-request.png

#50 lore raw

Re: [PATCH 09/22] sequencer: completely revamp the "todo" script parsing

In reply to Jakub Narębski

Hi Kuba,

Quoted 7 lines · Jakub Narębski

W dniu 01.09.2016 o 09:49, Johannes Schindelin pisze:

Quoted 6 lines · Jakub Narębski
Quoted 4 lines

Here todo_list uses growable array implementation of list. Which is I guess better on current CPU architecture, with slow memory, limited-size caches, and adjacency prefetching.

Quoted 7 lines
Quoted 2 lines

That is not the reason that an array is used here. The array allows us much more flexibility.

It would be nice if this reasoning (behind the change from linked list to growable array) was mentioned in appropriate commit message, and perhaps also in the cover letter for the series. It is IMVHO quite important information (that you thought obvious).

Amended.

Quoted 21 lines
Quoted 7 lines
Quoted 4 lines
Quoted 1 line

+struct todo_item *append_todo(struct todo_list *todo_list)

Errr... I don't quite understand the name of this function. What are you appending here to the todo_list?

A new item.

Quoted 8 lines
Quoted 2 lines

Compare string_list_append() and string_list_append_nodup(), where the second parameter is item to append.

Yes, that is correct. In the case of a todo_item, things are a lot more complicated, though. Some of the values have to be determined tediously (such as the offset and length of the oneline after the "pick <oid>" command). I just put those values directly into the newly allocated item, is all.

I would expect sth_append command to take a list (or other collection), an element, and return [modified] collection with the new element added. Such API would require temporary variable in caller and memcopy in the sth_append() function.

Quoted 6 lines

This is not it. It creates a new element, expanding a list (a collection), and then expose this element. Which spares us memcopy... on non-critical path.

I don't know how to name operation "grow list and return new element". But "append" it is not.

I renamed it to append_new_todo().

Quoted 9 lines
Quoted 7 lines
Quoted 4 lines

sequencer.c static int read_and_refresh_cache(struct replay_opts *opts) · line 754

Quoted 2 lines
sequencer.c +1 -1
-	end_of_object_name = bol + strcspn(bol, " \t\n");
+	end_of_object_name = (char *) bol + strcspn(bol, " \t\n");

Why is this cast needed?

Because bol is a "const char *" and we need to put "NUL" temporarily to *end_of_object_name:

Would compiler complain without this const'ness-stripping cast?

Yes. I would not have added it otherwise.

Please note that this is only necessary because I changed the parameter from "char *" to "const char *" (which was The Right Thing To Do).

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 4 lines
sequencer.c +0 -0
 	saved = *end_of_object_name;
 	*end_of_object_name = '\0';
 	status = get_sha1(bol, commit_sha1);
 	*end_of_object_name = saved;
Quoted 8 lines
Quoted 6 lines

Technically, this would have made a fine excuse to teach get_sha1() a mode where it expects a length parameter instead of relying on a NUL-terminated string.

Practically, such fine excuses cost me months in this rebase--helper project already, and I need to protect my time better.

Put it in TODO list (and perhaps add a TODO comment) ;-).

I am also a realist: I won't be able to do anything about this. If you care enough, please go right to town.

Thanks again,
Dscho

#55 lore raw

Re: [PATCH 10/22] sequencer: avoid completely different messages for different actions

In reply to Jakub Narębski

Hi Kuba,

Quoted 18 lines · Jakub Narębski

W dniu 01.09.2016 o 09:52, Johannes Schindelin pisze:

Quoted 3 lines · Jakub Narębski
Quoted 2 lines

CC-ed to Jiang Xin, L10N coordinator.
W dniu 29.08.2016 o 10:05, Johannes Schindelin pisze:

[...]

Quoted 12 lines
Quoted 12 lines

sequencer.c static int error_dirty_index(struct replay_opts *opts) · line 236

Quoted 7 lines
sequencer.c +2 -5
-	/* Different translation strings for cherry-pick and revert */
-	if (opts->action == REPLAY_PICK)
-		error(_("Your local changes would be overwritten by cherry-pick."));
-	else
-		error(_("Your local changes would be overwritten by revert."));
+	error(_("Your local changes would be overwritten by %s."),
+		action_name(opts));

If I understand it correctly, it would make "revert" or "cherry-pick" untranslated part of error message. You would need to use translation on the result with "_(action_name(opts))", you would have to mark todo_command_strings elements for gettext lexicon with N_(...).

Okay, that is easy enough.

Ciao,
Dscho

#79 lore raw

Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

In reply to Junio C Hamano

Hi Junio,

Quoted 6 lines · Junio C Hamano
Quoted 3 lines · Jakub Narębski

I wonder how probable is situation where we save instruction sheet for interactive rebase, with shortened SHA-1, and during rebase shortened SHA-1 stops being unambiguous...

Quoted 6 lines

It is my understanding that the shortened ones are only for end-user consumption. The insn sheet internally uses fully expanded form for this exact reason, and then abbreviated back at each step before the updated one is presented to the end-user. Uniqueness guarantee is enforced with new objects created during each step taken into account by doing it this way.

Indeed, the rebase -i shortens the SHA-1s just before letting the user edit git-rebase-todo and then expands them back.

Ciao,
Dscho

#121 lore raw

Re: [PATCH 21/22] sequencer: left-trim the lines read from the script

In reply to Jakub Narębski

Hi Kuba,

Quoted 11 lines · Jakub Narębski

Hello Johannes,

W dniu 01.09.2016 o 16:13, Johannes Schindelin pisze:

Quoted 5 lines · Jakub Narębski
Quoted 2 lines

'bol' is beginning-of-line, isn't it (a complement to eol)?

Yep. How did you guess? :-)

Wouldn't 'beg' and 'end' instead of 'bol' and 'eol' be easier to understand, thus more readable?

It is just consistency with the code I inherited: sequencer.c used 'bol' and 'eol' before.

Ciao,
Dscho

#126 lore raw

Re: [PATCH 22/22] sequencer: refactor write_message()

In reply to Jakub Narębski

Hi Kuba,

Quoted 11 lines · Jakub Narębski

W dniu 01.09.2016 o 16:20, Johannes Schindelin pisze:

Quoted 10 lines · Jakub Narębski
Quoted 9 lines

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

sequencer.c static void print_advice(int show_hint, struct replay_opts *opts) · line 255

Quoted 2 lines
sequencer.c +0 -0
 	if (commit_lock_file(&msg_file) < 0)
 		return error(_("Error wrapping up %s."), filename);

Another "while at it"... though the one that can be safely postponed (well, the make message easier to understand part, not the quote filename part):

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

return error(_("Error wrapping up writing to '%s'."), filename);

As I inherited this message, I'll keep it.

Well, please then add quotes while at it, at least, for consistency

  		return error(_("Error wrapping up '%s'."), filename);

I may do that as a final patch, once all the other concerns are addressed. I really do not want to change the error message during the conversion.

Ciao,
Dscho

#119 lore raw

Re: [PATCH 21/22] sequencer: left-trim the lines read from the script

In reply to Junio C Hamano

Hi Junio,

Quoted 22 lines · Junio C Hamano
Quoted 20 lines · Johannes Schindelin
Quoted 5 lines
Quoted 3 lines

Interactive rebase's scripts may be indented; We need to handle this case, too, now that we prepare the sequencer to process interactive rebases.

s/; We need/; we need/

Hrmpf. From http://grammar.ccc.commnet.edu/grammar/marks/colon.htm:

There is some disagreement among writing reference manuals about when you should capitalize an independent clause following a colon. Most of the manuals advise that when you have more than one sentence in your explanation or when your sentence(s) is a formal quotation, a capital is a good idea. The NYPL Writer's Guide urges consistency within a document; the Chicago Manual of Style says you may begin an independent clause with a lowercase letter unless it's one of those two things (a quotation or more than one sentence). The APA Publication Manual is the most extreme: it advises us to always capitalize an independent clause following a colon. The advice given above is consistent with the Gregg Reference Manual.

Quoted 4 lines
Quoted 1 line

Based on that, I think that a capital is the correct case here.

Does that manual have anything to say about semicolons, which is a different thing?

You're correct, I overlooked that.

Fixed,
Dscho

#72 lore raw

Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

In reply to Junio C Hamano

Hi Junio,

Quoted 9 lines · Junio C Hamano
Quoted 7 lines · Johannes Schindelin
Quoted 6 lines

though). The "one sequencer to rule them all" may even have to say "now give name ':1' to the result of the previous operation" in one step and in another later step have an instruction "merge ':1'". When that happens, you cannot even pre-populate the commit object when the sequencer reads the file, as the commit has not yet been created at that point.

Quoted 7 lines
Quoted 3 lines

These considerations are pretty hypothetical. I would even place a bet that we will *never* have ":1" as names, not if I have anything to say... ;-)

If you can always work with pre-existing commit, then you can validate all object references that appear in the instructions upfront.

Or if *some* of the commands work with pre-existing commits, *those* commands can be validated up-front.

Which is exactly what my code does.

Quoted 7 lines

I was sort of expecting that, when you do the preserve-merges mode of "rebase -i", you would need to jump around, doing "we have reconstructed the side branch on a new 'onto', let's give the result this temporary name ':1', and then switch to the trunk (which would call for 'reset <commit>' instruction) and merge that thing (which would be 'merge :1' or perhaps called 'pick :1')", and at that point you no longer validate the object references upfront.

Except that is not how --preserve-merges works: it *still* uses the SHA-1s as identifiers, even when the SHA-1 may have changed in the meantime.

That is part of why it was a bad design.

Quoted 3 lines

If you do not have to have such a "mark this point" and a "refer to that point we previously marked", then I agree that you should be able to pre-validate and keep the result in the structure.

Even then, those markers should *still* be validated. They, too, need to be created and later used, usage before creation would be an error.

But...

1) this is not yet a problem, so why are we discussing it here? Do we not have actual problems with these patches to discuss anymore?

2) the SHA-1s that *can* be validated *should* be validated, so I find the objection a little bogus.

Ciao,
Dscho

#73 lore raw

Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

Hello Johannes,

Quoted 11 lines · Johannes Schindelin
Quoted 9 lines · Junio C Hamano

Johannes Schindelin <[email protected]> writes:

I was sort of expecting that, when you do the preserve-merges mode of "rebase -i", you would need to jump around, doing "we have reconstructed the side branch on a new 'onto', let's give the result this temporary name ':1', and then switch to the trunk (which would call for 'reset <commit>' instruction) and merge that thing (which would be 'merge :1' or perhaps called 'pick :1')", and at that point you no longer validate the object references upfront.

Quoted 4 lines

Except that is not how --preserve-merges works: it *still* uses the SHA-1s as identifiers, even when the SHA-1 may have changed in the meantime.

That is part of why it was a bad design.

When preserving merges, there are (as far as I understand it), two problems:
- what it means to preserve changes (which change to pick, that is what is the mainline changes rebase is re-applying)
- what are parents of the merge commit (at least one parent would be usually rewritten)

Maybe the internal (and perhaps also user-visible) representation of merge in instruction sheet could use the notation of filter-branch, that is 'map(<sha-1>)'... it could also imply the mainline.

That is the instruction in the internal instruction sheet could look like this:

merge -m 1 map(2fd4e1c67a2d28fced849ee1bb76e7391b93eb12) da39a3ee5e6b4b0d3255bfef95601890afd80709 \t Merge 'foo' into master

Note that it has nothing to do with this series!

Best regards,

Signature · 1 line
Jakub Narębski
#127 lore raw

Re: [PATCH 22/22] sequencer: refactor write_message()

In reply to Johannes Schindelin

Hello Johannes,

Quoted 14 lines · Johannes Schindelin
Quoted 13 lines · Jakub Narębski

W dniu 01.09.2016 o 16:20, Johannes Schindelin pisze:

Quoted 12 lines · Jakub Narębski
Quoted 10 lines

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

sequencer.c static void print_advice(int show_hint, struct replay_opts *opts) · line 255

Quoted 2 lines
sequencer.c +0 -0
 	if (commit_lock_file(&msg_file) < 0)
 		return error(_("Error wrapping up %s."), filename);

Another "while at it"... though the one that can be safely postponed (well, the make message easier to understand part, not the quote filename part):

return error(_("Error wrapping up writing to '%s'."), filename);

Quoted 8 lines
Quoted 5 lines
Quoted 1 line

As I inherited this message, I'll keep it.

Well, please then add quotes while at it, at least, for consistency

  		return error(_("Error wrapping up '%s'."), filename);

I may do that as a final patch, once all the other concerns are addressed. I really do not want to change the error message during the conversion.

Is not wanting to change error messages during conversion because of your use of Scientist tool to catch errors in conversion process?, as you wrote in
https://blogs.msdn.microsoft.com/visualstudioalm/2016/09/03/whats-new-in-git-for-windows-2-10/

BTW. could you tell us what were those three regression caught by the cross-validation?

Best,

Signature · 1 line
Jakub Narębski
#128 lore raw

Re: [PATCH 22/22] sequencer: refactor write_message()

Hi Kuba,

Quoted 19 lines · Jakub Narębski
Quoted 18 lines · Johannes Schindelin
Quoted 17 lines · Jakub Narębski

W dniu 01.09.2016 o 16:20, Johannes Schindelin pisze:

Quoted 6 lines · Jakub Narębski
Quoted 5 lines

W dniu 29.08.2016 o 10:06, Johannes Schindelin pisze:

Quoted 2 lines
sequencer.c +0 -0
 	if (commit_lock_file(&msg_file) < 0)
 		return error(_("Error wrapping up %s."), filename);
Quoted 7 lines
Quoted 5 lines

Another "while at it"... though the one that can be safely postponed (well, the make message easier to understand part, not the quote filename part):

return error(_("Error wrapping up writing to '%s'."), filename);

As I inherited this message, I'll keep it.

Well, please then add quotes while at it, at least, for consistency

Quoted 7 lines
Quoted 4 lines
Quoted 1 line
  		return error(_("Error wrapping up '%s'."), filename);

I may do that as a final patch, once all the other concerns are addressed. I really do not want to change the error message during the conversion.

Is not wanting to change error messages during conversion because of your use of Scientist tool to catch errors in conversion process?

It is more out of an aversion to mix unrelated purposes in the same patch. You will see that I inserted an extra patch with the purpose of fixing the style, that touches all the relevant error messages in sequencer.c.

Quoted 2 lines

BTW. could you tell us what were those three regression caught by the cross-validation?

Sure!

The first one was that my original version of the rebase-i-extra patches did not reorder patches correctly when there was more than one space after the fixup!. I fixed it, and added this test:

cbcd2cb (rebase -i: we allow extra spaces after fixup!/squash!, 2016-07-07)

The second one was that git commit --fixup unwraps the commit subject into one long line and rebase -i *still* manages to find the correct commit to fix up. The test is part of the rebase-i-extra patches (and therefore you will find this commit only in my fork):

9fc25ce (t3415: test fixup with wrapped oneline, 2016-07-24)

The third one was an obscure one: when I marked a commit as 'edit' and there was a merge conflict cherry-picking that particular commit, rebase --continue would squash the resolved changes *into the previous* commit, but with the cherry-picked commit's message. It was a simple, stupid oversight to write the "amend" file in the "edit" code path even if the cherry-pick failed.

All fixed, of course.

Ciao,
Dscho

#74 lore raw

Git garden shears, was Re: [PATCH 13/22] sequencer: remember the onelines when parsing the todo file

In reply to Jakub Narębski

Hi Kuba,

Quoted 30 lines · Jakub Narębski

Hello Johannes,

Quoted 11 lines · Johannes Schindelin
Quoted 9 lines · Junio C Hamano

Johannes Schindelin <[email protected]> writes:

I was sort of expecting that, when you do the preserve-merges mode of "rebase -i", you would need to jump around, doing "we have reconstructed the side branch on a new 'onto', let's give the result this temporary name ':1', and then switch to the trunk (which would call for 'reset <commit>' instruction) and merge that thing (which would be 'merge :1' or perhaps called 'pick :1')", and at that point you no longer validate the object references upfront.

Quoted 4 lines

Except that is not how --preserve-merges works: it *still* uses the SHA-1s as identifiers, even when the SHA-1 may have changed in the meantime.

That is part of why it was a bad design.

When preserving merges, there are (as far as I understand it), two problems:
- what it means to preserve changes (which change to pick, that is what is the mainline changes rebase is re-applying)
- what are parents of the merge commit (at least one parent would be usually rewritten)

Maybe the internal (and perhaps also user-visible) representation of merge in instruction sheet could use the notation of filter-branch, that is 'map(<sha-1>)'... it could also imply the mainline.

Quoted 7 lines

That is the instruction in the internal instruction sheet could look like this:

merge -m 1 map(2fd4e1c67a2d28fced849ee1bb76e7391b93eb12) da39a3ee5e6b4b0d3255bfef95601890afd80709 \t Merge 'foo' into master

Note that it has nothing to do with this series!

Right. But I did solve that already. In the Git garden shears [*1*] (essentially my New And Improved attempt at recreating branch structures while rebasing), I generate and process scripts like this:

mark onto

# Branch: super-cool-feature
rewind onto
pick 00001 feature
pick 00002 documentation
mark super-cool-feature

# Branch: typo-fix
rewind onto
pick 0000a fix a tyop

rewind onto
merge -C cafebabe super-cool-feature
merge -C babecafe typo-fix

cleanup super-cool-feature typo-fix

Of course this will change a little, still, once I get around to implement this on top of the rebase--helper.

For example, I am not so hot about the "merge -C ..." syntax. I'll probably split that into a "remerge <SHA-1> <mark>" and a new "merge <mark>" command (the latter asking interactively for the merge commit message).

And also: the cleanup stage should not be necessary, as the "mark" commands can accumulate the known marks into a file in the state directory.

But you get the idea.

No :1 or some such. That's machine readable. But it's utter nonsense for user-facing UIs.

Ciao,
Dscho

Footnote *1*:
https://github.com/git-for-windows/build-extra/blob/master/shears.sh

#134 lore raw

PATCH v2 1/25 sequencer: use static initializers for replay_opts

In reply to Johannes Schindelin

This change is not completely faithful: instead of initializing all fields to 0, we choose to initialize command and subcommand to -1 (instead of defaulting to REPLAY_REVERT and REPLAY_NONE, respectively). Practically, it makes no difference at all, but future-proofs the code to require explicit assignments for both fields.

---
 builtin/revert.c | 6 ++----
 sequencer.h      | 1 +
 2 files changed, 3 insertions(+), 4 deletions(-)
builtin/revert.c +2 -4
index 4e69380..7365559 100644
@@ -178,10 +178,9 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
 
 int cmd_revert(int argc, const char **argv, const char *prefix)
 {
-	struct replay_opts opts;
+	struct replay_opts opts = REPLAY_OPTS_INIT;
 	int res;
 
-	memset(&opts, 0, sizeof(opts));
 	if (isatty(0))
 		opts.edit = 1;
 	opts.action = REPLAY_REVERT;
@@ -195,10 +194,9 @@ int cmd_revert(int argc, const char **argv, const char *prefix)
 
 int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
 {
-	struct replay_opts opts;
+	struct replay_opts opts = REPLAY_OPTS_INIT;
 	int res;
 
-	memset(&opts, 0, sizeof(opts));
 	opts.action = REPLAY_PICK;
 	git_config(git_default_config, NULL);
 	parse_args(argc, argv, &opts);
sequencer.h +1 -0
index 5ed5cb1..db425ad 100644
@@ -47,6 +47,7 @@ struct replay_opts {
 	/* Only used by REPLAY_NONE */
 	struct rev_info *revs;
 };
+#define REPLAY_OPTS_INIT { -1, -1 }
 
 int sequencer_pick_revisions(struct replay_opts *opts);
 
Signature · 1 line
2.10.0.windows.1.10.g803177d
#133 lore raw

v2 cover Prepare the sequencer for the upcoming rebase -i patches

In reply to Johannes Schindelin

This patch series marks the '4' in the countdown to speed up rebase -i by implementing large parts in C. It is based on the libify-sequencer patch series that I submitted last week.

The patches in this series merely prepare the sequencer code for the next patch series that actually teaches the sequencer to run an interactive rebase.

The reason to split these two patch series is simple: to keep them at a sensible size.

The two patch series after that are much smaller: a two-patch "series" that switches rebase -i to use the sequencer (except with --root or --preserve-merges), and a couple of patches to move several pretty expensive script processing steps to C (think: autosquash).

The end game of this patch series is a git-rebase--helper that makes rebase -i 5x faster on Windows (according to t/perf/p3404). Travis says that even MacOSX and Linux benefit (4x and 3x, respectively).

I have been working on this since early February, whenever time allowed, and it is time to put it into the users' hands. To that end, I will most likely submit the remaining three patch series in the next two days, and integrate the whole shebang into Git for Windows 2.10.0.

Therefore I would be most grateful for every in-depth review.

Changes vs v1:

- clarified why the code refactoring into the short_commit_name() function is desirable.

- fixed a typo in a commit message (s/se/so/).

- removed a bogus call to read_and_refresh_cache(opts) that was added to sequencer_rollback() by mistake.

- clarified in the commit message why the TODO_LIST_INIT macro does not assign all-zeroes.

- converted IS_REBASE_I() into an inline function.

- clarified the comment about retaining author metadata when calling sequencer_commit() in rebase -i mode.

- simplified TODO_LIST_INIT and REPLAY_OPTS_INIT.

- added an example how to use sequencer_entrust() to the commit message introducing it.

- the gpg_sign option's value is now also entrusted to the sequencer (i.e. it is released in sequencer_remove_state()).

- renamed the "set_me_free_after_use" to "to_free". Since that is still not a self-explanatory name, add a comment to the function declaration.

- clarified in the "completely revamped todo parsing" commit message that the main benefit of the early parsing is for rebase -i.

- start the enum todo_command with TODO_PICK that is explicitly assigned the value 0.

- an error was marked as translatable.

- converted one forgotten git_path_todo_file() to use get_todo_path(opts) instead.

- fixed numbering of "malformed instruction sheet"s after removing one.

- reintroduced the overzealous assumption that todo scripts can only perform revert commands during git revert and pick commands during git cherry-pick. This overzealous assumption is *still* disabled in rebase -i mode, of course.

- clarified in the commit message why we call the field "arg", not "oneline", and fixed the description that claimed that we store the end offset (we store the length instead).

- clarified in the commit message of "allow editing the commit message on a case-by-case basis" that we are talking about the sequencer_commit() function, and how things were done previously.

- some grammar touch-ups.

- marked a couple of error messages for translation.

- explicitly assign the first todo_command the value 0, so that we can be certain that the array of command strings matches up.

- removed code to skip CR/LF at the end of line after reading with read_oneliner(): That function already ensures that.

- ensured that the todo_list is released even when reading/parsing fails.

- replaced an error(..., strerror()) call with an error_errno(...) one.

- clarified in the commit message why the new todo_list maintains the script as an array instead of a linked list.

- renamed the append_todo() function into append_new_todo(), to explain better what the function does and why it does not take the values as parameters that should populate the new todo_item.

- marked action_name() for translation.

- surrounded all file names in error messages in sequencer.c with single quotes, for consistency.

- removed a bogus hint for translators that asked them to translate commands of the git-rebase-todo file (or cherry-pick's equivalent).

- turned capitals after semicolons to lower-case.

Johannes Schindelin (25):
sequencer: use static initializers for replay_opts
sequencer: use memoized sequencer directory path
sequencer: avoid unnecessary indirection
sequencer: future-proof remove_sequencer_state()
sequencer: allow the sequencer to take custody of malloc()ed data
sequencer: release memory that was allocated when reading options
sequencer: future-proof read_populate_todo()
sequencer: completely revamp the "todo" script parsing
sequencer: avoid completely different messages for different actions
sequencer: get rid of the subcommand field
sequencer: refactor the code to obtain a short commit name
sequencer: remember the onelines when parsing the todo file
sequencer: prepare for rebase -i's commit functionality
sequencer: introduce a helper to read files written by scripts
sequencer: prepare for rebase -i's GPG settings
sequencer: allow editing the commit message on a case-by-case basis
sequencer: support amending commits
sequencer: support cleaning up commit messages
sequencer: remember do_recursive_merge()'s return value
sequencer: left-trim lines read from the script
sequencer: refactor write_message()
sequencer: remove overzealous assumption in rebase -i mode
sequencer: mark action_name() for translation
sequencer: quote filenames in error messages
sequencer: remove bogus hint for translators

 builtin/commit.c |   2 +-
 builtin/revert.c |  42 ++--
 sequencer.c      | 632 +++++++++++++++++++++++++++++++++++++------------------
 sequencer.h      |  31 +--
 4 files changed, 468 insertions(+), 239 deletions(-)

Based-On: libify-sequencer at https://github.com/dscho/git
Fetch-Base-Via: git fetch https://github.com/dscho/git libify-sequencer
Published-As: https://github.com/dscho/git/releases/tag/prepare-sequencer-v2
Fetch-It-Via: git fetch https://github.com/dscho/git prepare-sequencer-v2

Interdiff vs v1:

diff --git a/sequencer.c b/sequencer.c
index 7d56676..cdff0f1 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -41,7 +41,10 @@ static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script")
static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")

/* We will introduce the 'interactive rebase' mode later */
-#define IS_REBASE_I() 0
+static inline int is_rebase_i(const struct replay_opts *opts) +{
+ return 0;
+}

static const char *get_dir(const struct replay_opts *opts)
{
@@ -145,12 +148,12 @@ static const char *gpg_sign_opt_quoted(struct replay_opts *opts)
return buf.buf;
}

-void *sequencer_entrust(struct replay_opts *opts, void *set_me_free_after_use) +void *sequencer_entrust(struct replay_opts *opts, void *to_free)
{
ALLOC_GROW(opts->owned, opts->owned_nr + 1, opts->owned_alloc);
- opts->owned[opts->owned_nr++] = set_me_free_after_use;
+ opts->owned[opts->owned_nr++] = to_free;

- return set_me_free_after_use;
+ return to_free;
}

int sequencer_remove_state(struct replay_opts *opts)
@@ -173,7 +176,7 @@ int sequencer_remove_state(struct replay_opts *opts)

static const char *action_name(const struct replay_opts *opts) {
- return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
+ return opts->action == REPLAY_REVERT ? N_("revert") : N_("cherry-pick");
}

struct commit_message {
@@ -248,11 +251,11 @@ static int write_with_lock_file(const char *filename,
if (msg_fd < 0)
return error_errno(_("Could not lock '%s'"), filename);
if (write_in_full(msg_fd, buf, len) < 0)
- return error_errno(_("Could not write to %s"), filename);
+ return error_errno(_("Could not write to '%s'"), filename);
if (append_eol && write(msg_fd, "\n", 1) < 0)
- return error_errno(_("Could not write eol to %s"), filename);
+ return error_errno(_("Could not write eol to '%s"), filename);
if (commit_lock_file(&msg_file) < 0)
- return error(_("Error wrapping up %s."), filename);
+ return error(_("Error wrapping up '%s'."), filename);

return 0;
}
@@ -309,10 +312,10 @@ static struct tree *empty_tree(void)
static int error_dirty_index(struct replay_opts *opts) {
if (read_cache_unmerged())
- return error_resolve_conflict(action_name(opts));
+ return error_resolve_conflict(_(action_name(opts)));

error(_("Your local changes would be overwritten by %s."),
- action_name(opts));
+ _(action_name(opts)));

if (advice_commit_before_merge)
advise(_("Commit your changes or stash them to proceed."));
@@ -330,7 +333,7 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from,
if (checkout_fast_forward(from, to, 1))
return -1; /* the callee should have complained already */

- strbuf_addf(&sb, _("%s: fast-forward"), action_name(opts));
+ strbuf_addf(&sb, _("%s: fast-forward"), _(action_name(opts)));

transaction = ref_transaction_begin(&err);
if (!transaction ||
@@ -406,7 +409,7 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
/* TRANSLATORS: %s will be "revert" or "cherry-pick" */ return error(_("%s: Unable to write new index file"),
- action_name(opts));
+ _(action_name(opts)));
rollback_lock_file(&index_lock);

if (opts->signoff)
@@ -488,9 +491,13 @@ static char **read_author_script(void)
* If we are cherry-pick, and if the merge did not result in
* hand-editing, we will hit this commit and inherit the original
* author date and name.
+ *
* If we are revert, or if our cherry-pick results in a hand merge,
- * we had better say that the current user is responsible for that
- * (except, of course, while running an interactive rebase).
+ * we had better say that the current user is responsible for that.
+ *
+ * An exception is when sequencer_commit() is called during an
+ * interactive rebase: in that case, we will want to retain the
+ * author metadata.
*/
int sequencer_commit(const char *defmsg, struct replay_opts *opts,
int allow_empty, int edit, int amend,
@@ -501,7 +508,7 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
int rc;
const char *value;

- if (IS_REBASE_I()) {
+ if (is_rebase_i(opts)) {
env = read_author_script();
if (!env) {
const char *gpg_opt = gpg_sign_opt_quoted(opts);
@@ -513,7 +520,7 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
"If they are meant to go into a new commit, " "run:\n\n"
" git commit %s\n\n"
- "In both case, once you're done, continue "
+ "In both cases, once you're done, continue "
"with:\n\n"
" git rebase --continue\n", gpg_opt, gpg_opt);
}
@@ -613,7 +620,7 @@ static int allow_empty(struct replay_opts *opts, struct commit *commit)
}

enum todo_command {
- TODO_PICK,
+ TODO_PICK = 0,
TODO_REVERT
};

@@ -690,8 +697,6 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
return fast_forward_to(commit->object.oid.hash, head, unborn, opts);

if (parent && parse_commit(parent) < 0)
- /* TRANSLATORS: The first %s will be "revert" or
- "cherry-pick", the second %s a SHA1 */
return error(_("%s: cannot parse parent commit %s"),
command_to_string(command), oid_to_hex(&parent->object.oid));
@@ -833,14 +838,14 @@ static int read_and_refresh_cache(struct replay_opts *opts)
if (read_index_preload(&the_index, NULL) < 0) {
rollback_lock_file(&index_lock);
return error(_("git %s: failed to read the index"),
- action_name(opts));
+ _(action_name(opts)));
}
refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL); if (the_index.cache_changed && index_fd >= 0) {
if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) {
rollback_lock_file(&index_lock);
return error(_("git %s: failed to refresh the index"),
- action_name(opts));
+ _(action_name(opts)));
}
}
rollback_lock_file(&index_lock);
@@ -861,7 +866,7 @@ struct todo_list {
int nr, alloc, current;
};

-#define TODO_LIST_INIT { STRBUF_INIT, NULL, 0, 0, 0 } +#define TODO_LIST_INIT { STRBUF_INIT }

static void todo_list_release(struct todo_list *todo_list)
{
@@ -871,7 +876,7 @@ static void todo_list_release(struct todo_list *todo_list)
todo_list->nr = todo_list->alloc = 0;
}

-struct todo_item *append_todo(struct todo_list *todo_list)
+struct todo_item *append_new_todo(struct todo_list *todo_list)
{
ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc); return todo_list->items + todo_list->nr++;
@@ -925,11 +930,11 @@ static int parse_insn_buffer(char *buf, struct todo_list *todo_list)
for (i = 1; *p; i++) {
char *eol = strchrnul(p, '\n');

- item = append_todo(todo_list);
+ item = append_new_todo(todo_list);
item->offset_in_buf = p - todo_list->buf.buf;
if (parse_insn_line(item, p, eol)) {
- error("Invalid line: %.*s", (int)(eol - p), p);
- res |= error(_("Could not parse line %d."), i);
+ res |= error(_("Invalid line %d: %.*s"),
+ i, (int)(eol - p), p);
item->command = -1;
}
p = *eol ? eol + 1 : eol;
@@ -948,16 +953,31 @@ static int read_populate_todo(struct todo_list *todo_list,
strbuf_reset(&todo_list->buf);
fd = open(todo_file, O_RDONLY);
if (fd < 0)
- return error_errno(_("Could not open %s"), todo_file);
+ return error_errno(_("Could not open '%s'"), todo_file);
if (strbuf_read(&todo_list->buf, fd, 0) < 0) {
close(fd);
- return error(_("Could not read %s."), todo_file);
+ return error(_("Could not read '%s'."), todo_file);
}
close(fd);

res = parse_insn_buffer(todo_list->buf.buf, todo_list); if (res)
- return error(_("Unusable instruction sheet: %s"), todo_file);
+ return error(_("Unusable instruction sheet: '%s'"), todo_file);
+
+ if (!is_rebase_i(opts)) {
+ enum todo_command valid =
+ opts->action == REPLAY_PICK ? TODO_PICK : TODO_REVERT;
+ int i;
+
+ for (i = 0; i < todo_list->nr; i++)
+ if (valid == todo_list->items[i].command)
+ continue;
+ else if (valid == TODO_PICK)
+ return error(_("Cannot cherry-pick during a revert."));
+ else
+ return error(_("Cannot revert during a cherry-pick."));
+ }
+
return 0;
}

@@ -1003,22 +1023,16 @@ static int populate_opts_cb(const char *key, const char *value, void *data)

static int read_populate_opts(struct replay_opts *opts) {
- if (IS_REBASE_I()) {
+ if (is_rebase_i(opts)) {
struct strbuf buf = STRBUF_INIT;

if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), 1)) {
- if (buf.len && buf.buf[buf.len - 1] == '\n') {
- if (--buf.len &&
- buf.buf[buf.len - 1] == '\r')
- buf.len--;
- buf.buf[buf.len] = '\0';
- }
-
if (!starts_with(buf.buf, "-S"))
strbuf_reset(&buf);
else {
opts->gpg_sign = buf.buf + 2;
- strbuf_detach(&buf, NULL);
+ sequencer_entrust(opts,
+ strbuf_detach(&buf, NULL));
}
}

@@ -1034,7 +1048,7 @@ static int read_populate_opts(struct replay_opts *opts)
* are pretty certain that it is syntactically correct.
*/
if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0)
- return error(_("Malformed options sheet: %s"),
+ return error(_("Malformed options sheet: '%s'"),
git_path_opts_file());
return 0;
}
@@ -1044,13 +1058,14 @@ static int walk_revs_populate_todo(struct todo_list *todo_list,
{
enum todo_command command = opts->action == REPLAY_PICK ?
TODO_PICK : TODO_REVERT;
+ const char *command_string = todo_command_strings[command];
struct commit *commit;

if (prepare_revs(opts))
return -1;

while ((commit = get_revision(opts->revs))) {
- struct todo_item *item = append_todo(todo_list);
+ struct todo_item *item = append_new_todo(todo_list);
const char *commit_buffer = get_commit_buffer(commit, NULL); const char *subject;
int subject_len;
@@ -1061,8 +1076,7 @@ static int walk_revs_populate_todo(struct todo_list *todo_list,
item->arg_len = 0;
item->offset_in_buf = todo_list->buf.len;
subject_len = find_commit_subject(commit_buffer, &subject);
- strbuf_addf(&todo_list->buf, "%s %s %.*s\n",
- opts->action == REPLAY_PICK ? "pick" : "revert",
+ strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string,
short_commit_name(commit), subject_len, subject);
unuse_commit_buffer(commit, commit_buffer);
}
@@ -1077,7 +1091,7 @@ static int create_seq_dir(void)
return -1;
}
else if (mkdir(git_path_seq_dir(), 0777) < 0)
- return error_errno(_("Could not create sequencer directory %s"),
+ return error_errno(_("Could not create sequencer directory '%s'"),
git_path_seq_dir());
return 0;
}
@@ -1096,12 +1110,12 @@ static int save_head(const char *head)
strbuf_addf(&buf, "%s\n", head);
if (write_in_full(fd, buf.buf, buf.len) < 0) {
rollback_lock_file(&head_lock);
- return error_errno(_("Could not write to %s"),
+ return error_errno(_("Could not write to '%s'"),
git_path_head_file());
}
if (commit_lock_file(&head_lock) < 0) {
rollback_lock_file(&head_lock);
- return error(_("Error wrapping up %s."), git_path_head_file());
+ return error(_("Error wrapping up '%s'."), git_path_head_file());
}
return 0;
}
@@ -1136,9 +1150,6 @@ int sequencer_rollback(struct replay_opts *opts)
unsigned char sha1[20];
struct strbuf buf = STRBUF_INIT;

- if (read_and_refresh_cache(opts))
- return -1;
-
f = fopen(git_path_head_file(), "r");
if (!f && errno == ENOENT) {
/*
@@ -1149,9 +1160,9 @@ int sequencer_rollback(struct replay_opts *opts)
return rollback_single_pick();
}
if (!f)
- return error_errno(_("cannot open %s"), git_path_head_file());
+ return error_errno(_("cannot open '%s'"), git_path_head_file());
if (strbuf_getline_lf(&buf, f)) {
- error(_("cannot read %s: %s"), git_path_head_file(),
+ error(_("cannot read '%s': %s"), git_path_head_file(),
ferror(f) ? strerror(errno) : _("unexpected end of file"));
fclose(f);
goto fail;
@@ -1183,16 +1194,14 @@ static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)

fd = hold_lock_file_for_update(&todo_lock, todo_path, 0); if (fd < 0)
- return error_errno(_("Could not lock '%s'"),
- git_path_todo_file());
+ return error_errno(_("Could not lock '%s'"), todo_path);
offset = next < todo_list->nr ?
todo_list->items[next].offset_in_buf : todo_list->buf.len;
if (write_in_full(fd, todo_list->buf.buf + offset,
todo_list->buf.len - offset) < 0)
- return error(_("Could not write to %s (%s)"),
- todo_path, strerror(errno));
+ return error_errno(_("Could not write to '%s'"), todo_path);
if (commit_lock_file(&todo_lock) < 0)
- return error(_("Error wrapping up %s."), todo_path);
+ return error(_("Error wrapping up '%s'."), todo_path);
return 0;
}

@@ -1279,21 +1288,25 @@ int sequencer_continue(struct replay_opts *opts)

if (!file_exists(get_todo_path(opts)))
return continue_single_pick();
- if (read_populate_opts(opts) ||
- read_populate_todo(&todo_list, opts))
+ if (read_populate_opts(opts))
return -1;
+ if ((res = read_populate_todo(&todo_list, opts)))
+ goto release_todo_list;

/* Verify that the conflict has been resolved */
if (file_exists(git_path_cherry_pick_head()) ||
file_exists(git_path_revert_head())) {
- int ret = continue_single_pick();
- if (ret)
- return ret;
+ res = continue_single_pick();
+ if (res)
+ goto release_todo_list;
+ }
+ if (index_differs_from("HEAD", 0)) {
+ res = error_dirty_index(opts);
+ goto release_todo_list;
}
- if (index_differs_from("HEAD", 0))
- return error_dirty_index(opts);
todo_list.current++;
res = pick_commits(&todo_list, opts);
+release_todo_list:
todo_list_release(&todo_list);
return res;
}
diff --git a/sequencer.h b/sequencer.h
index e272549..688fff1 100644
--- a/sequencer.h
+++ b/sequencer.h
@@ -40,9 +40,13 @@ struct replay_opts {
void **owned;
int owned_nr, owned_alloc;
};
-#define REPLAY_OPTS_INIT { -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL, NULL, NULL, 0, 0, NULL, NULL, 0, 0 } +#define REPLAY_OPTS_INIT { -1 }

-void *sequencer_entrust(struct replay_opts *opts, void *set_me_free_after_use); +/*
+ * Make it the duty of sequencer_remove_state() to release the memory;
+ * For ease of use, return the same pointer.
+ */
+void *sequencer_entrust(struct replay_opts *opts, void *to_free);

int sequencer_pick_revisions(struct replay_opts *opts);
int sequencer_continue(struct replay_opts *opts);
diff --git a/t/t3510-cherry-pick-sequence.sh b/t/t3510-cherry-pick-sequence.sh index 6465edf..7b7a89d 100755
--- a/t/t3510-cherry-pick-sequence.sh
+++ b/t/t3510-cherry-pick-sequence.sh
@@ -459,6 +459,17 @@ test_expect_success 'malformed instruction sheet 1' '
test_expect_code 128 git cherry-pick --continue
'

+test_expect_success 'malformed instruction sheet 2' '
+ pristine_detach initial &&
+ test_expect_code 1 git cherry-pick base..anotherpick &&
+ echo "resolved" >foo &&
+ git add foo &&
+ git commit &&
+ sed "s/pick/revert/" .git/sequencer/todo >new_sheet &&
+ cp new_sheet .git/sequencer/todo &&
+ test_expect_code 128 git cherry-pick --continue
+'
+
test_expect_success 'empty commit set' '
pristine_detach initial && test_expect_code 128 git cherry-pick base..base

Signature · 3 lines
2.10.0.windows.1.10.g803177d

base-commit: 8a04d01a511ecffba8a397d26ee7fa6ca56b6e7e
#136 lore raw

PATCH v2 2/25 sequencer: use memoized sequencer directory path

---
 builtin/commit.c |  2 +-
 sequencer.c      | 11 ++++++-----
 sequencer.h      |  5 +----
 3 files changed, 8 insertions(+), 10 deletions(-)
builtin/commit.c +1 -1
index bb9f79b..e79af9d 100644
@@ -183,7 +183,7 @@ static void determine_whence(struct wt_status *s)
 		whence = FROM_MERGE;
 	else if (file_exists(git_path_cherry_pick_head())) {
 		whence = FROM_CHERRY_PICK;
-		if (file_exists(git_path(SEQ_DIR)))
+		if (file_exists(git_path_seq_dir()))
 			sequencer_in_use = 1;
 	}
 	else
sequencer.c +6 -5
index eec8a60..cb16cbd 100644
@@ -21,10 +21,11 @@
 const char sign_off_header[] = "Signed-off-by: ";
 static const char cherry_picked_prefix[] = "(cherry picked from commit ";
 
-static GIT_PATH_FUNC(git_path_todo_file, SEQ_TODO_FILE)
-static GIT_PATH_FUNC(git_path_opts_file, SEQ_OPTS_FILE)
-static GIT_PATH_FUNC(git_path_seq_dir, SEQ_DIR)
-static GIT_PATH_FUNC(git_path_head_file, SEQ_HEAD_FILE)
+GIT_PATH_FUNC(git_path_seq_dir, "sequencer")
+
+static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo")
+static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts")
+static GIT_PATH_FUNC(git_path_head_file, "sequencer/head")
 
 static int is_rfc2822_line(const char *buf, int len)
 {
@@ -112,7 +113,7 @@ static void remove_sequencer_state(void)
 {
 	struct strbuf seq_dir = STRBUF_INIT;
 
-	strbuf_addstr(&seq_dir, git_path(SEQ_DIR));
+	strbuf_addstr(&seq_dir, git_path_seq_dir());
 	remove_dir_recursively(&seq_dir, 0);
 	strbuf_release(&seq_dir);
 }
sequencer.h +1 -4
index db425ad..dd4d33a 100644
@@ -1,10 +1,7 @@
 #ifndef SEQUENCER_H
 #define SEQUENCER_H
 
-#define SEQ_DIR		"sequencer"
-#define SEQ_HEAD_FILE	"sequencer/head"
-#define SEQ_TODO_FILE	"sequencer/todo"
-#define SEQ_OPTS_FILE	"sequencer/opts"
+const char *git_path_seq_dir(void);
 
 #define APPEND_SIGNOFF_DEDUP (1u << 0)
 
Signature · 1 line
2.10.0.windows.1.10.g803177d
#138 lore raw

PATCH v2 3/25 sequencer: avoid unnecessary indirection

In reply to Johannes Schindelin

We really do not need the *pointer to a* pointer to the options in the read_populate_opts() function.

---
 sequencer.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
sequencer.c +3 -3
index cb16cbd..c2fbf6f 100644
@@ -813,7 +813,7 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
 	return 0;
 }
 
-static int read_populate_opts(struct replay_opts **opts)
+static int read_populate_opts(struct replay_opts *opts)
 {
 	if (!file_exists(git_path_opts_file()))
 		return 0;
@@ -823,7 +823,7 @@ static int read_populate_opts(struct replay_opts **opts)
 	 * about this case, though, because we wrote that file ourselves, so we
 	 * are pretty certain that it is syntactically correct.
 	 */
-	if (git_config_from_file(populate_opts_cb, git_path_opts_file(), *opts) < 0)
+	if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0)
 		return error(_("Malformed options sheet: %s"),
 			git_path_opts_file());
 	return 0;
@@ -1054,7 +1054,7 @@ static int sequencer_continue(struct replay_opts *opts)
 
 	if (!file_exists(git_path_todo_file()))
 		return continue_single_pick();
-	if (read_populate_opts(&opts) ||
+	if (read_populate_opts(opts) ||
 			read_populate_todo(&todo_list, opts))
 		return -1;
 
Signature · 1 line
2.10.0.windows.1.10.g803177d
#144 lore raw

PATCH v2 5/25 sequencer: allow the sequencer to take custody of malloc()ed data

In reply to Johannes Schindelin

The sequencer is our attempt to lib-ify cherry-pick. Yet it behaves like a one-shot command when it reads its configuration: memory is allocated and released only when the command exits.

This is kind of okay for git-cherry-pick, which *is* a one-shot command. All the work to make the sequencer its work horse was done to allow using the functionality as a library function, though, including proper clean-up after use.

This patch introduces an API to pass the responsibility of releasing certain memory to the sequencer. Example:

	const char *label =
		sequencer_entrust(opts, xstrfmt("From: %s", email));

The entrusted memory will remain valid until sequencer_remove_state() is called, or the program exits, whichever comes first.

---
 sequencer.c | 13 +++++++++++++
 sequencer.h | 10 ++++++++++
 2 files changed, 23 insertions(+)
sequencer.c +13 -0
index 8d272fb..8d56a05 100644
@@ -114,9 +114,22 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
 	return 1;
 }
 
+void *sequencer_entrust(struct replay_opts *opts, void *to_free)
+{
+	ALLOC_GROW(opts->owned, opts->owned_nr + 1, opts->owned_alloc);
+	opts->owned[opts->owned_nr++] = to_free;
+
+	return to_free;
+}
+
 static void remove_sequencer_state(const struct replay_opts *opts)
 {
 	struct strbuf dir = STRBUF_INIT;
+	int i;
+
+	for (i = 0; i < opts->owned_nr; i++)
+		free(opts->owned[i]);
+	free(opts->owned);
 
 	strbuf_addf(&dir, "%s", get_dir(opts));
 	remove_dir_recursively(&dir, 0);
sequencer.h +10 -0
index dd4d33a..04892a9 100644
@@ -43,9 +43,19 @@ struct replay_opts {
 
 	/* Only used by REPLAY_NONE */
 	struct rev_info *revs;
+
+	/* malloc()ed data entrusted to the sequencer */
+	void **owned;
+	int owned_nr, owned_alloc;
 };
 #define REPLAY_OPTS_INIT { -1, -1 }
 
+/*
+ * Make it the duty of sequencer_remove_state() to release the memory;
+ * For ease of use, return the same pointer.
+ */
+void *sequencer_entrust(struct replay_opts *opts, void *to_free);
+
 int sequencer_pick_revisions(struct replay_opts *opts);
 
 extern const char sign_off_header[];
Signature · 1 line
2.10.0.windows.1.10.g803177d
#140 lore raw

PATCH v2 4/25 sequencer: future-proof remove_sequencer_state()

In reply to Johannes Schindelin

In a couple of commits, we will teach the sequencer to handle the nitty gritty of the interactive rebase, which keeps its state in a different directory.

---
 sequencer.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)
sequencer.c +13 -8
index c2fbf6f..8d272fb 100644
@@ -27,6 +27,11 @@ static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo")
 static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts")
 static GIT_PATH_FUNC(git_path_head_file, "sequencer/head")
 
+static const char *get_dir(const struct replay_opts *opts)
+{
+	return git_path_seq_dir();
+}
+
 static int is_rfc2822_line(const char *buf, int len)
 {
 	int i;
@@ -109,13 +114,13 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
 	return 1;
 }
 
-static void remove_sequencer_state(void)
+static void remove_sequencer_state(const struct replay_opts *opts)
 {
-	struct strbuf seq_dir = STRBUF_INIT;
+	struct strbuf dir = STRBUF_INIT;
 
-	strbuf_addstr(&seq_dir, git_path_seq_dir());
-	remove_dir_recursively(&seq_dir, 0);
-	strbuf_release(&seq_dir);
+	strbuf_addf(&dir, "%s", get_dir(opts));
+	remove_dir_recursively(&dir, 0);
+	strbuf_release(&dir);
 }
 
 static const char *action_name(const struct replay_opts *opts)
@@ -940,7 +945,7 @@ static int sequencer_rollback(struct replay_opts *opts)
 	}
 	if (reset_for_rollback(sha1))
 		goto fail;
-	remove_sequencer_state();
+	remove_sequencer_state(opts);
 	strbuf_release(&buf);
 	return 0;
 fail:
@@ -1034,7 +1039,7 @@ static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
 	 * Sequence of picks finished successfully; cleanup by
 	 * removing the .git/sequencer directory
 	 */
-	remove_sequencer_state();
+	remove_sequencer_state(opts);
 	return 0;
 }
 
@@ -1095,7 +1100,7 @@ int sequencer_pick_revisions(struct replay_opts *opts)
 	 * one that is being continued
 	 */
 	if (opts->subcommand == REPLAY_REMOVE_STATE) {
-		remove_sequencer_state();
+		remove_sequencer_state(opts);
 		return 0;
 	}
 	if (opts->subcommand == REPLAY_ROLLBACK)
Signature · 1 line
2.10.0.windows.1.10.g803177d
#151 lore raw

PATCH v2 6/25 sequencer: release memory that was allocated when reading options

In reply to Johannes Schindelin

The sequencer reads options from disk and stores them in its struct for use during sequencer's operations.

With this patch, the memory is released afterwards, plugging a memory leak.

---
 sequencer.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
sequencer.c +10 -3
index 8d56a05..3ca231f 100644
@@ -131,6 +131,8 @@ static void remove_sequencer_state(const struct replay_opts *opts)
 		free(opts->owned[i]);
 	free(opts->owned);
 
+	free(opts->xopts);
+
 	strbuf_addf(&dir, "%s", get_dir(opts));
 	remove_dir_recursively(&dir, 0);
 	strbuf_release(&dir);
@@ -815,13 +817,18 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
 		opts->allow_ff = git_config_bool_or_int(key, value, &error_flag);
 	else if (!strcmp(key, "options.mainline"))
 		opts->mainline = git_config_int(key, value);
-	else if (!strcmp(key, "options.strategy"))
+	else if (!strcmp(key, "options.strategy")) {
 		git_config_string(&opts->strategy, key, value);
-	else if (!strcmp(key, "options.gpg-sign"))
+		sequencer_entrust(opts, (char *) opts->strategy);
+	}
+	else if (!strcmp(key, "options.gpg-sign")) {
 		git_config_string(&opts->gpg_sign, key, value);
+		sequencer_entrust(opts, (char *) opts->gpg_sign);
+	}
 	else if (!strcmp(key, "options.strategy-option")) {
 		ALLOC_GROW(opts->xopts, opts->xopts_nr + 1, opts->xopts_alloc);
-		opts->xopts[opts->xopts_nr++] = xstrdup(value);
+		opts->xopts[opts->xopts_nr++] =
+			sequencer_entrust(opts, xstrdup(value));
 	} else
 		return error(_("Invalid key: %s"), key);
 
Signature · 1 line
2.10.0.windows.1.10.g803177d
#152 lore raw

PATCH v2 7/25 sequencer: future-proof read_populate_todo()

In reply to Johannes Schindelin

Over the next commits, we will work on improving the sequencer to the point where it can process the edit script of an interactive rebase. To that end, we will need to teach the sequencer to read interactive rebase's todo file. In preparation, we consolidate all places where that todo file is needed to call a function that we will later extend.

---
 sequencer.c | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)
sequencer.c +11 -7
index 3ca231f..da6de22 100644
@@ -32,6 +32,11 @@ static const char *get_dir(const struct replay_opts *opts)
 	return git_path_seq_dir();
 }
 
+static const char *get_todo_path(const struct replay_opts *opts)
+{
+	return git_path_todo_file();
+}
+
 static int is_rfc2822_line(const char *buf, int len)
 {
 	int i;
@@ -776,25 +781,24 @@ static int parse_insn_buffer(char *buf, struct commit_list **todo_list,
 static int read_populate_todo(struct commit_list **todo_list,
 			struct replay_opts *opts)
 {
+	const char *todo_file = get_todo_path(opts);
 	struct strbuf buf = STRBUF_INIT;
 	int fd, res;
 
-	fd = open(git_path_todo_file(), O_RDONLY);
+	fd = open(todo_file, O_RDONLY);
 	if (fd < 0)
-		return error_errno(_("Could not open %s"),
-				   git_path_todo_file());
+		return error_errno(_("Could not open %s"), todo_file);
 	if (strbuf_read(&buf, fd, 0) < 0) {
 		close(fd);
 		strbuf_release(&buf);
-		return error(_("Could not read %s."), git_path_todo_file());
+		return error(_("Could not read %s."), todo_file);
 	}
 	close(fd);
 
 	res = parse_insn_buffer(buf.buf, todo_list, opts);
 	strbuf_release(&buf);
 	if (res)
-		return error(_("Unusable instruction sheet: %s"),
-			git_path_todo_file());
+		return error(_("Unusable instruction sheet: %s"), todo_file);
 	return 0;
 }
 
@@ -1077,7 +1081,7 @@ static int sequencer_continue(struct replay_opts *opts)
 {
 	struct commit_list *todo_list = NULL;
 
-	if (!file_exists(git_path_todo_file()))
+	if (!file_exists(get_todo_path(opts)))
 		return continue_single_pick();
 	if (read_populate_opts(opts) ||
 			read_populate_todo(&todo_list, opts))
Signature · 1 line
2.10.0.windows.1.10.g803177d
#153 lore raw

PATCH v2 8/25 sequencer: completely revamp the "todo" script parsing

In reply to Johannes Schindelin

When we came up with the "sequencer" idea, we really wanted to have kind of a plumbing equivalent of the interactive rebase. Hence the choice of words: the "todo" script, a "pick", etc.

However, when it came time to implement the entire shebang, somehow this idea got lost and the sequencer was used as working horse for cherry-pick and revert instead. So as not to interfere with the interactive rebase, it even uses a separate directory to store its state.

Furthermore, it also is stupidly strict about the "todo" script it
accepts: while it parses commands in a way that was *designed* to be similar to the interactive rebase, it then goes on to *error out* if the commands disagree with the overall action (cherry-pick or revert).

Finally, the sequencer code chose to deviate from the interactive rebase code insofar that it *reformats* the "todo" script instead of just writing the part of the parsed script that were not yet processed. This is not only unnecessary churn, but might well lose information that is valuable to the user (i.e. comments after the commands).

Let's just bite the bullet and rewrite the entire parser; the code now becomes not only more elegant: it allows us to go on and teach the sequencer how to parse *true* "todo" scripts as used by the interactive rebase itself. In a way, the sequencer is about to grow up to do its older brother's job. Better.

In particular, we choose to maintain the list of commands in an array instead of a linked list: this is flexible enough to allow us later on to even implement rebase -i's reordering of fixup!/squash! commits very easily (and with a very nice speed bonus, at least on Windows).

While at it, do not stop at the first problem, but list *all* of the problems. This will help the user when the sequencer will do rebase -i's work by allowing to address all issues in one go rather than going back and forth until the todo list is valid.

---
 sequencer.c | 275 +++++++++++++++++++++++++++++++++++-------------------------
 1 file changed, 159 insertions(+), 116 deletions(-)
sequencer.c +159 -116
index da6de22..b971ad0 100644
@@ -473,7 +473,26 @@ static int allow_empty(struct replay_opts *opts, struct commit *commit)
 		return 1;
 }
 
-static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
+enum todo_command {
+	TODO_PICK = 0,
+	TODO_REVERT
+};
+
+static const char *todo_command_strings[] = {
+	"pick",
+	"revert"
+};
+
+static const char *command_to_string(const enum todo_command command)
+{
+	if (command < ARRAY_SIZE(todo_command_strings))
+		return todo_command_strings[command];
+	die("Unknown command: %d", command);
+}
+
+
+static int do_pick_commit(enum todo_command command, struct commit *commit,
+		struct replay_opts *opts)
 {
 	unsigned char head[20];
 	struct commit *base, *next, *parent;
@@ -535,7 +554,8 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
 		/* TRANSLATORS: The first %s will be "revert" or
 		   "cherry-pick", the second %s a SHA1 */
 		return error(_("%s: cannot parse parent commit %s"),
-			action_name(opts), oid_to_hex(&parent->object.oid));
+			command_to_string(command),
+			oid_to_hex(&parent->object.oid));
 
 	if (get_message(commit, &msg) != 0)
 		return error(_("Cannot get commit message for %s"),
@@ -548,7 +568,7 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
 	 * reverse of it if we are revert.
 	 */
 
-	if (opts->action == REPLAY_REVERT) {
+	if (command == TODO_REVERT) {
 		base = commit;
 		base_label = msg.label;
 		next = parent;
@@ -589,7 +609,7 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
 		}
 	}
 
-	if (!opts->strategy || !strcmp(opts->strategy, "recursive") || opts->action == REPLAY_REVERT) {
+	if (!opts->strategy || !strcmp(opts->strategy, "recursive") || command == TODO_REVERT) {
 		res = do_recursive_merge(base, next, base_label, next_label,
 					 head, &msgbuf, opts);
 		if (res < 0)
@@ -615,17 +635,17 @@ static int do_pick_commit(struct commit *commit, struct replay_opts *opts)
 	 * However, if the merge did not even start, then we don't want to
 	 * write it at all.
 	 */
-	if (opts->action == REPLAY_PICK && !opts->no_commit && (res == 0 || res == 1) &&
+	if (command == TODO_PICK && !opts->no_commit && (res == 0 || res == 1) &&
 	    update_ref(NULL, "CHERRY_PICK_HEAD", commit->object.oid.hash, NULL,
 		       REF_NODEREF, UPDATE_REFS_MSG_ON_ERR))
 		res = -1;
-	if (opts->action == REPLAY_REVERT && ((opts->no_commit && res == 0) || res == 1) &&
+	if (command == TODO_REVERT && ((opts->no_commit && res == 0) || res == 1) &&
 	    update_ref(NULL, "REVERT_HEAD", commit->object.oid.hash, NULL,
 		       REF_NODEREF, UPDATE_REFS_MSG_ON_ERR))
 		res = -1;
 
 	if (res) {
-		error(opts->action == REPLAY_REVERT
+		error(command == TODO_REVERT
 		      ? _("could not revert %s... %s")
 		      : _("could not apply %s... %s"),
 		      find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV),
@@ -687,116 +707,121 @@ static int read_and_refresh_cache(struct replay_opts *opts)
 	return 0;
 }
 
-static int format_todo(struct strbuf *buf, struct commit_list *todo_list,
-		struct replay_opts *opts)
+struct todo_item {
+	enum todo_command command;
+	struct commit *commit;
+	size_t offset_in_buf;
+};
+
+struct todo_list {
+	struct strbuf buf;
+	struct todo_item *items;
+	int nr, alloc, current;
+};
+
+#define TODO_LIST_INIT { STRBUF_INIT }
+
+static void todo_list_release(struct todo_list *todo_list)
 {
-	struct commit_list *cur = NULL;
-	const char *sha1_abbrev = NULL;
-	const char *action_str = opts->action == REPLAY_REVERT ? "revert" : "pick";
-	const char *subject;
-	int subject_len;
+	strbuf_release(&todo_list->buf);
+	free(todo_list->items);
+	todo_list->items = NULL;
+	todo_list->nr = todo_list->alloc = 0;
+}
 
-	for (cur = todo_list; cur; cur = cur->next) {
-		const char *commit_buffer = get_commit_buffer(cur->item, NULL);
-		sha1_abbrev = find_unique_abbrev(cur->item->object.oid.hash, DEFAULT_ABBREV);
-		subject_len = find_commit_subject(commit_buffer, &subject);
-		strbuf_addf(buf, "%s %s %.*s\n", action_str, sha1_abbrev,
-			subject_len, subject);
-		unuse_commit_buffer(cur->item, commit_buffer);
-	}
-	return 0;
+struct todo_item *append_new_todo(struct todo_list *todo_list)
+{
+	ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc);
+	return todo_list->items + todo_list->nr++;
 }
 
-static struct commit *parse_insn_line(char *bol, char *eol, struct replay_opts *opts)
+static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)
 {
 	unsigned char commit_sha1[20];
-	enum replay_action action;
 	char *end_of_object_name;
-	int saved, status, padding;
-
-	if (starts_with(bol, "pick")) {
-		action = REPLAY_PICK;
-		bol += strlen("pick");
-	} else if (starts_with(bol, "revert")) {
-		action = REPLAY_REVERT;
-		bol += strlen("revert");
-	} else
-		return NULL;
+	int i, saved, status, padding;
+
+	for (i = 0; i < ARRAY_SIZE(todo_command_strings); i++)
+		if (skip_prefix(bol, todo_command_strings[i], &bol)) {
+			item->command = i;
+			break;
+		}
+	if (i >= ARRAY_SIZE(todo_command_strings))
+		return -1;
 
 	/* Eat up extra spaces/ tabs before object name */
 	padding = strspn(bol, " \t");
 	if (!padding)
-		return NULL;
+		return -1;
 	bol += padding;
 
-	end_of_object_name = bol + strcspn(bol, " \t\n");
+	end_of_object_name = (char *) bol + strcspn(bol, " \t\n");
 	saved = *end_of_object_name;
 	*end_of_object_name = '\0';
 	status = get_sha1(bol, commit_sha1);
 	*end_of_object_name = saved;
 
-	/*
-	 * Verify that the action matches up with the one in
-	 * opts; we don't support arbitrary instructions
-	 */
-	if (action != opts->action) {
-		if (action == REPLAY_REVERT)
-		      error((opts->action == REPLAY_REVERT)
-			    ? _("Cannot revert during another revert.")
-			    : _("Cannot revert during a cherry-pick."));
-		else
-		      error((opts->action == REPLAY_REVERT)
-			    ? _("Cannot cherry-pick during a revert.")
-			    : _("Cannot cherry-pick during another cherry-pick."));
-		return NULL;
-	}
-
 	if (status < 0)
-		return NULL;
+		return -1;
 
-	return lookup_commit_reference(commit_sha1);
+	item->commit = lookup_commit_reference(commit_sha1);
+	return !item->commit;
 }
 
-static int parse_insn_buffer(char *buf, struct commit_list **todo_list,
-			struct replay_opts *opts)
+static int parse_insn_buffer(char *buf, struct todo_list *todo_list)
 {
-	struct commit_list **next = todo_list;
-	struct commit *commit;
+	struct todo_item *item;
 	char *p = buf;
-	int i;
+	int i, res = 0;
 
 	for (i = 1; *p; i++) {
 		char *eol = strchrnul(p, '\n');
-		commit = parse_insn_line(p, eol, opts);
-		if (!commit)
-			return error(_("Could not parse line %d."), i);
-		next = commit_list_append(commit, next);
+
+		item = append_new_todo(todo_list);
+		item->offset_in_buf = p - todo_list->buf.buf;
+		if (parse_insn_line(item, p, eol)) {
+			res |= error(_("Invalid line %d: %.*s"),
+				i, (int)(eol - p), p);
+			item->command = -1;
+		}
 		p = *eol ? eol + 1 : eol;
 	}
-	if (!*todo_list)
+	if (!todo_list->nr)
 		return error(_("No commits parsed."));
-	return 0;
+	return res;
 }
 
-static int read_populate_todo(struct commit_list **todo_list,
+static int read_populate_todo(struct todo_list *todo_list,
 			struct replay_opts *opts)
 {
 	const char *todo_file = get_todo_path(opts);
-	struct strbuf buf = STRBUF_INIT;
 	int fd, res;
 
+	strbuf_reset(&todo_list->buf);
 	fd = open(todo_file, O_RDONLY);
 	if (fd < 0)
 		return error_errno(_("Could not open %s"), todo_file);
-	if (strbuf_read(&buf, fd, 0) < 0) {
+	if (strbuf_read(&todo_list->buf, fd, 0) < 0) {
 		close(fd);
-		strbuf_release(&buf);
 		return error(_("Could not read %s."), todo_file);
 	}
 	close(fd);
 
-	res = parse_insn_buffer(buf.buf, todo_list, opts);
-	strbuf_release(&buf);
+	res = parse_insn_buffer(todo_list->buf.buf, todo_list);
+	if (!res) {
+		enum todo_command valid =
+			opts->action == REPLAY_PICK ? TODO_PICK : TODO_REVERT;
+		int i;
+
+		for (i = 0; i < todo_list->nr; i++)
+			if (valid == todo_list->items[i].command)
+				continue;
+			else if (valid == TODO_PICK)
+				return error(_("Cannot cherry-pick during a revert."));
+			else
+				return error(_("Cannot revert during a cherry-pick."));
+	}
+
 	if (res)
 		return error(_("Unusable instruction sheet: %s"), todo_file);
 	return 0;
@@ -858,18 +883,33 @@ static int read_populate_opts(struct replay_opts *opts)
 	return 0;
 }
 
-static int walk_revs_populate_todo(struct commit_list **todo_list,
+static int walk_revs_populate_todo(struct todo_list *todo_list,
 				struct replay_opts *opts)
 {
+	enum todo_command command = opts->action == REPLAY_PICK ?
+		TODO_PICK : TODO_REVERT;
+	const char *command_string = todo_command_strings[command];
 	struct commit *commit;
-	struct commit_list **next;
 
 	if (prepare_revs(opts))
 		return -1;
 
-	next = todo_list;
-	while ((commit = get_revision(opts->revs)))
-		next = commit_list_append(commit, next);
+	while ((commit = get_revision(opts->revs))) {
+		struct todo_item *item = append_new_todo(todo_list);
+		const char *commit_buffer = get_commit_buffer(commit, NULL);
+		const char *subject;
+		int subject_len;
+
+		item->command = command;
+		item->commit = commit;
+		item->offset_in_buf = todo_list->buf.len;
+		subject_len = find_commit_subject(commit_buffer, &subject);
+		strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string,
+			find_unique_abbrev(commit->object.oid.hash,
+				DEFAULT_ABBREV),
+			subject_len, subject);
+		unuse_commit_buffer(commit, commit_buffer);
+	}
 	return 0;
 }
 
@@ -977,30 +1017,22 @@ static int sequencer_rollback(struct replay_opts *opts)
 	return -1;
 }
 
-static int save_todo(struct commit_list *todo_list, struct replay_opts *opts)
+static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
 {
 	static struct lock_file todo_lock;
-	struct strbuf buf = STRBUF_INIT;
-	int fd;
+	const char *todo_path = get_todo_path(opts);
+	int next = todo_list->current, offset, fd;
 
-	fd = hold_lock_file_for_update(&todo_lock, git_path_todo_file(), 0);
+	fd = hold_lock_file_for_update(&todo_lock, todo_path, 0);
 	if (fd < 0)
-		return error_errno(_("Could not lock '%s'"),
-				   git_path_todo_file());
-	if (format_todo(&buf, todo_list, opts) < 0) {
-		strbuf_release(&buf);
-		return error(_("Could not format %s."), git_path_todo_file());
-	}
-	if (write_in_full(fd, buf.buf, buf.len) < 0) {
-		strbuf_release(&buf);
-		return error_errno(_("Could not write to %s"),
-				   git_path_todo_file());
-	}
-	if (commit_lock_file(&todo_lock) < 0) {
-		strbuf_release(&buf);
-		return error(_("Error wrapping up %s."), git_path_todo_file());
-	}
-	strbuf_release(&buf);
+		return error_errno(_("Could not lock '%s'"), todo_path);
+	offset = next < todo_list->nr ?
+		todo_list->items[next].offset_in_buf : todo_list->buf.len;
+	if (write_in_full(fd, todo_list->buf.buf + offset,
+			todo_list->buf.len - offset) < 0)
+		return error_errno(_("Could not write to '%s'"), todo_path);
+	if (commit_lock_file(&todo_lock) < 0)
+		return error(_("Error wrapping up %s."), todo_path);
 	return 0;
 }
 
@@ -1039,9 +1071,8 @@ static int save_opts(struct replay_opts *opts)
 	return res;
 }
 
-static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
+static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
 {
-	struct commit_list *cur;
 	int res;
 
 	setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
@@ -1051,10 +1082,12 @@ static int pick_commits(struct commit_list *todo_list, struct replay_opts *opts)
 	if (read_and_refresh_cache(opts))
 		return -1;
 
-	for (cur = todo_list; cur; cur = cur->next) {
-		if (save_todo(cur, opts))
+	while (todo_list->current < todo_list->nr) {
+		struct todo_item *item = todo_list->items + todo_list->current;
+		if (save_todo(todo_list, opts))
 			return -1;
-		res = do_pick_commit(cur->item, opts);
+		res = do_pick_commit(item->command, item->commit, opts);
+		todo_list->current++;
 		if (res)
 			return res;
 	}
@@ -1079,38 +1112,46 @@ static int continue_single_pick(void)
 
 static int sequencer_continue(struct replay_opts *opts)
 {
-	struct commit_list *todo_list = NULL;
+	struct todo_list todo_list = TODO_LIST_INIT;
+	int res;
 
 	if (!file_exists(get_todo_path(opts)))
 		return continue_single_pick();
-	if (read_populate_opts(opts) ||
-			read_populate_todo(&todo_list, opts))
+	if (read_populate_opts(opts))
 		return -1;
+	if ((res = read_populate_todo(&todo_list, opts)))
+		goto release_todo_list;
 
 	/* Verify that the conflict has been resolved */
 	if (file_exists(git_path_cherry_pick_head()) ||
 	    file_exists(git_path_revert_head())) {
-		int ret = continue_single_pick();
-		if (ret)
-			return ret;
+		res = continue_single_pick();
+		if (res)
+			goto release_todo_list;
 	}
-	if (index_differs_from("HEAD", 0))
-		return error_dirty_index(opts);
-	todo_list = todo_list->next;
-	return pick_commits(todo_list, opts);
+	if (index_differs_from("HEAD", 0)) {
+		res = error_dirty_index(opts);
+		goto release_todo_list;
+	}
+	todo_list.current++;
+	res = pick_commits(&todo_list, opts);
+release_todo_list:
+	todo_list_release(&todo_list);
+	return res;
 }
 
 static int single_pick(struct commit *cmit, struct replay_opts *opts)
 {
 	setenv(GIT_REFLOG_ACTION, action_name(opts), 0);
-	return do_pick_commit(cmit, opts);
+	return do_pick_commit(opts->action == REPLAY_PICK ?
+		TODO_PICK : TODO_REVERT, cmit, opts);
 }
 
 int sequencer_pick_revisions(struct replay_opts *opts)
 {
-	struct commit_list *todo_list = NULL;
+	struct todo_list todo_list = TODO_LIST_INIT;
 	unsigned char sha1[20];
-	int i;
+	int i, res;
 
 	if (opts->subcommand == REPLAY_NONE)
 		assert(opts->revs);
@@ -1185,7 +1226,9 @@ int sequencer_pick_revisions(struct replay_opts *opts)
 		return -1;
 	if (save_opts(opts))
 		return -1;
-	return pick_commits(todo_list, opts);
+	res = pick_commits(&todo_list, opts);
+	todo_list_release(&todo_list);
+	return res;
 }
 
 void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag)
Signature · 1 line
2.10.0.windows.1.10.g803177d
#154 lore raw

PATCH v2 9/25 sequencer: avoid completely different messages for different actions

In reply to Johannes Schindelin
---
 sequencer.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
sequencer.c +2 -5
index b971ad0..7ba932b 100644
@@ -232,11 +232,8 @@ static int error_dirty_index(struct replay_opts *opts)
 	if (read_cache_unmerged())
 		return error_resolve_conflict(action_name(opts));
 
-	/* Different translation strings for cherry-pick and revert */
-	if (opts->action == REPLAY_PICK)
-		error(_("Your local changes would be overwritten by cherry-pick."));
-	else
-		error(_("Your local changes would be overwritten by revert."));
+	error(_("Your local changes would be overwritten by %s."),
+		action_name(opts));
 
 	if (advice_commit_before_merge)
 		advise(_("Commit your changes or stash them to proceed."));
Signature · 1 line
2.10.0.windows.1.10.g803177d
#155 lore raw

PATCH v2 10/25 sequencer: get rid of the subcommand field

In reply to Johannes Schindelin

The subcommands are used exactly once, at the very beginning of sequencer_pick_revisions(), to determine what to do. This is an unnecessary level of indirection: we can simply call the correct function to begin with. So let's do that.

While at it, ensure that the subcommands return an error code so that they do not have to die() all over the place (bad practice for library functions...).

---
 builtin/revert.c | 36 ++++++++++++++++--------------------
 sequencer.c      | 35 +++++++++++------------------------
 sequencer.h      | 13 ++++---------
 3 files changed, 31 insertions(+), 53 deletions(-)
builtin/revert.c +16 -20
index 7365559..c9ae4dc 100644
@@ -71,7 +71,7 @@ static void verify_opt_compatible(const char *me, const char *base_opt, ...)
 		die(_("%s: %s cannot be used with %s"), me, this_opt, base_opt);
 }
 
-static void parse_args(int argc, const char **argv, struct replay_opts *opts)
+static int run_sequencer(int argc, const char **argv, struct replay_opts *opts)
 {
 	const char * const * usage_str = revert_or_cherry_pick_usage(opts);
 	const char *me = action_name(opts);
@@ -115,25 +115,15 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
 	if (opts->keep_redundant_commits)
 		opts->allow_empty = 1;
 
-	/* Set the subcommand */
-	if (cmd == 'q')
-		opts->subcommand = REPLAY_REMOVE_STATE;
-	else if (cmd == 'c')
-		opts->subcommand = REPLAY_CONTINUE;
-	else if (cmd == 'a')
-		opts->subcommand = REPLAY_ROLLBACK;
-	else
-		opts->subcommand = REPLAY_NONE;
-
 	/* Check for incompatible command line arguments */
-	if (opts->subcommand != REPLAY_NONE) {
+	if (cmd) {
 		char *this_operation;
-		if (opts->subcommand == REPLAY_REMOVE_STATE)
+		if (cmd == 'q')
 			this_operation = "--quit";
-		else if (opts->subcommand == REPLAY_CONTINUE)
+		else if (cmd == 'c')
 			this_operation = "--continue";
 		else {
-			assert(opts->subcommand == REPLAY_ROLLBACK);
+			assert(cmd == 'a');
 			this_operation = "--abort";
 		}
 
@@ -156,7 +146,7 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
 				"--edit", opts->edit,
 				NULL);
 
-	if (opts->subcommand != REPLAY_NONE) {
+	if (cmd) {
 		opts->revs = NULL;
 	} else {
 		struct setup_revision_opt s_r_opt;
@@ -174,6 +164,14 @@ static void parse_args(int argc, const char **argv, struct replay_opts *opts)
 
 	if (argc > 1)
 		usage_with_options(usage_str, options);
+
+	if (cmd == 'q')
+		return sequencer_remove_state(opts);
+	if (cmd == 'c')
+		return sequencer_continue(opts);
+	if (cmd == 'a')
+		return sequencer_rollback(opts);
+	return sequencer_pick_revisions(opts);
 }
 
 int cmd_revert(int argc, const char **argv, const char *prefix)
@@ -185,8 +183,7 @@ int cmd_revert(int argc, const char **argv, const char *prefix)
 		opts.edit = 1;
 	opts.action = REPLAY_REVERT;
 	git_config(git_default_config, NULL);
-	parse_args(argc, argv, &opts);
-	res = sequencer_pick_revisions(&opts);
+	res = run_sequencer(argc, argv, &opts);
 	if (res < 0)
 		die(_("revert failed"));
 	return res;
@@ -199,8 +196,7 @@ int cmd_cherry_pick(int argc, const char **argv, const char *prefix)
 
 	opts.action = REPLAY_PICK;
 	git_config(git_default_config, NULL);
-	parse_args(argc, argv, &opts);
-	res = sequencer_pick_revisions(&opts);
+	res = run_sequencer(argc, argv, &opts);
 	if (res < 0)
 		die(_("cherry-pick failed"));
 	return res;
sequencer.c +11 -24
index 7ba932b..053e78c 100644
@@ -127,7 +127,7 @@ void *sequencer_entrust(struct replay_opts *opts, void *to_free)
 	return to_free;
 }
 
-static void remove_sequencer_state(const struct replay_opts *opts)
+int sequencer_remove_state(struct replay_opts *opts)
 {
 	struct strbuf dir = STRBUF_INIT;
 	int i;
@@ -141,6 +141,8 @@ static void remove_sequencer_state(const struct replay_opts *opts)
 	strbuf_addf(&dir, "%s", get_dir(opts));
 	remove_dir_recursively(&dir, 0);
 	strbuf_release(&dir);
+
+	return 0;
 }
 
 static const char *action_name(const struct replay_opts *opts)
@@ -971,7 +973,7 @@ static int rollback_single_pick(void)
 	return reset_for_rollback(head_sha1);
 }
 
-static int sequencer_rollback(struct replay_opts *opts)
+int sequencer_rollback(struct replay_opts *opts)
 {
 	FILE *f;
 	unsigned char sha1[20];
@@ -1006,9 +1008,8 @@ static int sequencer_rollback(struct replay_opts *opts)
 	}
 	if (reset_for_rollback(sha1))
 		goto fail;
-	remove_sequencer_state(opts);
 	strbuf_release(&buf);
-	return 0;
+	return sequencer_remove_state(opts);
 fail:
 	strbuf_release(&buf);
 	return -1;
@@ -1093,8 +1094,7 @@ static int pick_commits(struct todo_list *todo_list, struct replay_opts *opts)
 	 * Sequence of picks finished successfully; cleanup by
 	 * removing the .git/sequencer directory
 	 */
-	remove_sequencer_state(opts);
-	return 0;
+	return sequencer_remove_state(opts);
 }
 
 static int continue_single_pick(void)
@@ -1107,11 +1107,14 @@ static int continue_single_pick(void)
 	return run_command_v_opt(argv, RUN_GIT_CMD);
 }
 
-static int sequencer_continue(struct replay_opts *opts)
+int sequencer_continue(struct replay_opts *opts)
 {
 	struct todo_list todo_list = TODO_LIST_INIT;
 	int res;
 
+	if (read_and_refresh_cache(opts))
+		return -1;
+
 	if (!file_exists(get_todo_path(opts)))
 		return continue_single_pick();
 	if (read_populate_opts(opts))
@@ -1150,26 +1153,10 @@ int sequencer_pick_revisions(struct replay_opts *opts)
 	unsigned char sha1[20];
 	int i, res;
 
-	if (opts->subcommand == REPLAY_NONE)
-		assert(opts->revs);
-
+	assert(opts->revs);
 	if (read_and_refresh_cache(opts))
 		return -1;
 
-	/*
-	 * Decide what to do depending on the arguments; a fresh
-	 * cherry-pick should be handled differently from an existing
-	 * one that is being continued
-	 */
-	if (opts->subcommand == REPLAY_REMOVE_STATE) {
-		remove_sequencer_state(opts);
-		return 0;
-	}
-	if (opts->subcommand == REPLAY_ROLLBACK)
-		return sequencer_rollback(opts);
-	if (opts->subcommand == REPLAY_CONTINUE)
-		return sequencer_continue(opts);
-
 	for (i = 0; i < opts->revs->pending.nr; i++) {
 		unsigned char sha1[20];
 		const char *name = opts->revs->pending.objects[i].name;
sequencer.h +4 -9
index 04892a9..0b3950d 100644
@@ -10,16 +10,8 @@ enum replay_action {
 	REPLAY_PICK
 };
 
-enum replay_subcommand {
-	REPLAY_NONE,
-	REPLAY_REMOVE_STATE,
-	REPLAY_CONTINUE,
-	REPLAY_ROLLBACK
-};
-
 struct replay_opts {
 	enum replay_action action;
-	enum replay_subcommand subcommand;
 
 	/* Boolean options */
 	int edit;
@@ -48,7 +40,7 @@ struct replay_opts {
 	void **owned;
 	int owned_nr, owned_alloc;
 };
-#define REPLAY_OPTS_INIT { -1, -1 }
+#define REPLAY_OPTS_INIT { -1 }
 
 /*
  * Make it the duty of sequencer_remove_state() to release the memory;
@@ -57,6 +49,9 @@ struct replay_opts {
 void *sequencer_entrust(struct replay_opts *opts, void *to_free);
 
 int sequencer_pick_revisions(struct replay_opts *opts);
+int sequencer_continue(struct replay_opts *opts);
+int sequencer_rollback(struct replay_opts *opts);
+int sequencer_remove_state(struct replay_opts *opts);
 
 extern const char sign_off_header[];
 
Signature · 1 line
2.10.0.windows.1.10.g803177d
#157 lore raw

PATCH v2 11/25 sequencer: refactor the code to obtain a short commit name

In reply to Johannes Schindelin

Not only does this DRY up the code (providing a better documentation what the code is about, as well as allowing to change the behavior in a single place), it also makes it substantially shorter to use the same functionality in functions to be introduced when we teach the sequencer to process interactive-rebase's git-rebase-todo file.

---
 sequencer.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
sequencer.c +8 -6
index 053e78c..0c8dec4 100644
@@ -157,13 +157,18 @@ struct commit_message {
 	const char *message;
 };
 
+static const char *short_commit_name(struct commit *commit)
+{
+	return find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV);
+}
+
 static int get_message(struct commit *commit, struct commit_message *out)
 {
 	const char *abbrev, *subject;
 	int subject_len;
 
 	out->message = logmsg_reencode(commit, NULL, get_commit_output_encoding());
-	abbrev = find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV);
+	abbrev = short_commit_name(commit);
 
 	subject_len = find_commit_subject(out->message, &subject);
 
@@ -647,8 +652,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 		error(command == TODO_REVERT
 		      ? _("could not revert %s... %s")
 		      : _("could not apply %s... %s"),
-		      find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV),
-		      msg.subject);
+		      short_commit_name(commit), msg.subject);
 		print_advice(res == 1, opts);
 		rerere(opts->allow_rerere_auto);
 		goto leave;
@@ -904,9 +908,7 @@ static int walk_revs_populate_todo(struct todo_list *todo_list,
 		item->offset_in_buf = todo_list->buf.len;
 		subject_len = find_commit_subject(commit_buffer, &subject);
 		strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string,
-			find_unique_abbrev(commit->object.oid.hash,
-				DEFAULT_ABBREV),
-			subject_len, subject);
+			short_commit_name(commit), subject_len, subject);
 		unuse_commit_buffer(commit, commit_buffer);
 	}
 	return 0;
Signature · 1 line
2.10.0.windows.1.10.g803177d
#158 lore raw

PATCH v2 12/25 sequencer: remember the onelines when parsing the todo file

In reply to Johannes Schindelin

The git-rebase-todo file contains a list of commands. Most of those commands have the form

	<verb> <sha1> <oneline>

The <oneline> is displayed primarily for the user's convenience, as rebase -i really interprets only the <verb> <sha1> part. However, there are *some* places in interactive rebase where the <oneline> is used to display messages, e.g. for reporting at which commit we stopped.

So let's just remember it when parsing the todo file; we keep a copy of the entire todo file anyway (to write out the new done and git-rebase-todo file just before processing each command), so all we need to do is remember the begin offsets and lengths.

As we will have to parse and remember the command-line for exec commands later, we do not call the field "oneline" but rather "arg" (and will reuse that for exec's command-line).

---
 sequencer.c | 7 +++++++
 1 file changed, 7 insertions(+)
sequencer.c +7 -0
index 0c8dec4..ca1961c 100644
@@ -713,6 +713,8 @@ static int read_and_refresh_cache(struct replay_opts *opts)
 struct todo_item {
 	enum todo_command command;
 	struct commit *commit;
+	const char *arg;
+	int arg_len;
 	size_t offset_in_buf;
 };
 
@@ -764,6 +766,9 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)
 	status = get_sha1(bol, commit_sha1);
 	*end_of_object_name = saved;
 
+	item->arg = end_of_object_name + strspn(end_of_object_name, " \t");
+	item->arg_len = (int)(eol - item->arg);
+
 	if (status < 0)
 		return -1;
 
@@ -905,6 +910,8 @@ static int walk_revs_populate_todo(struct todo_list *todo_list,
 
 		item->command = command;
 		item->commit = commit;
+		item->arg = NULL;
+		item->arg_len = 0;
 		item->offset_in_buf = todo_list->buf.len;
 		subject_len = find_commit_subject(commit_buffer, &subject);
 		strbuf_addf(&todo_list->buf, "%s %s %.*s\n", command_string,
Signature · 1 line
2.10.0.windows.1.10.g803177d
#159 lore raw

PATCH v2 13/25 sequencer: prepare for rebase -i's commit functionality

In reply to Johannes Schindelin

In interactive rebases, we commit a little bit differently than the sequencer did so far: we heed the "author-script", the "message" and the "amend" files in the .git/rebase-merge/ subdirectory.

Likewise, we may want to edit the commit message *even* when providing a file containing the suggested commit message. Therefore we change the code to not even provide a default message when we do not want any, and to call the editor explicitly.

As interactive rebase's GPG settings are configured differently from how cherry-pick (and therefore sequencer) handles them, we will leave support for that to the next commit.

---
 sequencer.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 sequencer.h |  3 ++
 2 files changed, 89 insertions(+), 11 deletions(-)
sequencer.c +86 -11
index ca1961c..6c35fe8 100644
@@ -27,6 +27,19 @@ static GIT_PATH_FUNC(git_path_todo_file, "sequencer/todo")
 static GIT_PATH_FUNC(git_path_opts_file, "sequencer/opts")
 static GIT_PATH_FUNC(git_path_head_file, "sequencer/head")
 
+/*
+ * A script to set the GIT_AUTHOR_NAME, GIT_AUTHOR_EMAIL, and
+ * GIT_AUTHOR_DATE that will be used for the commit that is currently
+ * being rebased.
+ */
+static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script")
+
+/* We will introduce the 'interactive rebase' mode later */
+static inline int is_rebase_i(const struct replay_opts *opts)
+{
+	return 0;
+}
+
 static const char *get_dir(const struct replay_opts *opts)
 {
 	return git_path_seq_dir();
@@ -377,20 +390,76 @@ static int is_index_unchanged(void)
 	return !hashcmp(active_cache_tree->sha1, head_commit->tree->object.oid.hash);
 }
 
+static char **read_author_script(void)
+{
+	struct strbuf script = STRBUF_INIT;
+	int i, count = 0;
+	char *p, *p2, **env;
+	size_t env_size;
+
+	if (strbuf_read_file(&script, rebase_path_author_script(), 256) <= 0)
+		return NULL;
+
+	for (p = script.buf; *p; p++)
+		if (skip_prefix(p, "'\\\\''", (const char **)&p2))
+			strbuf_splice(&script, p - script.buf, p2 - p, "'", 1);
+		else if (*p == '\'')
+			strbuf_splice(&script, p-- - script.buf, 1, "", 0);
+		else if (*p == '\n') {
+			*p = '\0';
+			count++;
+		}
+
+	env_size = (count + 1) * sizeof(*env);
+	strbuf_grow(&script, env_size);
+	memmove(script.buf + env_size, script.buf, script.len);
+	p = script.buf + env_size;
+	env = (char **)strbuf_detach(&script, NULL);
+
+	for (i = 0; i < count; i++) {
+		env[i] = p;
+		p += strlen(p) + 1;
+	}
+	env[count] = NULL;
+
+	return env;
+}
+
 /*
  * If we are cherry-pick, and if the merge did not result in
  * hand-editing, we will hit this commit and inherit the original
  * author date and name.
+ *
  * If we are revert, or if our cherry-pick results in a hand merge,
  * we had better say that the current user is responsible for that.
+ *
+ * An exception is when sequencer_commit() is called during an
+ * interactive rebase: in that case, we will want to retain the
+ * author metadata.
  */
-static int run_git_commit(const char *defmsg, struct replay_opts *opts,
+int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 			  int allow_empty)
 {
+	char **env = NULL;
 	struct argv_array array;
 	int rc;
 	const char *value;
 
+	if (is_rebase_i(opts)) {
+		env = read_author_script();
+		if (!env)
+			return error("You have staged changes in your working "
+				"tree. If these changes are meant to be\n"
+				"squashed into the previous commit, run:\n\n"
+				"  git commit --amend $gpg_sign_opt_quoted\n\n"
+				"If they are meant to go into a new commit, "
+				"run:\n\n"
+				"  git commit $gpg_sign_opt_quoted\n\n"
+				"In both cases, once you're done, continue "
+				"with:\n\n"
+				"  git rebase --continue\n");
+	}
+
 	argv_array_init(&array);
 	argv_array_push(&array, "commit");
 	argv_array_push(&array, "-n");
@@ -399,14 +468,13 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
 		argv_array_pushf(&array, "-S%s", opts->gpg_sign);
 	if (opts->signoff)
 		argv_array_push(&array, "-s");
-	if (!opts->edit) {
-		argv_array_push(&array, "-F");
-		argv_array_push(&array, defmsg);
-		if (!opts->signoff &&
-		    !opts->record_origin &&
-		    git_config_get_value("commit.cleanup", &value))
-			argv_array_push(&array, "--cleanup=verbatim");
-	}
+	if (defmsg)
+		argv_array_pushl(&array, "-F", defmsg, NULL);
+	if (opts->edit)
+		argv_array_push(&array, "-e");
+	else if (!opts->signoff && !opts->record_origin &&
+		 git_config_get_value("commit.cleanup", &value))
+		argv_array_push(&array, "--cleanup=verbatim");
 
 	if (allow_empty)
 		argv_array_push(&array, "--allow-empty");
@@ -414,8 +482,11 @@ static int run_git_commit(const char *defmsg, struct replay_opts *opts,
 	if (opts->allow_empty_message)
 		argv_array_push(&array, "--allow-empty-message");
 
-	rc = run_command_v_opt(array.argv, RUN_GIT_CMD);
+	rc = run_command_v_opt_cd_env(array.argv, RUN_GIT_CMD, NULL,
+			(const char *const *)env);
 	argv_array_clear(&array);
+	free(env);
+
 	return rc;
 }
 
@@ -664,7 +735,8 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 		goto leave;
 	}
 	if (!opts->no_commit)
-		res = run_git_commit(git_path_merge_msg(), opts, allow);
+		res = sequencer_commit(opts->edit ? NULL : git_path_merge_msg(),
+			opts, allow);
 
 leave:
 	free_message(commit, &msg);
@@ -877,6 +949,9 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
 
 static int read_populate_opts(struct replay_opts *opts)
 {
+	if (is_rebase_i(opts))
+		return 0;
+
 	if (!file_exists(git_path_opts_file()))
 		return 0;
 	/*
sequencer.h +3 -0
index 0b3950d..16deb6c 100644
@@ -53,6 +53,9 @@ int sequencer_continue(struct replay_opts *opts);
 int sequencer_rollback(struct replay_opts *opts);
 int sequencer_remove_state(struct replay_opts *opts);
 
+int sequencer_commit(const char *defmsg, struct replay_opts *opts,
+			  int allow_empty);
+
 extern const char sign_off_header[];
 
 void append_signoff(struct strbuf *msgbuf, int ignore_footer, unsigned flag);
Signature · 1 line
2.10.0.windows.1.10.g803177d
#160 lore raw

PATCH v2 14/25 sequencer: introduce a helper to read files written by scripts

In reply to Johannes Schindelin

As we are slowly teaching the sequencer to perform the hard work for the interactive rebase, we need to read files that were written by shell scripts.

These files typically contain a single line and are invariably ended by a line feed (and possibly a carriage return before that). Let's use a helper to read such files and to remove the line ending.

---
 sequencer.c | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)
sequencer.c +31 -0
index 6c35fe8..086cd0b 100644
@@ -242,6 +242,37 @@ static int write_message(struct strbuf *msgbuf, const char *filename)
 	return 0;
 }
 
+/*
+ * Reads a file that was presumably written by a shell script, i.e.
+ * with an end-of-line marker that needs to be stripped.
+ *
+ * Returns 1 if the file was read, 0 if it could not be read or does not exist.
+ */
+static int read_oneliner(struct strbuf *buf,
+	const char *path, int skip_if_empty)
+{
+	int orig_len = buf->len;
+
+	if (!file_exists(path))
+		return 0;
+
+	if (strbuf_read_file(buf, path, 0) < 0) {
+		warning_errno("could not read '%s'", path);
+		return 0;
+	}
+
+	if (buf->len > orig_len && buf->buf[buf->len - 1] == '\n') {
+		if (--buf->len > orig_len && buf->buf[buf->len - 1] == '\r')
+			--buf->len;
+		buf->buf[buf->len] = '\0';
+	}
+
+	if (skip_if_empty && buf->len == orig_len)
+		return 0;
+
+	return 1;
+}
+
 static struct tree *empty_tree(void)
 {
 	return lookup_tree(EMPTY_TREE_SHA1_BIN);
Signature · 1 line
2.10.0.windows.1.10.g803177d
#161 lore raw

PATCH v2 15/25 sequencer: prepare for rebase -i's GPG settings

In reply to Johannes Schindelin

The rebase command sports a --gpg-sign option that is heeded by the interactive rebase.

This patch teaches the sequencer that trick, as part of the bigger effort to make the sequencer the work horse of the interactive rebase.

---
 sequencer.c | 42 +++++++++++++++++++++++++++++++++++++-----
 1 file changed, 37 insertions(+), 5 deletions(-)
sequencer.c +37 -5
index 086cd0b..bf02565 100644
@@ -15,6 +15,7 @@
 #include "merge-recursive.h"
 #include "refs.h"
 #include "argv-array.h"
+#include "quote.h"
 
 #define GIT_REFLOG_ACTION "GIT_REFLOG_ACTION"
 
@@ -33,6 +34,11 @@ static GIT_PATH_FUNC(git_path_head_file, "sequencer/head")
  * being rebased.
  */
 static GIT_PATH_FUNC(rebase_path_author_script, "rebase-merge/author-script")
+/*
+ * The following files are written by git-rebase just after parsing the
+ * command-line (and are only consumed, not modified, by the sequencer).
+ */
+static GIT_PATH_FUNC(rebase_path_gpg_sign_opt, "rebase-merge/gpg_sign_opt")
 
 /* We will introduce the 'interactive rebase' mode later */
 static inline int is_rebase_i(const struct replay_opts *opts)
@@ -132,6 +138,16 @@ static int has_conforming_footer(struct strbuf *sb, struct strbuf *sob,
 	return 1;
 }
 
+static const char *gpg_sign_opt_quoted(struct replay_opts *opts)
+{
+	static struct strbuf buf = STRBUF_INIT;
+
+	strbuf_reset(&buf);
+	if (opts->gpg_sign)
+		sq_quotef(&buf, "-S%s", opts->gpg_sign);
+	return buf.buf;
+}
+
 void *sequencer_entrust(struct replay_opts *opts, void *to_free)
 {
 	ALLOC_GROW(opts->owned, opts->owned_nr + 1, opts->owned_alloc);
@@ -478,17 +494,20 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 
 	if (is_rebase_i(opts)) {
 		env = read_author_script();
-		if (!env)
+		if (!env) {
+			const char *gpg_opt = gpg_sign_opt_quoted(opts);
+
 			return error("You have staged changes in your working "
 				"tree. If these changes are meant to be\n"
 				"squashed into the previous commit, run:\n\n"
-				"  git commit --amend $gpg_sign_opt_quoted\n\n"
+				"  git commit --amend %s\n\n"
 				"If they are meant to go into a new commit, "
 				"run:\n\n"
-				"  git commit $gpg_sign_opt_quoted\n\n"
+				"  git commit %s\n\n"
 				"In both cases, once you're done, continue "
 				"with:\n\n"
-				"  git rebase --continue\n");
+				"  git rebase --continue\n", gpg_opt, gpg_opt);
+		}
 	}
 
 	argv_array_init(&array);
@@ -980,8 +999,21 @@ static int populate_opts_cb(const char *key, const char *value, void *data)
 
 static int read_populate_opts(struct replay_opts *opts)
 {
-	if (is_rebase_i(opts))
+	if (is_rebase_i(opts)) {
+		struct strbuf buf = STRBUF_INIT;
+
+		if (read_oneliner(&buf, rebase_path_gpg_sign_opt(), 1)) {
+			if (!starts_with(buf.buf, "-S"))
+				strbuf_reset(&buf);
+			else {
+				opts->gpg_sign = buf.buf + 2;
+				sequencer_entrust(opts,
+					strbuf_detach(&buf, NULL));
+			}
+		}
+
 		return 0;
+	}
 
 	if (!file_exists(git_path_opts_file()))
 		return 0;
Signature · 1 line
2.10.0.windows.1.10.g803177d
#162 lore raw

PATCH v2 16/25 sequencer: allow editing the commit message on a case-by-case basis

In reply to Johannes Schindelin

In the upcoming commits, we will implement more and more of rebase -i's functionality. One particular feature of the commands to come is that some of them allow editing the commit message while others don't, i.e. we cannot define in the replay_opts whether the commit message should be edited or not.

Let's add a new parameter to the sequencer_commit() function. Previously, it was the duty of the caller to ensure that the opts->edit setting indicates whether to let the user edit the commit message or not, indicating that it is an "all or nothing" setting, i.e. that the sequencer wants to let the user edit *all* commit message, or none at all. In the upcoming rebase -i mode, it will depend on the particular command that is currently executed, though.

---
 sequencer.c | 6 +++---
 sequencer.h | 2 +-
 2 files changed, 4 insertions(+), 4 deletions(-)
sequencer.c +3 -3
index bf02565..6e9732c 100644
@@ -485,7 +485,7 @@ static char **read_author_script(void)
  * author metadata.
  */
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty)
+			  int allow_empty, int edit)
 {
 	char **env = NULL;
 	struct argv_array array;
@@ -520,7 +520,7 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 		argv_array_push(&array, "-s");
 	if (defmsg)
 		argv_array_pushl(&array, "-F", defmsg, NULL);
-	if (opts->edit)
+	if (edit)
 		argv_array_push(&array, "-e");
 	else if (!opts->signoff && !opts->record_origin &&
 		 git_config_get_value("commit.cleanup", &value))
@@ -786,7 +786,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	}
 	if (!opts->no_commit)
 		res = sequencer_commit(opts->edit ? NULL : git_path_merge_msg(),
-			opts, allow);
+			opts, allow, opts->edit);
 
 leave:
 	free_message(commit, &msg);
sequencer.h +1 -1
index 16deb6c..7f5222f 100644
@@ -54,7 +54,7 @@ int sequencer_rollback(struct replay_opts *opts);
 int sequencer_remove_state(struct replay_opts *opts);
 
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty);
+			  int allow_empty, int edit);
 
 extern const char sign_off_header[];
 
Signature · 1 line
2.10.0.windows.1.10.g803177d
#163 lore raw

PATCH v2 17/25 sequencer: support amending commits

In reply to Johannes Schindelin

This teaches the sequencer_commit() function to take an argument that will allow us to implement "todo" commands that need to amend the commit messages ("fixup", "squash" and "reword").

---
 sequencer.c | 6 ++++--
 sequencer.h | 2 +-
 2 files changed, 5 insertions(+), 3 deletions(-)
sequencer.c +4 -2
index 6e9732c..60b522e 100644
@@ -485,7 +485,7 @@ static char **read_author_script(void)
  * author metadata.
  */
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty, int edit)
+			  int allow_empty, int edit, int amend)
 {
 	char **env = NULL;
 	struct argv_array array;
@@ -514,6 +514,8 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 	argv_array_push(&array, "commit");
 	argv_array_push(&array, "-n");
 
+	if (amend)
+		argv_array_push(&array, "--amend");
 	if (opts->gpg_sign)
 		argv_array_pushf(&array, "-S%s", opts->gpg_sign);
 	if (opts->signoff)
@@ -786,7 +788,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	}
 	if (!opts->no_commit)
 		res = sequencer_commit(opts->edit ? NULL : git_path_merge_msg(),
-			opts, allow, opts->edit);
+			opts, allow, opts->edit, 0);
 
 leave:
 	free_message(commit, &msg);
sequencer.h +1 -1
index 7f5222f..c45f5c4 100644
@@ -54,7 +54,7 @@ int sequencer_rollback(struct replay_opts *opts);
 int sequencer_remove_state(struct replay_opts *opts);
 
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty, int edit);
+			  int allow_empty, int edit, int amend);
 
 extern const char sign_off_header[];
 
Signature · 1 line
2.10.0.windows.1.10.g803177d
#166 lore raw

PATCH v2 18/25 sequencer: support cleaning up commit messages

In reply to Johannes Schindelin

The sequencer_commit() function already knows how to amend commits, and with this new option, it can also clean up commit messages (i.e. strip out commented lines). This is needed to implement rebase -i's 'fixup' and 'squash' commands as sequencer commands.

---
 sequencer.c | 10 +++++++---
 sequencer.h |  3 ++-
 2 files changed, 9 insertions(+), 4 deletions(-)
sequencer.c +7 -3
index 60b522e..75772b8 100644
@@ -485,7 +485,8 @@ static char **read_author_script(void)
  * author metadata.
  */
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty, int edit, int amend)
+			  int allow_empty, int edit, int amend,
+			  int cleanup_commit_message)
 {
 	char **env = NULL;
 	struct argv_array array;
@@ -522,9 +523,12 @@ int sequencer_commit(const char *defmsg, struct replay_opts *opts,
 		argv_array_push(&array, "-s");
 	if (defmsg)
 		argv_array_pushl(&array, "-F", defmsg, NULL);
+	if (cleanup_commit_message)
+		argv_array_push(&array, "--cleanup=strip");
 	if (edit)
 		argv_array_push(&array, "-e");
-	else if (!opts->signoff && !opts->record_origin &&
+	else if (!cleanup_commit_message &&
+		 !opts->signoff && !opts->record_origin &&
 		 git_config_get_value("commit.cleanup", &value))
 		argv_array_push(&array, "--cleanup=verbatim");
 
@@ -788,7 +792,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	}
 	if (!opts->no_commit)
 		res = sequencer_commit(opts->edit ? NULL : git_path_merge_msg(),
-			opts, allow, opts->edit, 0);
+			opts, allow, opts->edit, 0, 0);
 
 leave:
 	free_message(commit, &msg);
sequencer.h +2 -1
index c45f5c4..688fff1 100644
@@ -54,7 +54,8 @@ int sequencer_rollback(struct replay_opts *opts);
 int sequencer_remove_state(struct replay_opts *opts);
 
 int sequencer_commit(const char *defmsg, struct replay_opts *opts,
-			  int allow_empty, int edit, int amend);
+			  int allow_empty, int edit, int amend,
+			  int cleanup_commit_message);
 
 extern const char sign_off_header[];
 
Signature · 1 line
2.10.0.windows.1.10.g803177d
#168 lore raw

PATCH v2 19/25 sequencer: remember do_recursive_merge()'s return value

In reply to Johannes Schindelin

The return value of do_recursive_merge() may be positive (indicating merge conflicts), so let's OR later error conditions so as not to overwrite them with 0.

This is not yet a problem, but preparing for the patches to come: we will teach the sequencer to do rebase -i's job.

---
 sequencer.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
sequencer.c +6 -5
index 75772b8..7953a05 100644
@@ -630,7 +630,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	const char *base_label, *next_label;
 	struct commit_message msg = { NULL, NULL, NULL, NULL };
 	struct strbuf msgbuf = STRBUF_INIT;
-	int res, unborn = 0, allow;
+	int res = 0, unborn = 0, allow;
 
 	if (opts->no_commit) {
 		/*
@@ -741,7 +741,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 	}
 
 	if (!opts->strategy || !strcmp(opts->strategy, "recursive") || command == TODO_REVERT) {
-		res = do_recursive_merge(base, next, base_label, next_label,
+		res |= do_recursive_merge(base, next, base_label, next_label,
 					 head, &msgbuf, opts);
 		if (res < 0)
 			return res;
@@ -750,7 +750,7 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 		struct commit_list *common = NULL;
 		struct commit_list *remotes = NULL;
 
-		res = write_message(&msgbuf, git_path_merge_msg());
+		res |= write_message(&msgbuf, git_path_merge_msg());
 
 		commit_list_insert(base, &common);
 		commit_list_insert(next, &remotes);
@@ -787,11 +787,12 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 
 	allow = allow_empty(opts, commit);
 	if (allow < 0) {
-		res = allow;
+		res |= allow;
 		goto leave;
 	}
 	if (!opts->no_commit)
-		res = sequencer_commit(opts->edit ? NULL : git_path_merge_msg(),
+		res |= sequencer_commit(opts->edit ?
+				NULL : git_path_merge_msg(),
 			opts, allow, opts->edit, 0, 0);
 
 leave:
Signature · 1 line
2.10.0.windows.1.10.g803177d
#172 lore raw

PATCH v2 20/25 sequencer: left-trim lines read from the script

In reply to Johannes Schindelin

Interactive rebase's scripts may be indented; we need to handle this case, too, now that we prepare the sequencer to process interactive rebases.

---
 sequencer.c | 3 +++
 1 file changed, 3 insertions(+)
sequencer.c +3 -0
index 7953a05..5e5d113 100644
@@ -875,6 +875,9 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)
 	char *end_of_object_name;
 	int i, saved, status, padding;
 
+	/* left-trim */
+	bol += strspn(bol, " \t");
+
 	for (i = 0; i < ARRAY_SIZE(todo_command_strings); i++)
 		if (skip_prefix(bol, todo_command_strings[i], &bol)) {
 			item->command = i;
Signature · 1 line
2.10.0.windows.1.10.g803177d
#181 lore raw

PATCH v2 21/25 sequencer: refactor write_message()

In reply to Johannes Schindelin

The write_message() function safely writes an strbuf to a file. Sometimes it is inconvenient to require an strbuf, though: the text to be written may not be stored in a strbuf, or the strbuf should not be released after writing.

Let's refactor "safely writing string to a file" into write_with_lock_file(), and make write_message() use it. The new function makes it easy to create new convenience function write_file_gently(); as some of the upcoming callers of this new function would want to append a newline character, add a flag for it in write_file_gently(), and thus in write_with_lock_file().

---
 sequencer.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
sequencer.c +19 -4
index 5e5d113..aa949d4 100644
@@ -242,22 +242,37 @@ static void print_advice(int show_hint, struct replay_opts *opts)
 	}
 }
 
-static int write_message(struct strbuf *msgbuf, const char *filename)
+static int write_with_lock_file(const char *filename,
+				const void *buf, size_t len, int append_eol)
 {
 	static struct lock_file msg_file;
 
 	int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0);
 	if (msg_fd < 0)
 		return error_errno(_("Could not lock '%s'"), filename);
-	if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0)
-		return error_errno(_("Could not write to %s"), filename);
-	strbuf_release(msgbuf);
+	if (write_in_full(msg_fd, buf, len) < 0)
+		return error_errno(_("Could not write to '%s'"), filename);
+	if (append_eol && write(msg_fd, "\n", 1) < 0)
+		return error_errno(_("Could not write eol to '%s"), filename);
 	if (commit_lock_file(&msg_file) < 0)
 		return error(_("Error wrapping up %s."), filename);
 
 	return 0;
 }
 
+static int write_message(struct strbuf *msgbuf, const char *filename)
+{
+	int res = write_with_lock_file(filename, msgbuf->buf, msgbuf->len, 0);
+	strbuf_release(msgbuf);
+	return res;
+}
+
+static int write_file_gently(const char *filename,
+			     const char *text, int append_eol)
+{
+	return write_with_lock_file(filename, text, strlen(text), append_eol);
+}
+
 /*
  * Reads a file that was presumably written by a shell script, i.e.
  * with an end-of-line marker that needs to be stripped.
Signature · 1 line
2.10.0.windows.1.10.g803177d
#186 lore raw

PATCH v2 22/25 sequencer: remove overzealous assumption in rebase -i mode

In reply to Johannes Schindelin

The sequencer was introduced to make the cherry-pick and revert functionality available as library function, with the original idea being to extend the sequencer to also implement the rebase -i functionality.

The test to ensure that all of the commands in the script are identical to the overall operation does not mesh well with that.

Therefore let's disable the test in rebase -i mode.

While at it, error out early if the "instruction sheet" (i.e. the todo script) could not be parsed.

---
 sequencer.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
sequencer.c +4 -3
index aa949d4..5144245 100644
@@ -963,7 +963,10 @@ static int read_populate_todo(struct todo_list *todo_list,
 	close(fd);
 
 	res = parse_insn_buffer(todo_list->buf.buf, todo_list);
-	if (!res) {
+	if (res)
+		return error(_("Unusable instruction sheet: %s"), todo_file);
+
+	if (!is_rebase_i(opts)) {
 		enum todo_command valid =
 			opts->action == REPLAY_PICK ? TODO_PICK : TODO_REVERT;
 		int i;
@@ -977,8 +980,6 @@ static int read_populate_todo(struct todo_list *todo_list,
 				return error(_("Cannot revert during a cherry-pick."));
 	}
 
-	if (res)
-		return error(_("Unusable instruction sheet: %s"), todo_file);
 	return 0;
 }
 
Signature · 1 line
2.10.0.windows.1.10.g803177d
#188 lore raw

PATCH v2 23/25 sequencer: mark action_name() for translation

In reply to Johannes Schindelin

The definition of this function goes back all the way to 043a449 (sequencer: factor code out of revert builtin, 2012-01-11), long before a serious effort was made to translate all the error messages.

It is slightly out of the context of the current patch series (whose purpose it is to re-implement the performance critical parts of the interactive rebase in C) to make the error messages in the sequencer translatable, but what the heck. We'll just do it while we're looking at this part of the code.

---
 sequencer.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
sequencer.c +7 -7
index 5144245..1e7f29e 100644
@@ -176,7 +176,7 @@ int sequencer_remove_state(struct replay_opts *opts)
 
 static const char *action_name(const struct replay_opts *opts)
 {
-	return opts->action == REPLAY_REVERT ? "revert" : "cherry-pick";
+	return opts->action == REPLAY_REVERT ? N_("revert") : N_("cherry-pick");
 }
 
 struct commit_message {
@@ -312,10 +312,10 @@ static struct tree *empty_tree(void)
 static int error_dirty_index(struct replay_opts *opts)
 {
 	if (read_cache_unmerged())
-		return error_resolve_conflict(action_name(opts));
+		return error_resolve_conflict(_(action_name(opts)));
 
 	error(_("Your local changes would be overwritten by %s."),
-		action_name(opts));
+		_(action_name(opts)));
 
 	if (advice_commit_before_merge)
 		advise(_("Commit your changes or stash them to proceed."));
@@ -333,7 +333,7 @@ static int fast_forward_to(const unsigned char *to, const unsigned char *from,
 	if (checkout_fast_forward(from, to, 1))
 		return -1; /* the callee should have complained already */
 
-	strbuf_addf(&sb, _("%s: fast-forward"), action_name(opts));
+	strbuf_addf(&sb, _("%s: fast-forward"), _(action_name(opts)));
 
 	transaction = ref_transaction_begin(&err);
 	if (!transaction ||
@@ -409,7 +409,7 @@ static int do_recursive_merge(struct commit *base, struct commit *next,
 	    write_locked_index(&the_index, &index_lock, COMMIT_LOCK))
 		/* TRANSLATORS: %s will be "revert" or "cherry-pick" */
 		return error(_("%s: Unable to write new index file"),
-			action_name(opts));
+			_(action_name(opts)));
 	rollback_lock_file(&index_lock);
 
 	if (opts->signoff)
@@ -840,14 +840,14 @@ static int read_and_refresh_cache(struct replay_opts *opts)
 	if (read_index_preload(&the_index, NULL) < 0) {
 		rollback_lock_file(&index_lock);
 		return error(_("git %s: failed to read the index"),
-			action_name(opts));
+			_(action_name(opts)));
 	}
 	refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, NULL, NULL, NULL);
 	if (the_index.cache_changed && index_fd >= 0) {
 		if (write_locked_index(&the_index, &index_lock, COMMIT_LOCK)) {
 			rollback_lock_file(&index_lock);
 			return error(_("git %s: failed to refresh the index"),
-				action_name(opts));
+				_(action_name(opts)));
 		}
 	}
 	rollback_lock_file(&index_lock);
Signature · 1 line
2.10.0.windows.1.10.g803177d
#189 lore raw

PATCH v2 24/25 sequencer: quote filenames in error messages

In reply to Johannes Schindelin

This makes the code consistent by fixing quite a couple of error messages.

Suggested by Jakub Narębski.

---
 sequencer.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
sequencer.c +11 -11
index 1e7f29e..465e018 100644
@@ -255,7 +255,7 @@ static int write_with_lock_file(const char *filename,
 	if (append_eol && write(msg_fd, "\n", 1) < 0)
 		return error_errno(_("Could not write eol to '%s"), filename);
 	if (commit_lock_file(&msg_file) < 0)
-		return error(_("Error wrapping up %s."), filename);
+		return error(_("Error wrapping up '%s'."), filename);
 
 	return 0;
 }
@@ -955,16 +955,16 @@ static int read_populate_todo(struct todo_list *todo_list,
 	strbuf_reset(&todo_list->buf);
 	fd = open(todo_file, O_RDONLY);
 	if (fd < 0)
-		return error_errno(_("Could not open %s"), todo_file);
+		return error_errno(_("Could not open '%s'"), todo_file);
 	if (strbuf_read(&todo_list->buf, fd, 0) < 0) {
 		close(fd);
-		return error(_("Could not read %s."), todo_file);
+		return error(_("Could not read '%s'."), todo_file);
 	}
 	close(fd);
 
 	res = parse_insn_buffer(todo_list->buf.buf, todo_list);
 	if (res)
-		return error(_("Unusable instruction sheet: %s"), todo_file);
+		return error(_("Unusable instruction sheet: '%s'"), todo_file);
 
 	if (!is_rebase_i(opts)) {
 		enum todo_command valid =
@@ -1050,7 +1050,7 @@ static int read_populate_opts(struct replay_opts *opts)
 	 * are pretty certain that it is syntactically correct.
 	 */
 	if (git_config_from_file(populate_opts_cb, git_path_opts_file(), opts) < 0)
-		return error(_("Malformed options sheet: %s"),
+		return error(_("Malformed options sheet: '%s'"),
 			git_path_opts_file());
 	return 0;
 }
@@ -1093,7 +1093,7 @@ static int create_seq_dir(void)
 		return -1;
 	}
 	else if (mkdir(git_path_seq_dir(), 0777) < 0)
-		return error_errno(_("Could not create sequencer directory %s"),
+		return error_errno(_("Could not create sequencer directory '%s'"),
 				   git_path_seq_dir());
 	return 0;
 }
@@ -1112,12 +1112,12 @@ static int save_head(const char *head)
 	strbuf_addf(&buf, "%s\n", head);
 	if (write_in_full(fd, buf.buf, buf.len) < 0) {
 		rollback_lock_file(&head_lock);
-		return error_errno(_("Could not write to %s"),
+		return error_errno(_("Could not write to '%s'"),
 				   git_path_head_file());
 	}
 	if (commit_lock_file(&head_lock) < 0) {
 		rollback_lock_file(&head_lock);
-		return error(_("Error wrapping up %s."), git_path_head_file());
+		return error(_("Error wrapping up '%s'."), git_path_head_file());
 	}
 	return 0;
 }
@@ -1162,9 +1162,9 @@ int sequencer_rollback(struct replay_opts *opts)
 		return rollback_single_pick();
 	}
 	if (!f)
-		return error_errno(_("cannot open %s"), git_path_head_file());
+		return error_errno(_("cannot open '%s'"), git_path_head_file());
 	if (strbuf_getline_lf(&buf, f)) {
-		error(_("cannot read %s: %s"), git_path_head_file(),
+		error(_("cannot read '%s': %s"), git_path_head_file(),
 		      ferror(f) ?  strerror(errno) : _("unexpected end of file"));
 		fclose(f);
 		goto fail;
@@ -1203,7 +1203,7 @@ static int save_todo(struct todo_list *todo_list, struct replay_opts *opts)
 			todo_list->buf.len - offset) < 0)
 		return error_errno(_("Could not write to '%s'"), todo_path);
 	if (commit_lock_file(&todo_lock) < 0)
-		return error(_("Error wrapping up %s."), todo_path);
+		return error(_("Error wrapping up '%s'."), todo_path);
 	return 0;
 }
 
Signature · 1 line
2.10.0.windows.1.10.g803177d
#192 lore raw

PATCH v2 25/25 sequencer: remove bogus hint for translators

In reply to Johannes Schindelin

When translating error messages, we need to be careful *not* to translate the todo commands such as "pick", "reword", etc because they are commands, and Git would not understand translated versions of those commands.

Therefore, translating the commands in the error messages would simply be misleading.

---
 sequencer.c | 2 --
 1 file changed, 2 deletions(-)
sequencer.c +0 -2
index 465e018..cdff0f1 100644
@@ -697,8 +697,6 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 		return fast_forward_to(commit->object.oid.hash, head, unborn, opts);
 
 	if (parent && parse_commit(parent) < 0)
-		/* TRANSLATORS: The first %s will be "revert" or
-		   "cherry-pick", the second %s a SHA1 */
 		return error(_("%s: cannot parse parent commit %s"),
 			command_to_string(command),
 			oid_to_hex(&parent->object.oid));
Signature · 1 line
2.10.0.windows.1.10.g803177d
#193 lore raw

Re: [PATCH v2 25/25] sequencer: remove bogus hint for translators

Quoted 6 lines · Johannes Schindelin

When translating error messages, we need to be careful *not* to translate the todo commands such as "pick", "reword", etc because they are commands, and Git would not understand translated versions of those commands.

Therefore, translating the commands in the error messages would simply be misleading.

This comes from b9c993e0 ("i18n: git-revert literal "me" messages", 2011-02-22) where it said

Author: Ævar Arnfjörð Bjarmason <[email protected]>

i18n: git-revert literal "me" messages

Translate messages that use the `me' variable. These are all error messages referencing the command name, so the name shouldn't be translated.

This looks like a positive attempt to remind translators that their translation must flow with these literal command names that will not get translated in place, not a bogus hint at all, at least to me.

Quoted 19 lines
---
 sequencer.c | 2 --
 1 file changed, 2 deletions(-)
sequencer.c +0 -2
index 465e018..cdff0f1 100644
@@ -697,8 +697,6 @@ static int do_pick_commit(enum todo_command command, struct commit *commit,
 		return fast_forward_to(commit->object.oid.hash, head, unborn, opts);
 
 	if (parent && parse_commit(parent) < 0)
-		/* TRANSLATORS: The first %s will be "revert" or
-		   "cherry-pick", the second %s a SHA1 */
 		return error(_("%s: cannot parse parent commit %s"),
 			command_to_string(command),
 			oid_to_hex(&parent->object.oid));
#190 lore raw

Re: [PATCH v2 24/25] sequencer: quote filenames in error messages

In reply to Johannes Schindelin
Quoted 1 line · Johannes Schindelin

This makes the code consistent by fixing quite a couple of error messages.

Looks OK. While at it, we may want another one to downcase the first word, perhaps?

These may not be messages added by your series and can be left outside this series, but I have to point out that

    if (commit_lock_file(&msg_file) < 0)
        return error(_("Error wrapping up '%s'."), filename);

results in "error: Error wrapping up", which sounds quite funny.

"failed to finalize" or something would flow a bit better, I'd say.

Quoted 13 lines
sequencer.c +1 -1
index 1e7f29e..465e018 100644
@@ -255,7 +255,7 @@ static int write_with_lock_file(const char *filename,
 	if (append_eol && write(msg_fd, "\n", 1) < 0)
 		return error_errno(_("Could not write eol to '%s"), filename);
 	if (commit_lock_file(&msg_file) < 0)
-		return error(_("Error wrapping up %s."), filename);
+		return error(_("Error wrapping up '%s'."), filename);
 
 	return 0;
 }
#187 lore raw

Re: [PATCH v2 22/25] sequencer: remove overzealous assumption in rebase -i mode

In reply to Johannes Schindelin
Quoted 2 lines · Johannes Schindelin

While at it, error out early if the "instruction sheet" (i.e. the todo script) could not be parsed.

Sounds good.

#182 lore raw

Re: [PATCH v2 21/25] sequencer: refactor write_message()

In reply to Johannes Schindelin
Quoted 11 lines · Johannes Schindelin

The write_message() function safely writes an strbuf to a file. Sometimes it is inconvenient to require an strbuf, though: the text to be written may not be stored in a strbuf, or the strbuf should not be released after writing.

Let's refactor "safely writing string to a file" into write_with_lock_file(), and make write_message() use it. The new function makes it easy to create new convenience function write_file_gently(); as some of the upcoming callers of this new function would want to append a newline character, add a flag for it in write_file_gently(), and thus in write_with_lock_file().

Makes sense. As an abstraction, "give me strbuf for my sole use, and I'll trash its contents when I am done" feels like a horrible helper interface; perhaps that was overly aggressively refactored by noticing that then-current callers all released the strbuf, but still feels wrong.

And this makes the underlying helper a lot more useful.

Quoted 52 lines
---
 sequencer.c | 23 +++++++++++++++++++----
 1 file changed, 19 insertions(+), 4 deletions(-)
sequencer.c +19 -4
index 5e5d113..aa949d4 100644
@@ -242,22 +242,37 @@ static void print_advice(int show_hint, struct replay_opts *opts)
 	}
 }
 
-static int write_message(struct strbuf *msgbuf, const char *filename)
+static int write_with_lock_file(const char *filename,
+				const void *buf, size_t len, int append_eol)
 {
 	static struct lock_file msg_file;
 
 	int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0);
 	if (msg_fd < 0)
 		return error_errno(_("Could not lock '%s'"), filename);
-	if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0)
-		return error_errno(_("Could not write to %s"), filename);
-	strbuf_release(msgbuf);
+	if (write_in_full(msg_fd, buf, len) < 0)
+		return error_errno(_("Could not write to '%s'"), filename);
+	if (append_eol && write(msg_fd, "\n", 1) < 0)
+		return error_errno(_("Could not write eol to '%s"), filename);
 	if (commit_lock_file(&msg_file) < 0)
 		return error(_("Error wrapping up %s."), filename);
 
 	return 0;
 }
 
+static int write_message(struct strbuf *msgbuf, const char *filename)
+{
+	int res = write_with_lock_file(filename, msgbuf->buf, msgbuf->len, 0);
+	strbuf_release(msgbuf);
+	return res;
+}
+
+static int write_file_gently(const char *filename,
+			     const char *text, int append_eol)
+{
+	return write_with_lock_file(filename, text, strlen(text), append_eol);
+}
+
 /*
  * Reads a file that was presumably written by a shell script, i.e.
  * with an end-of-line marker that needs to be stripped.
#173 lore raw

Re: [PATCH v2 20/25] sequencer: left-trim lines read from the script

In reply to Johannes Schindelin
Quoted 3 lines · Johannes Schindelin

Interactive rebase's scripts may be indented; we need to handle this case, too, now that we prepare the sequencer to process interactive rebases.

Hmph, have we ever given the sequencer instructions indented to the user to edit? I do not offhand see why we want to be lenient here, especially only to the left.

Quoted 20 lines
---
 sequencer.c | 3 +++
 1 file changed, 3 insertions(+)
sequencer.c +3 -0
index 7953a05..5e5d113 100644
@@ -875,6 +875,9 @@ static int parse_insn_line(struct todo_item *item, const char *bol, char *eol)
 	char *end_of_object_name;
 	int i, saved, status, padding;
 
+	/* left-trim */
+	bol += strspn(bol, " \t");
+
 	for (i = 0; i < ARRAY_SIZE(todo_command_strings); i++)
 		if (skip_prefix(bol, todo_command_strings[i], &bol)) {
 			item->command = i;
#174 lore raw

Re: [PATCH v2 20/25] sequencer: left-trim lines read from the script

Hi Junio,

Quoted 8 lines · Junio C Hamano
Quoted 3 lines · Johannes Schindelin

Interactive rebase's scripts may be indented; we need to handle this case, too, now that we prepare the sequencer to process interactive rebases.

Hmph, have we ever given the sequencer instructions indented to the user to edit?

No.

We also never provide the user with an indented todo script in the sequencer. Yet we still parse it. Because Postel's Law.

Quoted 2 lines

I do not offhand see why we want to be lenient here, especially only to the left.

Postel's Law.

We do not care about the right: that's only comments (onelines, for the users' pleasure).

Ciao,
Dscho

#183 lore raw

Re: [PATCH v2 21/25] sequencer: refactor write_message()

In reply to Johannes Schindelin

Am 11.09.2016 um 12:55 schrieb Johannes Schindelin:

Quoted 11 lines
sequencer.c +2 -3
-static int write_message(struct strbuf *msgbuf, const char *filename)
+static int write_with_lock_file(const char *filename,
+				const void *buf, size_t len, int append_eol)
 {
 	static struct lock_file msg_file;
 
 	int msg_fd = hold_lock_file_for_update(&msg_file, filename, 0);
 	if (msg_fd < 0)
 		return error_errno(_("Could not lock '%s'"), filename);
-	if (write_in_full(msg_fd, msgbuf->buf, msgbuf->len) < 0)
-		return error_errno(_("Could not write to %s"), filename);

sequencer.c static void print_advice(int show_hint, struct replay_opts *opts) · line 261

Quoted 10 lines
sequencer.c +4 -1
-	strbuf_release(msgbuf);
+	if (write_in_full(msg_fd, buf, len) < 0)
+		return error_errno(_("Could not write to '%s'"), filename);
+	if (append_eol && write(msg_fd, "\n", 1) < 0)
+		return error_errno(_("Could not write eol to '%s"), filename);
 	if (commit_lock_file(&msg_file) < 0)
 		return error(_("Error wrapping up %s."), filename);
 
 	return 0;
 }

The two error paths in the added lines should both

		rollback_lock_file(&msg_file);

, I think. But I do notice that this is not exactly new, so...

-- Hannes

#175 lore raw

Re: [PATCH v2 20/25] sequencer: left-trim lines read from the script

In reply to Johannes Schindelin
Quoted 4 lines · Johannes Schindelin
Quoted 2 lines

I do not offhand see why we want to be lenient here, especially only to the left.

Postel's Law.

How would that compare/relate to yagni, though?

#145 lore raw

Re: [PATCH v2 05/25] sequencer: allow the sequencer to take custody of malloc()ed data

In reply to Johannes Schindelin
Quoted 9 lines · Johannes Schindelin

The sequencer is our attempt to lib-ify cherry-pick. Yet it behaves like a one-shot command when it reads its configuration: memory is allocated and released only when the command exits.

This is kind of okay for git-cherry-pick, which *is* a one-shot command. All the work to make the sequencer its work horse was done to allow using the functionality as a library function, though, including proper clean-up after use.

Quoted 5 lines

This patch introduces an API to pass the responsibility of releasing certain memory to the sequencer. Example:

	const char *label =
		sequencer_entrust(opts, xstrfmt("From: %s", email));

I thought we (not just me) were already pretty clear during the last round of review that we will not want this entrust() thing.