summaryrefslogtreecommitdiffstats
path: root/scripts/automerge7.sh
blob: 33f046226760c23d5c40cb77c0105177084cdec6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/bin/bash

FROM=origin/7.0
TO=master
PUSH="origin HEAD:refs/for/master"

show() {
        sCommit=$1
        if [ "$sCommit" == "" ]
        then
                echo "show() missing commit id"
                exit 1
        fi
        git show -s  $sCommit
}
merge() {
        mCommit=$1
        if [ "$mCommit" == "" ]
        then
                echo "merge() missing commit id"
                exit 1
        fi

#       echo "merge($mCommit)"

        git merge $mCommit $2
        if [ "$?" != "0" ]
        then
                echo "Merge failed for commit $mCommit"
                echo "Manual merge is needed"
                exit 2
        fi
        # Add a change id using git hook
        git commit --amend --no-edit

}

pushMerged() {
#       echo "pushMerged()"
        git push $PUSH
        if [ "$?" != "0" ]
        then
                echo "Push failed!"
                exit 2
        fi
}

maybe_commit_and_push() {
#       echo "maybe_commit_and_push()"
        cpCommit=$1
        if [ "$cpCommit" == "" ]
        then
                # Nothing to merge currently
                return
        fi
        cpCommitMsg=$2
        if [ "$cpCommitMsg" == "" ]
        then
        	echo "Internal error, no commit message passed to maybe_commit_and_push()"
        	exit 1;
        fi
#       echo "maybe_commit_and_push: Merging $cpCommit"
        merge $cpCommit
	echo -e "Merge changes from $FROM\n\n$cpCommitMsg"|git commit --amend -F -
        pushMerged
}

git checkout $TO
git fetch

pending=`git log $TO..$FROM --reverse|grep "^commit "|sed "s/commit //"`

pendingCommit=
pendingCommitMessage=
for commit in $pending
do
        echo "Checking $commit..."
        mergeDirective=`git log -n 1 --format=%B $commit|grep "^Merge:"|sed "s/Merge: //"`
        commitMsg=`git log -n 1 --format=oneline --abbrev-commit $commit`
        if [ "$mergeDirective" == "" ]
        then
                pendingCommit=$commit
                pendingCommitMessage=$pendingCommitMessage"$commitMsg\n"
                echo pendingCommitMessage: $pendingCommitMessage
        elif [ "$mergeDirective" == "no" ]
        then
                maybe_commit_and_push $pendingCommit "$pendingCommitMessage"
                pendingCommit=
                pendingCommitMessage=
                echo
                echo "Doing a no-op merge because of Merge: no for $commit"
                git log -n 1 --format=%B $commit
                echo
                # Do a no-op merge
                git merge $commit -s ours
                echo -e "No-op merge from $FROM\n\n$commitMsg"|git commit --amend -F -
                pushMerged
        elif [ "$mergeDirective" == "manual" ]
        then
                maybe_commit_and_push $pendingCommit "$pendingCommitMessage"
                pendingCommit=
                pendingCommitMessage=
                echo
                echo "Stopping merge at $commit (merge: manual)"
                echo "The following commit must be manually merged."
                show $commit
                exit 3
        else
                maybe_commit_and_push $pendingCommit "$pendingCommitMessage"
                pendingCommit=
                pendingCommitMessage=
                echo
                echo "Commit $commit contains an unknown merge directive, Merge: $mergeDirective"
                echo "Stopping merge."
                show $commit
                exit 3
        fi
done

# Push any pending merges
maybe_commit_and_push $pendingCommit "$pendingCommitMessage"