Skip to content

Commit 4426ac7

Browse files
author
Junio C Hamano
committed
Add hooks to tools/git-applypatch.
This teachs git-applypatch, which is used from git-applymbox, three hooks, similar to what git-commit-script uses. Signed-off-by: Junio C Hamano <junkio@cox.net>
1 parent 89e2c5f commit 4426ac7

File tree

3 files changed

+97
-18
lines changed

3 files changed

+97
-18
lines changed

templates/hooks--applypatch-msg

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to check the commit log message taken by
4+
# applypatch from an e-mail message.
5+
#
6+
# The hook should exit with non-zero status after issuing an
7+
# appropriate message if it wants to stop the commit. The hook is
8+
# allowed to edit the commit message file.
9+
#
10+
# To enable this hook, make this file executable.
11+
12+
test -x "$GIT_DIR/hooks/commit-msg" &&
13+
exec "$GIT_DIR/hooks/commit-msg" ${1+"$@"}
14+
:

templates/hooks--pre-applypatch

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/sh
2+
#
3+
# An example hook script to verify what is about to be committed
4+
# by applypatch from an e-mail message.
5+
#
6+
# The hook should exit with non-zero status after issuing an
7+
# appropriate message if it wants to stop the commit.
8+
#
9+
# To enable this hook, make this file executable.
10+
11+
test -x "$GIT_DIR/hooks/pre-commit" &&
12+
exec "$GIT_DIR/hooks/pre-commit" ${1+"$@"}
13+
:
14+

tools/git-applypatch

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,109 @@
1010
## $3 - "info" file with Author, email and subject
1111
## $4 - optional file containing signoff to add
1212
##
13-
signoff="$4"
13+
. git-sh-setup-script || die "Not a git archive."
14+
1415
final=.dotest/final-commit
1516
##
1617
## If this file exists, we ask before applying
1718
##
1819
query_apply=.dotest/.query_apply
20+
21+
## We do not munge the first line of the commit message too much
22+
## if this file exists.
1923
keep_subject=.dotest/.keep_subject
24+
25+
2026
MSGFILE=$1
2127
PATCHFILE=$2
2228
INFO=$3
23-
EDIT=${VISUAL:-$EDITOR}
24-
EDIT=${EDIT:-vi}
29+
SIGNOFF=$4
30+
EDIT=${VISUAL:-${EDITOR:-vi}}
2531

2632
export GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' .dotest/info)"
2733
export GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' .dotest/info)"
2834
export GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' .dotest/info)"
2935
export SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' .dotest/info)"
3036

31-
if [ -n "$signoff" -a -f "$signoff" ]; then
32-
cat $signoff >> $MSGFILE
37+
if test '' != "$SIGNOFF"
38+
then
39+
if test -f "$SIGNOFF"
40+
then
41+
SIGNOFF=`cat "$SIGNOFF"` || exit
42+
elif case "$SIGNOFF" in yes | true | me | please) : ;; *) false ;; esac
43+
then
44+
SIGNOFF=`git-var GIT_COMMITTER_IDENT | sed -e '
45+
s/>.*/>/
46+
s/^/Signed-off-by: /'
47+
`
48+
else
49+
SIGNOFF=
50+
fi
51+
if test '' != "$SIGNOFF"
52+
then
53+
LAST_SIGNED_OFF_BY=`
54+
sed -ne '/^Signed-off-by: /p' "$MSGFILE" |
55+
tail -n 1
56+
`
57+
test "$LAST_SIGNED_OFF_BY" = "$SIGNOFF" ||
58+
echo "$SIGNOFF" >>"$MSGFILE"
59+
fi
3360
fi
61+
3462
patch_header=
3563
test -f "$keep_subject" || patch_header='[PATCH] '
3664

37-
(echo "$patch_header$SUBJECT" ; if [ -s $MSGFILE ]; then echo ; cat $MSGFILE; fi ) > $final
65+
{
66+
echo "$patch_header$SUBJECT"
67+
if test -s "$MSGFILE"
68+
then
69+
echo
70+
cat "$MSGFILE"
71+
fi
72+
} >"$final"
3873

39-
f=0
40-
[ -f $query_apply ] || f=1
74+
interactive=yes
75+
test -f "$query_apply" || interactive=no
4176

42-
while [ $f -eq 0 ]; do
77+
while [ "$interactive" = yes ]; do
4378
echo "Commit Body is:"
4479
echo "--------------------------"
45-
cat $final
80+
cat "$final"
4681
echo "--------------------------"
4782
echo -n "Apply? [y]es/[n]o/[e]dit/[a]ccept all "
4883
read reply
49-
case $reply in
50-
y|Y) f=1;;
84+
case "$reply" in
85+
y|Y) interactive=no;;
5186
n|N) exit 2;; # special value to tell dotest to keep going
52-
e|E) $EDIT $final;;
53-
a|A) rm -f $query_apply
54-
f=1;;
87+
e|E) "$EDIT" "$final";;
88+
a|A) rm -f "$query_apply"
89+
interactive=no ;;
5590
esac
5691
done
5792

93+
if test -x "$GIT_DIR"/hooks/applypatch-msg
94+
then
95+
"$GIT_DIR"/hooks/applypatch-msg "$final" || exit
96+
fi
97+
5898
echo
5999
echo Applying "'$SUBJECT'"
60100
echo
61101

62-
git-apply --index $PATCHFILE || exit 1
102+
git-apply --index "$PATCHFILE" || exit 1
103+
104+
if test -x "$GIT_DIR"/hooks/pre-applypatch
105+
then
106+
"$GIT_DIR"/hooks/pre-applypatch || exit
107+
fi
108+
63109
tree=$(git-write-tree) || exit 1
64110
echo Wrote tree $tree
65-
commit=$(git-commit-tree $tree -p $(cat .git/HEAD) < $final) || exit 1
111+
commit=$(git-commit-tree $tree -p $(cat "$GIT_DIR"/HEAD) < "$final") || exit 1
66112
echo Committed: $commit
67-
echo $commit > .git/HEAD
113+
echo $commit > "$GIT_DIR"/HEAD
114+
115+
if test -x "$GIT_DIR"/hooks/post-applypatch
116+
then
117+
"$GIT_DIR"/hooks/post-applypatch
118+
fi

0 commit comments

Comments
 (0)