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 23SUBDIRECTORY_OK=Yes ;# we will cd up 24. git-sh-setup 25require_work_tree 26cd_to_toplevel 27 28no_commit= 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-e|--e|--ed|--edi|--edit) 37 edit=-e 38;; 39--n|--no|--no-|--no-e|--no-ed|--no-edi|--no-edit) 40 edit= 41;; 42-r) 43: no-op;; 44-x|--i-really-want-to-expose-my-private-commit-object-name) 45 replay= 46;; 47-*) 48 usage 49;; 50*) 51break 52;; 53esac 54shift 55done 56 57test"$me,$replay"="revert,t"&& usage 58 59case"$no_commit"in 60t) 61# We do not intend to commit immediately. We just want to 62# merge the differences in. 63head=$(git-write-tree)|| 64 die "Your index file is unmerged." 65;; 66*) 67head=$(git-rev-parse --verify HEAD)|| 68 die "You do not have a valid HEAD" 69 files=$(git-diff-index --cached --name-only $head)||exit 70if["$files"];then 71 die "Dirty index: cannot$me(dirty:$files)" 72fi 73;; 74esac 75 76rev=$(git-rev-parse --verify "$@")&& 77commit=$(git-rev-parse --verify "$rev^0")|| 78 die "Not a single commit $@" 79prev=$(git-rev-parse --verify "$commit^1" 2>/dev/null)|| 80 die "Cannot run$mea root commit" 81git-rev-parse --verify"$commit^2">/dev/null 2>&1&& 82 die "Cannot run$mea multi-parent commit." 83 84# "commit" is an existing commit. We would want to apply 85# the difference it introduces since its first parent "prev" 86# on top of the current HEAD if we are cherry-pick. Or the 87# reverse of it if we are revert. 88 89case"$me"in 90revert) 91 git-rev-list --pretty=oneline --max-count=1$commit| 92sed-e' 93 s/^[^ ]* /Revert "/ 94 s/$/"/' 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 set_author_env=`git-cat-file commit "$commit" | 124 LANG=C LC_ALL=C sed -ne "$pick_author_script"` 125 eval "$set_author_env" 126 export GIT_AUTHOR_NAME 127 export GIT_AUTHOR_EMAIL 128 export GIT_AUTHOR_DATE 129 130 git-cat-file commit$commit| sed -e '1,/^$/d' 131 case "$replay" in 132 '') 133 echo "(cherry picked from commit$commit)" 134 test "$rev" = "$commit" || 135 echo "(original 'git cherry-pick' arguments: $@)" 136 ;; 137 esac 138 base=$prevnext=$commit 139 ;; 140 141esac >.msg 142 143# This three way merge is an interesting one. We are at 144#$head, and would want to apply the change between$commit 145# and$prevon top of us (when reverting), or the change between 146#$prevand$commiton top of us (when cherry-picking or replaying). 147 148echo >&2 "First trying simple merge strategy to$me." 149git-read-tree -m -u --aggressive$base$head$next&& 150result=$(git-write-tree 2>/dev/null)|| { 151 echo >&2 "Simple$mefails; trying Automatic$me." 152 git-merge-index -o git-merge-one-file -a || { 153 mv -f .msg "$GIT_DIR/MERGE_MSG" 154 { 155 echo ' 156Conflicts: 157' 158 git ls-files --unmerged | 159 sed -e 's/^[^ ]* / /' | 160 uniq 161 } >>"$GIT_DIR/MERGE_MSG" 162 echo >&2 "Automatic$mefailed. After resolving the conflicts," 163 echo >&2 "mark the corrected paths with 'git-add<paths>'" 164 echo >&2 "and commit the result." 165 case "$me" in 166 cherry-pick) 167 echo >&2 "You may choose to use the following when making" 168 echo >&2 "the commit:" 169 echo >&2 "$set_author_env" 170 esac 171 exit 1 172 } 173 result=$(git-write-tree)|| exit 174} 175echo >&2 "Finished one$me." 176 177# If we are cherry-pick, and if the merge did not result in 178# hand-editing, we will hit this commit and inherit the original 179# author date and name. 180# If we are revert, or if our cherry-pick results in a hand merge, 181# we had better say that the current user is responsible for that. 182 183case "$no_commit" in 184'') 185 git-commit -n -F .msg$edit 186 rm -f .msg 187 ;; 188esac