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

[PATCH 2/3] rebase-i: slight internal improvements

From
Stephan Beyer <s-beyer@gmx.net>
Date
Jun 20, 2008, 02:45 UTC
Message-ID
<1213929935-15093-3-git-send-email-s-beyer@gmx.net>
In-Reply-To
<1213929935-15093-2-git-send-email-s-beyer@gmx.net>

Add commit_message function to get the commit message from a commit and other slight internal improvements.

Signed-off-by: Stephan Beyer <s-beyer@gmx.net>
---
 git-rebase--interactive.sh |   61 +++++++++++++++++++++++++------------------
 1 files changed, 35 insertions(+), 26 deletions(-)
diff --git a/git-rebase--interactive.sh b/git-rebase--interactive.sh
index 3f926d8..e8ac2ae 100755
--- a/git-rebase--interactive.sh
+++ b/git-rebase--interactive.sh
@@ -25,6 +25,7 @@ SQUASH_MSG="$DOTEST"/message-squash
 PRESERVE_MERGES=
 STRATEGY=
 VERBOSE=
+MARK_PREFIX='refs/rebase-marks'
 test -f "$DOTEST"/strategy && STRATEGY="$(cat "$DOTEST"/strategy)"
 test -f "$DOTEST"/verbose && VERBOSE=t
 
@@ -33,7 +34,6 @@ mark the corrected paths with 'git add <paths>', and
 run 'git rebase --continue'"
 export GIT_CHERRY_PICK_HELP
 
-mark_prefix=refs/rebase-marks/
 
 warn () {
 	echo "$*" >&2
@@ -73,13 +73,18 @@ comment_for_reflog () {
 	esac
 }
 
+# Get commit message from commit $1
+commit_message () {
+	git cat-file commit "$1" | sed -e '1,/^$/d'
+}
+
 last_count=
 mark_action_done () {
-	sed -e 1q < "$TODO" >> "$DONE"
-	sed -e 1d < "$TODO" >> "$TODO".new
-	mv -f "$TODO".new "$TODO"
-	count=$(grep -c '^[^#]' < "$DONE")
-	total=$(($count+$(grep -c '^[^#]' < "$TODO")))
+	sed -e 1q "$TODO" >>"$DONE"
+	sed -e 1d "$TODO" >>"$TODO.new"
+	mv -f "$TODO.new" "$TODO"
+	count="$(grep -c '^[^#]' "$DONE")"
+	total="$(expr "$count" + "$(grep -c '^[^#]' "$TODO")")"
 	if test "$last_count" != "$count"
 	then
 		last_count=$count
@@ -88,16 +93,18 @@ mark_action_done () {
 	fi
 }
 
+# Generate message, patch and author script files
 make_patch () {
 	parent_sha1=$(git rev-parse --verify "$1"^) ||
 		die "Cannot get patch for $1^"
 	git diff-tree -p "$parent_sha1".."$1" > "$DOTEST"/patch
 	test -f "$DOTEST"/message ||
-		git cat-file commit "$1" | sed "1,/^$/d" > "$DOTEST"/message
+		commit_message "$1" >"$DOTEST"/message
 	test -f "$DOTEST"/author-script ||
 		get_author_ident_from_commit "$1" > "$DOTEST"/author-script
 }
 
+# Generate a patch and die
 die_with_patch () {
 	make_patch "$1"
 	git rerere
@@ -105,9 +112,9 @@ die_with_patch () {
 }
 
 cleanup_before_quit () {
-	for ref in $(git for-each-ref --format='%(refname)' "${mark_prefix%/}")
+	for ref in $(git for-each-ref --format='%(refname)' "$MARK_PREFIX")
 	do
-		git update-ref -d "$ref" "$ref" || return 1
+		git update-ref -d "$ref" "$ref" || return
 	done
 	rm -rf "$DOTEST"
 }
@@ -118,7 +125,7 @@ die_abort () {
 }
 
 has_action () {
-	grep '^[^#]' "$1" >/dev/null
+	grep -q '^[^#]' "$1"
 }
 
 redo_merge () {
@@ -126,7 +133,7 @@ redo_merge () {
 	shift
 
 	eval "$(get_author_ident_from_commit $rm_sha1)"
-	msg="$(git cat-file commit $rm_sha1 | sed -e '1,/^$/d')"
+	msg="$(commit_message "$rm_sha1")"
 
 	if ! GIT_AUTHOR_NAME="$GIT_AUTHOR_NAME" \
 		GIT_AUTHOR_EMAIL="$GIT_AUTHOR_EMAIL" \
@@ -167,37 +174,39 @@ nth_string () {
 }
 
 make_squash_message () {
-	if test -f "$SQUASH_MSG"; then
-		COUNT=$(($(sed -n "s/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p" \
-			< "$SQUASH_MSG" | sed -ne '$p')+1))
+	if test -f "$SQUASH_MSG"
+	then
+		count="$(($(sed -n -e 's/^# This is [^0-9]*\([1-9][0-9]*\).*/\1/p' \
+			"$SQUASH_MSG" | sed -n -e '$p')+1))"
 		echo "# This is a combination of $COUNT commits."
 		sed -e 1d -e '2,/^./{
 			/^$/d
-		}' <"$SQUASH_MSG"
+		}' "$SQUASH_MSG"
 	else
 		COUNT=2
 		echo "# This is a combination of two commits."
 		echo "# The first commit's message is:"
 		echo
-		git cat-file commit HEAD | sed -e '1,/^$/d'
+		commit_message HEAD
 	fi
 	echo
 	echo "# This is the $(nth_string $COUNT) commit message:"
 	echo
-	git cat-file commit $1 | sed -e '1,/^$/d'
+	commit_message "$1"
 }
 
 peek_next_command () {
-	sed -n "1s/ .*$//p" < "$TODO"
+	sed -n -e '1s/ .*$//p' "$TODO"
 }
 
+# If $1 is a mark, make a ref from it; otherwise keep it
 mark_to_ref () {
-	if expr "$1" : "^:[0-9][0-9]*$" >/dev/null
-	then
-		echo "$mark_prefix$(printf %d ${1#:})"
-	else
-		echo "$1"
-	fi
+	arg="$1"
+	ref="$(expr "$arg" : '^:0*\([0-9]\+\)$')"
+	test -n "$ref" &&
+		arg="$MARK_PREFIX/$ref"
+	printf '%s\n' "$arg"
+	unset arg ref
 }
 
 do_next () {
@@ -634,7 +643,7 @@ do
 		UPSTREAM=$(git rev-parse --verify "$1") || die "Invalid base"
 		test -z "$ONTO" && ONTO=$UPSTREAM
 
-		if test ! -z "$2"
+		if test -n "$2"
 		then
 			output git show-ref --verify --quiet "refs/heads/$2" ||
 				die "Invalid branchname: $2"
@@ -674,7 +683,7 @@ do
 				create_extended_todo_list
 		else
 			git rev-list --no-merges --reverse --pretty=oneline \
-				 $common_rev_list_opts | sed -n "s/^>/pick /p"
+				 $common_rev_list_opts | sed -n -e 's/^>/pick /p'
 		fi > "$TODO"
 
 		cat >> "$TODO" << EOF
-- 
1.5.5.1.561.gd8556
Previous: Stephan BeyerNext: Stephan Beyer
Message 3 of 21 in “some small changes for rebase-i”
  1. Stephan BeyerJun 20, 2008
  2. 1/3 t3404: slight improvementsStephan Beyer, Jun 20, 2008
  3. 2/3 rebase-i: slight internal improvementsStephan Beyer, Jun 20, 2008
  4. 3/3 Make rebase--interactive use OPTIONS_SPECStephan Beyer, Jun 20, 2008
  5. Stephan BeyerJun 20, 2008
  6. Johannes SchindelinJun 20, 2008
  7. 1/2 t3404: extra checks and s/! git/test_must_fail git/Stephan Beyer, Jun 20, 2008
  8. 2/2 Make rebase--interactive use OPTIONS_SPECStephan Beyer, Jun 20, 2008
  9. Brandon CaseyJun 20, 2008
  10. Stephan BeyerJun 20, 2008
  11. しらいしななこJun 20, 2008
  12. Stephan BeyerJun 21, 2008
  13. Junio C HamanoJun 21, 2008
  14. 1/2 t3404: stricter tests for git-rebase--interactiveStephan Beyer, Jun 21, 2008
  15. 2/2 Make rebase--interactive use OPTIONS_SPECStephan Beyer, Jun 21, 2008
  16. Johannes SixtJun 20, 2008
  17. Stephan BeyerJun 20, 2008
  18. Johannes SixtJun 20, 2008
  19. Johannes SchindelinJun 20, 2008
  20. Stephan BeyerJun 20, 2008
  21. Johannes SchindelinJun 20, 2008

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.