aboutsummaryrefslogtreecommitdiff
path: root/pw_presubmit
diff options
context:
space:
mode:
authorRob Mohr <mohrr@google.com>2022-01-04 11:00:10 -0800
committerCQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>2022-01-05 22:30:58 +0000
commitb839d428445ebad1119f6337cee56c3a5f1574da (patch)
tree20f74cf4af083109aed0c4d7a9373771fe391d2e /pw_presubmit
parent66c509979444b2de1954a12b9a8988066f5c65af (diff)
downloadpigweed-b839d428445ebad1119f6337cee56c3a5f1574da.tar.gz
pw_presubmit: Default --base to tracking branch
Default 'pw presubmit' to using the current branch's tracking branch as the base commit for finding changed files (instead of running on all files). Bug: 588 Change-Id: I2cdf2919e83b63b5b884cc1cb869e1cdda7a30e5 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/76102 Reviewed-by: John Wiederhirn <wiederhirn@google.com> Reviewed-by: Ewout van Bekkum <ewout@google.com> Commit-Queue: Rob Mohr <mohrr@google.com>
Diffstat (limited to 'pw_presubmit')
-rw-r--r--pw_presubmit/py/pw_presubmit/cli.py4
-rw-r--r--pw_presubmit/py/pw_presubmit/git_repo.py44
-rw-r--r--pw_presubmit/py/pw_presubmit/presubmit.py8
3 files changed, 53 insertions, 3 deletions
diff --git a/pw_presubmit/py/pw_presubmit/cli.py b/pw_presubmit/py/pw_presubmit/cli.py
index 0bb82f869..528143a9f 100644
--- a/pw_presubmit/py/pw_presubmit/cli.py
+++ b/pw_presubmit/py/pw_presubmit/cli.py
@@ -41,7 +41,9 @@ def add_path_arguments(parser) -> None:
'-b',
'--base',
metavar='commit',
- help='Git revision against which to diff for changed files.')
+ default=git_repo.USE_TRACKING_BRANCH,
+ help=('Git revision against which to diff for changed files. '
+ 'Default is the tracking branch of the current branch.'))
base.add_argument(
'--full',
dest='base',
diff --git a/pw_presubmit/py/pw_presubmit/git_repo.py b/pw_presubmit/py/pw_presubmit/git_repo.py
index 5ab5301ab..f2fb22d82 100644
--- a/pw_presubmit/py/pw_presubmit/git_repo.py
+++ b/pw_presubmit/py/pw_presubmit/git_repo.py
@@ -24,6 +24,8 @@ from pw_presubmit.tools import log_run, plural
_LOG = logging.getLogger(__name__)
PathOrStr = Union[Path, str]
+USE_TRACKING_BRANCH = object()
+
def git_stdout(*args: PathOrStr,
show_stderr=False,
@@ -55,6 +57,45 @@ def _diff_names(commit: str, pathspecs: Collection[PathOrStr],
yield git_root / file
+def tracking_branch(repo_path: Path = None,
+ suppress_exception: bool = True) -> Optional[str]:
+ """Returns the tracking branch of the current branch.
+
+ Since most callers of this function can safely handle a return value of
+ None, default to suppressing exceptions and returning None when they
+ happen.
+
+ Args:
+ repo_path: repo path from which to run commands; defaults to Path.cwd()
+ suppress_exception: Whether to suppress CalledProcessError exceptions
+
+ Raises:
+ CalledProcessError: HEAD does not point to a branch or the branch is not
+ tracking another branch.
+ """
+ if repo_path is None:
+ repo_path = Path.cwd()
+
+ # This command should raise an exception if repo_path is not a git
+ # repository. Always raise errors in that case.
+ _ = git_stdout('remote', '-v', repo=repo_path)
+
+ # This command should only error out if there's no upstream branch set.
+ try:
+ return git_stdout('rev-parse',
+ '--abbrev-ref',
+ '--symbolic-full-name',
+ '@{u}',
+ repo=repo_path)
+
+ except subprocess.CalledProcessError:
+ if not suppress_exception:
+ raise
+ _LOG.warning('Error retrieving remote tracking branch of %s',
+ repo_path)
+ return None
+
+
def list_files(commit: Optional[str] = None,
pathspecs: Collection[PathOrStr] = (),
repo_path: Optional[Path] = None) -> List[Path]:
@@ -71,6 +112,9 @@ def list_files(commit: Optional[str] = None,
if repo_path is None:
repo_path = Path.cwd()
+ if commit is USE_TRACKING_BRANCH:
+ commit = tracking_branch(repo_path)
+
if commit:
try:
return sorted(_diff_names(commit, pathspecs, repo_path))
diff --git a/pw_presubmit/py/pw_presubmit/presubmit.py b/pw_presubmit/py/pw_presubmit/presubmit.py
index 599ef1736..e64ba3cc2 100644
--- a/pw_presubmit/py/pw_presubmit/presubmit.py
+++ b/pw_presubmit/py/pw_presubmit/presubmit.py
@@ -439,12 +439,16 @@ def run(program: Sequence[Callable],
files: List[Path] = []
for repo, pathspecs in pathspecs_by_repo.items():
+ repo_base = base
+ if repo_base is git_repo.USE_TRACKING_BRANCH:
+ repo_base = git_repo.tracking_branch(repo)
+
files += tools.exclude_paths(
- exclude, git_repo.list_files(base, pathspecs, repo), root)
+ exclude, git_repo.list_files(repo_base, pathspecs, repo), root)
_LOG.info(
'Checking %s',
- git_repo.describe_files(repo, repo, base, pathspecs, exclude))
+ git_repo.describe_files(repo, repo, repo_base, pathspecs, exclude))
if output_directory is None:
output_directory = root / '.presubmit'