git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonas Fonseca <fonseca@diku.dk>
To: Petr Baudis <pasky@ucw.cz>
Cc: git@vger.kernel.org
Subject: [PROBE] cg-commit: show and enable editing of changes with --review
Date: Wed, 30 Nov 2005 01:01:31 +0100	[thread overview]
Message-ID: <20051130000131.GC5365@diku.dk> (raw)

Show changes being commited as a patch appended to the commit message
buffer. If the original patch is different from the patch extracted from
the commit message file the original patch will be reverted and the edited
patch applied before completing the commit.

Due to limitations with cg-patch this can only be used when commiting
from the project root directory. The error handling if the either the
original patch or the edited patch does not apply is not optimal, since
cg-patch will not report errors properly.

Signed-off-by: Jonas Fonseca <fonseca@diku.dk>

---
commit 5031c74c4716793784314f836f4ceb6bcfa6698a
tree d01b7a66054352a89c637d58c7ab6454a26ba700
parent dd872816a04f6462686e282b00d3e1523e3ecf4c
author Jonas Fonseca <fonseca@diku.dk> Wed, 30 Nov 2005 00:41:06 +0100
committer Jonas Fonseca <fonseca@antimatter.localdomain> Wed, 30 Nov 2005 00:41:06 +0100

 TODO      |    4 ----
 cg-commit |   63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 61 insertions(+), 6 deletions(-)

diff --git a/TODO b/TODO
index 777c728..cffa753 100644
--- a/TODO
+++ b/TODO
@@ -79,10 +79,6 @@ cg-*patch should be pre-1.0.)
 		  whitespace errors and stuff; hooks are good for this too,
 		  but I think it's good to have internal support for the
 		  basic stuff.
-	-- post 1.0 --
-	* Patch-altering cg-commit
-		You can already alter the list of files to be committed,
-		let's optionally show the patch and let you alter it, too.
 
 * cg-push over HTTP
 
diff --git a/cg-commit b/cg-commit
index 9f8d5d5..1a3c361 100755
--- a/cg-commit
+++ b/cg-commit
@@ -63,6 +63,12 @@
 #	Optionally, specify the exact name and email to sign off with by
 #	passing: `--signoff="Author Name <user@example.com>"`.
 #
+# --review:: Show and enable editing of changes being committed
+#	Show changes being commited as a patch appended to the commit message
+#	buffer. Changes made to the patch will be reapplied before completing
+#	the commit. Only makes sense with interactive commit message editing.
+#	Note, can only be used when commiting from the project root directory.
+#
 # FILES
 # -----
 # $GIT_DIR/author::
@@ -153,6 +159,7 @@ ignorecache=
 infoonly=
 commitalways=
 missingok=
+review=
 signoff=
 copy_commit=
 msgs=()
@@ -172,6 +179,9 @@ while optparse; do
 		force=1
 	elif optparse -q; then
 		quiet=1
+	elif optparse --review; then
+		[ "$_git_relpath" ] && die "--review can only be used when run from project root"
+		review=1
 	elif optparse -s || optparse --signoff; then
 		[ "$signoff" ] || signoff="$(git-var GIT_AUTHOR_IDENT | sed 's/> .*/>/')"
 	elif optparse --signoff=; then
@@ -187,6 +197,11 @@ done
 
 [ "$ignorecache" ] || cg-object-id HEAD >/dev/null 2>&1 || die "no previous commit; use -C for the initial commit"
 
+if [ "$review" ]; then
+	PATCH=$(mktemp -t gitci.XXXXXX)
+	PATCH2=$(mktemp -t gitci.XXXXXX)
+fi
+
 if [ "$ARGS" -o "$_git_relpath" ]; then
 	[ "$ignorecache" ] && die "-C and listing files to commit does not make sense"
 	[ -s $_git/merging ] && die "cannot commit individual files when merging"
@@ -201,6 +216,7 @@ if [ "$ARGS" -o "$_git_relpath" ]; then
 		sed 's/^\([^	]*\)\(.	.*\)\(	.*\)*$/"\2"/'))
 	customfiles=1
 
+	[ "$review" ] && < $filter | path_xargs git-diff-index -r -p HEAD > $PATCH
 	rm $filter
 
 else
@@ -227,6 +243,8 @@ else
 		fi
 	fi
 
+	[ "$review" ] && git-diff-index -r -p HEAD > $PATCH
+
 	merging=
 	[ -s $_git/merging ] && merging=$(cat $_git/merging | sed 's/^/-p /')
 fi
@@ -347,7 +365,21 @@ if [ ! "$ignorecache" ]; then
 	fi
 fi
 echo "CG: -----------------------------------------------------------------------" >>$LOGMSG
-echo "CG: vim: textwidth=75" >>$LOGMSG
+
+if [ "$review" ]; then
+	{
+		echo "CG:"
+		echo "CG: Change summary:"
+		echo "CG:"
+		git-apply --stat --summary < $PATCH | sed 's/^/CG: /'
+		echo "CG:"
+		cat $PATCH
+	} >>$LOGMSG
+
+	ftdiff="filetype=diff"
+fi
+
+echo "CG: vim: textwidth=75 $ftdiff" >>$LOGMSG
 
 cp $LOGMSG $LOGMSG2
 if tty -s; then
@@ -359,6 +391,7 @@ if tty -s; then
 				read -p 'Abort or commit? [ac] ' choice
 				if [ "$choice" = "a" ] || [ "$choice" = "q" ]; then
 					rm $LOGMSG $LOGMSG2
+					[ "$review" ] && rm $PATCH $PATCH2
 					echo "Commit message not modified, commit aborted" >&2
 					if [ "$merging" ]; then
 						cat >&2 <<__END__
@@ -400,9 +433,35 @@ else
 	cat >$LOGMSG2
 fi
 # Remove heading and trailing blank lines.
-grep -v ^CG: $LOGMSG2 | git-stripspace >$LOGMSG
+if [ ! "$review" ]; then
+	grep -v ^CG: $LOGMSG2 | git-stripspace >$LOGMSG
+else
+	sed '/^CG: Change summary:/,$d' < $LOGMSG2 | grep -v ^CG: | git-stripspace >$LOGMSG
+	sed -n '/^CG: Change summary:/,$p' < $LOGMSG2 | grep -v ^CG: > $PATCH2
+fi
 rm $LOGMSG2
 
+if [ "$review" ]; then
+	error_msg=
+	if ! cmp -s $PATCH $PATCH2; then
+		echo "Updating changes to edited patch"
+		# FIXME: Can only be run from the top level
+		# FIXME: Is very 'fragile' error handling. We should probably
+		# save the original patch in a local file for recovery?
+		if ! cg-patch -R < $PATCH; then
+			backup=$(mktemp commit-backup.XXXXXX)
+			cp $PATCH $backup
+			error_msg="unable to revert original patch, backup saved to $backup"
+		elif ! cg-patch < $PATCH2; then
+			backup=$(mktemp commit-backup.XXXXXX)
+			cp $PATCH2 $backup
+			error_msg="unable to apply edited patch, backup saved to $backup"
+		fi
+	fi
+	rm $PATCH $PATCH2
+	[ "$error_msg" ] && { rm $LOGMSG; die "$error_msg" }
+fi
+
 
 precommit_update()
 {

-- 
Jonas Fonseca

             reply	other threads:[~2005-11-30  0:01 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-30  0:01 Jonas Fonseca [this message]
2005-11-30  0:30 ` [PROBE] cg-commit: show and enable editing of changes with --review Junio C Hamano
2005-12-01 13:20   ` Jonas Fonseca

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20051130000131.GC5365@diku.dk \
    --to=fonseca@diku.dk \
    --cc=git@vger.kernel.org \
    --cc=pasky@ucw.cz \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).