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

[PATCH] Added giteditor script to show diff while editing commit message.

From
ted@tedpavlic.com <ted@tedpavlic.com>
Date
Jan 21, 2009, 20:47 UTC
Message-ID
<1232570841-25641-1-git-send-email-ted@tedpavlic.com>
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
Next: Johannes Schindelin
Message 1 of 11 in “Added giteditor script to show diff while editing commit message.”
  1. Added giteditor script to show diff while editing commit message.ted@tedpavlic.com, Jan 21, 2009
  2. Johannes SchindelinJan 21, 2009
  3. Ted PavlicJan 21, 2009
  4. contrib: A script to show diff in new window while editing commit message.Ted Pavlic, Jan 21, 2009
  5. Junio C HamanoJan 21, 2009
  6. Ted PavlicJan 22, 2009
  7. contrib: A script to show diff in new window while editing commit message.Ted Pavlic, Jan 22, 2009
  8. Junio C HamanoJan 22, 2009
  9. Johannes SchindelinJan 21, 2009
  10. Ted PavlicJan 22, 2009
  11. Ted PavlicJan 21, 2009

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.