Add -c parameter to cg-commit to set commit data based on prior commit.

This will then commit any changes using the author and message from the commit
specified.  The actual logic for parsing the old commit values was taken from
git-commit-script.

git-commit-script supports this option via -C, which was already used in 
cg-commit
for something else.

Signed-off-by: James Ketrenos <[EMAIL PROTECTED]>
---

 cg-commit |   46 +++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 45 insertions(+), 1 deletions(-)

b07037397659763271e946fc15c0b3c61a8fcdfa
diff --git a/cg-commit b/cg-commit
--- a/cg-commit
+++ b/cg-commit
@@ -3,6 +3,8 @@
 # Commit into a GIT repository.
 # Copyright (c) Petr Baudis, 2005
 # Based on an example script fragment sent to LKML by Linus Torvalds.
+# 
+# -c commit based on code in git-commit-script by Linus Torvalds
 #
 # Commits changes to a GIT repository. Accepts the commit message from
 # `stdin`. If the commit message is not modified the commit will be
@@ -45,6 +47,12 @@
 #      might not actually _have_ any object database. This option is
 #      normally not interesting.
 #
+# -c::
+#      Specifify the commit SHA to inherit the GIT_AUTHOR_* variables and
+#      commit message from.  The GIT_COMMITTER_* variables will not be
+#      inherited from the specified commit.  This option is typically used
+#      when replaying commits from one lineage or repository to another.
+#      
 # FILES
 # -----
 # $GIT_DIR/author::
@@ -112,6 +120,7 @@ ignorecache=
 infoonly=
 commitalways=
 missingok=
+use_commit=
 msgs=()
 while optparse; do
        if optparse -C; then
@@ -128,6 +137,8 @@ while optparse; do
                force=1
        elif optparse -m=; then
                [EMAIL PROTECTED]"$OPTARG"
+       elif optparse -c=; then
+           use_commit="$OPTARG"
        else
                optfail
        fi
@@ -196,6 +207,39 @@ for msg in "[EMAIL PROTECTED]"; do
        echo "$msg" | fmt -s >>$LOGMSG
        written=1
 done
+
+if [ "$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'
+        written=1
+fi >> $LOGMSG
+
 # Always have at least one blank line, to ease the editing for
 # the poor people whose text editor has no 'O' command.
 [ "$written" ] || echo >>$LOGMSG
@@ -239,7 +283,7 @@ echo "CG: vim: textwidth=75" >>$LOGMSG
 
 cp $LOGMSG $LOGMSG2
 if tty -s; then
-       if ! [ "$msgs" ] || [ "$forceeditor" ]; then
+       if ! ([ "$use_commit" ] || [ "$msgs" ]) || [ "$forceeditor" ]; then
                ${EDITOR:-vi} $LOGMSG2
                if ! [ "$commitalways" ] && ! [ $LOGMSG2 -nt $LOGMSG ]; then
                        echo "Log message unchanged or not specified" >&2


[PATCH] Add -c parameter to cg-commit to set commit data based on prior commit.

This will then commit any changes using the author and message from the commit
specified.  The actual logic for parsing the old commit values was taken from
git-commit-script.

git-commit-script supports this option via -C, which was already used in 
cg-commit
for something else.

Signed-off-by: James Ketrenos <[EMAIL PROTECTED]>
---

 cg-commit |   46 +++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 45 insertions(+), 1 deletions(-)

b07037397659763271e946fc15c0b3c61a8fcdfa
diff --git a/cg-commit b/cg-commit
--- a/cg-commit
+++ b/cg-commit
@@ -3,6 +3,8 @@
 # Commit into a GIT repository.
 # Copyright (c) Petr Baudis, 2005
 # Based on an example script fragment sent to LKML by Linus Torvalds.
+# 
+# -c commit based on code in git-commit-script by Linus Torvalds
 #
 # Commits changes to a GIT repository. Accepts the commit message from
 # `stdin`. If the commit message is not modified the commit will be
@@ -45,6 +47,12 @@
 #      might not actually _have_ any object database. This option is
 #      normally not interesting.
 #
+# -c::
+#      Specifify the commit SHA to inherit the GIT_AUTHOR_* variables and
+#      commit message from.  The GIT_COMMITTER_* variables will not be
+#      inherited from the specified commit.  This option is typically used
+#      when replaying commits from one lineage or repository to another.
+#      
 # FILES
 # -----
 # $GIT_DIR/author::
@@ -112,6 +120,7 @@ ignorecache=
 infoonly=
 commitalways=
 missingok=
+use_commit=
 msgs=()
 while optparse; do
        if optparse -C; then
@@ -128,6 +137,8 @@ while optparse; do
                force=1
        elif optparse -m=; then
                [EMAIL PROTECTED]"$OPTARG"
+       elif optparse -c=; then
+           use_commit="$OPTARG"
        else
                optfail
        fi
@@ -196,6 +207,39 @@ for msg in "[EMAIL PROTECTED]"; do
        echo "$msg" | fmt -s >>$LOGMSG
        written=1
 done
+
+if [ "$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'
+        written=1
+fi >> $LOGMSG
+
 # Always have at least one blank line, to ease the editing for
 # the poor people whose text editor has no 'O' command.
 [ "$written" ] || echo >>$LOGMSG
@@ -239,7 +283,7 @@ echo "CG: vim: textwidth=75" >>$LOGMSG
 
 cp $LOGMSG $LOGMSG2
 if tty -s; then
-       if ! [ "$msgs" ] || [ "$forceeditor" ]; then
+       if ! ([ "$use_commit" ] || [ "$msgs" ]) || [ "$forceeditor" ]; then
                ${EDITOR:-vi} $LOGMSG2
                if ! [ "$commitalways" ] && ! [ $LOGMSG2 -nt $LOGMSG ]; then
                        echo "Log message unchanged or not specified" >&2

Reply via email to