Fix spack style arbitrary git rev as base (#31019)

Allow `spack style -b @` or `spack style -b origin/develop` to work as
expected.

Regression since spack 0.17 #25085
This commit is contained in:
Jordan Galby 2022-06-13 17:22:35 +02:00 committed by GitHub
parent 696d81513d
commit e50d08ce48
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 5 deletions

View file

@ -94,16 +94,16 @@ def changed_files(base="develop", untracked=True, all_files=False, root=None):
git = which("git", required=True) git = which("git", required=True)
# ensure base is in the repo # ensure base is in the repo
git("show-ref", "--verify", "--quiet", "refs/heads/%s" % base, base_sha = git("rev-parse", "--quiet", "--verify", "--revs-only", base,
fail_on_error=False) fail_on_error=False, output=str)
if git.returncode != 0: if git.returncode != 0:
tty.die( tty.die(
"This repository does not have a '%s' branch." % base, "This repository does not have a '%s' revision." % base,
"spack style needs this branch to determine which files changed.", "spack style needs this branch to determine which files changed.",
"Ensure that '%s' exists, or specify files to check explicitly." % base "Ensure that '%s' exists, or specify files to check explicitly." % base
) )
range = "{0}...".format(base) range = "{0}...".format(base_sha.strip())
git_args = [ git_args = [
# Add changed files committed since branching off of develop # Add changed files committed since branching off of develop

View file

@ -98,6 +98,26 @@ def test_changed_files(flake8_package):
assert flake8_package in files assert flake8_package in files
def test_changed_files_from_git_rev_base(tmpdir, capfd):
"""Test arbitrary git ref as base."""
git = which("git", required=True)
with tmpdir.as_cwd():
git("init")
git("checkout", "-b", "main")
git("config", "user.name", "test user")
git("config", "user.email", "test@user.com")
git("commit", "--allow-empty", "-m", "initial commit")
tmpdir.ensure('bin/spack')
assert changed_files(base="HEAD") == ['bin/spack']
assert changed_files(base="main") == ['bin/spack']
git("add", 'bin/spack')
git("commit", "-m", "v1")
assert changed_files(base="HEAD") == []
assert changed_files(base="HEAD~") == ["bin/spack"]
def test_changed_no_base(tmpdir, capfd): def test_changed_no_base(tmpdir, capfd):
"""Ensure that we fail gracefully with no base branch.""" """Ensure that we fail gracefully with no base branch."""
tmpdir.join("bin").ensure("spack") tmpdir.join("bin").ensure("spack")
@ -113,7 +133,7 @@ def test_changed_no_base(tmpdir, capfd):
changed_files(base="foobar") changed_files(base="foobar")
out, err = capfd.readouterr() out, err = capfd.readouterr()
assert "This repository does not have a 'foobar' branch." in err assert "This repository does not have a 'foobar'" in err
def test_changed_files_all_files(flake8_package): def test_changed_files_all_files(flake8_package):