pw_presubmit: Fix running from different directory
Replace --repository with -C, which changes the directory before doing
anything.
Change-Id: I630ebb198122320aa402277f365dec9e44b696c6
diff --git a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
index a0c4ded..b3249c1 100755
--- a/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
+++ b/pw_presubmit/py/pw_presubmit/pigweed_presubmit.py
@@ -355,20 +355,21 @@
parser.add_argument(
'--clean',
action='store_true',
- help='Deletes the entire .presubmit directory before starting')
+ help='Delete the entire .presubmit directory before starting.')
parser.add_argument(
'--clean-py',
action='store_true',
- help='Deletes the Python virtualenv at .presubmit/venv before starting'
+ help='Delete the Python virtualenv at .presubmit/venv before starting.'
)
exclusive = parser.add_mutually_exclusive_group()
exclusive.add_argument(
'--install',
action='store_true',
- help='Installs the presubmit as a Git pre-push hook and exits')
+ help='Install the presubmit as a Git pre-push hook and exits.')
exclusive.add_argument('-p',
'--program',
+ dest='program_name',
choices=PROGRAMS,
default='full',
help='Which presubmit program to run')
@@ -386,17 +387,17 @@
def main(
- program: str,
+ program_name: str,
clean: bool,
clean_py: bool,
install: bool,
- repository: str,
+ directory: str,
step: Sequence[str],
**presubmit_args,
) -> int:
"""Entry point for presubmit."""
- environment = pw_presubmit.git_repo_path(PRESUBMIT_PREFIX, repo=repository)
+ environment = pw_presubmit.git_repo_path(PRESUBMIT_PREFIX, repo=directory)
_LOG.debug('Using environment at %s', environment)
if clean and environment.exists():
@@ -406,15 +407,15 @@
if install:
install_hook(__file__, 'pre-push', ['--base', 'origin/master'],
- presubmit_args['repository'])
+ presubmit_args['directory'])
return 0
- program = PROGRAMS[program]
+ program = PROGRAMS[program_name]
if step:
program = [x for x in PROGRAMS['full'] if x.__name__ in step]
if pw_presubmit.run_presubmit(program,
- repository=repository,
+ directory=directory,
**presubmit_args):
return 0
diff --git a/pw_presubmit/py/pw_presubmit/tools.py b/pw_presubmit/py/pw_presubmit/tools.py
index 2e1715d..803cb7d 100644
--- a/pw_presubmit/py/pw_presubmit/tools.py
+++ b/pw_presubmit/py/pw_presubmit/tools.py
@@ -359,11 +359,12 @@
add_path_arguments(parser)
parser.add_argument(
- '-r',
- '--repository',
+ '-C',
+ dest='directory',
default='.',
- help=('Path to the repository in which to run the checks; '
- "defaults to the current directory's Git repo."))
+ help=(
+ 'Change to this directory before resolving paths or running the '
+ 'presubmit. Presubmit checks must be run from a Git repository.'))
parser.add_argument('-k',
'--keep-going',
action='store_true',
@@ -374,7 +375,7 @@
base: Optional[str] = None,
paths: Sequence[str] = (),
exclude: Sequence = (),
- repository: str = '.',
+ directory=None,
keep_going: bool = False) -> bool:
"""Lists files in the current Git repo and runs a Presubmit with them.
@@ -386,18 +387,21 @@
base: optional base Git commit to list files against
paths: optional list of paths to run the presubmit checks against
exclude: regular expressions of paths to exclude from checks
- repository: path to the repository in which to run the checks
+ directory: change to this directory before resolving paths or running
keep_going: whether to continue running checks if an error occurs
Returns:
True if all presubmit checks succeeded
"""
- if not is_git_repo(repository):
+ if directory:
+ os.chdir(directory)
+
+ if not is_git_repo():
_LOG.critical('Presubmit checks must be run from a Git repo')
return False
files = list_git_files(base, paths, exclude)
- root = git_repo_path(repo=repository)
+ root = git_repo_path()
if not root.samefile(pathlib.Path.cwd()):
_LOG.info('Checking files in the %s subdirectory of the %s repository',