1#!/bin/sh 2# 3# Copyright (c) 2005 Linus Torvalds 4# Copyright (c) 2005 Junio C Hamano 5# 6. git-sh-setup-script|| die "Not a git archive" 7 8case"$0"in 9*-revert-* ) 10 me=revert ;; 11*-cherry-pick-* ) 12 me=cherry-pick;; 13esac 14 15usage () { 16case"$me"in 17 cherry-pick) 18 die "usage git$me[-n] [-r] <commit-ish>" 19;; 20 revert) 21 die "usage git$me[-n] <commit-ish>" 22;; 23esac 24} 25 26no_commit= replay= 27while case"$#"in0)break;;esac 28do 29case"$1"in 30-n|--n|--no|--no-|--no-c|--no-co|--no-com|--no-comm|\ 31--no-commi|--no-commit) 32 no_commit=t 33;; 34-r|--r|--re|--rep|--repl|--repla|--replay) 35 replay=t 36;; 37-*) 38 usage 39;; 40*) 41break 42;; 43esac 44shift 45done 46 47test"$me,$replay"="revert,t"&& usage 48 49case"$no_commit"in 50t) 51# We do not intend to commit immediately. We just want to 52# merge the differences in. 53head=$(git-write-tree)|| 54 die "Your index file is unmerged." 55;; 56*) 57 check_clean_tree || die "Cannot run$mefrom a dirty tree." 58head=$(git-rev-parse --verify HEAD)|| 59 die "You do not have a valid HEAD" 60;; 61esac 62 63rev=$(git-rev-parse --verify "$@")&& 64commit=$(git-rev-parse --verify "$rev^0")|| 65 die "Not a single commit $@" 66prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null)|| 67 die "Cannot run$mea root commit" 68git-rev-parse --verify"$commit^2">/dev/null 2>&1&& 69 die "Cannot run$mea multi-parent commit." 70 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. 75 76case"$me"in 77revert) 78 git-rev-list --pretty=oneline --max-count=1$commit| 79sed-e' 80 s/^[^ ]* /Revert "/ 81 s/$/"/' 82echo 83echo"This reverts$commitcommit." 84test"$rev"="$commit"|| 85echo"(original 'git revert' arguments: $@)" 86 base=$commit next=$prev 87;; 88 89cherry-pick) 90 pick_author_script=' 91 /^author /{ 92 h 93 s/^author \([^<]*\) <[^>]*> .*$/\1/ 94 s/'\''/'\''\'\'\''/g 95 s/.*/GIT_AUTHOR_NAME='\''&'\''/p 96 97 g 98 s/^author [^<]* <\([^>]*\)> .*$/\1/ 99 s/'\''/'\''\'\'\''/g 100 s/.*/GIT_AUTHOR_EMAIL='\''&'\''/p 101 102 g 103 s/^author [^<]* <[^>]*> \(.*\)$/\1/ 104 s/'\''/'\''\'\'\''/g 105 s/.*/GIT_AUTHOR_DATE='\''&'\''/p 106 107 q 108}' 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 115 116 git-cat-file commit$commit| sed -e '1,/^$/d' 117 case "$replay" in 118 '') 119 echo "(cherry picked from$commitcommit)" 120 test "$rev" = "$commit" || 121 echo "(original 'git cherry-pick' arguments: $@)" 122 ;; 123 esac 124 base=$prevnext=$commit 125 ;; 126 127esac >.msg 128 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$prevon top of us (when reverting), or the change between 132#$prevand$commiton top of us (when cherry-picking or replaying). 133 134echo >&2 "First trying simple merge strategy to$me." 135git-read-tree -m -u$base$head$next&& 136result=$(git-write-tree 2>/dev/null)|| { 137 echo >&2 "Simple$mefails; trying Automatic$me." 138 git-merge-cache -o git-merge-one-file-script -a || { 139 echo >&2 "Automatic$mefailed. After fixing it up," 140 echo >&2 "you can use\"git commit -F .msg\"" 141 case "$me" in 142 cherry-pick) 143 echo >&2 "You may choose to use the following when making" 144 echo >&2 "the commit:" 145 echo >&2 "$set_author_env" 146 esac 147 exit 1 148 } 149 result=$(git-write-tree)|| exit 150} 151echo >&2 "Finished one$me." 152 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. 158 159case "$no_commit" in 160'') 161 git-commit-script -n -F .msg 162 rm -f .msg 163 ;; 164esac