Update draft release notes to 1.7.0
[git/jnareb-git/bp-gitweb.git] / contrib / examples / git-revert.sh
blob49f00321b28833c24ebb78ea2104f34091d43017
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 replay=
11 me=revert
12 USAGE='[--edit | --no-edit] [-n] <commit-ish>' ;;
13 *-cherry-pick* )
14 replay=t
15 edit=
16 me=cherry-pick
17 USAGE='[--edit] [-n] [-r] [-x] <commit-ish>' ;;
18 * )
19 echo >&2 "What are you talking about?"
20 exit 1 ;;
21 esac
23 SUBDIRECTORY_OK=Yes ;# we will cd up
24 . git-sh-setup
25 require_work_tree
26 cd_to_toplevel
28 no_commit=
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 -e|--e|--ed|--edi|--edit)
37 edit=-e
39 --n|--no|--no-|--no-e|--no-ed|--no-edi|--no-edit)
40 edit=
42 -r)
43 : no-op ;;
44 -x|--i-really-want-to-expose-my-private-commit-object-name)
45 replay=
47 -*)
48 usage
51 break
53 esac
54 shift
55 done
57 set_reflog_action "$me"
59 test "$me,$replay" = "revert,t" && usage
61 case "$no_commit" in
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."
69 head=$(git-rev-parse --verify HEAD) ||
70 die "You do not have a valid HEAD"
71 files=$(git-diff-index --cached --name-only $head) || exit
72 if [ "$files" ]; then
73 die "Dirty index: cannot $me (dirty: $files)"
76 esac
78 rev=$(git-rev-parse --verify "$@") &&
79 commit=$(git-rev-parse --verify "$rev^0") ||
80 die "Not a single commit $@"
81 prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null) ||
82 die "Cannot run $me a root commit"
83 git-rev-parse --verify "$commit^2" >/dev/null 2>&1 &&
84 die "Cannot run $me a multi-parent commit."
86 encoding=$(git config i18n.commitencoding || echo UTF-8)
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.
93 case "$me" in
94 revert)
95 git show -s --pretty=oneline --encoding="$encoding" $commit |
96 sed -e '
97 s/^[^ ]* /Revert "/
98 s/$/"/
100 echo
101 echo "This reverts commit $commit."
102 test "$rev" = "$commit" ||
103 echo "(original 'git revert' arguments: $@)"
104 base=$commit next=$prev
107 cherry-pick)
108 pick_author_script='
109 /^author /{
110 s/'\''/'\''\\'\'\''/g
112 s/^author \([^<]*\) <[^>]*> .*$/\1/
113 s/'\''/'\''\'\'\''/g
114 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
117 s/^author [^<]* <\([^>]*\)> .*$/\1/
118 s/'\''/'\''\'\'\''/g
119 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
122 s/^author [^<]* <[^>]*> \(.*\)$/\1/
123 s/'\''/'\''\'\'\''/g
124 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
129 logmsg=`git show -s --pretty=raw --encoding="$encoding" "$commit"`
130 set_author_env=`echo "$logmsg" |
131 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
132 eval "$set_author_env"
133 export GIT_AUTHOR_NAME
134 export GIT_AUTHOR_EMAIL
135 export GIT_AUTHOR_DATE
137 echo "$logmsg" |
138 sed -e '1,/^$/d' -e 's/^ //'
139 case "$replay" in
141 echo "(cherry picked from commit $commit)"
142 test "$rev" = "$commit" ||
143 echo "(original 'git cherry-pick' arguments: $@)"
145 esac
146 base=$prev next=$commit
149 esac >.msg
151 eval GITHEAD_$head=HEAD
152 eval GITHEAD_$next='`git show -s \
153 --pretty=oneline --encoding="$encoding" "$commit" |
154 sed -e "s/^[^ ]* //"`'
155 export GITHEAD_$head GITHEAD_$next
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).
162 git-merge-recursive $base -- $head $next &&
163 result=$(git-write-tree 2>/dev/null) || {
164 mv -f .msg "$GIT_DIR/MERGE_MSG"
166 echo '
167 Conflicts:
169 git ls-files --unmerged |
170 sed -e 's/^[^ ]* / /' |
171 uniq
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)
178 echo >&2 "You may choose to use the following when making"
179 echo >&2 "the commit:"
180 echo >&2 "$set_author_env"
181 esac
182 exit 1
184 echo >&2 "Finished one $me."
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.
192 case "$no_commit" in
194 git-commit -n -F .msg $edit
195 rm -f .msg
197 esac