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

[PATCH/RFC 17/20] rebase: improve detection of rebase in progress

From
Martin von Zweigbergk <martin.von.zweigbergk@gmail.com>
Date
Nov 25, 2010, 19:58 UTC
Message-ID
<1290715083-16919-18-git-send-email-martin.von.zweigbergk@gmail.com>
In-Reply-To
<1290715083-16919-1-git-send-email-martin.von.zweigbergk@gmail.com>

Detect early on if a rebase is in progress and what kind of rebase it is. This prepares for further refactoring where plain (am) rebase will be dispatched to git-rebase--am.sh and merge rebase will be dispatched to git-rebase--merge.sh.

The idea is to use the same variables whether the type of rebase was detected from rebase-apply/ or rebase-merge/ directories or from on the command line options.

Also show a consistent error message independent of the kind of rebase that was in progress. Also remove the obsolete wording about an being in the middle of a 'patch application', since that (an existing "$GIT_DIR"/rebase-apply/applying) aborts 'git rebase' at an earlier stage.

Signed-off-by: Martin von Zweigbergk <martin.von.zweigbergk@gmail.com>
---
 git-rebase.sh |   74 ++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 47 insertions(+), 27 deletions(-)
diff --git a/git-rebase.sh b/git-rebase.sh
index 43cab41..d233da6 100755
--- a/git-rebase.sh
+++ b/git-rebase.sh
@@ -59,16 +59,19 @@ action=
 preserve_merges=
 autosquash=
 test "$(git config --bool rebase.autosquash)" = "true" && autosquash=t
+# Non-empty if a rebase was in progress when 'git rebase' was invoked
+in_progress=
+# One of {am, merge, interactive}
+type=
+# One of {"$GIT_DIR"/rebase-apply, "$GIT_DIR"/rebase-merge}
+state_dir=
 
 read_state () {
-	if test -d "$merge_dir"
+	if test "$type" = merge
 	then
-		state_dir="$merge_dir"
 		onto_name=$(cat "$merge_dir"/onto_name) &&
 		end=$(cat "$merge_dir"/end) &&
 		msgnum=$(cat "$merge_dir"/msgnum)
-	else
-		state_dir="$apply_dir"
 	fi &&
 	head_name=$(cat "$state_dir"/head-name) &&
 	onto=$(cat "$state_dir"/onto) &&
@@ -196,6 +199,23 @@ run_pre_rebase_hook () {
 test -f "$apply_dir"/applying &&
 	die 'It looks like git-am is in progress. Cannot rebase.'
 
+if test -d "$apply_dir"
+then
+	type=am
+	state_dir="$apply_dir"
+elif test -d "$merge_dir"
+then
+	if test -f "$merge_dir"/interactive
+	then
+		type=interactive
+		interactive_rebase=explicit
+	else
+		type=merge
+	fi
+	state_dir="$merge_dir"
+fi
+test -n "$type" && in_progress=t
+
 while test $# != 0
 do
 	case "$1" in
@@ -324,37 +344,24 @@ then
 	test -z "$onto" &&
 	test -z "$preserve_merges" ||
 	usage
-	test -d "$apply_dir" -a -z "$interactive_rebase" ||
-	test -d "$merge_dir" || die "No rebase in progress?"
+	test -z "$in_progress" && die "No rebase in progress?"
 else
-	# Make sure we do not have $apply_dir or $merge_dir
-	if test -z "$do_merge"
+	# Make sure we do not have a rebase in progress
+	if test -n "$in_progress"
 	then
-		if mkdir "$apply_dir" 2>/dev/null
-		then
-			rmdir "$apply_dir"
-		else
-			echo >&2 '
-It seems that I cannot create a rebase-apply directory, and
-I wonder if you are in the middle of patch application or another
-rebase.  If that is not the case, please
-	rm -fr '"$apply_dir"'
+		die '
+It seems that there is already a "${state_dir##*/}" directory, and
+I wonder if you are in the middle of another rebase.  If that is the
+case, please try
+	git rebase (--continue | --abort | --skip)
+If that is not the case, please
+	rm -fr '"$state_dir"'
 and run me again.  I am stopping in case you still have something
 valuable there.'
-		exit 1
-		fi
-	else
-		if test -d "$merge_dir"
-		then
-			die "previous rebase directory $merge_dir still exists." \
-				'Try git rebase (--continue | --abort | --skip)'
-		fi
 	fi
 	test $# -eq 0 && test -z "$rebase_root" && usage
 fi
 
-test -f "$merge_dir"/interactive && interactive_rebase=explicit
-
 test -n "$action" && test -n "$interactive_rebase" && run_interactive_rebase
 
 case "$action" in
@@ -415,6 +422,19 @@ abort)
 	;;
 esac
 
+if test -n "$interactive_rebase"
+then
+	type=interactive
+	state_dir="$merge_dir"
+elif test -n "$do_merge"
+then
+	type=merge
+	state_dir="$merge_dir"
+else
+	type=am
+	state_dir="$apply_dir"
+fi
+
 if test -z "$rebase_root"
 then
 	# The upstream head must be given.  Make sure it is valid.
-- 
1.7.3.2.864.gbbb96
Previous: Martin von ZweigbergkNext: Martin von Zweigbergk
Message 22 of 29 in “Refactor rebase”
  1. 00/20 Refactor rebaseMartin von Zweigbergk, Nov 25, 2010
  2. 01/20 rebase: clearer names for directory variablesMartin von Zweigbergk, Nov 25, 2010
  3. 02/20 rebase: refactor reading of stateMartin von Zweigbergk, Nov 25, 2010
  4. 03/20 rebase: read state outside loopMartin von Zweigbergk, Nov 25, 2010
  5. 04/20 rebase: remove unused rebase state 'prev_head'Martin von Zweigbergk, Nov 25, 2010
  6. Michael J GruberNov 26, 2010
  7. Martin von ZweigbergkNov 26, 2010
  8. Junio C HamanoNov 29, 2010
  9. Michael J GruberNov 30, 2010
  10. 05/20 rebase: act on command line outside parsing loopMartin von Zweigbergk, Nov 25, 2010
  11. 06/20 rebase: collect check for existing rebaseMartin von Zweigbergk, Nov 25, 2010
  12. 07/20 rebase: stricter check on argumentsMartin von Zweigbergk, Nov 25, 2010
  13. 08/20 rebase: align variable namesMartin von Zweigbergk, Nov 25, 2010
  14. 09/20 rebase: align variable contentMartin von Zweigbergk, Nov 25, 2010
  15. 10/20 rebase: factor out command line option processingMartin von Zweigbergk, Nov 25, 2010
  16. 11/20 rebase -i: remove now unnecessary directory checksMartin von Zweigbergk, Nov 25, 2010
  17. 12/20 rebase: reorder validation stepsMartin von Zweigbergk, Nov 25, 2010
  18. 13/20 rebase: factor out reference parsingMartin von Zweigbergk, Nov 25, 2010
  19. 14/20 rebase: factor out clean work tree checkMartin von Zweigbergk, Nov 25, 2010
  20. 15/20 rebase: factor out call to pre-rebase hookMartin von Zweigbergk, Nov 25, 2010
  21. 16/20 rebase -i: support --statMartin von Zweigbergk, Nov 25, 2010
  22. 17/20 rebase: improve detection of rebase in progressMartin von Zweigbergk, Nov 25, 2010
  23. 18/20 rebase -m: extract code to new source fileMartin von Zweigbergk, Nov 25, 2010
  24. 19/20 rebase: extract am code to new source fileMartin von Zweigbergk, Nov 25, 2010
  25. 20/20 rebase: show consistent conflict resolution hintMartin von Zweigbergk, Nov 25, 2010
  26. Martin von ZweigbergkNov 25, 2010
  27. Sverre RabbelierNov 26, 2010
  28. Martin von ZweigbergkNov 26, 2010
  29. Sverre RabbelierNov 27, 2010

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.