1#!/bin/sh 2. git-sh-setup-script|| die "Not a git archive" 3 4# We want a clean tree and clean index to be able to revert. 5status=$(git status) 6case"$status"in 7'nothing to commit') ;; 8*) 9echo"$status" 10 die "Your working tree is dirty; cannot revert a previous patch.";; 11esac 12 13rev=$(git-rev-parse --verify "$@")&& 14commit=$(git-rev-parse --verify "$rev^0")||exit 15if git-diff-tree -R -M -p$commit| git-apply --index&& 16 msg=$(git-rev-list --pretty=oneline --max-count=1 $commit) 17then 18{ 19echo"$msg"|sed-e' 20 s/^[^ ]* /Revert "/ 21 s/$/"/' 22echo 23echo"This reverts$commitcommit." 24test"$rev"="$commit"|| 25echo"(original 'git revert' arguments: $@)" 26} | git commit -F- 27else 28# Now why did it fail? 29 parents=`git-cat-file commit "$commit" 2>/dev/null | 30 sed -ne '/^$/q;/^parent /p' | 31 wc -l` 32case$parentsin 330) die "Cannot revert the root commit nor non commit-ish.";; 341) die "The patch does not apply.";; 35*) die "Cannot revert a merge commit.";; 36esac 37fi