diff options
author | Jürg Billeter <j@bitron.ch> | 2018-09-27 13:46:41 +0000 |
---|---|---|
committer | Jürg Billeter <j@bitron.ch> | 2018-09-27 13:46:41 +0000 |
commit | ab1cb6724b115e3d6f4f17676d11339e3094e6dc (patch) | |
tree | e8cf639100378995d5f00cb1ad3f0b5e1b014d1d | |
parent | 44da8175253c78b03dcfefd4693f804c45c72d11 (diff) | |
parent | 8060ad8c37afa5b09813fd74625c1a2d54be7a7e (diff) | |
download | buildstream-ab1cb6724b115e3d6f4f17676d11339e3094e6dc.tar.gz |
Merge branch 'juerg/git-describe' into 'master'
git.py: Make `ref` human readable
See merge request BuildStream/buildstream!291
-rw-r--r-- | buildstream/plugins/sources/git.py | 26 | ||||
-rw-r--r-- | tests/sources/git.py | 47 | ||||
-rw-r--r-- | tests/testutils/repo/git.py | 3 |
3 files changed, 74 insertions, 2 deletions
diff --git a/buildstream/plugins/sources/git.py b/buildstream/plugins/sources/git.py index cc6d35cd0..77db9534a 100644 --- a/buildstream/plugins/sources/git.py +++ b/buildstream/plugins/sources/git.py @@ -43,6 +43,12 @@ git - stage files from a git repository # will be used to update the 'ref' when refreshing the pipeline. track: master + # Optionally specify the ref format used for tracking. + # The default is 'sha1' for the raw commit hash. + # If you specify 'git-describe', the commit hash will be prefixed + # with the closest tag. + ref-format: sha1 + # Specify the commit ref, this must be specified in order to # checkout sources and build, but can be automatically updated # if the 'track' attribute was specified. @@ -205,7 +211,18 @@ class GitMirror(SourceFetcher): [self.source.host_git, 'rev-parse', tracking], fail="Unable to find commit for specified branch name '{}'".format(tracking), cwd=self.mirror) - return output.rstrip('\n') + ref = output.rstrip('\n') + + if self.source.ref_format == 'git-describe': + # Prefix the ref with the closest tag, if available, + # to make the ref human readable + exit_code, output = self.source.check_output( + [self.source.host_git, 'describe', '--tags', '--abbrev=40', '--long', ref], + cwd=self.mirror) + if exit_code == 0: + ref = output.rstrip('\n') + + return ref def stage(self, directory, track=None): fullpath = os.path.join(directory, self.path) @@ -341,13 +358,18 @@ class GitSource(Source): def configure(self, node): ref = self.node_get_member(node, str, 'ref', None) - config_keys = ['url', 'track', 'ref', 'submodules', 'checkout-submodules'] + config_keys = ['url', 'track', 'ref', 'submodules', 'checkout-submodules', 'ref-format'] self.node_validate(node, config_keys + Source.COMMON_CONFIG_KEYS) self.original_url = self.node_get_member(node, str, 'url') self.mirror = GitMirror(self, '', self.original_url, ref, primary=True) self.tracking = self.node_get_member(node, str, 'track', None) + self.ref_format = self.node_get_member(node, str, 'ref-format', 'sha1') + if self.ref_format not in ['sha1', 'git-describe']: + provenance = self.node_provenance(node, member_name='ref-format') + raise SourceError("{}: Unexpected value for ref-format: {}".format(provenance, self.ref_format)) + # At this point we now know if the source has a ref and/or a track. # If it is missing both then we will be unable to track or build. if self.mirror.ref is None and self.tracking is None: diff --git a/tests/sources/git.py b/tests/sources/git.py index 8f6074fae..7ab32a6b5 100644 --- a/tests/sources/git.py +++ b/tests/sources/git.py @@ -476,3 +476,50 @@ def test_ref_not_in_track_warn_error(cli, tmpdir, datafiles): result = cli.run(project=project, args=['build', 'target.bst']) result.assert_main_error(ErrorDomain.STREAM, None) result.assert_task_error(ErrorDomain.PLUGIN, CoreWarnings.REF_NOT_IN_TRACK) + + +@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available") +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'template')) +@pytest.mark.parametrize("ref_format", ['sha1', 'git-describe']) +@pytest.mark.parametrize("tag,extra_commit", [(False, False), (True, False), (True, True)]) +def test_track_fetch(cli, tmpdir, datafiles, ref_format, tag, extra_commit): + project = os.path.join(datafiles.dirname, datafiles.basename) + + # Create the repo from 'repofiles' subdir + repo = create_repo('git', str(tmpdir)) + ref = repo.create(os.path.join(project, 'repofiles')) + if tag: + repo.add_tag('tag') + if extra_commit: + repo.add_commit() + + # Write out our test target + element = { + 'kind': 'import', + 'sources': [ + repo.source_config() + ] + } + element['sources'][0]['ref-format'] = ref_format + element_path = os.path.join(project, 'target.bst') + _yaml.dump(element, element_path) + + # Track it + result = cli.run(project=project, args=['track', 'target.bst']) + result.assert_success() + + element = _yaml.load(element_path) + new_ref = element['sources'][0]['ref'] + + if ref_format == 'git-describe' and tag: + # Check and strip prefix + prefix = 'tag-{}-g'.format(0 if not extra_commit else 1) + assert new_ref.startswith(prefix) + new_ref = new_ref[len(prefix):] + + # 40 chars for SHA-1 + assert len(new_ref) == 40 + + # Fetch it + result = cli.run(project=project, args=['fetch', 'target.bst']) + result.assert_success() diff --git a/tests/testutils/repo/git.py b/tests/testutils/repo/git.py index c598eb4d0..bc2dae691 100644 --- a/tests/testutils/repo/git.py +++ b/tests/testutils/repo/git.py @@ -42,6 +42,9 @@ class Git(Repo): self._run_git('commit', '-m', 'Initial commit') return self.latest_commit() + def add_tag(self, tag): + self._run_git('tag', tag) + def add_commit(self): self._run_git('commit', '--allow-empty', '-m', 'Additional commit') return self.latest_commit() |