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