git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Liam Beguin <liambeguin@gmail.com>
To: git@vger.kernel.org
Cc: Johannes.Schindelin@gmx.de, gitster@pobox.com, avarab@gmail.com,
	Liam Beguin <liambeguin@gmail.com>
Subject: [PATCH 3/5] rebase -i: add exec commands via the rebase--helper
Date: Sun, 26 Nov 2017 23:55:12 -0500	[thread overview]
Message-ID: <20171127045514.25647-4-liambeguin@gmail.com> (raw)
In-Reply-To: <20171127045514.25647-1-liambeguin@gmail.com>

Recent work on `git-rebase--interactive` aim to convert shell code to C.
Even if this is most likely not a big performance enhacement, let's
convert it too since a comming 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                | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 sequencer.h                |  1 +
 4 files changed, 54 insertions(+), 23 deletions(-)

diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c
index f8519363a393..9d94c874c5bb 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)
 	int keep_empty = 0;
 	enum {
 		CONTINUE = 1, ABORT, MAKE_SCRIPT, SHORTEN_SHA1S, EXPAND_SHA1S,
-		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", &command,
+			N_("insert exec commands in todo list"), ADD_EXEC),
 		OPT_END()
 	};
 
@@ -64,5 +67,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 !!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..760334d3a8b3 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 "$cmd"
 
 todocount=$(git stripspace --strip-comments <"$todo" | wc -l)
 todocount=${todocount##* }
diff --git a/sequencer.c b/sequencer.c
index fa94ed652d2c..810b7850748e 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -2492,6 +2492,52 @@ int sequencer_make_script(int keep_empty, FILE *out,
 	return 0;
 }
 
+int add_exec_commands(const char *command)
+{
+	const char *todo_file = rebase_path_todo();
+	struct todo_list todo_list = TODO_LIST_INIT;
+	int fd, res, i, first = 1;
+	FILE *out;
+
+	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(&todo_list.buf, fd, 0) < 0) {
+		close(fd);
+		return error(_("could not read '%s'."), todo_file);
+	}
+	close(fd);
+
+	res = parse_insn_buffer(todo_list.buf.buf, &todo_list);
+	if (res) {
+		todo_list_release(&todo_list);
+		return error(_("unusable todo list: '%s'"), todo_file);
+	}
+
+	out = fopen(todo_file, "w");
+	if (!out) {
+		todo_list_release(&todo_list);
+		return error(_("unable to open '%s' for writing"), todo_file);
+	}
+	for (i = 0; i < todo_list.nr; i++) {
+		struct todo_item *item = todo_list.items + i;
+		int bol = item->offset_in_buf;
+		const char *p = todo_list.buf.buf + bol;
+		int eol = i + 1 < todo_list.nr ?
+			todo_list.items[i + 1].offset_in_buf :
+			todo_list.buf.len;
+
+		if (item->command == TODO_PICK && !first)
+			fputs(command, out);
+		fwrite(p, eol - bol, 1, out);
+		first = 0;
+	}
+	fputs(command, out);
+	fclose(out);
+	todo_list_release(&todo_list);
+	return 0;
+}
 
 int transform_todo_ids(int shorten_ids)
 {
diff --git a/sequencer.h b/sequencer.h
index 6f3d3df82c0a..a2715e6c7589 100644
--- a/sequencer.h
+++ b/sequencer.h
@@ -48,6 +48,7 @@ int sequencer_remove_state(struct replay_opts *opts);
 int sequencer_make_script(int keep_empty, FILE *out,
 		int argc, const char **argv);
 
+int add_exec_commands(const char *command);
 int transform_todo_ids(int shorten_ids);
 int check_todo_list(void);
 int skip_unnecessary_picks(void);
-- 
2.15.0.321.g19bf2bb99cee.dirty


  parent reply	other threads:[~2017-11-27  4:55 UTC|newest]

Thread overview: 70+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-27  4:55 [PATCH 0/5] rebase -i: add config to abbreviate command names Liam Beguin
2017-11-27  4:55 ` [PATCH 1/5] Documentation: move rebase.* configs to new file Liam Beguin
2017-11-27 21:27   ` Johannes Schindelin
2017-11-27  4:55 ` [PATCH 2/5] Documentation: use preferred name for the 'todo list' script Liam Beguin
2017-11-27 21:28   ` Johannes Schindelin
2017-11-27  4:55 ` Liam Beguin [this message]
2017-11-27  5:14   ` [PATCH 3/5] rebase -i: add exec commands via the rebase--helper Junio C Hamano
2017-11-27 21:41     ` Johannes Schindelin
2017-11-27 23:45       ` Junio C Hamano
2017-11-29  2:01     ` liam Beguin
2017-11-27 22:42   ` Johannes Schindelin
2017-11-27 23:48     ` Junio C Hamano
2017-11-29  2:06     ` liam Beguin
2017-11-29 21:35       ` Johannes Schindelin
2017-11-27  4:55 ` [PATCH 4/5] rebase -i: learn to abbreviate command names Liam Beguin
2017-11-27  5:19   ` Junio C Hamano
2017-11-29  2:08     ` liam Beguin
2017-11-27 23:04   ` Johannes Schindelin
2017-11-27 23:11     ` Jeff King
2017-11-29  2:11       ` liam Beguin
2017-11-29  2:10     ` liam Beguin
2017-11-29 21:40       ` Johannes Schindelin
2017-12-03  1:18         ` Junio C Hamano
2017-11-27  4:55 ` [PATCH 5/5] t3404: add test case for abbreviated commands Liam Beguin
2017-11-27  5:28   ` Junio C Hamano
2017-11-27 23:16   ` Johannes Schindelin
2017-11-27  5:23 ` [PATCH 0/5] rebase -i: add config to abbreviate command names Junio C Hamano
2017-11-29  1:56   ` liam Beguin
2017-12-03 22:17 ` [PATCH v2 0/9] " Liam Beguin
2017-12-03 22:17   ` [PATCH v2 1/9] Documentation: move rebase.* configs to new file Liam Beguin
2017-12-03 22:17   ` [PATCH v2 2/9] Documentation: use preferred name for the 'todo list' script Liam Beguin
2017-12-03 22:17   ` [PATCH v2 3/9] rebase -i: set commit to null in exec commands Liam Beguin
2017-12-03 22:17   ` [PATCH v2 4/9] rebase -i: refactor transform_todo_ids Liam Beguin
2017-12-04 14:42     ` Johannes Schindelin
2017-12-04 16:09       ` Junio C Hamano
2017-12-05  3:36         ` liam Beguin
2017-12-05 12:35           ` Junio C Hamano
2017-12-05  3:39       ` liam Beguin
2017-12-03 22:17   ` [PATCH v2 5/9] rebase -i: replace reference to sha1 with oid Liam Beguin
2017-12-03 22:17   ` [PATCH v2 6/9] rebase -i: update functions to use a flags parameter Liam Beguin
2017-12-04 15:46     ` Johannes Schindelin
2017-12-04 16:06       ` Johannes Schindelin
2017-12-05  3:42       ` liam Beguin
2017-12-05 12:37         ` Junio C Hamano
2017-12-05 12:41           ` Kerry, Richard
2017-12-05 14:42             ` liam Beguin
2017-12-05 16:05             ` Junio C Hamano
2017-12-05 16:14               ` Kerry, Richard
2017-12-03 22:17   ` [PATCH v2 7/9] rebase -i -x: add exec commands via the rebase--helper Liam Beguin
2017-12-03 22:17   ` [PATCH v2 8/9] rebase -i: learn to abbreviate command names Liam Beguin
2017-12-25 12:48     ` Duy Nguyen
2017-12-25 15:39       ` Liam Beguin
2017-12-25 23:58         ` Duy Nguyen
2017-12-27 19:15       ` Junio C Hamano
2017-12-27 21:58         ` Liam Beguin
2017-12-28 18:55           ` Junio C Hamano
2017-12-03 22:17   ` [PATCH v2 9/9] t3404: add test case for abbreviated commands Liam Beguin
2017-12-04 16:07   ` [PATCH v2 0/9] rebase -i: add config to abbreviate command names Johannes Schindelin
2017-12-05 17:52 ` Liam Beguin
2017-12-05 17:52   ` [PATCH v3 1/9] Documentation: move rebase.* configs to new file Liam Beguin
2017-12-05 17:52   ` [PATCH v3 2/9] Documentation: use preferred name for the 'todo list' script Liam Beguin
2017-12-05 17:52   ` [PATCH v3 3/9] rebase -i: set commit to null in exec commands Liam Beguin
2017-12-05 17:52   ` [PATCH v3 4/9] rebase -i: refactor transform_todo_ids Liam Beguin
2017-12-05 17:52   ` [PATCH v3 5/9] rebase -i: replace reference to sha1 with oid Liam Beguin
2017-12-05 17:52   ` [PATCH v3 6/9] rebase -i: update functions to use a flags parameter Liam Beguin
2017-12-05 17:52   ` [PATCH v3 7/9] rebase -i -x: add exec commands via the rebase--helper Liam Beguin
2017-12-05 17:52   ` [PATCH v3 8/9] rebase -i: learn to abbreviate command names Liam Beguin
2017-12-05 17:52   ` [PATCH v3 9/9] t3404: add test case for abbreviated commands Liam Beguin
2017-12-05 22:21   ` [PATCH v2 0/9] rebase -i: add config to abbreviate command names Junio C Hamano
2017-12-06  2:42     ` liam Beguin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20171127045514.25647-4-liambeguin@gmail.com \
    --to=liambeguin@gmail.com \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=avarab@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).