aboutsummaryrefslogtreecommitdiff
path: root/llvm_tools/git.py
blob: 7ca44b047a748f412e3281e51d59f7e37b1f9101 (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
#!/usr/bin/env python3
# Copyright 2020 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Git helper functions."""

import collections
import os
from pathlib import Path
import re
import subprocess
import tempfile
from typing import Iterable, Optional, Union


CommitContents = collections.namedtuple("CommitContents", ["url", "cl_number"])


def CreateBranch(repo: Union[Path, str], branch: str) -> None:
    """Creates a branch in the given repo.

    Args:
        repo: The absolute path to the repo.
        branch: The name of the branch to create.

    Raises:
        ValueError: Failed to create a repo in that directory.
    """

    if not os.path.isdir(repo):
        raise ValueError("Invalid directory path provided: %s" % repo)

    subprocess.check_output(["git", "-C", repo, "reset", "HEAD", "--hard"])

    subprocess.check_output(["repo", "start", branch], cwd=repo)


def DeleteBranch(repo: Union[Path, str], branch: str) -> None:
    """Deletes a branch in the given repo.

    Args:
        repo: The absolute path of the repo.
        branch: The name of the branch to delete.

    Raises:
        ValueError: Failed to delete the repo in that directory.
    """

    if not os.path.isdir(repo):
        raise ValueError("Invalid directory path provided: %s" % repo)

    def run_checked(cmd):
        subprocess.run(["git", "-C", repo] + cmd, check=True)

    run_checked(["checkout", "-q", "m/main"])
    run_checked(["reset", "-q", "HEAD", "--hard"])
    run_checked(["branch", "-q", "-D", branch])


def CommitChanges(
    repo: Union[Path, str], commit_messages: Iterable[str]
) -> None:
    """Commit changes without uploading them.

    Args:
        repo: The absolute path to the repo where changes were made.
        commit_messages: Messages to concatenate to form the commit message.
    """
    if not os.path.isdir(repo):
        raise ValueError("Invalid path provided: %s" % repo)

    # Create a git commit.
    with tempfile.NamedTemporaryFile(mode="w+t", encoding="utf-8") as f:
        f.write("\n".join(commit_messages))
        f.flush()

        subprocess.check_output(["git", "commit", "-F", f.name], cwd=repo)


def UploadChanges(
    repo: Union[Path, str],
    branch: str,
    reviewers: Optional[Iterable[str]] = None,
    cc: Optional[Iterable[str]] = None,
    wip: bool = False,
) -> CommitContents:
    """Uploads the changes in the specifed branch of the given repo for review.

    Args:
        repo: The absolute path to the repo where changes were made.
        branch: The name of the branch to upload.
        of the changes made.
        reviewers: A list of reviewers to add to the CL.
        cc: A list of contributors to CC about the CL.
        wip: Whether to upload the change as a work-in-progress.

    Returns:
        A CommitContents value containing the commit URL and change list number.

    Raises:
        ValueError: Failed to create a commit or failed to upload the
        changes for review.
    """

    if not os.path.isdir(repo):
        raise ValueError("Invalid path provided: %s" % repo)

    # Upload the changes for review.
    git_args = [
        "repo",
        "upload",
        "--yes",
        f'--reviewers={",".join(reviewers)}' if reviewers else "--ne",
        "--no-verify",
        f"--br={branch}",
    ]

    if cc:
        git_args.append(f'--cc={",".join(cc)}')
    if wip:
        git_args.append("--wip")

    out = subprocess.check_output(
        git_args,
        stderr=subprocess.STDOUT,
        cwd=repo,
        encoding="utf-8",
    )

    print(out)
    # Matches both internal and external CLs.
    found_url = re.search(
        r"https?://[\w-]*-review.googlesource.com/c/.*/([0-9]+)",
        out.rstrip(),
    )
    if not found_url:
        raise ValueError("Failed to find change list URL.")

    return CommitContents(
        url=found_url.group(0), cl_number=int(found_url.group(1))
    )