diff --git a/src/buildstream_plugins/sources/git.py b/src/buildstream_plugins/sources/git.py index 0643947..06ec92a 100644 --- a/src/buildstream_plugins/sources/git.py +++ b/src/buildstream_plugins/sources/git.py @@ -1076,7 +1076,11 @@ def _guess_version(self): commit_sha = None string = self.mirror.ref - + if string is None: + # An untracked source has no ref to guess from: the "sha" + # cannot be discovered, so honor the documented contract + # ("either None or strings") instead of crashing on None. + return (None, version_guess, commits) main_split = string.rsplit("-g", 1) if len(main_split) < 2: # Didn't find the `-g`, assume its a raw git sha as input @@ -1097,6 +1101,8 @@ def _guess_version(self): return (commit_sha, version_guess, commits) def collect_source_info(self): + if self.mirror.ref is None: + return [] extra_data = None commit_sha, version_guess, commits = self._guess_version() if commits: diff --git a/tests/sources/git.py b/tests/sources/git.py index 5b766a3..2983dc2 100644 --- a/tests/sources/git.py +++ b/tests/sources/git.py @@ -1243,3 +1243,22 @@ def test_source_info( commit_offset = None assert commit_offset == expected_offset + + +@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") +@pytest.mark.datafiles(os.path.join(DATA_DIR, "template")) +def test_source_info_untracked_source(cli, tmpdir, datafiles): + project = str(datafiles) + + # Create the repo, but never track the element: the source + # is valid (has a track) yet has no ref. + repo = create_repo("git", str(tmpdir)) + repo.create(os.path.join(project, "repofiles")) + + # Write out our test target with track and NO ref + element = {"kind": "import", "sources": [repo.source_config(ref=None)]} + generate_element(project, "target.bst", element) + + # `bst show` with %{source-info} must not crash on the untracked source + result = cli.run(project=project, args=["show", "--format", "%{name}:\n%{source-info}", "target.bst"]) + result.assert_success() \ No newline at end of file