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

[PATCH v2 2/2] rebase -r: fix the total number shown in the progress

From
Johannes Schindelin via GitGitGadget <gitgitgadget@gmail.com>
Date
May 13, 2023, 08:11 UTC
Message-ID
<cac809bcffd8ac6905ff823e1ab3503160ac54fd.1683965487.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1531.v2.git.1683965487.gitgitgadget@gmail.com>
From: Johannes Schindelin <johannes.schindelin@gmx.de>

For regular, non-`--rebase-merges` runs, there is very little work to do for the parser when determining the total number of commands in a rebase script: it is simply the number of lines after stripping the commented lines and then trimming the trailing empty line, if any.

The `--rebase-merges` mode complicates things by introducing empty lines and comments in the middle of the script. These should _not_ be counted as commands, and indeed, when an interactive rebase is interrupted and subsequently resumed, the total number of commands can magically shrink, sometimes dramatically.

The reason for this strange behavior is that empty lines _are_ counted in `edit_todo_list()` (but not the comments, as they are stripped via `strbuf_stripspace(..., 1)`, which is a bug.

Let's fix this so that the correct total number is shown from the get-go, by carefully adjusting it according to what's in the rebase script. Extra care needs to be taken in case the user edits the script: the number of commands might be different after the user edited than beforehand.

Note: Even though commented lines are skipped in `edit_todo_list()`, we
still need to handle `TODO_COMMENT` items by decrementing the
already-incremented `total_nr` again: empty lines are also marked as
`TODO_COMMENT`.
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 sequencer.c              | 9 ++++++---
 t/t3430-rebase-merges.sh | 8 ++++++++
 2 files changed, 14 insertions(+), 3 deletions(-)
diff --git a/sequencer.c b/sequencer.c
index f5d89abdc5e..9fb8ef17618 100644
--- a/sequencer.c
+++ b/sequencer.c
@@ -2453,7 +2453,6 @@ void todo_list_release(struct todo_list *todo_list)
 static struct todo_item *append_new_todo(struct todo_list *todo_list)
 {
 	ALLOC_GROW(todo_list->items, todo_list->nr + 1, todo_list->alloc);
-	todo_list->total_nr++;
 	return todo_list->items + todo_list->nr++;
 }
 
@@ -2609,7 +2608,7 @@ int todo_list_parse_insn_buffer(struct repository *r, char *buf,
 	char *p = buf, *next_p;
 	int i, res = 0, fixup_okay = file_exists(rebase_path_done());
 
-	todo_list->current = todo_list->nr = 0;
+	todo_list->current = todo_list->nr = todo_list->total_nr = 0;
 
 	for (i = 1; *p; i++, p = next_p) {
 		char *eol = strchrnul(p, '\n');
@@ -2630,6 +2629,9 @@ int todo_list_parse_insn_buffer(struct repository *r, char *buf,
 			item->commit = NULL;
 		}
 
+		if (item->command != TODO_COMMENT)
+			todo_list->total_nr++;
+
 		if (fixup_okay)
 			; /* do nothing */
 		else if (is_fixup(item->command))
@@ -6092,7 +6094,8 @@ int complete_action(struct repository *r, struct replay_opts *opts, unsigned fla
 	todo_list_to_strbuf(r, &new_todo, &buf2, -1, 0);
 	strbuf_swap(&new_todo.buf, &buf2);
 	strbuf_release(&buf2);
-	new_todo.total_nr -= new_todo.nr;
+	/* Nothing is done yet, and we're reparsing, so let's reset the count */
+	new_todo.total_nr = 0;
 	if (todo_list_parse_insn_buffer(r, new_todo.buf.buf, &new_todo) < 0)
 		BUG("invalid todo list after expanding IDs:\n%s",
 		    new_todo.buf.buf);
diff --git a/t/t3430-rebase-merges.sh b/t/t3430-rebase-merges.sh
index f351701fec2..499c31a8d9a 100755
--- a/t/t3430-rebase-merges.sh
+++ b/t/t3430-rebase-merges.sh
@@ -517,4 +517,12 @@ test_expect_success '--rebase-merges with message matched with onto label' '
 	EOF
 '
 
+test_expect_success 'progress shows the correct total' '
+	git checkout -b progress H &&
+	git rebase --rebase-merges --force-rebase --verbose A 2> err &&
+	# Expecting "Rebasing (N/14)" here, no bogus total number
+	grep "^Rebasing.*/14.$" err >progress &&
+	test_line_count = 14 progress
+'
+
 test_done
-- 
gitgitgadget
Previous: Johannes Schindelin via GitGitGadgetNext: Phillip Wood
Message 10 of 11 in “Fix two rebase bugs related to total_nr”
  1. 0/2 Fix two rebase bugs related to total_nrJohannes Schindelin via GitGitGadget, May 10, 2023
  2. 1/2 rebase --update-refs: fix loopsJohannes Schindelin via GitGitGadget, May 10, 2023
  3. Junio C HamanoMay 10, 2023
  4. 2/2 rebase -r: fix the total number shown in the progressJohannes Schindelin via GitGitGadget, May 10, 2023
  5. Junio C HamanoMay 10, 2023
  6. Phillip WoodMay 11, 2023
  7. Junio C HamanoMay 11, 2023
  8. 0/2 Fix two rebase bugs related to total_nrJohannes Schindelin via GitGitGadget, May 13, 2023
  9. 1/2 rebase --update-refs: fix loopsJohannes Schindelin via GitGitGadget, May 13, 2023
  10. 2/2 rebase -r: fix the total number shown in the progressJohannes Schindelin via GitGitGadget, May 13, 2023
  11. Phillip WoodMay 14, 2023

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.