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