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

[PATCH 2/9] git-commit-script: get commit message from an existing one.

From
Junio C Hamano <junkio@cox.net>
Date
Jun 25, 2005, 09:22 UTC
Message-ID
<7vacle4wqq.fsf_-_@assigned-by-dhcp.cox.net>
In-Reply-To
<7vmzpe4x08.fsf_-_@assigned-by-dhcp.cox.net>

With -m flag specified, git-commit-script takes the commit message along with author information from an existing commit.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 git-commit-script |   75 +++++++++++++++++++++++++++++++++++++++++++++++------
 1 files changed, 67 insertions(+), 8 deletions(-)
da2ff8148a94b982c7ee369d5a37c4d20e8dd2a4
diff --git a/git-commit-script b/git-commit-script
--- a/git-commit-script
+++ b/git-commit-script
@@ -1,12 +1,37 @@
 #!/bin/sh
+#
+# Copyright (c) 2005 Linus Torvalds
+#
+
+usage () {
+    echo 'git commit [-m existing-commit] [<path>...]'
+    exit 1
+}
+
 : ${GIT_DIR=.git}
-if [ ! -d $GIT_DIR ]; then
+if [ ! -d "$GIT_DIR" ]; then
 	echo Not a git directory 1>&2
 	exit 1
 fi
+while case "$#" in 0) break ;; esac
+do
+    case "$1" in
+    -m) shift
+        case "$#" in
+	0) usage ;;
+	*) use_commit=`git-rev-parse "$1"` ||
+	   exit ;;
+	esac
+	;;
+    *)  break
+        ;;
+    esac
+    shift
+done
+
 git-update-cache -q --refresh -- "$@" || exit 1
 PARENTS="-p HEAD"
-if [ ! -r $GIT_DIR/HEAD ]; then
+if [ ! -r "$GIT_DIR/HEAD" ]; then
 	if [ -z "$(git-ls-files)" ]; then
 		echo Nothing to commit 1>&2
 		exit 1
@@ -20,7 +45,7 @@ if [ ! -r $GIT_DIR/HEAD ]; then
 	) > .editmsg
 	PARENTS=""
 else
-	if [ -f $GIT_DIR/MERGE_HEAD ]; then
+	if [ -f "$GIT_DIR/MERGE_HEAD" ]; then
 		echo "#"
 		echo "# It looks like your may be committing a MERGE."
 		echo "# If this is not correct, please remove the file"
@@ -28,8 +53,38 @@ else
 		echo "# and try again"
 		echo "#"
 		PARENTS="-p HEAD -p MERGE_HEAD"
-	fi > .editmsg
-	git-status-script >> .editmsg
+	elif test "$use_commit" != ""
+	then
+		pick_author_script='
+		/^author /{
+			h
+			s/^author \([^<]*\) <[^>]*> .*$/\1/
+			s/'\''/'\''\'\'\''/g
+			s/.*/GIT_AUTHOR_NAME='\''&'\''/p
+
+			g
+			s/^author [^<]* <\([^>]*\)> .*$/\1/
+			s/'\''/'\''\'\'\''/g
+			s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
+
+			g
+			s/^author [^<]* <[^>]*> \(.*\)$/\1/
+			s/'\''/'\''\'\'\''/g
+			s/.*/GIT_AUTHOR_DATE='\''&'\''/p
+
+			q
+		}
+		'
+		set_author_env=`git-cat-file commit "$use_commit" |
+		sed -ne "$pick_author_script"`
+		eval "$set_author_env"
+		export GIT_AUTHOR_NAME
+		export GIT_AUTHOR_EMAIL
+		export GIT_AUTHOR_DATE
+		git-cat-file commit "$use_commit" |
+		sed -e '1,/^$/d'
+	fi >.editmsg
+	git-status-script >>.editmsg
 fi
 if [ "$?" != "0" ]
 then
@@ -37,13 +92,17 @@ then
 	rm .editmsg
 	exit 1
 fi
-${VISUAL:-${EDITOR:-vi}} .editmsg
+case "$use_commit" in
+'')
+	${VISUAL:-${EDITOR:-vi}} .editmsg
+	;;
+esac
 grep -v '^#' < .editmsg | git-stripspace > .cmitmsg
 [ -s .cmitmsg ] && 
 	tree=$(git-write-tree) &&
 	commit=$(cat .cmitmsg | git-commit-tree $tree $PARENTS) &&
-	echo $commit > $GIT_DIR/HEAD &&
-	rm -f -- $GIT_DIR/MERGE_HEAD
+	echo $commit > "$GIT_DIR/HEAD" &&
+	rm -f -- "$GIT_DIR/MERGE_HEAD"
 ret="$?"
 rm -f .cmitmsg .editmsg
 exit "$ret"
------------
Previous: Junio C HamanoNext: Junio C Hamano
Message 8 of 19 in “D/F conflicts fixes.”
  1. 0/2 D/F conflicts fixes.Junio C Hamano, Jun 23, 2005
  2. 1/2 Add more tests for read-tree --emu23.Junio C Hamano, Jun 23, 2005
  3. 2/2 Fix oversimplified optimization for add_cache_entry().Junio C Hamano, Jun 24, 2005
  4. Linus TorvaldsJun 25, 2005
  5. Junio C HamanoJun 25, 2005
  6. 0/9 Fix oversimplified optimization for add_cache_entry().Junio C Hamano, Jun 25, 2005
  7. 1/9 fix date parsing for GIT raw commit timestamp format.Junio C Hamano, Jun 25, 2005
  8. 2/9 git-commit-script: get commit message from an existing one.Junio C Hamano, Jun 25, 2005
  9. 3/9 git-cherry: find commits not merged upstream.Junio C Hamano, Jun 25, 2005
  10. 4/9 git-rebase-script: rebase local commits to new upstream head.Junio C Hamano, Jun 25, 2005
  11. 5/9 Add more tests for read-tree --emu23.Junio C Hamano, Jun 25, 2005
  12. 6/9 git-merge-one-file-script: do not misinterpret rm failure.Junio C Hamano, Jun 25, 2005
  13. 7/9 Fix oversimplified optimization for add_cache_entry().Junio C Hamano, Jun 25, 2005
  14. 8/9 http-pull: documentation updates.Junio C Hamano, Jun 25, 2005
  15. 9/9 Add a bit of developer documentation to pull.hJunio C Hamano, Jun 25, 2005
  16. [RFD] consider "git" wrapper semi-PorcelainJunio C Hamano, Jun 26, 2005
  17. Linus TorvaldsJun 26, 2005
  18. Junio C HamanoJun 26, 2005
  19. Martijn KuipersJun 26, 2005

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.