Merge branch 'master' of git://git.kernel.org/pub/scm/git/git
[git/jnareb-git.git] / git-revert.sh
blob50cc47b0634201e7acaaa0a0642be779aaf7c2da
1 #!/bin/sh
3 # Copyright (c) 2005 Linus Torvalds
4 # Copyright (c) 2005 Junio C Hamano
7 case "$0" in
8 *-revert* )
9 test -t 0 && edit=-e
10 replay=
11 me=revert
12 USAGE='[--edit | --no-edit] [-n] <commit-ish>' ;;
13 *-cherry-pick* )
14 replay=t
15 edit=
16 me=cherry-pick
17 USAGE='[--edit] [-n] [-r] [-x] <commit-ish>' ;;
18 * )
19 die "What are you talking about?" ;;
20 esac
21 . git-sh-setup
23 no_commit=
24 while case "$#" in 0) break ;; esac
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
31 -e|--e|--ed|--edi|--edit)
32 edit=-e
34 --n|--no|--no-|--no-e|--no-ed|--no-edi|--no-edit)
35 edit=
37 -r)
38 : no-op ;;
39 -x|--i-really-want-to-expose-my-private-commit-object-name)
40 replay=
42 -*)
43 usage
46 break
48 esac
49 shift
50 done
52 test "$me,$replay" = "revert,t" && usage
54 case "$no_commit" in
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."
62 head=$(git-rev-parse --verify HEAD) ||
63 die "You do not have a valid HEAD"
64 files=$(git-diff-index --cached --name-only $head) || exit
65 if [ "$files" ]; then
66 die "Dirty index: cannot $me (dirty: $files)"
69 esac
71 rev=$(git-rev-parse --verify "$@") &&
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."
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.
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
91 echo "This reverts commit $commit."
92 test "$rev" = "$commit" ||
93 echo "(original 'git revert' arguments: $@)"
94 base=$commit next=$prev
97 cherry-pick)
98 pick_author_script='
99 /^author /{
100 s/'\''/'\''\\'\'\''/g
102 s/^author \([^<]*\) <[^>]*> .*$/\1/
103 s/'\''/'\''\'\'\''/g
104 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
107 s/^author [^<]* <\([^>]*\)> .*$/\1/
108 s/'\''/'\''\'\'\''/g
109 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
112 s/^author [^<]* <[^>]*> \(.*\)$/\1/
113 s/'\''/'\''\'\'\''/g
114 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
118 set_author_env=`git-cat-file commit "$commit" |
119 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
120 eval "$set_author_env"
121 export GIT_AUTHOR_NAME
122 export GIT_AUTHOR_EMAIL
123 export GIT_AUTHOR_DATE
125 git-cat-file commit $commit | sed -e '1,/^$/d'
126 case "$replay" in
128 echo "(cherry picked from commit $commit)"
129 test "$rev" = "$commit" ||
130 echo "(original 'git cherry-pick' arguments: $@)"
132 esac
133 base=$prev next=$commit
136 esac >.msg
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).
143 echo >&2 "First trying simple merge strategy to $me."
144 git-read-tree -m -u --aggressive $base $head $next &&
145 result=$(git-write-tree 2>/dev/null) || {
146 echo >&2 "Simple $me fails; trying Automatic $me."
147 git-merge-index -o git-merge-one-file -a || {
148 mv -f .msg "$GIT_DIR/MERGE_MSG"
150 echo '
151 Conflicts:
153 git ls-files --unmerged |
154 sed -e 's/^[^ ]* / /' |
155 uniq
156 } >>"$GIT_DIR/MERGE_MSG"
157 echo >&2 "Automatic $me failed. After resolving the conflicts,"
158 echo >&2 "mark the corrected paths with 'git-add <paths>'"
159 echo >&2 "and commit the result."
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
168 result=$(git-write-tree) || exit
170 echo >&2 "Finished one $me."
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.
178 case "$no_commit" in
180 git-commit -n -F .msg $edit
181 rm -f .msg
183 esac