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