]>
Commit | Line | Data |
---|---|---|
045f82cb | 1 | #!/bin/sh |
48313592 JH |
2 | # |
3 | # Copyright (c) 2005 Linus Torvalds | |
4 | # Copyright (c) 2005 Junio C Hamano | |
5 | # | |
ae2b0f15 | 6 | . git-sh-setup |
045f82cb | 7 | |
48313592 | 8 | case "$0" in |
215a7ad1 | 9 | *-revert* ) |
b7884981 | 10 | test -t 0 && edit=-e |
48313592 | 11 | me=revert ;; |
215a7ad1 | 12 | *-cherry-pick* ) |
b7884981 | 13 | edit= |
48313592 | 14 | me=cherry-pick ;; |
215a7ad1 | 15 | * ) |
b7884981 | 16 | die "What are you talking about?" ;; |
48313592 JH |
17 | esac |
18 | ||
19 | usage () { | |
20 | case "$me" in | |
21 | cherry-pick) | |
8bf14d6e | 22 | die "usage git $me [--edit] [-n] [-r] <commit-ish>" |
48313592 JH |
23 | ;; |
24 | revert) | |
8bf14d6e | 25 | die "usage git $me [--edit | --no-edit] [-n] <commit-ish>" |
48313592 JH |
26 | ;; |
27 | esac | |
28 | } | |
29 | ||
b7884981 | 30 | no_commit= replay= |
48313592 JH |
31 | while case "$#" in 0) break ;; esac |
32 | do | |
33 | case "$1" in | |
34 | -n|--n|--no|--no-|--no-c|--no-co|--no-com|--no-comm|\ | |
35 | --no-commi|--no-commit) | |
36 | no_commit=t | |
37 | ;; | |
b7884981 JH |
38 | -e|--e|--ed|--edi|--edit) |
39 | edit=-e | |
40 | ;; | |
41 | -n|--n|--no|--no-|--no-e|--no-ed|--no-edi|--no-edit) | |
42 | edit= | |
43 | ;; | |
48313592 JH |
44 | -r|--r|--re|--rep|--repl|--repla|--replay) |
45 | replay=t | |
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 | ||
84 | # "commit" is an existing commit. We would want to apply | |
85 | # the difference it introduces since its first parent "prev" | |
86 | # on top of the current HEAD if we are cherry-pick. Or the | |
87 | # reverse of it if we are revert. | |
88 | ||
89 | case "$me" in | |
90 | revert) | |
91 | git-rev-list --pretty=oneline --max-count=1 $commit | | |
92 | sed -e ' | |
93 | s/^[^ ]* /Revert "/ | |
94 | s/$/"/' | |
95 | echo | |
96 | echo "This reverts $commit commit." | |
97 | test "$rev" = "$commit" || | |
98 | echo "(original 'git revert' arguments: $@)" | |
99 | base=$commit next=$prev | |
100 | ;; | |
101 | ||
102 | cherry-pick) | |
103 | pick_author_script=' | |
104 | /^author /{ | |
105 | h | |
106 | s/^author \([^<]*\) <[^>]*> .*$/\1/ | |
107 | s/'\''/'\''\'\'\''/g | |
108 | s/.*/GIT_AUTHOR_NAME='\''&'\''/p | |
109 | ||
110 | g | |
111 | s/^author [^<]* <\([^>]*\)> .*$/\1/ | |
112 | s/'\''/'\''\'\'\''/g | |
113 | s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p | |
114 | ||
115 | g | |
116 | s/^author [^<]* <[^>]*> \(.*\)$/\1/ | |
117 | s/'\''/'\''\'\'\''/g | |
118 | s/.*/GIT_AUTHOR_DATE='\''&'\''/p | |
119 | ||
120 | q | |
121 | }' | |
122 | set_author_env=`git-cat-file commit "$commit" | | |
e3e291fc | 123 | LANG=C LC_ALL=C sed -ne "$pick_author_script"` |
48313592 JH |
124 | eval "$set_author_env" |
125 | export GIT_AUTHOR_NAME | |
126 | export GIT_AUTHOR_EMAIL | |
127 | export GIT_AUTHOR_DATE | |
128 | ||
129 | git-cat-file commit $commit | sed -e '1,/^$/d' | |
130 | case "$replay" in | |
131 | '') | |
132 | echo "(cherry picked from $commit commit)" | |
133 | test "$rev" = "$commit" || | |
134 | echo "(original 'git cherry-pick' arguments: $@)" | |
135 | ;; | |
136 | esac | |
137 | base=$prev next=$commit | |
138 | ;; | |
139 | ||
140 | esac >.msg | |
141 | ||
142 | # This three way merge is an interesting one. We are at | |
143 | # $head, and would want to apply the change between $commit | |
144 | # and $prev on top of us (when reverting), or the change between | |
145 | # $prev and $commit on top of us (when cherry-picking or replaying). | |
146 | ||
147 | echo >&2 "First trying simple merge strategy to $me." | |
148 | git-read-tree -m -u $base $head $next && | |
149 | result=$(git-write-tree 2>/dev/null) || { | |
150 | echo >&2 "Simple $me fails; trying Automatic $me." | |
215a7ad1 | 151 | git-merge-index -o git-merge-one-file -a || { |
48313592 JH |
152 | echo >&2 "Automatic $me failed. After fixing it up," |
153 | echo >&2 "you can use \"git commit -F .msg\"" | |
154 | case "$me" in | |
155 | cherry-pick) | |
156 | echo >&2 "You may choose to use the following when making" | |
157 | echo >&2 "the commit:" | |
158 | echo >&2 "$set_author_env" | |
159 | esac | |
160 | exit 1 | |
161 | } | |
162 | result=$(git-write-tree) || exit | |
163 | } | |
164 | echo >&2 "Finished one $me." | |
165 | ||
166 | # If we are cherry-pick, and if the merge did not result in | |
167 | # hand-editing, we will hit this commit and inherit the original | |
168 | # author date and name. | |
169 | # If we are revert, or if our cherry-pick results in a hand merge, | |
170 | # we had better say that the current user is responsible for that. | |
171 | ||
172 | case "$no_commit" in | |
173 | '') | |
b7884981 | 174 | git-commit -n -F .msg $edit |
48313592 JH |
175 | rm -f .msg |
176 | ;; | |
177 | esac |