summaryrefslogtreecommitdiff
path: root/util/update_release_branch.py
blob: 93527c312ce808422d3b03508c9bf982106bf8fd (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
#!/usr/bin/env python3
# Copyright 2021 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Release branch updater tool.

This is a tool to merge from the main branch into a release branch.

Inspired by the fingerprint release process:
http://go/cros-fingerprint-firmware-branching-and-signing and now used by other
boards.
"""
from __future__ import print_function

import argparse
import os
import re
import subprocess
import sys
import textwrap


BUG_NONE_PATTERN = re.compile("none", flags=re.IGNORECASE)


def git_commit_msg(cros_main, branch, head, merge_head, rel_paths, cmd):
    """Generates a merge commit message based off of relevant changes.

    This function obtains the relevant commits from the given relative paths in
    order to extract the bug numbers. It constructs the git commit message
    showing the command used to find the relevant commits.

    Args:
        cros_main: String indicating the origin branch name
        branch: String indicating the release branch name
        head: String indicating the HEAD refspec
        merge_head: String indicating the merge branch refspec.
        rel_paths: String containing all the relevant paths for this particular
                   baseboard or board.
        cmd: String of the input command.

    Returns:
        A String containing the git commit message with the exception of the
        Signed-Off-By field and Change-ID field.
    """
    relevant_commits_cmd, relevant_commits, relevant_hdr = get_relevant_commits(
        head, merge_head, "--oneline", rel_paths
    )

    _, relevant_bugs, _ = get_relevant_commits(head, merge_head, "", rel_paths)
    relevant_bugs = set(re.findall("BUG=(.*)", relevant_bugs))
    # Filter out "none" from set of bugs
    filtered = []
    for bug_line in relevant_bugs:
        bug_line = bug_line.replace(",", " ")
        bugs = bug_line.split(" ")
        for bug in bugs:
            if bug and not BUG_NONE_PATTERN.match(bug):
                filtered.append(bug)
    relevant_bugs = filtered

    # TODO(b/179509333): remove Cq-Include-Trybots line when regular CQ and
    # firmware CQ do not behave differently.
    commit_msg_template = """
Merge remote-tracking branch {CROS_MAIN} into {BRANCH}

Generated by: {COMMAND_LINE}

{RELEVANT_COMMITS_HEADER}

{RELEVANT_COMMITS_CMD}

{RELEVANT_COMMITS}

BRANCH=None
{BUG_FIELD}
TEST=`make -j buildall`

Force-Relevant-Builds: all
"""
    # Wrap the commands and bug field such that we don't exceed 72 cols.
    relevant_commits_cmd = textwrap.fill(relevant_commits_cmd, width=72)
    cmd = textwrap.fill(cmd, width=72)
    # Wrap at 68 cols to save room for 'BUG='
    bugs = textwrap.wrap(" ".join(relevant_bugs), width=68)
    bug_field = ""
    for line in bugs:
        bug_field += "BUG=" + line + "\n"
    # Remove the final newline since the template adds it for us.
    bug_field = bug_field[:-1]

    return commit_msg_template.format(
        CROS_MAIN=cros_main,
        BRANCH=branch,
        RELEVANT_COMMITS_CMD=relevant_commits_cmd,
        RELEVANT_COMMITS=relevant_commits,
        RELEVANT_COMMITS_HEADER=relevant_hdr,
        BUG_FIELD=bug_field,
        COMMAND_LINE=cmd,
    )


def get_relevant_boards(baseboard):
    """Searches the EC repo looking for boards that use the given baseboard.

    Args:
        baseboard: String containing the baseboard to consider

    Returns:
        A list of strings containing the boards based off of the baseboard.
    """
    proc = subprocess.run(
        ["git", "grep", "BASEBOARD:=" + baseboard, "--", "board/"],
        stdout=subprocess.PIPE,
        encoding="utf-8",
        check=True,
    )
    boards = []
    res = proc.stdout.splitlines()
    for line in res:
        boards.append(line.split("/")[1])
    return boards


def get_relevant_commits(head, merge_head, fmt, relevant_paths):
    """Find relevant changes since last merge.

    Searches git history to find changes since the last merge which modify
    files present in relevant_paths.

    Args:
        head: String indicating the HEAD refspec
        merge_head: String indicating the merge branch refspec.
        fmt: An optional string containing the format option for `git log`
        relevant_paths: String containing all the relevant paths for this
                        particular baseboard or board.

    Returns:
        A tuple containing the arguments passed to the git log command and
        stdout, and the header for the message
    """
    if not relevant_paths:
        return "", "", ""
    if fmt:
        cmd = [
            "git",
            "log",
            fmt,
            head + ".." + merge_head,
            "--",
            relevant_paths,
        ]
    else:
        cmd = ["git", "log", head + ".." + merge_head, "--", relevant_paths]

    # Pass cmd as a string to subprocess.run() since we need to run with shell
    # equal to True.  The reason we are using shell equal to True is to take
    # advantage of the glob expansion for the relevant paths.
    cmd = " ".join(cmd)
    proc = subprocess.run(
        cmd, stdout=subprocess.PIPE, encoding="utf-8", check=True, shell=True
    )
    return "".join(proc.args), proc.stdout, "Relevant changes:"


def merge_repo(
    base, cros_main, cmd_checkout, strategy, cmd, prunelist, relevant_paths
):
    """Merge changes from ToT into this repo's branch.

    For this repo, merge the changes from ToT to the branch set
    in the merge command.

    Args:
        base: String indicating the Source directory of repo.
        cros_main: String indicating the origin branch name
        cmd_checkout: String list containing the checkout command to use.
        strategy: String list containing the merge strategy,
        cmd: String containing the command line
        prunelist: String list containing the files to remove.
        relevant_paths: String containing all the relevant paths for this
                        particular baseboard or board.
    """
    # Change directory to the repo being merged
    print('Starting merge in "%s"' % base)
    print('Checkout command: "%s"' % " ".join(cmd_checkout))
    os.chdir(base)
    # Check if we are already in merge process
    result = subprocess.run(
        ["git", "rev-parse", "--quiet", "--verify", "MERGE_HEAD"],
        stdout=subprocess.DEVNULL,
        stderr=subprocess.DEVNULL,
        check=False,
    )

    if result.returncode:
        # Let's perform the merge
        print("Updating remote...")
        subprocess.run(["git", "remote", "update"], check=True)
        subprocess.run(cmd_checkout, check=True)
        cmd_merge = [
            "git",
            "merge",
            "--no-ff",
            "--no-commit",
            cros_main,
            "-s",
        ]
        cmd_merge.extend(strategy)
        print('Merge command: "%s"' % " ".join(cmd_merge))
        try:
            subprocess.run(cmd_merge, check=True)
        except subprocess.CalledProcessError:
            # We've likely encountered a merge conflict due to new OWNERS file
            # modifications. If we're removing the owners, we'll delete them.
            if prunelist:
                # Find the unmerged files
                unmerged = (
                    subprocess.run(
                        ["git", "diff", "--name-only", "--diff-filter=U"],
                        stdout=subprocess.PIPE,
                        encoding="utf-8",
                        check=True,
                    )
                    .stdout.rstrip()
                    .split()
                )

                # Prune OWNERS files
                for file in unmerged:
                    if file in prunelist:
                        subprocess.run(["git", "rm", file], check=False)
                        unmerged.remove(file)

                print("Removed non-root OWNERS files.")
                if unmerged:
                    print(
                        "Unmerged files still exist! You need to manually resolve this."
                    )
                    print("\n".join(unmerged))
                    sys.exit(1)
            else:
                raise
    else:
        print(
            "We have already started merge process.",
            "Attempt to generate commit.",
        )
    # Check whether any commit is needed.
    changes = subprocess.run(
        ["git", "status", "--porcelain"],
        stdout=subprocess.PIPE,
        encoding="utf-8",
        check=True,
    ).stdout.rstrip()
    if not changes:
        print("No changes have been found, skipping commit.")
        return

    print("Generating commit message...")
    branch = subprocess.run(
        ["git", "rev-parse", "--abbrev-ref", "HEAD"],
        stdout=subprocess.PIPE,
        encoding="utf-8",
        check=True,
    ).stdout.rstrip()
    head = subprocess.run(
        ["git", "rev-parse", "--short", "HEAD"],
        stdout=subprocess.PIPE,
        encoding="utf-8",
        check=True,
    ).stdout.rstrip()
    merge_head = subprocess.run(
        ["git", "rev-parse", "--short", "MERGE_HEAD"],
        stdout=subprocess.PIPE,
        encoding="utf-8",
        check=True,
    ).stdout.rstrip()

    print("Typing as fast as I can...")
    commit_msg = git_commit_msg(
        cros_main, branch, head, merge_head, relevant_paths, cmd
    )
    subprocess.run(["git", "commit", "--signoff", "-m", commit_msg], check=True)
    subprocess.run(["git", "commit", "--amend"], check=True)


def main(argv):
    """Generates a merge commit from ToT to a desired release branch.

    For the commit message, it finds all the commits that have modified a
    relevant path. By default this is the baseboard or board directory.  The
    user may optionally specify a path to a text file which contains a longer
    list of relevant files.  The format should be in the glob syntax that git
    log expects.

    Args:
        argv: A list of the command line arguments passed to this script.
    """
    # Set up argument parser.
    parser = argparse.ArgumentParser(
        description=(
            "A script that generates a "
            "merge commit from cros/main"
            " to a desired release "
            "branch.  By default, the "
            '"recursive" merge strategy '
            'with the "theirs" strategy '
            "option is used."
        )
    )
    parser.add_argument("--baseboard")
    parser.add_argument("--board")
    parser.add_argument(
        "release_branch",
        help=(
            "The name of the target release branch, "
            "without the trailing '-main'."
        ),
    )
    parser.add_argument(
        "--remote_prefix",
        help=(
            "The name of the remote branch prefix (default cros). "
            "Private repos typically use cros-internal instead."
        ),
        default="cros",
    )
    parser.add_argument(
        "--srcbase",
        help=("The base directory where the src tree exists."),
        default="/mnt/host/source/",
    )
    parser.add_argument(
        "--relevant_paths_file",
        help=(
            "A path to a text file which includes other "
            "relevant paths of interest for this board "
            "or baseboard"
        ),
    )
    parser.add_argument(
        "--merge_strategy",
        "-s",
        default="recursive",
        help="The merge strategy to pass to `git merge -s`",
    )
    parser.add_argument(
        "--strategy_option",
        "-X",
        help=("The strategy option for the chosen merge strategy"),
    )
    parser.add_argument(
        "--remove_owners",
        "-r",
        action=("store_true"),
        help=("Remove non-root OWNERS level files if present"),
    )
    parser.add_argument(
        "--zephyr",
        "-z",
        action=("store_true"),
        help=("If set, treat the board as a Zephyr based program"),
    )

    opts = parser.parse_args(argv[1:])

    baseboard_dir = ""
    board_dir = ""

    if opts.baseboard:
        # If a zephyr board, no baseboard allowed
        if opts.zephyr:
            raise Exception("--baseboard not allowed for Zephyr boards")
        # Dereference symlinks so "git log" works as expected.
        baseboard_dir = os.path.relpath("baseboard/" + opts.baseboard)
        baseboard_dir = os.path.relpath(os.path.realpath(baseboard_dir))

        boards = get_relevant_boards(opts.baseboard)
    elif opts.board:
        if opts.zephyr:
            board_dir = os.path.relpath("zephyr/program/" + opts.board)
        else:
            board_dir = os.path.relpath("board/" + opts.board)
        board_dir = os.path.relpath(os.path.realpath(board_dir))
        boards = [opts.board]
    else:
        # With no board or baseboard, not sure whether this should proceed
        raise Exception("no board or baseboard specified")

    print("Gathering relevant paths...")
    relevant_paths = []
    if opts.baseboard:
        relevant_paths.append(baseboard_dir)
    elif opts.board:
        relevant_paths.append(board_dir)

    if not opts.zephyr:
        for board in boards:
            relevant_paths.append("board/" + board)

    # Check for the existence of a file that has other paths of interest.
    # Also check for 'relevant-paths.txt' in the board directory
    if opts.relevant_paths_file and os.path.exists(opts.relevant_paths_file):
        with open(opts.relevant_paths_file, "r") as relevant_paths_file:
            for line in relevant_paths_file:
                if not line.startswith("#"):
                    relevant_paths.append(line.rstrip())
    if os.path.exists("util/getversion.sh"):
        relevant_paths.append("util/getversion.sh")
    relevant_paths = " ".join(relevant_paths)

    # Prune OWNERS files if desired
    prunelist = []
    if opts.remove_owners:
        for root, dirs, files in os.walk("."):
            for name in dirs:
                if "build" in name:
                    continue
            for name in files:
                if "OWNERS" in name:
                    path = os.path.join(root, name)
                    prunelist.append(path[2:])  # Strip the "./"

        # Remove the top level OWNERS file from the prunelist.
        try:
            prunelist.remove("OWNERS")
        except ValueError:
            pass

        if prunelist:
            print("Not merging the following OWNERS files:")
            for path in prunelist:
                print("  " + path)

    # Create the merge and checkout commands to use.
    cmd_checkout = [
        "git",
        "checkout",
        "-B",
        opts.release_branch,
        opts.remote_prefix + "/" + opts.release_branch,
    ]
    if opts.merge_strategy == "recursive" and not opts.strategy_option:
        opts.strategy_option = "theirs"
    print(
        'Using "%s" merge strategy' % opts.merge_strategy,
        (
            "with strategy option '%s'" % opts.strategy_option
            if opts.strategy_option
            else ""
        ),
    )
    cros_main = opts.remote_prefix + "/" + "main"
    strategy = [
        opts.merge_strategy,
    ]
    if opts.strategy_option:
        strategy.append("-X" + opts.strategy_option)
    cmd = " ".join(argv)

    # Merge each of the repos
    merge_repo(
        os.path.join(opts.srcbase, "src/platform/ec"),
        cros_main,
        cmd_checkout,
        strategy,
        cmd,
        prunelist,
        relevant_paths,
    )
    if opts.zephyr:
        # Strip off any trailing -main or -master from branch name
        if opts.release_branch.endswith("-main"):
            opts.release_branch = opts.release_branch[:-5]
        if opts.release_branch.endswith("-master"):
            opts.release_branch = opts.release_branch[:-7]
        cmd_checkout = [
            "git",
            "checkout",
            "-B",
            opts.release_branch,
            opts.remote_prefix + "/" + opts.release_branch,
        ]
        prunelist = []
        if opts.remove_owners:
            # Remove the top level OWNERS file from the list
            # to avoid any conflict with the modified branch file.
            prunelist.append("OWNERS")
        merge_repo(
            os.path.join(opts.srcbase, "src/third_party/zephyr/main"),
            cros_main,
            cmd_checkout,
            strategy,
            cmd,
            prunelist,
            [],
        )
        # cmsis repo has different remote
        cros_main = opts.remote_prefix + "/" + "chromeos-main"
        merge_repo(
            os.path.join(opts.srcbase, "src/third_party/zephyr/cmsis"),
            cros_main,
            cmd_checkout,
            strategy,
            cmd,
            prunelist,
            [],
        )
    print(
        (
            "Finished! **Please review the commit(s) to see if they're to your "
            "liking.**"
        )
    )


if __name__ == "__main__":
    main(sys.argv)