diff options
author | Sean Whitton <spwhitton@spwhitton.name> | 2017-02-16 11:32:01 -0700 |
---|---|---|
committer | Sean Whitton <spwhitton@spwhitton.name> | 2017-02-16 11:32:01 -0700 |
commit | 1d6a1056600b15102578d00a3e06546eb554936e (patch) | |
tree | 3d5464bdaa0d1b22928b5485e6d445fb9a722118 /bin/git-merge-ff | |
parent | 3fd505e90eca996c510dec182bcf76bcbec3b578 (diff) | |
download | dotfiles-1d6a1056600b15102578d00a3e06546eb554936e.tar.gz |
import git-merge-ff from Stack Overflow
Diffstat (limited to 'bin/git-merge-ff')
-rwxr-xr-x | bin/git-merge-ff | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/bin/git-merge-ff b/bin/git-merge-ff new file mode 100755 index 00000000..812cbd04 --- /dev/null +++ b/bin/git-merge-ff @@ -0,0 +1,56 @@ +#!/bin/bash + +# source: Jefromi on Stack Overflow -- https://stackoverflow.com/a/4157435 + +_usage() { + echo "Usage: git merge-ff <branch> <committish-to-merge>" 1>&2 + exit 1 +} + +_merge_ff() { + branch="$1" + commit="$2" + + branch_orig_hash="$(git show-ref -s --verify refs/heads/$branch 2> /dev/null)" + if [ $? -ne 0 ]; then + echo "Error: unknown branch $branch" 1>&2 + _usage + fi + + commit_orig_hash="$(git rev-parse --verify $commit 2> /dev/null)" + if [ $? -ne 0 ]; then + echo "Error: unknown revision $commit" 1>&2 + _usage + fi + + if [ "$(git symbolic-ref HEAD)" = "refs/heads/$branch" ]; then + git merge $quiet --ff-only "$commit" + else + if [ "$(git merge-base $branch_orig_hash $commit_orig_hash)" != "$branch_orig_hash" ]; then + echo "Error: merging $commit into $branch would not be a fast-forward" 1>&2 + exit 1 + fi + echo "Updating ${branch_orig_hash:0:7}..${commit_orig_hash:0:7}" + if git update-ref -m "merge $commit: Fast forward" "refs/heads/$branch" "$commit_orig_hash" "$branch_orig_hash"; then + if [ -z $quiet ]; then + echo "Fast forward" + git diff --stat "$branch@{1}" "$branch" + fi + else + echo "Error: fast forward using update-ref failed" 1>&2 + fi + fi +} + +while getopts "q" opt; do + case $opt in + q ) quiet="-q";; + * ) ;; + esac +done +shift $((OPTIND-1)) + +case $# in + 2 ) _merge_ff "$1" "$2";; + * ) _usage +esac |