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