6e21409682bd858a3f63bdbaeacce29138be56a6
[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 AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' .dotest/info)"
28 export AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' .dotest/info)"
29 export SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' .dotest/info)"
30
31 if [ -n "$signoff" -a -f "$signoff" ]; then
32         cat $signoff >> $MSGFILE
33 fi
34
35 (echo "[PATCH] $SUBJECT" ; echo ; cat $MSGFILE ) > $final
36
37 f=0
38 [ -f $query_apply ] || f=1
39
40 while [ $f -eq 0 ]; do
41         echo "Commit Body is:"
42         echo "--------------------------"
43         cat $final
44         echo "--------------------------"
45         echo -n "Apply? [y]es/[n]o/[e]dit/[a]ccept all "
46         read reply
47         case $reply in
48                 y|Y) f=1;;
49                 n|N) exit 2;;   # special value to tell dotest to keep going
50                 e|E) $EDIT $final;;
51                 a|A) rm -f $query_apply
52                      f=1;;
53         esac
54 done
55
56 echo
57 echo Applying "'$SUBJECT'"
58 echo
59
60 git-check-files $(cat $FILES) || exit 1
61 git-checkout-cache -q $(cat $FILES) || exit 1
62 patch -u --no-backup-if-mismatch -f -p1 --fuzz=0 --input=$PATCHFILE || exit 1
63 git-update-cache --add --remove $(cat $FILES) || exit 1
64 tree=$(git-write-tree) || exit 1
65 echo Wrote tree $tree
66 commit=$(git-commit-tree $tree -p $(cat .git/HEAD) < $final) || exit 1
67 echo Committed: $commit
68 echo $commit > .git/HEAD