tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

vendor_from_git.py (3240B)


      1 #! /usr/bin/env python3
      2 # This Source Code Form is subject to the terms of the Mozilla Public
      3 # License, v. 2.0. If a copy of the MPL was not distributed with this
      4 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
      5 
      6 assert __name__ != "__main__"
      7 
      8 """
      9 Any time we vendor[1] from an external git repo, we want to keep a record of the csets
     10 we're pulling from.
     11 
     12 This script leaves a record of the merge-base reference tip and cherry-picks that we pull
     13 into Gecko. (such as gfx/angle/cherry_picks.txt)
     14 """
     15 
     16 from pathlib import *
     17 import subprocess
     18 import sys
     19 
     20 # --
     21 
     22 
     23 def print_now(*args):
     24    print(*args)
     25    sys.stdout.flush()
     26 
     27 
     28 def run_checked(*args, **kwargs):
     29    print(" ", args)
     30    sys.stdout.flush()
     31    return subprocess.run(args, check=True, **kwargs)
     32 
     33 
     34 # --
     35 
     36 
     37 def record_cherry_picks(dir_in_gecko, merge_base_origin):
     38    # merge_base_origin is not always 'origin'!
     39    base_merge_base_from = Path(dir_in_gecko, "MERGE_BASE").read_text().split("\n")[0]
     40    merge_base_from = merge_base_origin + "/" + base_merge_base_from
     41 
     42    assert "/" in merge_base_from, "Please specify a reference tip from a remote."
     43    log_path = Path(dir_in_gecko, "cherry_picks.txt")
     44    print_now("Logging cherry picks to {}.".format(log_path))
     45 
     46    merge_base = (
     47        run_checked(
     48            "git", "merge-base", "HEAD", merge_base_from, stdout=subprocess.PIPE
     49        )
     50        .stdout.decode()
     51        .strip()
     52    )
     53    merge_base_readable = (
     54        run_checked(
     55            "git",
     56            "show",
     57            "-s",
     58            "--format=commit %H %cd",
     59            merge_base,
     60            stdout=subprocess.PIPE,
     61        )
     62        .stdout.decode()
     63        .strip()
     64    )
     65 
     66    mb_info = run_checked(
     67        "git",
     68        "log",
     69        "--format=medium",
     70        "--no-decorate",
     71        "--no-abbrev-commit",
     72        "{}~1..{}".format(merge_base, merge_base),
     73        stdout=subprocess.PIPE,
     74    ).stdout
     75    cherries = run_checked(
     76        "git",
     77        "log",
     78        "--format=medium",
     79        "--no-decorate",
     80        "--no-abbrev-commit",
     81        merge_base + "..",
     82        stdout=subprocess.PIPE,
     83    ).stdout
     84 
     85    with open(log_path, "wb") as f:
     86        f.write(cherries)
     87        f.write(b"\nAbove: cherries picked")
     88        f.write(b"\n" + (b"=" * 80))
     89        f.write(b"\nBelow: merge base from: " + merge_base_from.encode())
     90        f.write(b"\n\n")
     91        f.write(mb_info)
     92 
     93    # The below supports only a single commit-alert task. If/when we add another task, this
     94    # will need to be improved.
     95    print_now("Updating moz.yaml")
     96    moz_yaml_file = Path(dir_in_gecko, "moz.yaml")
     97    with open(moz_yaml_file, "r") as f:
     98        moz_yaml_contents = f.readlines()
     99    with open(moz_yaml_file, "wb") as f:
    100        for line in moz_yaml_contents:
    101 
    102            def prefix():
    103                return line[0 : line.index(": ") + 2].encode()
    104 
    105            if "branch: " in line:
    106                f.write(prefix() + base_merge_base_from.encode() + b"\n")
    107            elif "release: " in line:
    108                f.write(prefix() + merge_base_readable.encode() + b"\n")
    109            elif "revision: " in line:
    110                f.write(prefix() + merge_base.encode() + b"\n")
    111            else:
    112                f.write(line.encode())