789917f3c51325e80418cfdeccfa6fb4dcb7d290
[git.git] / applypatch
1 #!/bin/sh
2 ##
3 ## applypatch takes four file arguments, and uses those to
4 ## apply the unpacked patch (surprise surprise) that they
5 ## represent to the current tree.
6 ##
7 ## The arguments are:
8 ##      $1 - file with commit message
9 ##      $2 - file with the actual patch
10 ##      $3 - file with list of filenames the patch touches
11 ##      $4 - "info" file with Author, email and subject
12 ##      $5 - optional file containing signoff to add
13 ##
14 signoff="$5"
15 final=.dotest/final-commit
16 ##
17 ## If this file exists, we ask before applying
18 ##
19 query_apply=.dotest/.query_apply
20 MSGFILE=$1
21 PATCHFILE=$2
22 FILES=$3
23 INFO=$4
24 EDIT=${VISUAL:-$EDITOR}
25 EDIT=${EDIT:-vi}
26
27 export GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' .dotest/info)"
28 export GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' .dotest/info)"
29 export GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' .dotest/info)"
30 export SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' .dotest/info)"
31
32 if [ -n "$signoff" -a -f "$signoff" ]; then
33         cat $signoff >> $MSGFILE
34 fi
35
36 (echo "[PATCH] $SUBJECT" ; if [ -s $MSGFILE ]; then echo ; cat $MSGFILE; fi ) > $final
37
38 f=0
39 [ -f $query_apply ] || f=1
40
41 while [ $f -eq 0 ]; do
42         echo "Commit Body is:"
43         echo "--------------------------"
44         cat $final
45         echo "--------------------------"
46         echo -n "Apply? [y]es/[n]o/[e]dit/[a]ccept all "
47         read reply
48         case $reply in
49                 y|Y) f=1;;
50                 n|N) exit 2;;   # special value to tell dotest to keep going
51                 e|E) $EDIT $final;;
52                 a|A) rm -f $query_apply
53                      f=1;;
54         esac
55 done
56
57 echo
58 echo Applying "'$SUBJECT'"
59 echo
60
61 git-apply --index $PATCHFILE || exit 1
62 tree=$(git-write-tree) || exit 1
63 echo Wrote tree $tree
64 commit=$(git-commit-tree $tree -p $(cat .git/HEAD) < $final) || exit 1
65 echo Committed: $commit
66 echo $commit > .git/HEAD