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

[PATCH v3 7/9] rebase -i -x: add exec commands via the rebase--helper

From
Liam Beguin <liambeguin@gmail.com>
Date
Dec 5, 2017, 17:52 UTC
Message-ID
<20171205175235.32319-8-liambeguin@gmail.com>
In-Reply-To
<20171205175235.32319-1-liambeguin@gmail.com>

Recent work on `git-rebase--interactive` aims to convert shell code to C. Even if this is most likely not a big performance enhancement, let's convert it too since a coming change to abbreviate command names requires it to be updated.

Signed-off-by: Liam Beguin <liambeguin@gmail.com>
---
 builtin/rebase--helper.c   |  7 ++++++-
 git-rebase--interactive.sh | 23 +---------------------
 sequencer.c                | 39 ++++++++++++++++++++++++++++++++++++++
 sequencer.h                |  1 +
 4 files changed, 47 insertions(+), 23 deletions(-)
diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c
index 1102ecb43b67..4229ea0dc122 100644
--- a/builtin/rebase--helper.c
+++ b/builtin/rebase--helper.c
@@ -15,7 +15,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
 	unsigned flags = 0, keep_empty = 0;
 	enum {
 		CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_OIDS, EXPAND_OIDS,
-		CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH
+		CHECK_TODO_LIST, SKIP_UNNECESSARY_PICKS, REARRANGE_SQUASH,
+		ADD_EXEC
 	} command = 0;
 	struct option options[] = {
 		OPT_BOOL(0, "ff", &opts.allow_ff, N_("allow fast-forward")),
@@ -36,6 +37,8 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
 			N_("skip unnecessary picks"), SKIP_UNNECESSARY_PICKS),
 		OPT_CMDMODE(0, "rearrange-squash", &command,
 			N_("rearrange fixup/squash lines"), REARRANGE_SQUASH),
+		OPT_CMDMODE(0, "add-exec-commands", &command,
+			N_("insert exec commands in todo list"), ADD_EXEC),
 		OPT_END()
 	};
 
@@ -65,5 +68,7 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
 		return !!skip_unnecessary_picks();
 	if (command == REARRANGE_SQUASH && argc == 1)
 		return !!rearrange_squash();
+	if (command == ADD_EXEC && argc == 2)
+		return !!sequencer_add_exec_commands(argv[1]);
 	usage_with_options(builtin_rebase_helper_usage, options);
 }
diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index 437815669f00..e3f5a0abf3c7 100644
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -722,27 +722,6 @@ collapse_todo_ids() {
 	git rebase--helper --shorten-ids
 }
 
-# Add commands after a pick or after a squash/fixup series
-# in the todo list.
-add_exec_commands () {
-	{
-		first=t
-		while read -r insn rest
-		do
-			case $insn in
-			pick)
-				test -n "$first" ||
-				printf "%s" "$cmd"
-				;;
-			esac
-			printf "%s %s\n" "$insn" "$rest"
-			first=
-		done
-		printf "%s" "$cmd"
-	} <"$1" >"$1.new" &&
-	mv "$1.new" "$1"
-}
-
 # Switch to the branch in $into and notify it in the reflog
 checkout_onto () {
 	GIT_REFLOG_ACTION="$GIT_REFLOG_ACTION: checkout $onto_name"
@@ -982,7 +961,7 @@ fi
 
 test -s "$todo" || echo noop >> "$todo"
 test -z "$autosquash" || git rebase--helper --rearrange-squash || exit
-test -n "$cmd" && add_exec_commands "$todo"
+test -n "$cmd" && git rebase--helper --add-exec-commands "$cmd"
 
 todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
 todocount=${todocount##* }
diff --git a/sequencer.c b/sequencer.c
index 8b0dd610c881..892d242f6966 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -2494,6 +2494,45 @@ int sequencer_make_script(FILE *out, int argc, const char **argv,
 	return 0;
 }
 
+/*
+ * Add commands after pick and (series of) squash/fixup commands
+ * in the todo list.
+ */
+int sequencer_add_exec_commands(const char *commands)
+{
+	const char *todo_file = rebase_path_todo();
+	struct todo_list todo_list = TODO_LIST_INIT;
+	struct todo_item *item;
+	struct strbuf *buf = &todo_list.buf;
+	size_t offset = 0, commands_len = strlen(commands);
+	int i, first;
+
+	if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
+		return error(_("could not read '%s'."), todo_file);
+
+	if (parse_insn_buffer(todo_list.buf.buf, &todo_list)) {
+		todo_list_release(&todo_list);
+		return error(_("unusable todo list: '%s'"), todo_file);
+	}
+
+	first = 1;
+	/* insert <commands> before every pick except the first one */
+	for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) {
+		if (item->command == TODO_PICK && !first) {
+			strbuf_insert(buf, item->offset_in_buf + offset,
+				      commands, commands_len);
+			offset += commands_len;
+		}
+		first = 0;
+	}
+
+	/* append final <commands> */
+	strbuf_add(buf, commands, commands_len);
+
+	i = write_message(buf->buf, buf->len, todo_file, 0);
+	todo_list_release(&todo_list);
+	return i;
+}
 
 int transform_todos(unsigned flags)
 {
diff --git a/sequencer.h b/sequencer.h
index 68284e9762c8..212426c44548 100644
--- a/sequencer.h
+++ b/sequencer.h
@@ -50,6 +50,7 @@ int sequencer_remove_state(struct replay_opts *opts);
 int sequencer_make_script(FILE *out, int argc, const char **argv,
 			  unsigned flags);
 
+int sequencer_add_exec_commands(const char *command);
 int transform_todos(unsigned flags);
 int check_todo_list(void);
 int skip_unnecessary_picks(void);
-- 
2.15.1.280.g10402c1f5b5c
Previous: Liam BeguinNext: Liam Beguin
Message 65 of 70 in “rebase -i: add config to abbreviate command names”
  1. 0/5 rebase -i: add config to abbreviate command namesLiam Beguin, Nov 27, 2017
  2. 3/5 rebase -i: add exec commands via the rebase--helperLiam Beguin, Nov 27, 2017
  3. Junio C HamanoNov 27, 2017
  4. Johannes SchindelinNov 27, 2017
  5. Junio C HamanoNov 27, 2017
  6. liam BeguinNov 29, 2017
  7. Johannes SchindelinNov 27, 2017
  8. Junio C HamanoNov 27, 2017
  9. liam BeguinNov 29, 2017
  10. Johannes SchindelinNov 29, 2017
  11. 4/5 rebase -i: learn to abbreviate command namesLiam Beguin, Nov 27, 2017
  12. Junio C HamanoNov 27, 2017
  13. liam BeguinNov 29, 2017
  14. Johannes SchindelinNov 27, 2017
  15. Jeff KingNov 27, 2017
  16. liam BeguinNov 29, 2017
  17. liam BeguinNov 29, 2017
  18. Johannes SchindelinNov 29, 2017
  19. Junio C HamanoDec 3, 2017
  20. 5/5 t3404: add test case for abbreviated commandsLiam Beguin, Nov 27, 2017
  21. Junio C HamanoNov 27, 2017
  22. Johannes SchindelinNov 27, 2017
  23. 2/5 Documentation: use preferred name for the 'todo list' scriptLiam Beguin, Nov 27, 2017
  24. Johannes SchindelinNov 27, 2017
  25. 1/5 Documentation: move rebase.* configs to new fileLiam Beguin, Nov 27, 2017
  26. Johannes SchindelinNov 27, 2017
  27. Junio C HamanoNov 27, 2017
  28. liam BeguinNov 29, 2017
  29. 0/9 rebase -i: add config to abbreviate command namesLiam Beguin, Dec 3, 2017
  30. 1/9 Documentation: move rebase.* configs to new fileLiam Beguin, Dec 3, 2017
  31. 2/9 Documentation: use preferred name for the 'todo list' scriptLiam Beguin, Dec 3, 2017
  32. 5/9 rebase -i: replace reference to sha1 with oidLiam Beguin, Dec 3, 2017
  33. 4/9 rebase -i: refactor transform_todo_idsLiam Beguin, Dec 3, 2017
  34. Johannes SchindelinDec 4, 2017
  35. Junio C HamanoDec 4, 2017
  36. liam BeguinDec 5, 2017
  37. Junio C HamanoDec 5, 2017
  38. liam BeguinDec 5, 2017
  39. 7/9 rebase -i -x: add exec commands via the rebase--helperLiam Beguin, Dec 3, 2017
  40. 6/9 rebase -i: update functions to use a flags parameterLiam Beguin, Dec 3, 2017
  41. Johannes SchindelinDec 4, 2017
  42. Johannes SchindelinDec 4, 2017
  43. liam BeguinDec 5, 2017
  44. Junio C HamanoDec 5, 2017
  45. Kerry, RichardDec 5, 2017
  46. liam BeguinDec 5, 2017
  47. Junio C HamanoDec 5, 2017
  48. Kerry, RichardDec 5, 2017
  49. 3/9 rebase -i: set commit to null in exec commandsLiam Beguin, Dec 3, 2017
  50. 9/9 t3404: add test case for abbreviated commandsLiam Beguin, Dec 3, 2017
  51. 8/9 rebase -i: learn to abbreviate command namesLiam Beguin, Dec 3, 2017
  52. Duy NguyenDec 25, 2017
  53. Liam BeguinDec 25, 2017
  54. Duy NguyenDec 25, 2017
  55. Junio C HamanoDec 27, 2017
  56. Liam BeguinDec 27, 2017
  57. Junio C HamanoDec 28, 2017
  58. Johannes SchindelinDec 4, 2017
  59. 0/9 rebase -i: add config to abbreviate command namesLiam Beguin, Dec 5, 2017
  60. 1/9 Documentation: move rebase.* configs to new fileLiam Beguin, Dec 5, 2017
  61. 2/9 Documentation: use preferred name for the 'todo list' scriptLiam Beguin, Dec 5, 2017
  62. 3/9 rebase -i: set commit to null in exec commandsLiam Beguin, Dec 5, 2017
  63. 4/9 rebase -i: refactor transform_todo_idsLiam Beguin, Dec 5, 2017
  64. 5/9 rebase -i: replace reference to sha1 with oidLiam Beguin, Dec 5, 2017
  65. 7/9 rebase -i -x: add exec commands via the rebase--helperLiam Beguin, Dec 5, 2017
  66. 8/9 rebase -i: learn to abbreviate command namesLiam Beguin, Dec 5, 2017
  67. 6/9 rebase -i: update functions to use a flags parameterLiam Beguin, Dec 5, 2017
  68. 9/9 t3404: add test case for abbreviated commandsLiam Beguin, Dec 5, 2017
  69. Junio C HamanoDec 5, 2017
  70. liam BeguinDec 6, 2017

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

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