git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: ted@tedpavlic.com
To: gitster@pobox.com
Cc: git@vger.kernel.org, Ted Pavlic <ted@tedpavlic.com>
Subject: [PATCH] Added giteditor script to show diff while editing commit message.
Date: Wed, 21 Jan 2009 15:47:21 -0500	[thread overview]
Message-ID: <1232570841-25641-1-git-send-email-ted@tedpavlic.com> (raw)

From: Ted Pavlic <ted@tedpavlic.com>

This new script (contrib/giteditor/giteditor) is an example GIT_EDITOR
that causes the editor to open the commit message as well as a "git diff
--cached". As a result, a window showing a diff of what is being
committed is opened alongside the commit message.

This script also detects when "stg edit" is being called and uses "stg
show" instead.

This script is highly influenced by the "hgeditor" script distributed
with the Mercurial SCM.

Signed-off-by: Ted Pavlic <ted@tedpavlic.com>
---
 contrib/giteditor/README    |    9 ++++
 contrib/giteditor/giteditor |  111 +++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 120 insertions(+), 0 deletions(-)
 create mode 100644 contrib/giteditor/README
 create mode 100755 contrib/giteditor/giteditor

diff --git a/contrib/giteditor/README b/contrib/giteditor/README
new file mode 100644
index 0000000..b769c3e
--- /dev/null
+++ b/contrib/giteditor/README
@@ -0,0 +1,9 @@
+A GIT_EDITOR to show diff alongside commit message. User can review diff
+within commit edit window. Works with StGit ("stg edit") as well.
+
+To use this script, set it as the value of GIT_EDITOR (or core.editor).
+
+
+Copyright (c) 2009 by Theodore P. Pavlic <ted@tedpavlic.com>
+Highly influenced by hgeditor script distributed with Mercurial SCM.
+Distributed under the GNU General Public License, version 2.0.
diff --git a/contrib/giteditor/giteditor b/contrib/giteditor/giteditor
new file mode 100755
index 0000000..13ca5f6
--- /dev/null
+++ b/contrib/giteditor/giteditor
@@ -0,0 +1,111 @@
+#!/bin/sh
+#
+# A GIT_EDITOR to show diff alongside commit message. User can review
+# diff within commit edit window. Works with StGit ("stg edit") as well.
+#
+# Copyright (c) 2009 by Theodore P. Pavlic <ted@tedpavlic.com>
+# Highly influenced by hgeditor script distributed with Mercurial SCM.
+# Distributed under the GNU General Public License, version 2.0.
+#
+# To use this script, set it as the value of GIT_EDITOR (or
+# core.editor).
+#
+
+# Find git
+[ -z "${GIT}" ] && GIT="git"
+
+# Find stg
+[ -z "${STG}" ] && STG="stg"
+
+# Use an editor. To prevent loops, avoid GIT_EDITOR and core.editor.
+EDITOR=${GIT_EDITOR_EDITOR} || \
+    EDITOR=${VISUAL} || \
+    EDITOR=${EDITOR} || \
+    EDITOR="vi";
+
+# If we recognize a popular editor, add necessary flags
+case "${EDITOR}" in
+    emacs)
+        EDITOR="${EDITOR} -nw"
+        ;;
+    mvim|gvim|vim)
+        EDITOR="${EDITOR} -f -o"
+        ;;
+esac
+
+# Remove temporary files even if we get interrupted
+GITTMP=""
+cleanup_exit() { 
+    [ -n "${GITTMP}" ] && rm -rf "${GITTMP}" 
+}
+trap "cleanup_exit" 0 # normal exit
+trap "exit 255" 1 2 3 6 15 # HUP INT QUIT ABRT TERM
+
+# End GITTMP in ".git" so that "*.git/" syntax highlighting recognition
+# doesn't break
+GITTMP="${TMPDIR-/tmp}/giteditor.$RANDOM.$RANDOM.$RANDOM.$$.git"
+(umask 077 && mkdir "${GITTMP}") || {
+    echo "Could not create temporary directory! Exiting." 1>&2
+    exit 1
+}
+
+# For git, COMMITMSG=COMMIT_EDITMSG
+# For stg, COMMITMSG=.stgit-edit.txt
+# etc.
+COMMITMSG=$( basename "$1" )
+
+case "${COMMITMSG}" in
+    .stgit-edit.txt) 
+        DIFFCMD="${STG}"
+        DIFFARGS="show"
+        ;;
+    *) 
+        DIFFCMD="${GIT}"
+        DIFFARGS="diff --cached"
+        ;;
+esac
+
+if [ -f "$1" ]; then
+    # We were passed an existing commit message
+
+    "${DIFFCMD}" ${DIFFARGS} >> "${GITTMP}/diff"
+## Uncomment if you only want to see diff of what changed
+## (note that it only works if DIFFCMD is git)
+#    (
+#        grep '^#.*modified:' "$1" | cut -b 15- | while read changed; do
+#            "${DIFFCMD}" ${DIFFARGS} "${changed}" >> "${GITTMP}/diff"
+#        done
+#    )
+
+     cat "$1" > "${GITTMP}/${COMMITMSG}"
+
+else
+
+    # Give us a blank COMMITMSG to edit
+    touch "${GITTMP}/${COMMITMSG}"
+
+    # Generate the diff
+    "${DIFFCMD}" ${DIFFARGS} >> "${GITTMP}/diff"
+    #touch "${GITTMP}/diff"
+
+fi
+
+# Use MD5 to see if commit message changed (necessary?)
+MD5=$(which md5sum 2>/dev/null) || \
+    MD5=$(which md5 2>/dev/null)
+
+[ -x "${MD5}" ] && CHECKSUM=$( ${MD5} "${GITTMP}/${COMMITMSG}" )
+if [ -s "${GITTMP}/diff" ]; then
+    # Diff is non-empty, so edit msg and diff
+    ${EDITOR} "${GITTMP}/${COMMITMSG}" "${GITTMP}/diff" || exit $?
+else
+    # Empty diff. Only edit msg
+    ${EDITOR} "${GITTMP}/${COMMITMSG}" || exit $?
+fi
+[ -x "${MD5}" ] && (echo "${CHECKSUM}" | ${MD5} -c >/dev/null 2>&1 && exit 13)
+
+# Commit message changed, so dump it on original message from Git
+mv "${GITTMP}/${COMMITMSG}" "$1"
+
+# (recall that GITTMP directory gets cleaned up by trap above)
+exit $?
-- 
1.6.1.213.g28da8

             reply	other threads:[~2009-01-21 20:48 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-01-21 20:47 ted [this message]
     [not found] ` <0E82F261-2D96-4204-9906-C5E8D47E9A5D@wincent.com>
2009-01-21 21:07   ` [PATCH] Added giteditor script to show diff while editing commit message Ted Pavlic
2009-01-21 21:46 ` Johannes Schindelin
2009-01-21 22:33   ` Ted Pavlic
2009-01-21 22:45     ` [PATCH] contrib: A script to show diff in new window " Ted Pavlic
2009-01-21 23:59       ` Junio C Hamano
2009-01-22  3:39         ` Ted Pavlic
2009-01-22  3:50         ` Ted Pavlic
2009-01-22  7:49           ` Junio C Hamano
2009-01-21 22:52     ` [PATCH] Added giteditor script to show diff " Johannes Schindelin
2009-01-22  1:46       ` Ted Pavlic

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=1232570841-25641-1-git-send-email-ted@tedpavlic.com \
    --to=ted@tedpavlic.com \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /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).