diff options
author | Jordan Galby <67924449+Jordan474@users.noreply.github.com> | 2022-06-13 17:22:35 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-13 15:22:35 +0000 |
commit | e50d08ce48b5938a292d6b2c85801c8e42759492 (patch) | |
tree | ea57ab011f14c9a866d6ca367569331da4998865 | |
parent | 696d81513df57f57e2b43ff08d1ffa46037007b5 (diff) | |
download | spack-e50d08ce48b5938a292d6b2c85801c8e42759492.tar.gz spack-e50d08ce48b5938a292d6b2c85801c8e42759492.tar.bz2 spack-e50d08ce48b5938a292d6b2c85801c8e42759492.tar.xz spack-e50d08ce48b5938a292d6b2c85801c8e42759492.zip |
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
-rw-r--r-- | lib/spack/spack/cmd/style.py | 8 | ||||
-rw-r--r-- | lib/spack/spack/test/cmd/style.py | 22 |
2 files changed, 25 insertions, 5 deletions
diff --git a/lib/spack/spack/cmd/style.py b/lib/spack/spack/cmd/style.py index 5c31228391..85d7afa2ce 100644 --- a/lib/spack/spack/cmd/style.py +++ b/lib/spack/spack/cmd/style.py @@ -94,16 +94,16 @@ def changed_files(base="develop", untracked=True, all_files=False, root=None): git = which("git", required=True) # ensure base is in the repo - git("show-ref", "--verify", "--quiet", "refs/heads/%s" % base, - fail_on_error=False) + base_sha = git("rev-parse", "--quiet", "--verify", "--revs-only", base, + fail_on_error=False, output=str) if git.returncode != 0: 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.", "Ensure that '%s' exists, or specify files to check explicitly." % base ) - range = "{0}...".format(base) + range = "{0}...".format(base_sha.strip()) git_args = [ # Add changed files committed since branching off of develop diff --git a/lib/spack/spack/test/cmd/style.py b/lib/spack/spack/test/cmd/style.py index 8b70a791dc..2b8330aa59 100644 --- a/lib/spack/spack/test/cmd/style.py +++ b/lib/spack/spack/test/cmd/style.py @@ -98,6 +98,26 @@ def test_changed_files(flake8_package): 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): """Ensure that we fail gracefully with no base branch.""" tmpdir.join("bin").ensure("spack") @@ -113,7 +133,7 @@ def test_changed_no_base(tmpdir, capfd): changed_files(base="foobar") 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): |