]> git.donarmstrong.com Git - bin.git/commitdiff
add git-merge-ff
authorDon Armstrong <don@donarmstrong.com>
Wed, 27 Feb 2013 19:43:19 +0000 (11:43 -0800)
committerDon Armstrong <don@donarmstrong.com>
Wed, 27 Feb 2013 19:43:19 +0000 (11:43 -0800)
git-merge-ff [new file with mode: 0644]

diff --git a/git-merge-ff b/git-merge-ff
new file mode 100644 (file)
index 0000000..46a394c
--- /dev/null
@@ -0,0 +1,56 @@
+#!/bin/bash
+
+# stolen from http://stackoverflow.com/questions/4156957/merging-branches-without-checkout/4157435#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
\ No newline at end of file