1#!/bin/sh 2# 3# Copyright (c) 2005 Linus Torvalds 4# Copyright (c) 2005 Junio C Hamano 5# 6. git-sh-setup|| die "Not a git archive" 7 8case"$0"in 9*-revert* ) 10 me=revert ;; 11*-cherry-pick* ) 12 me=cherry-pick;; 13* ) 14 die "What are ou talking about?";; 15esac 16 17usage () { 18case"$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;; 25esac 26} 27 28no_commit= replay= 29while case"$#"in0)break;;esac 30do 31case"$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*) 43break 44;; 45esac 46shift 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. 55head=$(git-write-tree)|| 56 die "Your index file is unmerged." 57;; 58*) 59 check_clean_tree || die "Cannot run$mefrom a dirty tree." 60head=$(git-rev-parse --verify HEAD)|| 61 die "You do not have a valid HEAD" 62;; 63esac 64 65rev=$(git-rev-parse --verify "$@")&& 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$mea root commit" 70git-rev-parse --verify"$commit^2">/dev/null 2>&1&& 71 die "Cannot run$mea 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| 81sed-e' 82 s/^[^ ]* /Revert "/ 83 s/$/"/' 84echo 85echo"This reverts$commitcommit." 86test"$rev"="$commit"|| 87echo"(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$commitcommit)" 122 test "$rev" = "$commit" || 123 echo "(original 'git cherry-pick' arguments: $@)" 124 ;; 125 esac 126 base=$prevnext=$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$prevon top of us (when reverting), or the change between 134#$prevand$commiton 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$mefails; trying Automatic$me." 140 git-merge-index -o git-merge-one-file -a || { 141 echo >&2 "Automatic$mefailed. 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'') 163 git-commit -n -F .msg 164 rm -f .msg 165 ;; 166esac