]> git.ipfire.org Git - thirdparty/git.git/blame - git-revert.sh
GIT-VERSION-FILE: check ./version first.
[thirdparty/git.git] / git-revert.sh
CommitLineData
045f82cb 1#!/bin/sh
48313592
JH
2#
3# Copyright (c) 2005 Linus Torvalds
4# Copyright (c) 2005 Junio C Hamano
5#
045f82cb 6
48313592 7case "$0" in
215a7ad1 8*-revert* )
b7884981 9 test -t 0 && edit=-e
96779be4 10 replay=
4e7824b1
FK
11 me=revert
12 USAGE='[--edit | --no-edit] [-n] <commit-ish>' ;;
215a7ad1 13*-cherry-pick* )
96779be4 14 replay=t
b7884981 15 edit=
4e7824b1 16 me=cherry-pick
abd6970a 17 USAGE='[--edit] [-n] [-r] [-x] <commit-ish>' ;;
215a7ad1 18* )
397dfe67
BP
19 echo >&2 "What are you talking about?"
20 exit 1 ;;
48313592 21esac
533b7039
JH
22
23SUBDIRECTORY_OK=Yes ;# we will cd up
4e7824b1 24. git-sh-setup
7eff28a9 25require_work_tree
533b7039 26cd_to_toplevel
48313592 27
96779be4 28no_commit=
48313592
JH
29while case "$#" in 0) break ;; esac
30do
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
35 ;;
b7884981
JH
36 -e|--e|--ed|--edi|--edit)
37 edit=-e
38 ;;
674b2808 39 --n|--no|--no-|--no-e|--no-ed|--no-edi|--no-edit)
b7884981
JH
40 edit=
41 ;;
abd6970a
JH
42 -r)
43 : no-op ;;
44 -x|--i-really-want-to-expose-my-private-commit-object-name)
45 replay=
48313592
JH
46 ;;
47 -*)
48 usage
49 ;;
50 *)
51 break
52 ;;
53 esac
54 shift
55done
56
23913dc7
JH
57set_reflog_action "$me"
58
48313592
JH
59test "$me,$replay" = "revert,t" && usage
60
61case "$no_commit" in
62t)
63 # We do not intend to commit immediately. We just want to
64 # merge the differences in.
65 head=$(git-write-tree) ||
66 die "Your index file is unmerged."
67 ;;
045f82cb 68*)
48313592
JH
69 head=$(git-rev-parse --verify HEAD) ||
70 die "You do not have a valid HEAD"
e2f5f6ef
JH
71 files=$(git-diff-index --cached --name-only $head) || exit
72 if [ "$files" ]; then
73 die "Dirty index: cannot $me (dirty: $files)"
74 fi
48313592 75 ;;
045f82cb
JH
76esac
77
ff84d327 78rev=$(git-rev-parse --verify "$@") &&
48313592
JH
79commit=$(git-rev-parse --verify "$rev^0") ||
80 die "Not a single commit $@"
81prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null) ||
82 die "Cannot run $me a root commit"
83git-rev-parse --verify "$commit^2" >/dev/null 2>&1 &&
84 die "Cannot run $me a multi-parent commit."
85
e0d10e1c 86encoding=$(git config i18n.commitencoding || echo UTF-8)
5ac2715f 87
48313592
JH
88# "commit" is an existing commit. We would want to apply
89# the difference it introduces since its first parent "prev"
90# on top of the current HEAD if we are cherry-pick. Or the
91# reverse of it if we are revert.
92
93case "$me" in
94revert)
5ac2715f 95 git show -s --pretty=oneline --encoding="$encoding" $commit |
48313592
JH
96 sed -e '
97 s/^[^ ]* /Revert "/
5ac2715f
JH
98 s/$/"/
99 '
48313592 100 echo
869659a6 101 echo "This reverts commit $commit."
48313592
JH
102 test "$rev" = "$commit" ||
103 echo "(original 'git revert' arguments: $@)"
104 base=$commit next=$prev
105 ;;
106
107cherry-pick)
108 pick_author_script='
109 /^author /{
013049c9 110 s/'\''/'\''\\'\'\''/g
48313592
JH
111 h
112 s/^author \([^<]*\) <[^>]*> .*$/\1/
113 s/'\''/'\''\'\'\''/g
114 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
115
116 g
117 s/^author [^<]* <\([^>]*\)> .*$/\1/
118 s/'\''/'\''\'\'\''/g
119 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
120
121 g
122 s/^author [^<]* <[^>]*> \(.*\)$/\1/
123 s/'\''/'\''\'\'\''/g
124 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
125
126 q
127 }'
5ac2715f
JH
128
129 logmsg=`git show -s --pretty=raw --encoding="$encoding" "$commit"`
130 set_author_env=`echo "$logmsg" |
e3e291fc 131 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
48313592
JH
132 eval "$set_author_env"
133 export GIT_AUTHOR_NAME
134 export GIT_AUTHOR_EMAIL
135 export GIT_AUTHOR_DATE
136
5ac2715f
JH
137 echo "$logmsg" |
138 sed -e '1,/^$/d' -e 's/^ //'
48313592
JH
139 case "$replay" in
140 '')
abd6970a 141 echo "(cherry picked from commit $commit)"
48313592
JH
142 test "$rev" = "$commit" ||
143 echo "(original 'git cherry-pick' arguments: $@)"
144 ;;
145 esac
146 base=$prev next=$commit
147 ;;
148
149esac >.msg
150
6e2931a8
SP
151eval GITHEAD_$head=HEAD
152eval GITHEAD_$next='`git show -s \
153 --pretty=oneline --encoding="$encoding" "$commit" |
154 sed -e "s/^[^ ]* //"`'
155export GITHEAD_$head GITHEAD_$next
156
48313592
JH
157# This three way merge is an interesting one. We are at
158# $head, and would want to apply the change between $commit
159# and $prev on top of us (when reverting), or the change between
160# $prev and $commit on top of us (when cherry-picking or replaying).
161
acb4441e 162git-merge-recursive $base -- $head $next &&
48313592 163result=$(git-write-tree 2>/dev/null) || {
acb4441e
JH
164 mv -f .msg "$GIT_DIR/MERGE_MSG"
165 {
166 echo '
a9cb3c6e
LT
167Conflicts:
168'
169 git ls-files --unmerged |
170 sed -e 's/^[^ ]* / /' |
171 uniq
acb4441e
JH
172 } >>"$GIT_DIR/MERGE_MSG"
173 echo >&2 "Automatic $me failed. After resolving the conflicts,"
174 echo >&2 "mark the corrected paths with 'git-add <paths>'"
175 echo >&2 "and commit the result."
176 case "$me" in
177 cherry-pick)
48313592
JH
178 echo >&2 "You may choose to use the following when making"
179 echo >&2 "the commit:"
180 echo >&2 "$set_author_env"
acb4441e
JH
181 esac
182 exit 1
48313592
JH
183}
184echo >&2 "Finished one $me."
185
186# If we are cherry-pick, and if the merge did not result in
187# hand-editing, we will hit this commit and inherit the original
188# author date and name.
189# If we are revert, or if our cherry-pick results in a hand merge,
190# we had better say that the current user is responsible for that.
191
192case "$no_commit" in
193'')
b7884981 194 git-commit -n -F .msg $edit
48313592
JH
195 rm -f .msg
196 ;;
197esac