Merge refs/heads/master from paulus
[git/dscho.git] / git-revert-script
blob82ed0938078fb060b6925f3de4ca6d703137ed24
1 #!/bin/sh
3 # Copyright (c) 2005 Linus Torvalds
4 # Copyright (c) 2005 Junio C Hamano
6 . git-sh-setup-script || die "Not a git archive"
8 case "$0" in
9 *-revert-* )
10 me=revert ;;
11 *-cherry-pick-* )
12 me=cherry-pick ;;
13 esac
15 usage () {
16 case "$me" in
17 cherry-pick)
18 die "usage git $me [-n] [-r] <commit-ish>"
20 revert)
21 die "usage git $me [-n] <commit-ish>"
23 esac
26 no_commit= replay=
27 while case "$#" in 0) break ;; esac
29 case "$1" in
30 -n|--n|--no|--no-|--no-c|--no-co|--no-com|--no-comm|\
31 --no-commi|--no-commit)
32 no_commit=t
34 -r|--r|--re|--rep|--repl|--repla|--replay)
35 replay=t
37 -*)
38 usage
41 break
43 esac
44 shift
45 done
47 test "$me,$replay" = "revert,t" && usage
49 case "$no_commit" in
51 # We do not intend to commit immediately. We just want to
52 # merge the differences in.
53 head=$(git-write-tree) ||
54 die "Your index file is unmerged."
57 check_clean_tree || die "Cannot run $me from a dirty tree."
58 head=$(git-rev-parse --verify HEAD) ||
59 die "You do not have a valid HEAD"
61 esac
63 rev=$(git-rev-parse --verify "$@") &&
64 commit=$(git-rev-parse --verify "$rev^0") ||
65 die "Not a single commit $@"
66 prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null) ||
67 die "Cannot run $me a root commit"
68 git-rev-parse --verify "$commit^2" >/dev/null 2>&1 &&
69 die "Cannot run $me a multi-parent commit."
71 # "commit" is an existing commit. We would want to apply
72 # the difference it introduces since its first parent "prev"
73 # on top of the current HEAD if we are cherry-pick. Or the
74 # reverse of it if we are revert.
76 case "$me" in
77 revert)
78 git-rev-list --pretty=oneline --max-count=1 $commit |
79 sed -e '
80 s/^[^ ]* /Revert "/
81 s/$/"/'
82 echo
83 echo "This reverts $commit commit."
84 test "$rev" = "$commit" ||
85 echo "(original 'git revert' arguments: $@)"
86 base=$commit next=$prev
89 cherry-pick)
90 pick_author_script='
91 /^author /{
93 s/^author \([^<]*\) <[^>]*> .*$/\1/
94 s/'\''/'\''\'\'\''/g
95 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
98 s/^author [^<]* <\([^>]*\)> .*$/\1/
99 s/'\''/'\''\'\'\''/g
100 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
103 s/^author [^<]* <[^>]*> \(.*\)$/\1/
104 s/'\''/'\''\'\'\''/g
105 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
109 set_author_env=`git-cat-file commit "$commit" |
110 sed -ne "$pick_author_script"`
111 eval "$set_author_env"
112 export GIT_AUTHOR_NAME
113 export GIT_AUTHOR_EMAIL
114 export GIT_AUTHOR_DATE
116 git-cat-file commit $commit | sed -e '1,/^$/d'
117 case "$replay" in
119 echo "(cherry picked from $commit commit)"
120 test "$rev" = "$commit" ||
121 echo "(original 'git cherry-pick' arguments: $@)"
123 esac
124 base=$prev next=$commit
127 esac >.msg
129 # This three way merge is an interesting one. We are at
130 # $head, and would want to apply the change between $commit
131 # and $prev on top of us (when reverting), or the change between
132 # $prev and $commit on top of us (when cherry-picking or replaying).
134 echo >&2 "First trying simple merge strategy to $me."
135 git-read-tree -m -u $base $head $next &&
136 result=$(git-write-tree 2>/dev/null) || {
137 echo >&2 "Simple $me fails; trying Automatic $me."
138 git-merge-cache -o git-merge-one-file-script -a || {
139 echo >&2 "Automatic $me failed. After fixing it up,"
140 echo >&2 "you can use \"git commit -F .msg\""
141 case "$me" in
142 cherry-pick)
143 echo >&2 "You may choose to use the following when making"
144 echo >&2 "the commit:"
145 echo >&2 "$set_author_env"
146 esac
147 exit 1
149 result=$(git-write-tree) || exit
151 echo >&2 "Finished one $me."
153 # If we are cherry-pick, and if the merge did not result in
154 # hand-editing, we will hit this commit and inherit the original
155 # author date and name.
156 # If we are revert, or if our cherry-pick results in a hand merge,
157 # we had better say that the current user is responsible for that.
159 case "$no_commit" in
161 git-commit-script -n -F .msg
162 rm -f .msg
164 esac