]> git.ipfire.org Git - thirdparty/git.git/blame - git-revert.sh
Use cd_to_toplevel in scripts that implement it by hand.
[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
4e7824b1 22. git-sh-setup
7eff28a9 23require_work_tree
48313592 24
96779be4 25no_commit=
48313592
JH
26while case "$#" in 0) break ;; esac
27do
28 case "$1" in
29 -n|--n|--no|--no-|--no-c|--no-co|--no-com|--no-comm|\
30 --no-commi|--no-commit)
31 no_commit=t
32 ;;
b7884981
JH
33 -e|--e|--ed|--edi|--edit)
34 edit=-e
35 ;;
674b2808 36 --n|--no|--no-|--no-e|--no-ed|--no-edi|--no-edit)
b7884981
JH
37 edit=
38 ;;
abd6970a
JH
39 -r)
40 : no-op ;;
41 -x|--i-really-want-to-expose-my-private-commit-object-name)
42 replay=
48313592
JH
43 ;;
44 -*)
45 usage
46 ;;
47 *)
48 break
49 ;;
50 esac
51 shift
52done
53
54test "$me,$replay" = "revert,t" && usage
55
56case "$no_commit" in
57t)
58 # We do not intend to commit immediately. We just want to
59 # merge the differences in.
60 head=$(git-write-tree) ||
61 die "Your index file is unmerged."
62 ;;
045f82cb 63*)
48313592
JH
64 head=$(git-rev-parse --verify HEAD) ||
65 die "You do not have a valid HEAD"
e2f5f6ef
JH
66 files=$(git-diff-index --cached --name-only $head) || exit
67 if [ "$files" ]; then
68 die "Dirty index: cannot $me (dirty: $files)"
69 fi
48313592 70 ;;
045f82cb
JH
71esac
72
ff84d327 73rev=$(git-rev-parse --verify "$@") &&
48313592
JH
74commit=$(git-rev-parse --verify "$rev^0") ||
75 die "Not a single commit $@"
76prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null) ||
77 die "Cannot run $me a root commit"
78git-rev-parse --verify "$commit^2" >/dev/null 2>&1 &&
79 die "Cannot run $me a multi-parent commit."
80
81# "commit" is an existing commit. We would want to apply
82# the difference it introduces since its first parent "prev"
83# on top of the current HEAD if we are cherry-pick. Or the
84# reverse of it if we are revert.
85
86case "$me" in
87revert)
88 git-rev-list --pretty=oneline --max-count=1 $commit |
89 sed -e '
90 s/^[^ ]* /Revert "/
91 s/$/"/'
92 echo
869659a6 93 echo "This reverts commit $commit."
48313592
JH
94 test "$rev" = "$commit" ||
95 echo "(original 'git revert' arguments: $@)"
96 base=$commit next=$prev
97 ;;
98
99cherry-pick)
100 pick_author_script='
101 /^author /{
013049c9 102 s/'\''/'\''\\'\'\''/g
48313592
JH
103 h
104 s/^author \([^<]*\) <[^>]*> .*$/\1/
105 s/'\''/'\''\'\'\''/g
106 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
107
108 g
109 s/^author [^<]* <\([^>]*\)> .*$/\1/
110 s/'\''/'\''\'\'\''/g
111 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
112
113 g
114 s/^author [^<]* <[^>]*> \(.*\)$/\1/
115 s/'\''/'\''\'\'\''/g
116 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
117
118 q
119 }'
120 set_author_env=`git-cat-file commit "$commit" |
e3e291fc 121 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
48313592
JH
122 eval "$set_author_env"
123 export GIT_AUTHOR_NAME
124 export GIT_AUTHOR_EMAIL
125 export GIT_AUTHOR_DATE
126
127 git-cat-file commit $commit | sed -e '1,/^$/d'
128 case "$replay" in
129 '')
abd6970a 130 echo "(cherry picked from commit $commit)"
48313592
JH
131 test "$rev" = "$commit" ||
132 echo "(original 'git cherry-pick' arguments: $@)"
133 ;;
134 esac
135 base=$prev next=$commit
136 ;;
137
138esac >.msg
139
140# This three way merge is an interesting one. We are at
141# $head, and would want to apply the change between $commit
142# and $prev on top of us (when reverting), or the change between
143# $prev and $commit on top of us (when cherry-picking or replaying).
144
145echo >&2 "First trying simple merge strategy to $me."
d1802851 146git-read-tree -m -u --aggressive $base $head $next &&
48313592
JH
147result=$(git-write-tree 2>/dev/null) || {
148 echo >&2 "Simple $me fails; trying Automatic $me."
215a7ad1 149 git-merge-index -o git-merge-one-file -a || {
a9cb3c6e
LT
150 mv -f .msg "$GIT_DIR/MERGE_MSG"
151 {
152 echo '
153Conflicts:
154'
155 git ls-files --unmerged |
156 sed -e 's/^[^ ]* / /' |
157 uniq
158 } >>"$GIT_DIR/MERGE_MSG"
0f73e92a 159 echo >&2 "Automatic $me failed. After resolving the conflicts,"
64646d11 160 echo >&2 "mark the corrected paths with 'git-add <paths>'"
a9cb3c6e 161 echo >&2 "and commit the result."
48313592
JH
162 case "$me" in
163 cherry-pick)
164 echo >&2 "You may choose to use the following when making"
165 echo >&2 "the commit:"
166 echo >&2 "$set_author_env"
167 esac
168 exit 1
169 }
170 result=$(git-write-tree) || exit
171}
172echo >&2 "Finished one $me."
173
174# If we are cherry-pick, and if the merge did not result in
175# hand-editing, we will hit this commit and inherit the original
176# author date and name.
177# If we are revert, or if our cherry-pick results in a hand merge,
178# we had better say that the current user is responsible for that.
179
180case "$no_commit" in
181'')
b7884981 182 git-commit -n -F .msg $edit
48313592
JH
183 rm -f .msg
184 ;;
185esac