]> git.donarmstrong.com Git - bin.git/blob - git-merge-ff
add a VERBOSE flag to getmail
[bin.git] / git-merge-ff
1 #!/bin/bash
2
3 # stolen from http://stackoverflow.com/questions/4156957/merging-branches-without-checkout/4157435#4157435
4
5 _usage() {
6     echo "Usage: git merge-ff <branch> <committish-to-merge>" 1>&2
7     exit 1
8 }
9
10 _merge_ff() {
11     branch="$1"
12     commit="$2"
13
14     branch_orig_hash="$(git show-ref -s --verify refs/heads/$branch 2> /dev/null)"
15     if [ $? -ne 0 ]; then
16         echo "Error: unknown branch $branch" 1>&2
17         _usage
18     fi
19
20     commit_orig_hash="$(git rev-parse --verify $commit 2> /dev/null)"
21     if [ $? -ne 0 ]; then
22         echo "Error: unknown revision $commit" 1>&2
23         _usage
24     fi
25
26     if [ "$(git symbolic-ref HEAD)" = "refs/heads/$branch" ]; then
27         git merge $quiet --ff-only "$commit" --
28     else
29         if [ "$(git merge-base $branch_orig_hash $commit_orig_hash)" != "$branch_orig_hash" ]; then
30             echo "Error: merging $commit into $branch would not be a fast-forward" 1>&2
31             exit 1
32         fi
33         echo "Updating ${branch_orig_hash:0:7}..${commit_orig_hash:0:7}"
34         if git update-ref -m "merge $commit: Fast forward" "refs/heads/$branch" "$commit_orig_hash" "$branch_orig_hash"; then
35             if [ -z $quiet ]; then
36                 echo "Fast forward"
37                  git diff --stat "$branch@{1}" "$branch" --
38             fi
39         else
40             echo "Error: fast forward using update-ref failed" 1>&2
41         fi
42     fi
43 }
44
45 while getopts "q" opt; do
46     case $opt in
47         q ) quiet="-q";;
48         * ) ;;
49     esac
50 done
51 shift $((OPTIND-1))
52
53 case $# in
54     2 ) _merge_ff "$1" "$2";;
55     * ) _usage
56 esac