Consistent message encoding while reusing log from an existing commit.
[git/gitweb.git] / git-revert.sh
blobfcbefb4e688317ebbd6563da898ff6010e8b660b
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
22 . git-sh-setup
23 require_work_tree
25 no_commit=
26 while case "$#" in 0) break ;; esac
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
33 -e|--e|--ed|--edi|--edit)
34 edit=-e
36 --n|--no|--no-|--no-e|--no-ed|--no-edi|--no-edit)
37 edit=
39 -r)
40 : no-op ;;
41 -x|--i-really-want-to-expose-my-private-commit-object-name)
42 replay=
44 -*)
45 usage
48 break
50 esac
51 shift
52 done
54 test "$me,$replay" = "revert,t" && usage
56 case "$no_commit" in
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."
64 head=$(git-rev-parse --verify HEAD) ||
65 die "You do not have a valid HEAD"
66 files=$(git-diff-index --cached --name-only $head) || exit
67 if [ "$files" ]; then
68 die "Dirty index: cannot $me (dirty: $files)"
71 esac
73 rev=$(git-rev-parse --verify "$@") &&
74 commit=$(git-rev-parse --verify "$rev^0") ||
75 die "Not a single commit $@"
76 prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null) ||
77 die "Cannot run $me a root commit"
78 git-rev-parse --verify "$commit^2" >/dev/null 2>&1 &&
79 die "Cannot run $me a multi-parent commit."
81 encoding=$(git repo-config i18n.commitencoding || echo UTF-8)
83 # "commit" is an existing commit. We would want to apply
84 # the difference it introduces since its first parent "prev"
85 # on top of the current HEAD if we are cherry-pick. Or the
86 # reverse of it if we are revert.
88 case "$me" in
89 revert)
90 git show -s --pretty=oneline --encoding="$encoding" $commit |
91 sed -e '
92 s/^[^ ]* /Revert "/
93 s/$/"/
95 echo
96 echo "This reverts commit $commit."
97 test "$rev" = "$commit" ||
98 echo "(original 'git revert' arguments: $@)"
99 base=$commit next=$prev
102 cherry-pick)
103 pick_author_script='
104 /^author /{
105 s/'\''/'\''\\'\'\''/g
107 s/^author \([^<]*\) <[^>]*> .*$/\1/
108 s/'\''/'\''\'\'\''/g
109 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
112 s/^author [^<]* <\([^>]*\)> .*$/\1/
113 s/'\''/'\''\'\'\''/g
114 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
117 s/^author [^<]* <[^>]*> \(.*\)$/\1/
118 s/'\''/'\''\'\'\''/g
119 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
124 logmsg=`git show -s --pretty=raw --encoding="$encoding" "$commit"`
125 set_author_env=`echo "$logmsg" |
126 LANG=C LC_ALL=C sed -ne "$pick_author_script"`
127 eval "$set_author_env"
128 export GIT_AUTHOR_NAME
129 export GIT_AUTHOR_EMAIL
130 export GIT_AUTHOR_DATE
132 echo "$logmsg" |
133 sed -e '1,/^$/d' -e 's/^ //'
134 case "$replay" in
136 echo "(cherry picked from commit $commit)"
137 test "$rev" = "$commit" ||
138 echo "(original 'git cherry-pick' arguments: $@)"
140 esac
141 base=$prev next=$commit
144 esac >.msg
146 # This three way merge is an interesting one. We are at
147 # $head, and would want to apply the change between $commit
148 # and $prev on top of us (when reverting), or the change between
149 # $prev and $commit on top of us (when cherry-picking or replaying).
151 echo >&2 "First trying simple merge strategy to $me."
152 git-read-tree -m -u --aggressive $base $head $next &&
153 result=$(git-write-tree 2>/dev/null) || {
154 echo >&2 "Simple $me fails; trying Automatic $me."
155 git-merge-index -o git-merge-one-file -a || {
156 mv -f .msg "$GIT_DIR/MERGE_MSG"
158 echo '
159 Conflicts:
161 git ls-files --unmerged |
162 sed -e 's/^[^ ]* / /' |
163 uniq
164 } >>"$GIT_DIR/MERGE_MSG"
165 echo >&2 "Automatic $me failed. After resolving the conflicts,"
166 echo >&2 "mark the corrected paths with 'git-add <paths>'"
167 echo >&2 "and commit the result."
168 case "$me" in
169 cherry-pick)
170 echo >&2 "You may choose to use the following when making"
171 echo >&2 "the commit:"
172 echo >&2 "$set_author_env"
173 esac
174 exit 1
176 result=$(git-write-tree) || exit
178 echo >&2 "Finished one $me."
180 # If we are cherry-pick, and if the merge did not result in
181 # hand-editing, we will hit this commit and inherit the original
182 # author date and name.
183 # If we are revert, or if our cherry-pick results in a hand merge,
184 # we had better say that the current user is responsible for that.
186 case "$no_commit" in
188 git-commit -n -F .msg $edit
189 rm -f .msg
191 esac