1#!/bin/sh 2# 3# Copyright (c) 2005 Linus Torvalds 4# Copyright (c) 2005 Junio C Hamano 5# 6 7case"$0"in 8*-revert* ) 9test -t0&& 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* ) 19echo>&2"What are you talking about?" 20exit1;; 21esac 22. git-sh-setup 23require_work_tree 24 25no_commit= 26while case"$#"in0)break;;esac 27do 28case"$1"in 29-n|--n|--no|--no-|--no-c|--no-co|--no-com|--no-comm|\ 30--no-commi|--no-commit) 31 no_commit=t 32;; 33-e|--e|--ed|--edi|--edit) 34 edit=-e 35;; 36--n|--no|--no-|--no-e|--no-ed|--no-edi|--no-edit) 37 edit= 38;; 39-r) 40: no-op;; 41-x|--i-really-want-to-expose-my-private-commit-object-name) 42 replay= 43;; 44-*) 45 usage 46;; 47*) 48break 49;; 50esac 51shift 52done 53 54test"$me,$replay"="revert,t"&& usage 55 56case"$no_commit"in 57t) 58# We do not intend to commit immediately. We just want to 59# merge the differences in. 60head=$(git-write-tree)|| 61 die "Your index file is unmerged." 62;; 63*) 64head=$(git-rev-parse --verify HEAD)|| 65 die "You do not have a valid HEAD" 66 files=$(git-diff-index --cached --name-only $head)||exit 67if["$files"];then 68 die "Dirty index: cannot$me(dirty:$files)" 69fi 70;; 71esac 72 73rev=$(git-rev-parse --verify "$@")&& 74commit=$(git-rev-parse --verify "$rev^0")|| 75 die "Not a single commit $@" 76prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null)|| 77 die "Cannot run$mea root commit" 78git-rev-parse --verify"$commit^2">/dev/null 2>&1&& 79 die "Cannot run$mea multi-parent commit." 80 81encoding=$(git repo-config i18n.commitencoding || echo UTF-8) 82 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. 87 88case"$me"in 89revert) 90 git show -s --pretty=oneline --encoding="$encoding"$commit| 91sed-e' 92 s/^[^ ]* /Revert "/ 93 s/$/"/ 94 ' 95echo 96echo"This reverts commit$commit." 97test"$rev"="$commit"|| 98echo"(original 'git revert' arguments: $@)" 99 base=$commit next=$prev 100;; 101 102cherry-pick) 103 pick_author_script=' 104 /^author /{ 105 s/'\''/'\''\\'\'\''/g 106 h 107 s/^author \([^<]*\) <[^>]*> .*$/\1/ 108 s/'\''/'\''\'\'\''/g 109 s/.*/GIT_AUTHOR_NAME='\''&'\''/p 110 111 g 112 s/^author [^<]* <\([^>]*\)> .*$/\1/ 113 s/'\''/'\''\'\'\''/g 114 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p 115 116 g 117 s/^author [^<]* <[^>]*> \(.*\)$/\1/ 118 s/'\''/'\''\'\'\''/g 119 s/.*/GIT_AUTHOR_DATE='\''&'\''/p 120 121 q 122}' 123 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 131 132 echo "$logmsg" | 133 sed -e '1,/^$/d' -e 's/^ //' 134 case "$replay" in 135 '') 136 echo "(cherry picked from commit$commit)" 137 test "$rev" = "$commit" || 138 echo "(original 'git cherry-pick' arguments: $@)" 139 ;; 140 esac 141 base=$prevnext=$commit 142 ;; 143 144esac >.msg 145 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$prevon top of us (when reverting), or the change between 149#$prevand$commiton top of us (when cherry-picking or replaying). 150 151echo >&2 "First trying simple merge strategy to$me." 152git-read-tree -m -u --aggressive$base$head$next&& 153result=$(git-write-tree 2>/dev/null)|| { 154 echo >&2 "Simple$mefails; trying Automatic$me." 155 git-merge-index -o git-merge-one-file -a || { 156 mv -f .msg "$GIT_DIR/MERGE_MSG" 157 { 158 echo ' 159Conflicts: 160' 161 git ls-files --unmerged | 162 sed -e 's/^[^ ]* / /' | 163 uniq 164 } >>"$GIT_DIR/MERGE_MSG" 165 echo >&2 "Automatic$mefailed. 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 175 } 176 result=$(git-write-tree)|| exit 177} 178echo >&2 "Finished one$me." 179 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. 185 186case "$no_commit" in 187'') 188 git-commit -n -F .msg$edit 189 rm -f .msg 190 ;; 191esac