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

[PATCH 2/5] rebase -i: style fixes and minor cleanups

From
Johannes Schindelin <johannes.schindelin@gmx.de>
Date
Sep 25, 2007, 15:42 UTC
Message-ID
<Pine.LNX.4.64.0709251642430.28395@racer.site>
In-Reply-To
<Pine.LNX.4.64.0709251640360.28395@racer.site>

This patch indents ";;" consistently with the rest of git's shell scripts, and makes sure that ";;" are before each "esac".

It introduces a helper function "has_action", to make it easier to read the intentions of the code.

Errors from "git rev-parse --verify" are no longer ignored.

Spaces are quoted using single quotes instead of a backslash, for readability.

A "test $preserve=f" (missing spaces) was fixed; hashes are no longer written to "$DOTEST"/rewritten/ unnecessarily.

We used to quote the message for a squash, only to have "echo" unquote it. Now we use "printf" and do not need to quote to start with.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
---
 git-rebase--interactive.sh |   48 +++++++++++++++++++++++++++----------------
 1 files changed, 30 insertions(+), 18 deletions(-)
diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index 58f6f28..c850411 100755
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -41,9 +41,10 @@ output () {
 		test $status != 0 &&
 			cat "$DOTEST"/output
 		return $status
-	;;
+		;;
 	*)
 		"$@"
+		;;
 	esac
 }
 
@@ -63,6 +64,7 @@ comment_for_reflog () {
 	''|rebase*)
 		GIT_REFLOG_ACTION="rebase -i ($1)"
 		export GIT_REFLOG_ACTION
+		;;
 	esac
 }
 
@@ -96,13 +98,18 @@ die_abort () {
 	die "$1"
 }
 
+has_action () {
+	grep -vqe '^$' -e '^#' "$1"
+}
+
 pick_one () {
 	no_ff=
 	case "$1" in -n) sha1=$2; no_ff=t ;; *) sha1=$1 ;; esac
 	output git rev-parse --verify $sha1 || die "Invalid commit name: $sha1"
 	test -d "$REWRITTEN" &&
 		pick_one_preserving_merges "$@" && return
-	parent_sha1=$(git rev-parse --verify $sha1^ 2>/dev/null)
+	parent_sha1=$(git rev-parse --verify $sha1^) ||
+		die "Could not get the parent of $sha1"
 	current_sha1=$(git rev-parse --verify HEAD)
 	if test $no_ff$current_sha1 = $parent_sha1; then
 		output git reset --hard $sha1
@@ -130,7 +137,7 @@ pick_one_preserving_merges () {
 	fast_forward=t
 	preserve=t
 	new_parents=
-	for p in $(git rev-list --parents -1 $sha1 | cut -d\  -f2-)
+	for p in $(git rev-list --parents -1 $sha1 | cut -d' ' -f2-)
 	do
 		if test -f "$REWRITTEN"/$p
 		then
@@ -142,41 +149,43 @@ pick_one_preserving_merges () {
 				;; # do nothing; that parent is already there
 			*)
 				new_parents="$new_parents $new_p"
+				;;
 			esac
 		fi
 	done
 	case $fast_forward in
 	t)
 		output warn "Fast forward to $sha1"
-		test $preserve=f && echo $sha1 > "$REWRITTEN"/$sha1
+		test $preserve = f || echo $sha1 > "$REWRITTEN"/$sha1
 		;;
 	f)
 		test "a$1" = a-n && die "Refusing to squash a merge: $sha1"
 
-		first_parent=$(expr "$new_parents" : " \([^ ]*\)")
+		first_parent=$(expr "$new_parents" : ' \([^ ]*\)')
 		# detach HEAD to current parent
 		output git checkout $first_parent 2> /dev/null ||
 			die "Cannot move HEAD to $first_parent"
 
 		echo $sha1 > "$DOTEST"/current-commit
 		case "$new_parents" in
-		\ *\ *)
+		' '*' '*)
 			# redo merge
 			author_script=$(get_author_ident_from_commit $sha1)
 			eval "$author_script"
-			msg="$(git cat-file commit $sha1 | \
-				sed -e '1,/^$/d' -e "s/[\"\\]/\\\\&/g")"
+			msg="$(git cat-file commit $sha1 | sed -e '1,/^$/d')"
 			# NEEDSWORK: give rerere a chance
 			if ! output git merge $STRATEGY -m "$msg" $new_parents
 			then
-				echo "$msg" > "$GIT_DIR"/MERGE_MSG
+				printf "%s\n" "$msg" > "$GIT_DIR"/MERGE_MSG
 				die Error redoing merge $sha1
 			fi
 			;;
 		*)
 			output git cherry-pick $STRATEGY "$@" ||
 				die_with_patch $sha1 "Could not pick $sha1"
+			;;
 		esac
+		;;
 	esac
 }
 
@@ -213,12 +222,11 @@ peek_next_command () {
 }
 
 do_next () {
-	test -f "$DOTEST"/message && rm "$DOTEST"/message
-	test -f "$DOTEST"/author-script && rm "$DOTEST"/author-script
-	test -f "$DOTEST"/amend && rm "$DOTEST"/amend
+	rm -f "$DOTEST"/message "$DOTEST"/author-script \
+		"$DOTEST"/amend || exit
 	read command sha1 rest < "$TODO"
 	case "$command" in
-	\#|'')
+	'#'*|'')
 		mark_action_done
 		;;
 	pick)
@@ -246,7 +254,7 @@ do_next () {
 	squash)
 		comment_for_reflog squash
 
-		test -z "$(grep -ve '^$' -e '^#' < $DONE)" &&
+		has_action "$DONE" ||
 			die "Cannot 'squash' without a previous commit"
 
 		mark_action_done
@@ -256,11 +264,12 @@ do_next () {
 			EDIT_COMMIT=
 			USE_OUTPUT=output
 			cp "$MSG" "$SQUASH_MSG"
-		;;
+			;;
 		*)
 			EDIT_COMMIT=-e
 			USE_OUTPUT=
-			test -f "$SQUASH_MSG" && rm "$SQUASH_MSG"
+			rm -f "$SQUASH_MSG" || exit
+			;;
 		esac
 
 		failed=f
@@ -280,11 +289,13 @@ do_next () {
 			warn
 			warn "Could not apply $sha1... $rest"
 			die_with_patch $sha1 ""
+			;;
 		esac
 		;;
 	*)
 		warn "Unknown command: $command $sha1 $rest"
 		die_with_patch $sha1 "Please fix this in the file $TODO."
+		;;
 	esac
 	test -s "$TODO" && return
 
@@ -475,17 +486,18 @@ EOF
 			$UPSTREAM...$HEAD | \
 			sed -n "s/^>/pick /p" >> "$TODO"
 
-		test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
+		has_action "$TODO" ||
 			die_abort "Nothing to do"
 
 		cp "$TODO" "$TODO".backup
 		git_editor "$TODO" ||
 			die "Could not execute editor"
 
-		test -z "$(grep -ve '^$' -e '^#' < $TODO)" &&
+		has_action "$TODO" ||
 			die_abort "Nothing to do"
 
 		output git checkout $ONTO && do_rest
+		;;
 	esac
 	shift
 done
-- 
1.5.3.2.1057.gf4dc1
Previous: Johannes SchindelinNext: Johannes Schindelin
Message 3 of 9 in “Two real fixes, two minor fixes, and a style fix”
  1. 0/5 Two real fixes, two minor fixes, and a style fixJohannes Schindelin, Sep 25, 2007
  2. 1/5 rebase -i: commit when continuing after "edit"Johannes Schindelin, Sep 25, 2007
  3. 2/5 rebase -i: style fixes and minor cleanupsJohannes Schindelin, Sep 25, 2007
  4. 3/5 rebase -i: Fix numbers in progress reportJohannes Schindelin, Sep 25, 2007
  5. 4/5 rebase -i: work on a detached HEADJohannes Schindelin, Sep 25, 2007
  6. 5/5 rebase -i: avoid exporting GIT_AUTHOR_* variablesJohannes Schindelin, Sep 25, 2007
  7. Johannes SixtSep 26, 2007
  8. Johannes SchindelinSep 26, 2007
  9. Johannes SixtSep 26, 2007

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.