Teach rsync transport about alternates.
[alt-git.git] / git-revert.sh
blob722c4f755af5ea07981712d8abf6d6eed81c5d56
1 #!/bin/sh
3 # Copyright (c) 2005 Linus Torvalds
4 # Copyright (c) 2005 Junio C Hamano
6 . git-sh-setup || die "Not a git archive"
8 case "$0" in
9 *-revert* )
10 me=revert ;;
11 *-cherry-pick* )
12 me=cherry-pick ;;
13 * )
14 die "What are ou talking about?" ;;
15 esac
17 usage () {
18 case "$me" in
19 cherry-pick)
20 die "usage git $me [-n] [-r] <commit-ish>"
22 revert)
23 die "usage git $me [-n] <commit-ish>"
25 esac
28 no_commit= replay=
29 while case "$#" in 0) break ;; esac
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
36 -r|--r|--re|--rep|--repl|--repla|--replay)
37 replay=t
39 -*)
40 usage
43 break
45 esac
46 shift
47 done
49 test "$me,$replay" = "revert,t" && usage
51 case "$no_commit" in
53 # We do not intend to commit immediately. We just want to
54 # merge the differences in.
55 head=$(git-write-tree) ||
56 die "Your index file is unmerged."
59 check_clean_tree || die "Cannot run $me from a dirty tree."
60 head=$(git-rev-parse --verify HEAD) ||
61 die "You do not have a valid HEAD"
63 esac
65 rev=$(git-rev-parse --verify "$@") &&
66 commit=$(git-rev-parse --verify "$rev^0") ||
67 die "Not a single commit $@"
68 prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null) ||
69 die "Cannot run $me a root commit"
70 git-rev-parse --verify "$commit^2" >/dev/null 2>&1 &&
71 die "Cannot run $me a multi-parent commit."
73 # "commit" is an existing commit. We would want to apply
74 # the difference it introduces since its first parent "prev"
75 # on top of the current HEAD if we are cherry-pick. Or the
76 # reverse of it if we are revert.
78 case "$me" in
79 revert)
80 git-rev-list --pretty=oneline --max-count=1 $commit |
81 sed -e '
82 s/^[^ ]* /Revert "/
83 s/$/"/'
84 echo
85 echo "This reverts $commit commit."
86 test "$rev" = "$commit" ||
87 echo "(original 'git revert' arguments: $@)"
88 base=$commit next=$prev
91 cherry-pick)
92 pick_author_script='
93 /^author /{
95 s/^author \([^<]*\) <[^>]*> .*$/\1/
96 s/'\''/'\''\'\'\''/g
97 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
100 s/^author [^<]* <\([^>]*\)> .*$/\1/
101 s/'\''/'\''\'\'\''/g
102 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
105 s/^author [^<]* <[^>]*> \(.*\)$/\1/
106 s/'\''/'\''\'\'\''/g
107 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
111 set_author_env=`git-cat-file commit "$commit" |
112 sed -ne "$pick_author_script"`
113 eval "$set_author_env"
114 export GIT_AUTHOR_NAME
115 export GIT_AUTHOR_EMAIL
116 export GIT_AUTHOR_DATE
118 git-cat-file commit $commit | sed -e '1,/^$/d'
119 case "$replay" in
121 echo "(cherry picked from $commit commit)"
122 test "$rev" = "$commit" ||
123 echo "(original 'git cherry-pick' arguments: $@)"
125 esac
126 base=$prev next=$commit
129 esac >.msg
131 # This three way merge is an interesting one. We are at
132 # $head, and would want to apply the change between $commit
133 # and $prev on top of us (when reverting), or the change between
134 # $prev and $commit on top of us (when cherry-picking or replaying).
136 echo >&2 "First trying simple merge strategy to $me."
137 git-read-tree -m -u $base $head $next &&
138 result=$(git-write-tree 2>/dev/null) || {
139 echo >&2 "Simple $me fails; trying Automatic $me."
140 git-merge-index -o git-merge-one-file -a || {
141 echo >&2 "Automatic $me failed. After fixing it up,"
142 echo >&2 "you can use \"git commit -F .msg\""
143 case "$me" in
144 cherry-pick)
145 echo >&2 "You may choose to use the following when making"
146 echo >&2 "the commit:"
147 echo >&2 "$set_author_env"
148 esac
149 exit 1
151 result=$(git-write-tree) || exit
153 echo >&2 "Finished one $me."
155 # If we are cherry-pick, and if the merge did not result in
156 # hand-editing, we will hit this commit and inherit the original
157 # author date and name.
158 # If we are revert, or if our cherry-pick results in a hand merge,
159 # we had better say that the current user is responsible for that.
161 case "$no_commit" in
163 git-commit -n -F .msg
164 rm -f .msg
166 esac