]>
Commit | Line | Data |
---|---|---|
045f82cb | 1 | #!/bin/sh |
48313592 JH |
2 | # |
3 | # Copyright (c) 2005 Linus Torvalds | |
4 | # Copyright (c) 2005 Junio C Hamano | |
5 | # | |
045f82cb | 6 | |
48313592 | 7 | case "$0" in |
215a7ad1 | 8 | *-revert* ) |
b7884981 | 9 | test -t 0 && edit=-e |
96779be4 | 10 | replay= |
4e7824b1 FK |
11 | me=revert |
12 | USAGE='[--edit | --no-edit] [-n] <commit-ish>' ;; | |
215a7ad1 | 13 | *-cherry-pick* ) |
96779be4 | 14 | replay=t |
b7884981 | 15 | edit= |
4e7824b1 | 16 | me=cherry-pick |
abd6970a | 17 | USAGE='[--edit] [-n] [-r] [-x] <commit-ish>' ;; |
215a7ad1 | 18 | * ) |
397dfe67 BP |
19 | echo >&2 "What are you talking about?" |
20 | exit 1 ;; | |
48313592 | 21 | esac |
533b7039 JH |
22 | |
23 | SUBDIRECTORY_OK=Yes ;# we will cd up | |
4e7824b1 | 24 | . git-sh-setup |
7eff28a9 | 25 | require_work_tree |
533b7039 | 26 | cd_to_toplevel |
48313592 | 27 | |
96779be4 | 28 | no_commit= |
48313592 JH |
29 | while case "$#" in 0) break ;; esac |
30 | do | |
31 | case "$1" in | |
32 | -n|--n|--no|--no-|--no-c|--no-co|--no-com|--no-comm|\ | |
33 | --no-commi|--no-commit) | |
34 | no_commit=t | |
35 | ;; | |
b7884981 JH |
36 | -e|--e|--ed|--edi|--edit) |
37 | edit=-e | |
38 | ;; | |
674b2808 | 39 | --n|--no|--no-|--no-e|--no-ed|--no-edi|--no-edit) |
b7884981 JH |
40 | edit= |
41 | ;; | |
abd6970a JH |
42 | -r) |
43 | : no-op ;; | |
44 | -x|--i-really-want-to-expose-my-private-commit-object-name) | |
45 | replay= | |
48313592 JH |
46 | ;; |
47 | -*) | |
48 | usage | |
49 | ;; | |
50 | *) | |
51 | break | |
52 | ;; | |
53 | esac | |
54 | shift | |
55 | done | |
56 | ||
57 | test "$me,$replay" = "revert,t" && usage | |
58 | ||
59 | case "$no_commit" in | |
60 | t) | |
61 | # We do not intend to commit immediately. We just want to | |
62 | # merge the differences in. | |
63 | head=$(git-write-tree) || | |
64 | die "Your index file is unmerged." | |
65 | ;; | |
045f82cb | 66 | *) |
48313592 JH |
67 | head=$(git-rev-parse --verify HEAD) || |
68 | die "You do not have a valid HEAD" | |
e2f5f6ef JH |
69 | files=$(git-diff-index --cached --name-only $head) || exit |
70 | if [ "$files" ]; then | |
71 | die "Dirty index: cannot $me (dirty: $files)" | |
72 | fi | |
48313592 | 73 | ;; |
045f82cb JH |
74 | esac |
75 | ||
ff84d327 | 76 | rev=$(git-rev-parse --verify "$@") && |
48313592 JH |
77 | commit=$(git-rev-parse --verify "$rev^0") || |
78 | die "Not a single commit $@" | |
79 | prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null) || | |
80 | die "Cannot run $me a root commit" | |
81 | git-rev-parse --verify "$commit^2" >/dev/null 2>&1 && | |
82 | die "Cannot run $me a multi-parent commit." | |
83 | ||
5ac2715f JH |
84 | encoding=$(git repo-config i18n.commitencoding || echo UTF-8) |
85 | ||
48313592 JH |
86 | # "commit" is an existing commit. We would want to apply |
87 | # the difference it introduces since its first parent "prev" | |
88 | # on top of the current HEAD if we are cherry-pick. Or the | |
89 | # reverse of it if we are revert. | |
90 | ||
91 | case "$me" in | |
92 | revert) | |
5ac2715f | 93 | git show -s --pretty=oneline --encoding="$encoding" $commit | |
48313592 JH |
94 | sed -e ' |
95 | s/^[^ ]* /Revert "/ | |
5ac2715f JH |
96 | s/$/"/ |
97 | ' | |
48313592 | 98 | echo |
869659a6 | 99 | echo "This reverts commit $commit." |
48313592 JH |
100 | test "$rev" = "$commit" || |
101 | echo "(original 'git revert' arguments: $@)" | |
102 | base=$commit next=$prev | |
103 | ;; | |
104 | ||
105 | cherry-pick) | |
106 | pick_author_script=' | |
107 | /^author /{ | |
013049c9 | 108 | s/'\''/'\''\\'\'\''/g |
48313592 JH |
109 | h |
110 | s/^author \([^<]*\) <[^>]*> .*$/\1/ | |
111 | s/'\''/'\''\'\'\''/g | |
112 | s/.*/GIT_AUTHOR_NAME='\''&'\''/p | |
113 | ||
114 | g | |
115 | s/^author [^<]* <\([^>]*\)> .*$/\1/ | |
116 | s/'\''/'\''\'\'\''/g | |
117 | s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p | |
118 | ||
119 | g | |
120 | s/^author [^<]* <[^>]*> \(.*\)$/\1/ | |
121 | s/'\''/'\''\'\'\''/g | |
122 | s/.*/GIT_AUTHOR_DATE='\''&'\''/p | |
123 | ||
124 | q | |
125 | }' | |
5ac2715f JH |
126 | |
127 | logmsg=`git show -s --pretty=raw --encoding="$encoding" "$commit"` | |
128 | set_author_env=`echo "$logmsg" | | |
e3e291fc | 129 | LANG=C LC_ALL=C sed -ne "$pick_author_script"` |
48313592 JH |
130 | eval "$set_author_env" |
131 | export GIT_AUTHOR_NAME | |
132 | export GIT_AUTHOR_EMAIL | |
133 | export GIT_AUTHOR_DATE | |
134 | ||
5ac2715f JH |
135 | echo "$logmsg" | |
136 | sed -e '1,/^$/d' -e 's/^ //' | |
48313592 JH |
137 | case "$replay" in |
138 | '') | |
abd6970a | 139 | echo "(cherry picked from commit $commit)" |
48313592 JH |
140 | test "$rev" = "$commit" || |
141 | echo "(original 'git cherry-pick' arguments: $@)" | |
142 | ;; | |
143 | esac | |
144 | base=$prev next=$commit | |
145 | ;; | |
146 | ||
147 | esac >.msg | |
148 | ||
6e2931a8 SP |
149 | eval GITHEAD_$head=HEAD |
150 | eval GITHEAD_$next='`git show -s \ | |
151 | --pretty=oneline --encoding="$encoding" "$commit" | | |
152 | sed -e "s/^[^ ]* //"`' | |
153 | export GITHEAD_$head GITHEAD_$next | |
154 | ||
48313592 JH |
155 | # This three way merge is an interesting one. We are at |
156 | # $head, and would want to apply the change between $commit | |
157 | # and $prev on top of us (when reverting), or the change between | |
158 | # $prev and $commit on top of us (when cherry-picking or replaying). | |
159 | ||
acb4441e | 160 | git-merge-recursive $base -- $head $next && |
48313592 | 161 | result=$(git-write-tree 2>/dev/null) || { |
acb4441e JH |
162 | mv -f .msg "$GIT_DIR/MERGE_MSG" |
163 | { | |
164 | echo ' | |
a9cb3c6e LT |
165 | Conflicts: |
166 | ' | |
167 | git ls-files --unmerged | | |
168 | sed -e 's/^[^ ]* / /' | | |
169 | uniq | |
acb4441e JH |
170 | } >>"$GIT_DIR/MERGE_MSG" |
171 | echo >&2 "Automatic $me failed. After resolving the conflicts," | |
172 | echo >&2 "mark the corrected paths with 'git-add <paths>'" | |
173 | echo >&2 "and commit the result." | |
174 | case "$me" in | |
175 | cherry-pick) | |
48313592 JH |
176 | echo >&2 "You may choose to use the following when making" |
177 | echo >&2 "the commit:" | |
178 | echo >&2 "$set_author_env" | |
acb4441e JH |
179 | esac |
180 | exit 1 | |
48313592 JH |
181 | } |
182 | echo >&2 "Finished one $me." | |
183 | ||
184 | # If we are cherry-pick, and if the merge did not result in | |
185 | # hand-editing, we will hit this commit and inherit the original | |
186 | # author date and name. | |
187 | # If we are revert, or if our cherry-pick results in a hand merge, | |
188 | # we had better say that the current user is responsible for that. | |
189 | ||
190 | case "$no_commit" in | |
191 | '') | |
b7884981 | 192 | git-commit -n -F .msg $edit |
48313592 JH |
193 | rm -f .msg |
194 | ;; | |
195 | esac |