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

[PATCH v3 4/9] rebase -i: refactor transform_todo_ids

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

The transform_todo_ids function is a little hard to read. Lets try to make it easier by using more of the strbuf API. Also, since we'll soon be adding command abbreviations, let's rename the function so it's name reflects that change.

Signed-off-by: Liam Beguin <liambeguin@gmail.com>
---
 builtin/rebase--helper.c |  4 +--
 sequencer.c              | 69 ++++++++++++++++------------------------
 sequencer.h              |  2 +-
 3 files changed, 31 insertions(+), 44 deletions(-)
diff --git a/builtin/rebase--helper.c b/builtin/rebase--helper.c
index f8519363a393..8ad4779d1650 100644
--- a/builtin/rebase--helper.c
+++ b/builtin/rebase--helper.c
@@ -55,9 +55,9 @@ int cmd_rebase__helper(int argc, const char **argv, const char *prefix)
 	if (command == MAKE_SCRIPT && argc > 1)
 		return !!sequencer_make_script(keep_empty, stdout, argc, argv);
 	if (command == SHORTEN_SHA1S && argc == 1)
-		return !!transform_todo_ids(1);
+		return !!transform_todos(1);
 	if (command == EXPAND_SHA1S && argc == 1)
-		return !!transform_todo_ids(0);
+		return !!transform_todos(0);
 	if (command == CHECK_TODO_LIST && argc == 1)
 		return !!check_todo_list();
 	if (command == SKIP_UNNECESSARY_PICKS && argc == 1)
diff --git a/sequencer.c b/sequencer.c
index 5033b049d995..c9a661a8c4bd 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -2494,60 +2494,47 @@ int sequencer_make_script(int keep_empty, FILE *out,
 }
 
 
-int transform_todo_ids(int shorten_ids)
+int transform_todos(int shorten_ids)
 {
 	const char *todo_file = rebase_path_todo();
 	struct todo_list todo_list = TODO_LIST_INIT;
-	int fd, res, i;
-	FILE *out;
+	struct strbuf buf = STRBUF_INIT;
+	struct todo_item *item;
+	int i;
 
-	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);
+	if (strbuf_read_file(&todo_list.buf, todo_file, 0) < 0)
 		return error(_("could not read '%s'."), todo_file);
-	}
-	close(fd);
 
-	res = parse_insn_buffer(todo_list.buf.buf, &todo_list);
-	if (res) {
+	if (parse_insn_buffer(todo_list.buf.buf, &todo_list)) {
 		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_EXEC && item->command != TODO_DROP)
-			fwrite(p, eol - bol, 1, out);
-		else {
-			const char *id = shorten_ids ?
-				short_commit_name(item->commit) :
-				oid_to_hex(&item->commit->object.oid);
-			int len;
-
-			p += strspn(p, " \t"); /* left-trim command */
-			len = strcspn(p, " \t"); /* length of command */
-
-			fprintf(out, "%.*s %s %.*s\n",
-				len, p, id, item->arg_len, item->arg);
+	for (item = todo_list.items, i = 0; i < todo_list.nr; i++, item++) {
+		/* if the item is not a command write it and continue */
+		if (item->command >= TODO_COMMENT) {
+			strbuf_addf(&buf, "%.*s\n", item->arg_len, item->arg);
+			continue;
+		}
+
+		/* add command to the buffer */
+		strbuf_addstr(&buf, command_to_string(item->command));
+
+		/* add commit id */
+		if (item->commit) {
+			const char *oid = shorten_ids ?
+					  short_commit_name(item->commit) :
+					  oid_to_hex(&item->commit->object.oid);
+
+			strbuf_addf(&buf, " %s", oid);
 		}
+		/* add all the rest */
+		strbuf_addf(&buf, " %.*s\n", item->arg_len, item->arg);
 	}
-	fclose(out);
+
+	i = write_message(buf.buf, buf.len, todo_file, 0);
 	todo_list_release(&todo_list);
-	return 0;
+	return i;
 }
 
 enum check_level {
diff --git a/sequencer.h b/sequencer.h
index 6f3d3df82c0a..4f7f2c93f83e 100644
--- a/sequencer.h
+++ b/sequencer.h
@@ -48,7 +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 transform_todo_ids(int shorten_ids);
+int transform_todos(int shorten_ids);
 int check_todo_list(void);
 int skip_unnecessary_picks(void);
 int rearrange_squash(void);
-- 
2.15.1.280.g10402c1f5b5c
Previous: Liam BeguinNext: Liam Beguin
Message 63 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.