[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [Git][tpo/applications/tor-browser][tor-browser-147.0a1-16.0-2] fixup! BB 41803: Add some developer tools for working on tor-browser.



Title: GitLab

henry pushed to branch tor-browser-147.0a1-16.0-2 at The Tor Project / Applications / Tor Browser

Commits:

  • c344d0b6
    by Henry Wilkes at 2026-01-14T17:14:26+00:00
    fixup! BB 41803: Add some developer tools for working on tor-browser.
    
    TB 44452: Improve the range-diff and diff-diff commands.
    

1 changed file:

Changes:

  • tools/base_browser/tb-dev
    ... ... @@ -1409,63 +1409,102 @@ def move_to_default(args: argparse.Namespace) -> None:
    1409 1409
         git_run(["cherry-pick", f"{current_basis}..{old_branch_name}"], check=False)
    
    1410 1410
     
    
    1411 1411
     
    
    1412
    +def convert_ref_to_range(ref: str) -> str:
    
    1413
    +    """
    
    1414
    +    Convert a reference given by the user into a reference range, defaulting to
    
    1415
    +    the last firefox reference.
    
    1416
    +
    
    1417
    +    :param ref: The reference to convert to a range, if it isn't one already.
    
    1418
    +    :returns: The reference range.
    
    1419
    +    """
    
    1420
    +    if ".." in ref:
    
    1421
    +        return ref
    
    1422
    +    firefox_commit = get_firefox_ref(ref).commit
    
    1423
    +    return f"{firefox_commit}..{ref}"
    
    1424
    +
    
    1425
    +
    
    1412 1426
     def show_range_diff(args: argparse.Namespace) -> None:
    
    1413 1427
         """
    
    1414
    -    Show the range diff between two branches, from their firefox bases.
    
    1428
    +    Show the range diff. If a single reference is given as one of the arguments,
    
    1429
    +    its last "FIREFOX_" tag is used as the range start.
    
    1415 1430
         """
    
    1416
    -    firefox_commit_1 = get_firefox_ref(args.branch1).commit
    
    1417
    -    firefox_commit_2 = get_firefox_ref(args.branch2).commit
    
    1431
    +    range1 = convert_ref_to_range(args.ref1)
    
    1432
    +    range2 = convert_ref_to_range(args.ref2)
    
    1418 1433
         git_run(
    
    1419
    -        [
    
    1420
    -            "range-diff",
    
    1421
    -            f"{firefox_commit_1}..{args.branch1}",
    
    1422
    -            f"{firefox_commit_2}..{args.branch2}",
    
    1423
    -        ],
    
    1424
    -        check=False,
    
    1434
    +        ["range-diff", *args.gitargs, range1, range2, "--", *args.path], check=False
    
    1425 1435
         )
    
    1426 1436
     
    
    1427 1437
     
    
    1428 1438
     def show_diff_diff(args: argparse.Namespace) -> None:
    
    1429 1439
         """
    
    1430
    -    Show the diff between the diffs of two branches, relative to their firefox
    
    1431
    -    bases.
    
    1440
    +    Show the diff between the diffs of two ranges. If a single reference is
    
    1441
    +    given as one of the arguments, its last "FIREFOX_" tag is used as the range
    
    1442
    +    start.
    
    1432 1443
         """
    
    1433 1444
         try:
    
    1434 1445
             diff_tool = next(git_lines(["config", "--get", "diff.tool"]))
    
    1435 1446
         except StopIteration:
    
    1436 1447
             raise TbDevException("No diff.tool configured for git")
    
    1437 1448
     
    
    1449
    +    drop_context = not args.keep_context
    
    1450
    +
    
    1438 1451
         # Filter out parts of the diff we expect to be different.
    
    1439 1452
         index_regex = re.compile(r"index [0-9a-f]{12}\.\.[0-9a-f]{12}")
    
    1440
    -    lines_regex = re.compile(r"@@ -[0-9]+,[0-9]+ \+[0-9]+,[0-9]+ @@(?P<rest>.*)")
    
    1453
    +    context_regex = re.compile(r"@@ -[0-9]+(:?,[0-9]+)? \+[0-9]+(:?,[0-9]+)? @@")
    
    1441 1454
     
    
    1442
    -    def save_diff(branch: str) -> str:
    
    1443
    -        firefox_commit = get_firefox_ref(branch).commit
    
    1455
    +    # Limit a line length. E.g. "meld" has a line length limit.
    
    1456
    +    LINE_LIMIT = 1024
    
    1457
    +
    
    1458
    +    def save_diff(ref_range: str) -> str:
    
    1444 1459
             file_desc, file_name = tempfile.mkstemp(
    
    1445
    -            text=True, prefix=f'{branch.split("/")[-1]}-'
    
    1460
    +            text=True, prefix=f'{ref_range.replace("/", "_")}'
    
    1446 1461
             )
    
    1447 1462
             # Register deleting the file at exit.
    
    1448 1463
             atexit.register(os.remove, file_name)
    
    1449 1464
     
    
    1450 1465
             diff_process = subprocess.Popen(
    
    1451
    -            [GIT_PATH, "diff", f"{firefox_commit}..{branch}"],
    
    1466
    +            # --unified=1 will reduce the context to just directly neighbouring
    
    1467
    +            # lines.
    
    1468
    +            [
    
    1469
    +                GIT_PATH,
    
    1470
    +                "diff",
    
    1471
    +                "--binary",
    
    1472
    +                "--unified=1",
    
    1473
    +                *args.gitargs,
    
    1474
    +                ref_range,
    
    1475
    +                "--",
    
    1476
    +                *args.path,
    
    1477
    +            ],
    
    1452 1478
                 stdout=subprocess.PIPE,
    
    1453 1479
                 text=True,
    
    1480
    +            encoding="utf-8",
    
    1481
    +            errors="replace",
    
    1454 1482
             )
    
    1455 1483
     
    
    1456 1484
             with os.fdopen(file_desc, "w") as file:
    
    1457 1485
                 assert diff_process.stdout is not None
    
    1458 1486
                 for line in diff_process.stdout:
    
    1459
    -                if index_regex.match(line):
    
    1460
    -                    # Fake data that will match.
    
    1461
    -                    file.write("index ????????????..????????????\n")
    
    1462
    -                    continue
    
    1463
    -                lines_match = lines_regex.match(line)
    
    1464
    -                if lines_match:
    
    1465
    -                    # Fake data that will match.
    
    1466
    -                    file.write("@@ ?,? ?,? @@" + lines_match.group("rest"))
    
    1467
    -                    continue
    
    1468
    -                file.write(line)
    
    1487
    +                if drop_context:
    
    1488
    +                    if index_regex.match(line):
    
    1489
    +                        # Fake data that will match.
    
    1490
    +                        file.write("index ????????????..????????????\n")
    
    1491
    +                        continue
    
    1492
    +                    if context_regex.match(line):
    
    1493
    +                        # Fake data that will match.
    
    1494
    +                        file.write("@@ ?,? ?,? @@\n")
    
    1495
    +                        continue
    
    1496
    +
    
    1497
    +                remaining_line = line
    
    1498
    +                while True:
    
    1499
    +                    if len(remaining_line) - 1 > LINE_LIMIT:
    
    1500
    +                        # NOTE: we use `len() - 1` to not count the trailing
    
    1501
    +                        # '\n', which we assume all lines in the diff have.
    
    1502
    +                        # Long line, split with a newline character.
    
    1503
    +                        file.write(remaining_line[:LINE_LIMIT] + "\n")
    
    1504
    +                        remaining_line = remaining_line[:LINE_LIMIT]
    
    1505
    +                    else:
    
    1506
    +                        file.write(remaining_line)
    
    1507
    +                        break
    
    1469 1508
     
    
    1470 1509
             status = diff_process.wait()
    
    1471 1510
             if status != 0:
    
    ... ... @@ -1473,8 +1512,10 @@ def show_diff_diff(args: argparse.Namespace) -> None:
    1473 1512
     
    
    1474 1513
             return file_name
    
    1475 1514
     
    
    1476
    -    file_1 = save_diff(args.branch1)
    
    1477
    -    file_2 = save_diff(args.branch2)
    
    1515
    +    range1 = convert_ref_to_range(args.ref1)
    
    1516
    +    range2 = convert_ref_to_range(args.ref2)
    
    1517
    +    file_1 = save_diff(range1)
    
    1518
    +    file_2 = save_diff(range2)
    
    1478 1519
         subprocess.run([diff_tool, file_1, file_2], check=False)
    
    1479 1520
     
    
    1480 1521
     
    
    ... ... @@ -1483,6 +1524,28 @@ def show_diff_diff(args: argparse.Namespace) -> None:
    1483 1524
     # * -------------------- *
    
    1484 1525
     
    
    1485 1526
     
    
    1527
    +def ref_complete(prefix: str, **_kwargs: Any) -> list[str]:
    
    1528
    +    """
    
    1529
    +    Complete the argument with a reference name.
    
    1530
    +    """
    
    1531
    +    if not within_browser_root():
    
    1532
    +        return []
    
    1533
    +    try:
    
    1534
    +        matching = []
    
    1535
    +        for symbolic_ref in ("HEAD",):
    
    1536
    +            if symbolic_ref.startswith(prefix):
    
    1537
    +                matching.append(symbolic_ref)
    
    1538
    +        for ref_type in ("head", "remote", "tag"):
    
    1539
    +            for ref in get_refs(ref_type, ""):
    
    1540
    +                if ref.name.startswith(prefix):
    
    1541
    +                    matching.append(ref.name)
    
    1542
    +                if ref.full_name.startswith(prefix):
    
    1543
    +                    matching.append(ref.full_name)
    
    1544
    +        return matching
    
    1545
    +    except Exception:
    
    1546
    +        return []
    
    1547
    +
    
    1548
    +
    
    1486 1549
     def branch_complete(prefix: str, **_kwargs: Any) -> list[str]:
    
    1487 1550
         """
    
    1488 1551
         Complete the argument with a branch name.
    
    ... ... @@ -1490,12 +1553,9 @@ def branch_complete(prefix: str, **_kwargs: Any) -> list[str]:
    1490 1553
         if not within_browser_root():
    
    1491 1554
             return []
    
    1492 1555
         try:
    
    1493
    -        branches = [ref.name for ref in get_refs("head", "")]
    
    1494
    -        branches.extend(ref.name for ref in get_refs("remote", ""))
    
    1495
    -        branches.append("HEAD")
    
    1556
    +        return [ref.name for ref in get_refs("head", "") if ref.name.startswith(prefix)]
    
    1496 1557
         except Exception:
    
    1497 1558
             return []
    
    1498
    -    return [br for br in branches if br.startswith(prefix)]
    
    1499 1559
     
    
    1500 1560
     
    
    1501 1561
     parser = argparse.ArgumentParser()
    
    ... ... @@ -1506,6 +1566,8 @@ class ArgConfig(TypedDict):
    1506 1566
         help: str
    
    1507 1567
         metavar: NotRequired[str]
    
    1508 1568
         nargs: NotRequired[str]
    
    1569
    +    action: NotRequired[str]
    
    1570
    +    default: NotRequired[Any]
    
    1509 1571
         completer: NotRequired[Callable[[str], list[str]]]
    
    1510 1572
     
    
    1511 1573
     
    
    ... ... @@ -1573,33 +1635,59 @@ all_commands: dict[str, CommandConfig] = {
    1573 1635
                 },
    
    1574 1636
             },
    
    1575 1637
         },
    
    1576
    -    "branch-range-diff": {
    
    1638
    +    "range-diff": {
    
    1577 1639
             "func": show_range_diff,
    
    1578 1640
             "args": {
    
    1579
    -            "branch1": {
    
    1580
    -                "help": "the first branch to compare",
    
    1581
    -                "metavar": "<branch-1>",
    
    1582
    -                "completer": branch_complete,
    
    1641
    +            "ref1": {
    
    1642
    +                "help": "the first range to compare",
    
    1643
    +                "metavar": "<ref-1>",
    
    1644
    +                "completer": ref_complete,
    
    1583 1645
                 },
    
    1584
    -            "branch2": {
    
    1585
    -                "help": "the second branch to compare",
    
    1586
    -                "metavar": "<branch-2>",
    
    1587
    -                "completer": branch_complete,
    
    1646
    +            "ref2": {
    
    1647
    +                "help": "the second range to compare",
    
    1648
    +                "metavar": "<ref-2>",
    
    1649
    +                "completer": ref_complete,
    
    1650
    +            },
    
    1651
    +            "--path": {
    
    1652
    +                "help": "path filter to pass to git range-diff. Can be given multiple times",
    
    1653
    +                "metavar": "<path>",
    
    1654
    +                "action": "append",
    
    1655
    +                "default": [],
    
    1656
    +            },
    
    1657
    +            "gitargs": {
    
    1658
    +                "help": "additional argument to pass to git range-diff",
    
    1659
    +                "metavar": "-- git-range-diff-arg",
    
    1660
    +                "nargs": "*",
    
    1588 1661
                 },
    
    1589 1662
             },
    
    1590 1663
         },
    
    1591
    -    "branch-diff-diff": {
    
    1664
    +    "diff-diff": {
    
    1592 1665
             "func": show_diff_diff,
    
    1593 1666
             "args": {
    
    1594
    -            "branch1": {
    
    1667
    +            "--keep-context": {
    
    1668
    +                "help": "keep the git context lines as they are, otherwise they are trivialised to reduce differences. You may want to use this if your diff tool, like meld, can filter out these lines from the diff without erasing them",
    
    1669
    +                "action": "store_true",
    
    1670
    +            },
    
    1671
    +            "ref1": {
    
    1595 1672
                     "help": "the first branch to compare",
    
    1596
    -                "metavar": "<branch-1>",
    
    1597
    -                "completer": branch_complete,
    
    1673
    +                "metavar": "<ref-1>",
    
    1674
    +                "completer": ref_complete,
    
    1598 1675
                 },
    
    1599
    -            "branch2": {
    
    1676
    +            "ref2": {
    
    1600 1677
                     "help": "the second branch to compare",
    
    1601
    -                "metavar": "<branch-2>",
    
    1602
    -                "completer": branch_complete,
    
    1678
    +                "metavar": "<ref-2>",
    
    1679
    +                "completer": ref_complete,
    
    1680
    +            },
    
    1681
    +            "--path": {
    
    1682
    +                "help": "path filter to pass to git diff. Can be given multiple times",
    
    1683
    +                "metavar": "<path>",
    
    1684
    +                "action": "append",
    
    1685
    +                "default": [],
    
    1686
    +            },
    
    1687
    +            "gitargs": {
    
    1688
    +                "help": "additional argument to pass to git diff",
    
    1689
    +                "metavar": "-- git-diff-arg",
    
    1690
    +                "nargs": "*",
    
    1603 1691
                 },
    
    1604 1692
             },
    
    1605 1693
         },
    

  • _______________________________________________
    tor-commits mailing list -- tor-commits@xxxxxxxxxxxxxxxxxxxx
    To unsubscribe send an email to tor-commits-leave@xxxxxxxxxxxxxxxxxxxx