contrib: resurrect scripted git-revert.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Junio C Hamano committed
Dec 26, 2007 at 17:38 UTC
e25cfae6677423d05a81c0862f58d3319141794c
1 file changed
+197
contrib/examples/git-revert.sh
new
+197
@@ -0,0 +1,197 @@
1
+#!/bin/sh
2
+#
3
+# Copyright (c) 2005 Linus Torvalds
4
+# Copyright (c) 2005 Junio C Hamano
5
+#
6
+
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
+
23
+SUBDIRECTORY_OK=Yes ;# we will cd up
24
+. git-sh-setup
25
+require_work_tree
26
+cd_to_toplevel
27
+
28
+no_commit=
29
+while case "$#" in 0) break ;; esac
30
+do
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
+ ;;
36
+ -e|--e|--ed|--edi|--edit)
37
+ edit=-e
38
+ ;;
39
+ --n|--no|--no-|--no-e|--no-ed|--no-edi|--no-edit)
40
+ edit=
41
+ ;;
42
+ -r)
43
+ : no-op ;;
44
+ -x|--i-really-want-to-expose-my-private-commit-object-name)
45
+ replay=
46
+ ;;
47
+ -*)
48
+ usage
49
+ ;;
50
+ *)
51
+ break
52
+ ;;
53
+ esac
54
+ shift
55
+done
56
+
57
+set_reflog_action "$me"
58
+
59
+test "$me,$replay" = "revert,t" && usage
60
+
61
+case "$no_commit" in
62
+t)
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
+ ;;
68
+*)
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)"
74
+ fi
75
+ ;;
76
+esac
77
+
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."
85
+
86
+encoding=$(git config i18n.commitencoding || echo UTF-8)
87
+
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
+
93
+case "$me" in
94
+revert)
95
+ git show -s --pretty=oneline --encoding="$encoding" $commit |
96
+ sed -e '
97
+ s/^[^ ]* /Revert "/
98
+ s/$/"/
99
+ '
100
+ echo
101
+ echo "This reverts commit $commit."
102
+ test "$rev" = "$commit" ||
103
+ echo "(original 'git revert' arguments: $@)"
104
+ base=$commit next=$prev
105
+ ;;
106
+
107
+cherry-pick)
108
+ pick_author_script='
109
+ /^author /{
110
+ s/'\''/'\''\\'\'\''/g
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
+ }'
128
+
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
136
+
137
+ echo "$logmsg" |
138
+ sed -e '1,/^$/d' -e 's/^ //'
139
+ case "$replay" in
140
+ '')
141
+ echo "(cherry picked from commit $commit)"
142
+ test "$rev" = "$commit" ||
143
+ echo "(original 'git cherry-pick' arguments: $@)"
144
+ ;;
145
+ esac
146
+ base=$prev next=$commit
147
+ ;;
148
+
149
+esac >.msg
150
+
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
156
+
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
+
162
+git-merge-recursive $base -- $head $next &&
163
+result=$(git-write-tree 2>/dev/null) || {
164
+ mv -f .msg "$GIT_DIR/MERGE_MSG"
165
+ {
166
+ echo '
167
+Conflicts:
168
+'
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
183
+}
184
+echo >&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
+
192
+case "$no_commit" in
193
+'')
194
+ git-commit -n -F .msg $edit
195
+ rm -f .msg
196
+ ;;
197
+esac