summaryrefslogtreecommitdiffstats
path: root/src/build/tools/conv_rel_branch
blob: 9f1d807a41f2042adc23b4acd17b6f7ca1f1371f (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/build/tools/conv_rel_branch $
#
# OpenPOWER HostBoot Project
#
# Contributors Listed Below - COPYRIGHT 2015
# [+] International Business Machines Corp.
#
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied. See the License for the specific language governing
# permissions and limitations under the License.
#
# IBM_PROLOG_END_TAG

use strict;
use warnings;
use Data::Dumper;
use Getopt::Long qw(:config pass_through);

# Globals
my %power_platforms = ();
my %relations = ();
my $latest_power_platform = "";
my $fsp_ci_jenkins_rel_file = "/gsa/ausgsa/home/f/s/fspcibld/patches/cipatch_xml";
use constant MASTER => "master";
my $debug = 0;
my $help = 0;

# Set local variables based on ENV variables
my $PROJECT_ROOT = `git rev-parse --show-toplevel`;

# Parse command line parameters
GetOptions("debug!" => \$debug,
           "help" => \$help);

# List of commands tool can run
my %commands = ( "rtob" => \&execute_rel_to_branch,
                 "btor" => \&execute_branch_to_rel,
               );

if ($help)
{
    execute_help();
}
else
{
    my $command = shift @ARGV;
    if ($commands{$command})
    {
        system("git remote -v | grep gerrit -q");
        die "Gerrit remote DNE, must run in repo with gerrit remote" if $?;
        # Build release to branch relation hash.
        build_relations();
        &{$commands{$command}}();
    }
    else
    {
        execute_help();
    }
}

############################## Begin Actions ###################################

sub execute_rel_to_branch
{
    my $release = "";

    GetOptions("release:s" => \$release);
    die "Missing release" if $release eq "";

    # Get power platform associated with release
    my $power_platform = get_power_platform($release);
    # Find release in relations hash.
    my $branch = $relations{$power_platform}{$release};
    die "Fips release => $release has not corresponding gerrit branch" if (!$branch);
    print "$branch \n";
}

sub execute_branch_to_rel
{
    my $branch = "";

    GetOptions("branch:s" => \$branch);
    die "Missing branch" if $branch eq "";

    # Get power platform associated with branch
    my $power_platform = get_power_platform($branch);

    # Find branch in relations hash.
    my $release = "";
    foreach my $rel (sort keys %{$relations{$power_platform}})
    {
        if ($relations{$power_platform}{$rel} eq "$branch")
        {
            $release = $rel;
            last;
        }
    }
    die "Gerrit branch => $branch has not corresponding fips release" if ($release eq "");
    print "$release \n";
}

sub execute_help
{
    my $command = shift @ARGV;

    if ($command eq "")
    {
        print "  Usage:\n";
        print "      rel_branch [options] <tool>\n\n";
        print "  Tool to convert fips release to branches and vice versa\n\n";
        print "  Requires:\n";
        print "      Tool to run in git repo that has gerrit remote\n\n";
        print "  Available subtools:\n";
        foreach my $key (sort keys %commands)
        {
            print "      $key\n";
        }
        print "\n";
        print "  Global options:\n";
        print "      --debug     Enable debug mode.\n";
        print "      --help      Display help on a specific tool.\n";
    }
    elsif (not defined $commands{$command})
    {
        die "Unknown subcommand: $command.\n";
    }
    else
    {
        my %help = (
        "rtob" =>
q(
  Convert release to branch

  Options:
    --release=<release>     Fips release name (e.g. fips810) [required].
),
        "btor" =>
q(
  Convert branch to fips release

  Options:
    --branch=<remote-gerrit-branch>     Remote gerrit branch (e.g. release-fips810, master) [required].
),
        );

        print "rel_branch $command:";
        print $help{$command};
    }
}

############################## Begin Sub Routines ##############################

# sub get_release_branches
#
# Get all branches in gerrit that are prefixed 'release-' and remove the prefix
# *Note branches with string 'master' are removed from this to result in direct
# matches of fips releases only. Master branches will be dervied later.
#
# @return array - sorted names of branches (e.g release-fips810 ...)
#
sub get_release_branches
{
    chdir($PROJECT_ROOT);
    die $? if ($?);

    # Parse for remote gerrit branches associated directly with a release
    my $cmd = "git branch -a | grep -e remotes/gerrit/release";
    $cmd .= " | sed -e 's/^[ \\t]*remotes\\/gerrit\\///'";

    my @release_branches = sort (split('\n',`$cmd`));
    print "Release Branches:\n" if $debug;
    print Dumper \@release_branches if $debug;
    return @release_branches;
}

# sub get_fips_releases
#
# Get all fips releases that fsp-ci-jenkins uses in sorted order.
#
# @return array - sorted names of releases (e.g fips810, fips820, etc)
#
sub get_fips_releases
{
    chdir($PROJECT_ROOT);
    die $? if ($?);

    # Parse fsp-ci-jenkins xml file for releases
    my $cmd = "cat $fsp_ci_jenkins_rel_file | grep release | ";
    $cmd .= "sed -e 's/^[ \\t]*<release>//' -e 's/<\\/release>[ \\t]*//'";

    my @fips_releases = sort (split('\n',`$cmd`));
    print "Fips Release:\n" if $debug;
    print Dumper \@fips_releases if $debug;
    return @fips_releases;
}

# sub get_power_platform
#
# Takes a references (release or gerrit branch) and determines the power
# platform it belongs to.
#
# @return string - power platform (e.g. p8, p9)
#
sub get_power_platform
{
    my $reference = shift;

    my $power_platform = "";
    if ($reference =~ m/master/)
    {
        # Remove prefix from older platforms (e.g. master-p8). If nothing is
        # replaced then it is the latest power platform.
        $reference =~ s/master-//;
        if ($reference eq MASTER)
        {
            $power_platform = $latest_power_platform;
        }
        else
        {
            $power_platform = $reference;
        }
    }
    else
    {
        ($power_platform) = $reference =~ /fips(.*)[0-9][0-9]/;
        $power_platform = "p$power_platform";
        $power_platforms{$power_platform} = 1;
    }
    return $power_platform;
}

# sub build_relations
#
# Build a relationship hash between fips releases and gerrit branches using
# fsp-ci-jenkins xml and git branch command within hostboot.
#
# Structure:
#   power_platform =>
#       fips-release => gerrit-branch
# Example:
#   p8 =>
#       fips820 => release-fips820
#       fips811 => release-fips811
#       fips840 => release-fips840
#       fips850 => master-p8
#       fips830 => release-fips830
#       fips810 => release-fips810
#   p9 =>
#       fips910 => master
#
sub build_relations
{
    my @releases = get_fips_releases();
    my @branches = get_release_branches();

    # Fill in fips release keys
    foreach my $release (@releases)
    {
        my $power_platform = get_power_platform($release);
        $relations{$power_platform}{$release} = "";
    }

    # Fill in fips release values, which are gerrit release branches.
    foreach my $branch (@branches)
    {
        my $power_platform = get_power_platform($branch);
        my $release = $branch;
        $release =~ s/release-//;
        $relations{$power_platform}{$release} = $branch;
    }

    # Handle master branches for each platform
    my @platforms = sort keys %power_platforms;
    foreach my $i (0 .. $#platforms)
    {
        my $power_platform = $platforms[$i];

        # Lastest power platform matches branch master
        my $master = MASTER;
        # Previous power platforms match branch "master-$platform"
        if ($i < $#platforms)
        {
            $master = MASTER."-$power_platform";
        }
        else
        {
            # Set latest power platform
            $latest_power_platform = $power_platform;
        }

        # Check for first fips release without a gerrit branch. Due to sort
        # order, it will be the next in order release. It is done this way
        # to avoid issues when fips releases are ahead of gerrit branches. In
        # other words it is possible to have fips releases past gerrit master.
        foreach my $release (sort keys %{$relations{$power_platform}})
        {
            if ($relations{$power_platform}{$release} eq "")
            {
                $relations{$power_platform}{$release} = $master;
                last;
            }
        }
    }

    print "Relations:\n" if $debug;
    print Dumper \%relations if $debug;
}
OpenPOWER on IntegriCloud