3 # Copyright (c) 2005 Linus Torvalds
4 # Copyright (c) 2005 Junio C Hamano
6 . git-sh-setup-script || die "Not a git archive"
18 die "usage git $me [-n] [-r] <commit-ish>"
21 die "usage git $me [-n] <commit-ish>"
27 while case "$#" in 0) break ;; esac
30 -n|--n|--no|--no-|--no-c|--no-co|--no-com|--no-comm|\
31 --no-commi|--no-commit)
34 -r|--r|--re|--rep|--repl|--repla|--replay)
47 test "$me,$replay" = "revert,t" && usage
51 # We do not intend to commit immediately. We just want to
52 # merge the differences in.
53 head=$(git-write-tree) ||
54 die "Your index file is unmerged."
57 check_clean_tree || die "Cannot run $me from a dirty tree."
58 head=$(git-rev-parse --verify HEAD) ||
59 die "You do not have a valid HEAD"
63 rev=$(git-rev-parse --verify "$@") &&
64 commit=$(git-rev-parse --verify "$rev^0") ||
65 die "Not a single commit $@"
66 prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null) ||
67 die "Cannot run $me a root commit"
68 git-rev-parse --verify "$commit^2" >/dev/null 2>&1 &&
69 die "Cannot run $me a multi-parent commit."
71 # "commit" is an existing commit. We would want to apply
72 # the difference it introduces since its first parent "prev"
73 # on top of the current HEAD if we are cherry-pick. Or the
74 # reverse of it if we are revert.
78 git-rev-list --pretty=oneline --max-count=1 $commit |
83 echo "This reverts $commit commit."
84 test "$rev" = "$commit" ||
85 echo "(original 'git revert' arguments: $@)"
86 base=$commit next=$prev
93 s/^author \([^<]*\) <[^>]*> .*$/\1/
95 s/.*/GIT_AUTHOR_NAME='\''&'\''/p
98 s/^author [^<]* <\([^>]*\)> .*$/\1/
100 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p
103 s/^author [^<]* <[^>]*> \(.*\)$/\1/
105 s/.*/GIT_AUTHOR_DATE='\''&'\''/p
109 set_author_env=`git-cat-file commit "$commit" |
110 sed -ne "$pick_author_script"`
111 eval "$set_author_env"
112 export GIT_AUTHOR_NAME
113 export GIT_AUTHOR_EMAIL
114 export GIT_AUTHOR_DATE
116 git-cat-file commit $commit | sed -e '1,/^$/d'
119 echo "(cherry picked from $commit commit)"
120 test "$rev" = "$commit" ||
121 echo "(original 'git cherry-pick' arguments: $@)"
124 base=$prev next=$commit
129 # This three way merge is an interesting one. We are at
130 # $head, and would want to apply the change between $commit
131 # and $prev on top of us (when reverting), or the change between
132 # $prev and $commit on top of us (when cherry-picking or replaying).
134 echo >&2 "First trying simple merge strategy to $me."
135 git-read-tree -m -u $base $head $next &&
136 result=$(git-write-tree 2>/dev/null) || {
137 echo >&2 "Simple $me fails; trying Automatic $me."
138 git-merge-cache -o git-merge-one-file-script -a || {
139 echo >&2 "Automatic $me failed. After fixing it up,"
140 echo >&2 "you can use \"git commit -F .msg\""
143 echo >&2 "You may choose to use the following when making"
144 echo >&2 "the commit:"
145 echo >&2 "$set_author_env"
149 result=$(git-write-tree) || exit
151 echo >&2 "Finished one $me."
153 # If we are cherry-pick, and if the merge did not result in
154 # hand-editing, we will hit this commit and inherit the original
155 # author date and name.
156 # If we are revert, or if our cherry-pick results in a hand merge,
157 # we had better say that the current user is responsible for that.
161 git-commit-script -n -F .msg