Merge "Clean up hwvtep csit combinations and triggers"
[releng/builder.git] / scripts / branch_cut / branch_cut.awk
1 #!/usr/bin/awk -f
2
3 # @License EPL-1.0 <http://spdx.org/licenses/EPL-1.0>
4 ##############################################################################
5 # Copyright (c) 2017 The Linux Foundation and others.
6 #
7 # All rights reserved. This program and the accompanying materials
8 # are made available under the terms of the Eclipse Public License v1.0
9 # which accompanies this distribution, and is available at
10 # http://www.eclipse.org/legal/epl-v10.html
11 #
12 ##############################################################################
13
14 BEGIN {
15     new_tag                     = new_reltag       # new release tag
16     curr_tag                    = curr_reltag      # current release tag
17     prev_tag                    = prev_reltag      # previous release tag
18
19     new_release                 = tolower(new_tag)
20     curr_release                = tolower(curr_tag)
21     prev_release                = tolower(prev_tag)
22
23     ws = "[\\t ]*"                                 # white-spaces
24     startpat = "^" ws "- project:"                 # start pattern
25     endpat = startpat                              # end pattern
26     op = "^" ws "---" ws "$"                       # match files starts with "---"
27
28     next_release_tag            = "^" ws "next-release-tag: " curr_tag
29     master                      = "'master'"
30     new_branch                  = "'stable/" new_release "'"
31     curr_branch                 = "'stable/" curr_release "'"
32     prev_branch                 = "'stable/" prev_release "'"
33
34     # replace block to add new release
35     new_rel_yaml_tag            = "- " new_release ":";
36     br_master_yaml_tag          = "    branch: 'master'";
37     jre_yaml_tag                = "    jre: 'openjdk8'";
38     curr_rel_yaml_tag           = "- " curr_release ":";
39     br_stable_curr_yaml_tag     = "    branch: 'stable/" curr_release "'";
40
41     # replace block for autorelease-projects
42     #new_rel_yaml_tag           = "- " new_release ":";
43     next_rel_tag_new_yaml_tag   = "    next-release-tag: " new_tag;
44     #br_master_yaml_tag         = "    branch: 'master'";
45     jdk_yaml_tag                = "    jdk: 'openjdk8'";
46     intg_test_yaml_tag          = "    integration-test: " new_release;
47     #curr_rel_yaml_tag          = "- " curr_release ":";
48     next_rel_tag_curr_yaml_tag  = "    next-release-tag: " curr_tag;
49     #br_stable_curr_yaml_tag    = "    branch: 'stable/" curr_release "'";
50
51     # search patterns
52     smaster = "^" ws "- master:"
53     sstream = "^" ws "stream:"
54     srelease = "^" ws "- " curr_release ":"
55     #if (l ~ next_release_tag) { next_release_tag = 1; continue; }
56     sbranch = "^" ws "branch: " master
57
58     debug = 0                                   # set to 1 to print debug info
59     file_format = 2                             # project stream format
60
61     release_found = 0
62     stream_found = 0
63     nrt_found = 0
64 }
65
66 {
67     # exit if release info is not available
68     if ((length(new_release) == 0 || length(curr_release) == 0)) {
69         exit;
70     }
71
72     # read all lines of the file into an array
73     file[NR] = $0
74 }
75
76 END {
77     n = NR                                      # total number of lines
78     find_blks()                                 # gets number of blocks between start/end pattern
79     process_blk(arr_bs[1], arr_be[1], 1)        # pass start and end of each block and process first block
80     update_file(arr_be[1])                      # write processed content
81
82     if (debug) {
83         print "number of blocks="nb;
84         print "total records in file[]="length(f);
85         print "size of firstblk[]="length(firstblk);
86         print "size of newblk[]="length(newblk);
87         print "size of oldmaster[]="length(oldmaster);
88         print "size of newblk[]="length(newblk);
89     }
90 }
91
92 function find_blks(   i, l, bs, be) {
93     for (i = 1; i <= n; i++) {
94         l = file[i]
95         if (l ~ startpat) project = 1                        # start pattern
96         if (bs > be && l ~ endpat) arr_be[++be] = i - 1      # block end
97         if (           l ~ startpat) arr_bs[++bs] = i - 1    # block start
98     }
99     nb = be
100
101     # to handle files with single blocks
102     if (nb == 0 && length(file) > 1 && project == 1) {
103         nb = 1
104         arr_bs[1] = 1                               # start after line '---'
105         arr_be[1] = length(file)                    # set length of the file
106     }
107
108     if (debug) {
109         for (i = 1; i < nb; i++)
110             print "find_blks: nb=" nb " arr_bs[" i "]="arr_bs[i]" arr_be[" i "]="arr_be[i];
111     }
112 }
113
114 function process_blk(bs, be, bn,   i, l) {
115     if (debug) {
116         print "process_blk: bn=" bn ", bs=" bs " ,be=" be
117     }
118
119     # get the first block
120     for (i = bs + 1; i <= be ; i++) {
121         l = file[i]
122         # determine file format
123         if (l ~ /stream:/) {
124             x=index(l,":")
125             s = substr(l, x+2, length(l) - x)
126             if (s == curr_release || s == new_release) {
127                 file_format = 1
128             } else if (length(s) == 0 ) {
129                 file_format = 0
130             }
131         }
132         firstblk[++nex] = l
133     }
134
135     if (debug) {
136         print "process_blk: stream='" s "' length(s)=" length(s)" file_format='" file_format "'"
137     }
138
139     # Handle single stream format
140     if (file_format == 1) {
141         # create new block to be inserted
142         for (i = 1; i <= length(firstblk); i++) {
143             l = firstblk[i]
144             if (l ~ /name:|stream:/) sub(curr_release, new_release, l)
145             newblk[++nex1] = l
146         }
147         # re-create old block and change master to stable/branch
148         for (i = 1; i <= length(firstblk)-1; i++) {
149             l = firstblk[i]
150             if (l ~ /branch:/) sub(master, curr_branch, l)
151             oldmaster[++nex2] = l
152         }
153     } else if (file_format == 0) {
154         # Handle multi-stream format
155         for (i = 1; i <= length(firstblk)-1; i++) {
156             l = firstblk[i]
157             if (l ~ sstream) { stream_found = 1; }
158             if (l ~ srelease) { release_found = 1; indent = substr(l, 1, index(l, "-")-1); continue; }
159             if (l ~ next_release_tag) { nrt_found = 1; continue; }
160             if (l ~ sbranch) {
161                 # append lines
162                 if (stream_found && release_found && !nrt_found) {
163                     newblk[++nex3] = indent new_rel_yaml_tag;
164                     newblk[++nex3] = indent br_master_yaml_tag;
165                     newblk[++nex3] = indent jre_yaml_tag;
166                     newblk[++nex3] = indent curr_rel_yaml_tag;
167                     newblk[++nex3] = indent br_stable_curr_yaml_tag;
168                     stream_found = 0; release_found = 0;
169                     continue;
170                 }
171                 if (stream_found && release_found && nrt_found) {
172                     newblk[++nex3] = indent new_rel_yaml_tag;
173                     newblk[++nex3] = indent next_rel_tag_new_yaml_tag;
174                     newblk[++nex3] = indent br_master_yaml_tag;
175                     newblk[++nex3] = indent jdk_yaml_tag;
176                     newblk[++nex3] = indent intg_test_yaml_tag;
177                     newblk[++nex3] = indent curr_rel_yaml_tag;
178                     newblk[++nex3] = indent next_rel_tag_curr_yaml_tag;
179                     newblk[++nex3] = indent br_stable_curr_yaml_tag;
180                     stream_found = 0; release_found = 0; nrt_found=0;
181                     continue;
182                 }
183             }
184             newblk[++nex3] = l
185
186             if (debug) {
187                 print "process_blk: append(newblk[]) : stream="stream" release_found="release_found
188             }
189         }
190     } else {
191         # exit on unknown file format
192         exit;
193     }
194 }
195
196 function update_file(be,   i, j, l) {
197     i = 1
198     # handle lines before "---"
199     while (i <= n) {
200         print l = file[i++]
201         if (l ~ op) break
202     }
203
204     if (debug) {
205         print "writing master block"
206     }
207
208     # Handle single stream format
209     if (file_format == 1) {
210         for (j = 1; j <= nex1; j++)                   # write new branch block
211             print newblk[j]
212
213         if (debug) {
214             print "writing stable block"
215         }
216
217         for (j = 1; j <= nex2; j++)                   # write updated branch block
218             print oldmaster[j]
219
220     # Handle multi-stream format
221     } else if (file_format == 0) {
222         # print the first block
223         for (j = 1; j <= nex3; j++)                   # write multi-stream block
224             print newblk[j]
225     }
226
227     if (debug) {
228         print "writing rest of the file"
229     }
230
231     while (be <= n) {                                 # write rest of the file
232         print file[be++]
233     }
234 }