]> git.ozlabs.org Git - next-scripts/blob - do_merge
do_build: consolidate failure action
[next-scripts] / do_merge
1 #!/bin/sh
2
3 no_build=false
4 start_from=""
5
6 while getopts 'ns:' opt; do
7         case "$opt" in
8         n)
9                 no_build=true
10                 ;;
11         s)
12                 if [ -f "$SHA1_FILE" ]; then
13                         printf '%s: %s exists, so -s is invalid\n' "$0" "$SHA1_FILE" 1>&2
14                         exit 1
15                 fi
16                 start_from="$OPTARG"
17                 ;;
18         *)
19                 exit 1
20                 ;;
21         esac
22 done
23 shift $((OPTIND - 1))
24
25 tools_dir=$(dirname "$0")
26 . "$tools_dir/common.sh"
27
28 log()
29 {
30         echo "$@" | tee -a "$LOG_FILE"
31 }
32
33 execute()
34 {
35         log '$' "$@"
36         _out="$( "$@" 2>&1 )"
37         _err=$?
38         echo "$_out" | tee -a "$LOG_FILE"
39         return "$_err"
40 }
41
42 report()
43 {
44         if [ -n "$1" ]; then
45                 linux-next-notify "$1"
46         fi
47         if [ -n "$2" ]; then
48                 printf '%s\n' "$2" 1>&2
49         fi
50 }
51
52 fix_up()
53 {
54         report "$@"
55         if ! bash -i; then
56                 exit
57         fi
58 }
59
60 if [ -f "$SHA1_FILE" ]; then
61         previous_tree=$(tail -n1 "$SHA1_FILE" | cut -f1 -d"$_TAB")
62 elif [ -z "$start_from" ]; then
63         cp /dev/null "$LOG_FILE"
64         execute date -R
65         execute git checkout master
66         execute git reset --hard stable
67         printf 'Name\t\tSHA1\n----\t\t----\n' > "$SHA1_FILE"
68 fi
69
70 heads=$(awk -F '\t' '
71         /^#/ { next; }
72         $2=="git" { printf("git/%s/%s ", $3, $5); }
73         $2=="branch" { printf("branch/%s ", $1); }
74         $2=="switch" { printf("switch/%s/%s ", $3, $4); }' "$CTRL_FILE")
75
76 need_build=false
77
78 for h in $heads; do
79         op=${h%%/*}
80         args=${h#*/}
81         arg1=${args%%/*}
82         arg2=${args#*/}
83
84         if [ -n "$previous_tree" ]; then
85                 if [ "$op" = 'git' ] && [ "$arg1" = "$previous_tree" ]; then
86                         previous_tree=''
87                 fi
88                 continue
89         elif [ -n "$start_from" ]; then
90                 if [ "$op" = 'git' ] && [ "$arg1" = "$start_from" ]; then
91                         start_from=""
92                 else
93                         continue
94                 fi
95         fi
96
97         if [ "$op" = "branch" ]; then
98                 git branch -f "$arg1"
99                 linux-next-notify "update branch $arg1"
100                 continue
101         fi
102
103         if [ "$op" = 'switch' ]; then
104                 new_branch=$arg1
105                 if [ "$new_branch" = 'master' ]; then
106                         old_branch=$(git branch --show-current)
107                         if ! [ "$old_branch" = 'master' ]; then
108                                 if ! git checkout master; then
109                                         fix_up 'git checkout master failed'
110                                 fi
111                         fi
112                 else
113                         base=$arg2
114                         if ! git checkout -B "$new_branch" "$base"; then
115                                 fix_up "git checkout $new_branch failed"
116                         fi
117                 fi
118                 continue
119         fi
120
121 # $op == "git"
122
123         tree=$arg1
124         branch=$arg2
125         h="$tree${branch:+/$branch}"
126         hlog=$(git log -1 --oneline "$h") 2>/dev/null
127         old_head=$(git rev-parse HEAD)
128         if [ -f "../pre-merge/$tree" ]; then
129                 while read -r p <&7; do
130                         if ! "$bin_dir/do_patch" -n "$p"; then
131                                 fix_up "premerge patch failed"
132                         fi 7<&-
133                 done 7<"../pre-merge/$tree"
134         fi
135         log Merging "$h" "($hlog)"
136         check_dups "$tree" HEAD "$h" | tee -a "$LOG_FILE"
137         msg=$(printf "Merge branch '%s' of %s" "${branch:-$tree}" "$(get_url "$tree")")
138         if ! execute git merge -m "$msg" "$h"; then
139                 echo Merge failed 1>&2
140                 echo "$h" >>../merge.debug
141                 git diff >>../merge.debug 2>&1
142
143                 check_unmerged_files "$tree"
144
145                 if git diff 2>&1 | grep -E -q '<<<<<|^\*'; then
146                         if [ -f "../merge-fixes/$tree" ]; then
147                                 echo "Merge fixes exist for this tree:"
148                                 cat "../merge-fixes/$tree"
149                         fi
150                         fix_up "new conflict found merging $tree"
151                 fi
152 #               if [ "$(git status --porcelain)" ]; then
153                         if ! execute git commit --no-edit -v -a; then
154                                 fix_up "next commit failed for $tree"
155                         fi
156                         execute git diff -M --stat --summary 'HEAD^..'
157 #               fi
158         fi
159
160         tab="$_TAB"
161         if [ "${#tree}" -lt 8 ]; then
162                 tab="$_TAB$_TAB"
163         fi
164         printf '%s%s%s\n' "$tree" "$tab" "$(git rev-parse "$h^{}")" >> "$SHA1_FILE"
165
166         if [ -f "../merge-fixes/$tree" ]; then
167                 while read -r p <&7; do
168                         if ! "$bin_dir/merge_fix" -n "$p"; then
169                                 fix_up "merge fix failed for $tree"
170                         fi 7<&-
171                 done 7<"../merge-fixes/$tree"
172         fi
173
174         if $no_build; then
175                 continue
176         fi
177
178         if ! $need_build; then
179                 # See if we need to build after merging this tree
180                 if [ -n "$(git diff "$old_head"..)" ]; then
181                         need_build=true
182                 fi
183         fi
184         if ! $need_build; then
185                 continue
186         fi
187
188         if [ "$(get_build_flag "$tree")" != "yes" ]; then
189                 if ! [ -f "$top_dir/old-versions/$tree" ]; then
190                         continue
191                 fi
192         fi
193
194         repo="${build_host:+$build_host:}${build_dir}"
195         if ! git push -f "$repo" HEAD:refs/heads/next; then
196                 fix_up "push failed for $tree" "git push failed"
197         fi
198
199         if [ -x "../pre-build/$tree" ] &&
200            ! "../pre-build/$tree"; then
201                 fix_up "pre-build script failed for $tree" "Prebuild script failed"
202         fi
203         if ! "$bin_dir/do_build" "$tree"; then
204                 report "build failed for $tree" "Build failed - run do_merge when fixed"
205                 exit 1
206         fi
207
208         rm -f "$top_dir/old-versions/$tree"
209
210         need_build=false
211 done
212
213 exit 0