Implement parent branching. Track who a branch comes from and only allow merging...
[socialgit.git] / bin / mergebranch
blobbede12cc214c46ddf8bc4f61973cfb4c39f76eb4
1 #!/bin/sh
2 # mergebranch
4 function is_parent_branch {
5 if [ "$(parent_branch $2)" = "$1" ]; then
6 return 0;
7 else
8 return 1;
9 fi
12 function parent_branch {
13 # Given a branch, $1, find the parent.
14 PARENT_BRANCH=`git cat-file -p branch-created.$1 2> /dev/null | tail -1 | grep "parent:" | cut -d":" -f2`
15 if [ "$PARENT_BRANCH" != "" ]; then
16 echo $PARENT_BRANCH
17 else
18 return 1;
22 function other_branches {
23 for b in $(git branch -r); do
24 if [[ `echo $b|grep *` ]]; then b=$(cut -d" " -f0); fi
25 b=`echo $b|cut -d"/" -f2`
26 if [[ `echo $*|grep $b` ]]; then continue; fi
27 echo $b
28 done
31 # Parameter processing here
32 # mergebranch [-d] [from_branch] [to_branch]
33 while [ "$1" != "" ]; do
34 if [ "$1" = "-d" ]; then
35 DRYRUN=1
36 elif [ "$MERGE_FROM" = "" ]; then
37 MERGE_FROM=$1
38 elif [ "$MERGE_INTO" = "" ]; then
39 MERGE_INTO=$1
42 shift
43 done
45 if [ "$MERGE_FROM" = "" ]; then
46 MERGE_FROM=`currentbranchname`
49 if [ "$MERGE_INTO" = "" ]; then
50 MERGE_INTO=$(parent_branch $MERGE_FROM)
53 if [ "$DRYRUN" != 1 ]; then
54 updatebranch
57 echo $DRYRUN - $MERGE_FROM - $MERGE_INTO
59 if ( git status|grep ".dotest/" ); then
60 echo "Need to resolve conflicts updating branch before merge can continue..."
61 exit -1
62 else
63 changebranch master &> /dev/null
64 if [ "$DRYRUN" != 1 ]; then
65 updatebranch
66 else
67 git fetch
69 if ( git status|grep ".dotest/" ); then
70 echo "Need to resolve conflicts updating branch before merge can continue..."
71 exit -1
72 else
73 if ( is_parent_branch $MERGE_FROM $MERGE_INTO ); then
74 git diff --stat $MERGE_INTO...$MERGE_FROM
75 if [ "$DRYRUN" != 1 ]; then
76 updatebranch
77 else
78 changebranch $MERGE_FROM &> /dev/null
80 else
81 echo "Cannot merge from branch $MERGE_FROM into non-parent $MERGE_INTO. $MERGE_FROM's parent is $(parent_branch $MERGE_FROM)."