summaryrefslogtreecommitdiffstats
path: root/src/tools/utils/modules/gitUtil.pm
blob: b94f711c9f86a157a0956c2f50e8e9679528b4b9 (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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG
# This is an automatically generated prolog.
#
# $Source: src/tools/utils/modules/gitUtil.pm $
#
# OpenPOWER sbe Project
#
# Contributors Listed Below - COPYRIGHT 2016
#
#
# 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

package gitUtil;

use strict;

my %globals = {};

# Function     : gitRoot
#
# @brief       : Determine the root of the GIT repository
#
# @return root : Root of GIT repository
#
sub gitRoot
{
    return $globals{git_root} if (defined $globals{git_root});

    open COMMAND, "git rev-parse --show-toplevel |";
    my $root = <COMMAND>;
    close COMMAND;
    chomp $root;

    die "Unable to determine git_root" if ($root eq "");

    $globals{git_root} = $root;
    return $root;
}

#################### Begin Gerrit JSON Utility Subroutines #####################

# @note There are perl modules for doing this but they are not installed on
#       the pool machines.  The parsing for JSON (at least the content from
#       the Gerrit server) isn't so bad...

# Function        : jsonParse
#
# @brief          : Parse a line of JSON into an hash-object.
#
# @param[in] line : The JSON content.
#
# @return hash    : The parsed object.
#
sub jsonParse
{
    my $line = shift;

    die "Invalid JSON format: $line" unless ($line =~ m/^\{.*\}$/);
    $line =~ s/^\{(.*)}$/$1/;

    my %object = ();

    while($line ne "")
    {
        my $key;
        my $value;

        ($key, $line) = jsonGetString($line);
        $key =~ s/^"(.*)"$/$1/;

        $line =~ s/^://;
        if ($line =~ m/^"/)
        {
            ($value, $line) = jsonGetString($line);
            $value =~ s/^"(.*)"$/$1/;
        }
        elsif ($line =~ m/^{/)
        {
            ($value, $line) = jsonGetObject($line);
            $value = jsonParse($value);
        }
        elsif ($line =~ m/^\[/)
        {
            ($value, $line) = jsonGetArray($line);
            $value = jsonParseArray($value);
        }
        else
        {
            $line =~ s/([^,]*)//;
            $value = $1;
        }

        $object{$key} = $value;
    }

    return \%object;
}

# Function        : jsonGetString
#
# @brief          : Utility function for jsonParse that extracts
#                   the string data in a given object
#
# @param[in] line : The JSON line containing the strings.
#
# @return strings : The parsed strings.
#
sub jsonGetString
{
    my $line = shift;

    $line =~ /("[^"]*")(.*)/;
    my $first = $1;
    my $second = $2;

    if ($first =~ m/\\"$/)
    {
        my ($more, $rest) = jsonGetString($second);
        return ($first.$more , $rest);
    }
    else
    {
        return ($first, $second);
    }
}

# Function        : jsonGetObject
#
# @brief          : Utility function for jsonParse that extracts
#                   the nested JSON object data in a given object
#
# @param[in] line : The JSON line containing the object
#
# @return  object : The nested object
#
sub jsonGetObject
{
    my $line = shift;

    $line =~ s/^{//;
    my $object = "{";
    my $frag = "";

    my $found_object = 0;

    until ((not $found_object) && ($object =~ m/}$/))
    {
        $found_object = 0;

        if ($line =~ m/^\{/)
        {
            ($frag, $line) = jsonGetObject($line);
            $object = $object.$frag;
            $found_object = 1;
        }
        elsif ($line =~ m/^"/)
        {
            ($frag, $line) = jsonGetString($line);
            $object = $object.$frag;
        }
        elsif ($line =~ m/^\[/)
        {
            ($frag, $line) = jsonGetArray($line);
            $object = $object.$frag;
        }
        elsif ($line =~ m/^[:,}]/)
        {
            $line =~ s/^([:,}])//;
            $frag = $1;
            $object = $object.$frag;
        }
        else
        {
            $line =~ s/([^,}]*)//;
            $frag = $1;
            $object = $object.$frag;
        }
    }

    return ($object, $line);
}

# Function        : jsonGetArray
#
# @brief          : Utility function for jsonParse that extracts
#                   the array in a given object
#
# @param[in] line : The JSON line containing the array
#
# @return  array  : The array object
#
sub jsonGetArray
{
    my $line = shift;

    $line =~ s/^\[//;
    my $array = "[";
    my $frag = "";

    my $found_array = 0;

    until ((not $found_array) && ($array =~ m/]$/))
    {
        $found_array = 0;

        if ($line =~ m/^\[/)
        {
            ($frag, $line) = jsonGetArray($line);
            $array = $array.$frag;
            $found_array;
        }
        elsif ($line =~ m/^\{/)
        {
            ($frag, $line) = jsonGetObject($line);
            $array = $array.$frag;
        }
        elsif ($line =~ m/^"/)
        {
            ($frag, $line) = jsonGetString($line);
            $array = $array.$frag;
        }
        elsif ($line =~ m/^[:,\]]/)
        {
            $line =~ s/^([:,\]])//;
            $frag = $1;
            $array = $array.$frag;
        }
        else
        {
            $line =~ s/([^,]*)//;
            $frag = $1;
            $array = $array.$frag;
        }
    }

    return ($array, $line);
}

# Function        : jsonParseArray
#
# @brief          : Utility function for jsonParse that parses
#                   the array object
#
# @param[in] line : The array
#
# @return  array  : The parsed array object
#
#
sub jsonParseArray
{
    my $line = shift;

    $line =~ s/^\[(.*)\]$/$1/;

    my @array = ();

    while ($line ne "")
    {
        my $value;

        if ($line =~ m/^"/)
        {
            ($value, $line) = jsonGetString($line);
            $value =~ s/^"(.*)"$/$1/;
        }
        elsif ($line =~ m/^\{/)
        {
            ($value, $line) = jsonGetObject($line);
            $value = jsonParse($value);
        }
        elsif ($line =~ m/^\[/)
        {
            ($value, $line) = jsonGetArray($line);
            $value = jsonParseArray($value);
        }
        else
        {
            $line =~ s/([^,]*)//;
            $value = $1;
        }

        push @array, $value;
        $line =~ s/^,//;
    }

    return \@array;
}

#################### End Gerrit JSON Utility Subroutines #######################

# Function           : gerritIsPatch
#
# @brief             : Determine if patch identifier is a Gerrit patch or not.
#
# @param[in] i_patch : The patch to make determination about.
#
# @retval flag       : true/false (patch is/not a valid ID)
#
sub gerritIsPatch
{
    my $patch = shift;
    return 1 if ($patch =~ m/I[0-9a-f]+/);
    return 0;
}

# Function : configFilename
#
# @brief   : Create the file that stroes the GIT server details
#
# @return  : Location of the config file
#
sub configFilename
{
    return gitRoot()."/.git/gitRelease.config";
}

# Function : configInit
#
# @brief   : Fetch & write server details to the config file
#
sub configInit
{
    return if (defined $globals{configInit});

    unless (-e configFilename())
    {
        open COMMAND, "git config --list | grep remote.*ssh |";
        my $url = <COMMAND>;
        close COMMAND;
        chomp $url;

        die "Undefined git-remote 'gerrit'" if ($url eq "");

        die "Unexpected url found: $url" if (not ($url =~ m/ssh:\/\/.*\/.*/));

        my $server = $url;
        my $project = $url;

        # match first occurance of '/' after ssh://
        # eg: remote.gerrit.url=ssh://hw.gerrit/hw/ppe
        # $2 is 'hw.gerrit'
        # $3 is 'hw/ppe'
        $server =~ s/(.*)ssh:\/\/(.*?)\/(.*)/$2/;
        $project =~ s/(.*)ssh:\/\/(.*?)\/(.*)/$3/;

        open(UNUSED, ">".configFilename()) || die;
        close UNUSED;

        system("git config --file ".configFilename().
               " --add releaseLevels.server $server");
        system("git config --file ".configFilename().
               " --add releaseLevels.project $project");
    }
    $globals{configInit} = 1;
}

# Function : configProject
#
# @brief   : Fetch the project name of the current configured repository
#
# @return  : GIT project name
#
sub configProject
{
    return $globals{config_project} if (defined $globals{config_project});

    configInit();

    open COMMAND, "git config --file ".configFilename().
                  " --get releaseLevels.project |";
    my $project = <COMMAND>; chomp($project);
    close COMMAND;

    die "Project config does not exist" if ($project eq "");

    $globals{config_project} = $project;

    return $project;
}

# Function : configServer
#
# @brief   : Fetch the server name of the current configured repository
#
# @return  : GIT server location
#
sub configServer
{
    return $globals{config_server} if (defined $globals{config_server});

    configInit();


    open COMMAND, "git config --file ".configFilename().
                  " --get releaseLevels.server |";
    my $server = <COMMAND>; chomp($server);
    close COMMAND;

    die "Server config does not exist" if ($server eq "");

    $globals{config_server} = $server;
    return $server;

}

# Function : gerritSSHCommand
#
# @brief   : Creates a properly formed ssh command based on the server address
#
# @return  : The basic ssh command to connect to the server.
#
sub gerritSSHCommand
{
    return $globals{gerrit_ssh_command}
        if (defined $globals{gerrit_ssh_command});

    my $server = configServer();
    my $port = "";

    if ($server =~ m/.*:.*/)
    {
        $port = $server;
        $server =~ s/(.*):.*/$1/;
        $port =~ s/.*:(.*)/$1/;

        $port = "-p $port";
    }

    my $command = "ssh -qx $port $server gerrit";

    $globals{gerrit_ssh_command} = $command;
    return $command;
}

# Function         : gerritQuery
#
# @brief           : Performs a gerrit query and parses the resulting JSON.
#
# @param[in] query : The query to perform.
#
# @return  item    : A list of items from the JSON query.  Each item is a
#                    hash (key-value pair) for the item attributes.
#
sub gerritQuery
{
    my $query = shift;
    my @items = ();

    $query = gerritSSHCommand()." query $query --current-patch-set --patch-sets --format=JSON |";

    open COMMAND, $query;
    while (my $line = <COMMAND>)
    {
        chomp $line;
        push @items, jsonParse($line);
    }

    return \@items;
}

# Function               : gerritQueryReference
#
# @brief                 : Retrieves reference for a patch id, patchset number
#
# @param[in] changeId    : Change id of the patch
# @param[in] patchNumber : Patch set number
#
# @return  reference     : The reference string
#
sub gerritQueryReference
{
    my $changeId = shift;
    my $patchNumber = shift;

    my $project = configProject();

    my $query_result = gerritQuery("$changeId project:$project");

    foreach my $result (@{$query_result})
    {
        if ($result->{id} eq $changeId)
        {
            # If all patchsets queried, search all of them for the commit
            foreach my $patchset (@{$result->{patchSets}})
            {
                if ($patchNumber eq " ")
                {
                    return $patchset->{currentPatchSet}->{ref};
                }
                else
                {
                    if ($patchset->{number} =~ m/$patchNumber/)
                    {
                        return $patchset->{ref};
                    }
                }
            }
        }
    }
    die "Cannot find $changeId in $project";
}

# Function               : gerritQueryCommit
#
# @brief                 : Retrieves commit for a patch id, patchset number
#
# @param[in] changeId    : Change id of the patch
# @param[in] patchNumber : Patch set number
#
# @return  commit        : The commit string
#
sub gerritQueryCommit
{
    my $changeId = shift;
    my $patchNumber = shift;

    my $project = configProject();

    my $query_result = gerritQuery("$changeId project:$project");

    foreach my $result (@{$query_result})
    {
        if ($result->{id} eq $changeId)
        {
            # If all patchsets queried, search all of them for the commit
            foreach my $patchset (@{$result->{patchSets}})
            {
                if ($patchNumber eq "")
                {
                    return $patchset->{currentPatchSet}->{revision};
                }
                else
                {
                    if ($patchset->{number} =~ m/$patchNumber/)
                    {
                        return $patchset->{revision};
                    }
                }
            }
        }
    }
    die "Cannot find $changeId in $project";
}

# Function             : patchMergeStatus
#
# @brief               : Check if given patch is merged into repository
#
# @param[in] changeId  : Change id of the patch
#
# @return  mergeStatus : 1 if merged; else 0
#
sub patchMergeStatus
{
    my $mergeStatus = 1;

    my $changeId = shift;

    my $project = configProject();

    my $query_result = gerritQuery("$changeId project:$project");

    foreach my $result (@{$query_result})
    {
        if ($result->{id} eq $changeId)
        {
            if ($result->{status} eq "MERGED" || $result->{status} eq "merged")
            {
                $mergeStatus = 1;
            }
            else
            {
                $mergeStatus = 0;
            }
            return $mergeStatus;
        }
    }
    die "Cannot find $changeId in $project";
}
OpenPOWER on IntegriCloud