gitweb: [commit view] Do not suppress commitdiff link in root commit
[git/debian.git] / git-revert.sh
blob0784f74c18c4a5a2c34a37c16ba1ad094cd41a06
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 me=revert
11 USAGE='[--edit | --no-edit] [-n] <commit-ish>' ;;
12 *-cherry-pick* )
13 edit=
14 me=cherry-pick
15 USAGE='[--edit] [-n] [-r] [-x] <commit-ish>' ;;
16 * )
17 die "What are you talking about?" ;;
18 esac
19 . git-sh-setup
21 no_commit= replay=t
22 while case "$#" in 0) break ;; esac
24 case "$1" in
25 -n|--n|--no|--no-|--no-c|--no-co|--no-com|--no-comm|\
26 --no-commi|--no-commit)
27 no_commit=t
29 -e|--e|--ed|--edi|--edit)
30 edit=-e
32 --n|--no|--no-|--no-e|--no-ed|--no-edi|--no-edit)
33 edit=
35 -r)
36 : no-op ;;
37 -x|--i-really-want-to-expose-my-private-commit-object-name)
38 replay=
40 -*)
41 usage
44 break
46 esac
47 shift
48 done
50 test "$me,$replay" = "revert,t" && usage
52 case "$no_commit" in
54 # We do not intend to commit immediately. We just want to
55 # merge the differences in.
56 head=$(git-write-tree) ||
57 die "Your index file is unmerged."
60 head=$(git-rev-parse --verify HEAD) ||
61 die "You do not have a valid HEAD"
62 files=$(git-diff-index --cached --name-only $head) || exit
63 if [ "$files" ]; then
64 die "Dirty index: cannot $me (dirty: $files)"
67 esac
69 rev=$(git-rev-parse --verify "$@") &&
70 commit=$(git-rev-parse --verify "$rev^0") ||
71 die "Not a single commit $@"
72 prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null) ||
73 die "Cannot run $me a root commit"
74 git-rev-parse --verify "$commit^2" >/dev/null 2>&1 &&
75 die "Cannot run $me a multi-parent commit."
77 # "commit" is an existing commit. We would want to apply
78 # the difference it introduces since its first parent "prev"
79 # on top of the current HEAD if we are cherry-pick. Or the
80 # reverse of it if we are revert.
82 case "$me" in
83 revert)
84 git-rev-list --pretty=oneline --max-count=1 $commit |
85 sed -e '
86 s/^[^ ]* /Revert "/
87 s/$/"/'
88 echo
89 echo "This reverts commit $commit."
90 test "$rev" = "$commit" ||
91 echo "(original 'git revert' arguments: $@)"
92 base=$commit next=$prev
95 cherry-pick)
96 pick_author_script='
97 /^author /{
98 s/'\''/'\''\\'\'\''/g
100 s/^author \([^<]*\) <[^>]*> .*$/\1/
101 s/'\''/'\''\'\'\''/g
102 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
105 s/^author [^<]* <\([^>]*\)> .*$/\1/
106 s/'\''/'\''\'\'\''/g
107 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
110 s/^author [^<]* <[^>]*> \(.*\)$/\1/
111 s/'\''/'\''\'\'\''/g
112 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
116 set_author_env=`git-cat-file commit "$commit" |
117 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
118 eval "$set_author_env"
119 export GIT_AUTHOR_NAME
120 export GIT_AUTHOR_EMAIL
121 export GIT_AUTHOR_DATE
123 git-cat-file commit $commit | sed -e '1,/^$/d'
124 case "$replay" in
126 echo "(cherry picked from commit $commit)"
127 test "$rev" = "$commit" ||
128 echo "(original 'git cherry-pick' arguments: $@)"
130 esac
131 base=$prev next=$commit
134 esac >.msg
136 # This three way merge is an interesting one. We are at
137 # $head, and would want to apply the change between $commit
138 # and $prev on top of us (when reverting), or the change between
139 # $prev and $commit on top of us (when cherry-picking or replaying).
141 echo >&2 "First trying simple merge strategy to $me."
142 git-read-tree -m -u --aggressive $base $head $next &&
143 result=$(git-write-tree 2>/dev/null) || {
144 echo >&2 "Simple $me fails; trying Automatic $me."
145 git-merge-index -o git-merge-one-file -a || {
146 echo >&2 "Automatic $me failed. After resolving the conflicts,"
147 echo >&2 "mark the corrected paths with 'git-update-index <paths>'"
148 echo >&2 "and commit with 'git commit -F .msg'"
149 case "$me" in
150 cherry-pick)
151 echo >&2 "You may choose to use the following when making"
152 echo >&2 "the commit:"
153 echo >&2 "$set_author_env"
154 esac
155 exit 1
157 result=$(git-write-tree) || exit
159 echo >&2 "Finished one $me."
161 # If we are cherry-pick, and if the merge did not result in
162 # hand-editing, we will hit this commit and inherit the original
163 # author date and name.
164 # If we are revert, or if our cherry-pick results in a hand merge,
165 # we had better say that the current user is responsible for that.
167 case "$no_commit" in
169 git-commit -n -F .msg $edit
170 rm -f .msg
172 esac