-
Notifications
You must be signed in to change notification settings - Fork 1
/
giteditor.sh
executable file
·60 lines (45 loc) · 1.32 KB
/
giteditor.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/bin/sh
#
# This is a modification of the excellent script HGEDITOR.SH that is
# distributed with the Mercurial version control system. I've simply
# modified it to work with Git.
#
[ -z "${GIT}" ] || GIT=`which git`
[ -z "${GIT_EDITOR_EDITOR}" ] || EDITOR="${GIT_EDITOR_EDITOR}"
case "${EDITOR}" in
"")
EDITOR="vi"
;;
emacs)
EDITOR="${EDITOR} -nw"
;;
gvim|vim)
EDITOR="${EDITOR} -f -o"
;;
esac
GITTMP=""
cleanup_exit() {
rm -rf "${GITTMP}"
}
# Remove temporary files even if we get interrupted
trap "cleanup_exit" 0 # normal exit
trap "exit 255" HUP INT QUIT ABRT TERM
GITTMP=$(mktemp -d ${TMPDIR-/tmp}/giteditor.XXXXXX)
[ ${GITTMP} != x -a -d ${GITTMP} ] || {
echo "Could not create temporary directory! Exiting." 1>&2
exit 1
}
(
"${GIT}" diffall >> "${GITTMP}/diff"
)
cat "$1" > "${GITTMP}/msg"
MD5=$(which md5sum 2>/dev/null) || MD5=$(which md5 2>/dev/null)
[ -x "${MD5}" ] && CHECKSUM=`${MD5} "${GITTMP}/msg"`
if [ -s "${GITTMP}/diff" ]; then
${EDITOR} "+e ${GITTMP}/diff" '+set buftype=help filetype=diff' "+vsplit ${GITTMP}/msg" '+set filetype=gitcommit tw=78' || exit $?
else
${EDITOR} -c "r ${TEMPLATE}" "${GITTMP}/msg" || exit $?
fi
[ -x "${MD5}" ] && (echo "${CHECKSUM}" | ${MD5} -c >/dev/null 2>&1 && exit 13)
mv "${GITTMP}/msg" "$1"
exit $?