]> git.ipfire.org Git - thirdparty/git.git/blob - git-applypatch.sh
git-applypatch: cleanup.
[thirdparty/git.git] / git-applypatch.sh
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 - "info" file with Author, email and subject
11 ## $4 - optional file containing signoff to add
12 ##
13 . git-sh-setup || die "Not a git archive."
14
15 final=.dotest/final-commit
16 ##
17 ## If this file exists, we ask before applying
18 ##
19 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.
23 keep_subject=.dotest/.keep_subject
24
25
26 MSGFILE=$1
27 PATCHFILE=$2
28 INFO=$3
29 SIGNOFF=$4
30 EDIT=${VISUAL:-${EDITOR:-vi}}
31
32 export GIT_AUTHOR_NAME="$(sed -n '/^Author/ s/Author: //p' "$INFO")"
33 export GIT_AUTHOR_EMAIL="$(sed -n '/^Email/ s/Email: //p' "$INFO")"
34 export GIT_AUTHOR_DATE="$(sed -n '/^Date/ s/Date: //p' "$INFO")"
35 export SUBJECT="$(sed -n '/^Subject/ s/Subject: //p' "$INFO")"
36
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 test '' = "$LAST_SIGNED_OFF_BY" && echo
59 echo "$SIGNOFF"
60 } >>"$MSGFILE"
61 fi
62 fi
63
64 patch_header=
65 test -f "$keep_subject" || patch_header='[PATCH] '
66
67 {
68 echo "$patch_header$SUBJECT"
69 if test -s "$MSGFILE"
70 then
71 echo
72 cat "$MSGFILE"
73 fi
74 } >"$final"
75
76 interactive=yes
77 test -f "$query_apply" || interactive=no
78
79 while [ "$interactive" = yes ]; do
80 echo "Commit Body is:"
81 echo "--------------------------"
82 cat "$final"
83 echo "--------------------------"
84 echo -n "Apply? [y]es/[n]o/[e]dit/[a]ccept all "
85 read reply
86 case "$reply" in
87 y|Y) interactive=no;;
88 n|N) exit 2;; # special value to tell dotest to keep going
89 e|E) "$EDIT" "$final";;
90 a|A) rm -f "$query_apply"
91 interactive=no ;;
92 esac
93 done
94
95 if test -x "$GIT_DIR"/hooks/applypatch-msg
96 then
97 "$GIT_DIR"/hooks/applypatch-msg "$final" || exit
98 fi
99
100 echo
101 echo Applying "'$SUBJECT'"
102 echo
103
104 git-apply --index "$PATCHFILE" || {
105 # Here if we know which revision the patch applies to,
106 # we create a temporary working tree and index, apply the
107 # patch, and attempt 3-way merge with the resulting tree.
108 exit 1
109 }
110
111 if test -x "$GIT_DIR"/hooks/pre-applypatch
112 then
113 "$GIT_DIR"/hooks/pre-applypatch || exit
114 fi
115
116 tree=$(git-write-tree) || exit 1
117 echo Wrote tree $tree
118 parent=$(git-rev-parse --verify HEAD) &&
119 commit=$(git-commit-tree $tree -p $parent <"$final") || exit 1
120 echo Committed: $commit
121 git-update-ref HEAD $commit $parent || exit
122
123 if test -x "$GIT_DIR"/hooks/post-applypatch
124 then
125 "$GIT_DIR"/hooks/post-applypatch
126 fi