[Notes] [Git][BuildStream/buildstream][tpollard/483] plugins/git.py: Fail if ref is not in given track



Title: GitLab

Tom Pollard pushed to branch tpollard/483 at BuildStream / buildstream

Commits:

2 changed files:

Changes:

  • buildstream/plugins/sources/git.py
    ... ... @@ -190,7 +190,7 @@ class GitMirror(SourceFetcher):
    190 190
                 cwd=self.mirror)
    
    191 191
             return output.rstrip('\n')
    
    192 192
     
    
    193
    -    def stage(self, directory):
    
    193
    +    def stage(self, directory, track=None):
    
    194 194
             fullpath = os.path.join(directory, self.path)
    
    195 195
     
    
    196 196
             # Using --shared here avoids copying the objects into the checkout, in any
    
    ... ... @@ -204,10 +204,14 @@ class GitMirror(SourceFetcher):
    204 204
                              fail="Failed to checkout git ref {}".format(self.ref),
    
    205 205
                              cwd=fullpath)
    
    206 206
     
    
    207
    +        # Check that the user specified ref exists in the track if provided & not already tracked
    
    208
    +        if track:
    
    209
    +            self.assert_ref_in_track(fullpath, track)
    
    210
    +
    
    207 211
             # Remove .git dir
    
    208 212
             shutil.rmtree(os.path.join(fullpath, ".git"))
    
    209 213
     
    
    210
    -    def init_workspace(self, directory):
    
    214
    +    def init_workspace(self, directory, track=None):
    
    211 215
             fullpath = os.path.join(directory, self.path)
    
    212 216
             url = self.source.translate_url(self.url)
    
    213 217
     
    
    ... ... @@ -223,6 +227,10 @@ class GitMirror(SourceFetcher):
    223 227
                              fail="Failed to checkout git ref {}".format(self.ref),
    
    224 228
                              cwd=fullpath)
    
    225 229
     
    
    230
    +        # Check that the user specified ref exists in the track if provided & not already tracked
    
    231
    +        if track:
    
    232
    +            self.assert_ref_in_track(fullpath, track)
    
    233
    +
    
    226 234
         # List the submodules (path/url tuples) present at the given ref of this repo
    
    227 235
         def submodule_list(self):
    
    228 236
             modules = "{}:{}".format(self.ref, GIT_MODULES)
    
    ... ... @@ -287,6 +295,22 @@ class GitMirror(SourceFetcher):
    287 295
     
    
    288 296
                 return None
    
    289 297
     
    
    298
    +    # Assert that ref exists in track, if track has been specified.
    
    299
    +    def assert_ref_in_track(self, fullpath, track):
    
    300
    +        _, branch = self.source.check_output([self.source.host_git, 'branch', '--list', track,
    
    301
    +                                              '--contains', self.ref],
    
    302
    +                                             cwd=fullpath,)
    
    303
    +        if branch:
    
    304
    +            return True
    
    305
    +        else:
    
    306
    +            _, tag = self.source.check_output([self.source.host_git, 'tag', '--list', track,
    
    307
    +                                               '--contains', self.ref],
    
    308
    +                                              cwd=fullpath,)
    
    309
    +            if tag:
    
    310
    +                return True
    
    311
    +        raise SourceError("{}: expected ref '{}' was not found in given track '{}' for staged repository: '{}'"
    
    312
    +                          .format(self.source, self.ref, track, self.url))
    
    313
    +
    
    290 314
     
    
    291 315
     class GitSource(Source):
    
    292 316
         # pylint: disable=attribute-defined-outside-init
    
    ... ... @@ -317,6 +341,7 @@ class GitSource(Source):
    317 341
                     self.submodule_checkout_overrides[path] = checkout
    
    318 342
     
    
    319 343
             self.mark_download_url(self.original_url)
    
    344
    +        self.tracked = False
    
    320 345
     
    
    321 346
         def preflight(self):
    
    322 347
             # Check if git is installed, get the binary at the same time
    
    ... ... @@ -374,6 +399,8 @@ class GitSource(Source):
    374 399
                 # Update self.mirror.ref and node.ref from the self.tracking branch
    
    375 400
                 ret = self.mirror.latest_commit(self.tracking)
    
    376 401
     
    
    402
    +        # Set tracked attribute, parameter for if self.mirror.assert_ref_in_track is needed
    
    403
    +        self.tracked = True
    
    377 404
             return ret
    
    378 405
     
    
    379 406
         def init_workspace(self, directory):
    
    ... ... @@ -381,7 +408,7 @@ class GitSource(Source):
    381 408
             self.refresh_submodules()
    
    382 409
     
    
    383 410
             with self.timed_activity('Setting up workspace "{}"'.format(directory), silent_nested=True):
    
    384
    -            self.mirror.init_workspace(directory)
    
    411
    +            self.mirror.init_workspace(directory, track=(self.tracking if not self.tracked else None))
    
    385 412
                 for mirror in self.submodules:
    
    386 413
                     mirror.init_workspace(directory)
    
    387 414
     
    
    ... ... @@ -397,7 +424,7 @@ class GitSource(Source):
    397 424
             # Stage the main repo in the specified directory
    
    398 425
             #
    
    399 426
             with self.timed_activity("Staging {}".format(self.mirror.url), silent_nested=True):
    
    400
    -            self.mirror.stage(directory)
    
    427
    +            self.mirror.stage(directory, track=(self.tracking if not self.tracked else None))
    
    401 428
                 for mirror in self.submodules:
    
    402 429
                     if mirror.path in self.submodule_checkout_overrides:
    
    403 430
                         checkout = self.submodule_checkout_overrides[mirror.path]
    

  • tests/sources/git.py
    ... ... @@ -359,3 +359,33 @@ def test_submodule_track_ignore_inconsistent(cli, tmpdir, datafiles):
    359 359
     
    
    360 360
         # Assert that we are just fine without it, and emit a warning to the user.
    
    361 361
         assert "Ignoring inconsistent submodule" in result.stderr
    
    362
    +
    
    363
    +
    
    364
    +@pytest.mark.skipif(HAVE_GIT is False, reason="git is not available")
    
    365
    +@pytest.mark.datafiles(os.path.join(DATA_DIR, 'template'))
    
    366
    +def test_ref_not_in_track(cli, tmpdir, datafiles):
    
    367
    +    project = os.path.join(datafiles.dirname, datafiles.basename)
    
    368
    +
    
    369
    +    # Create the repo from 'repofiles', create a branch without latest commit
    
    370
    +    repo = create_repo('git', str(tmpdir))
    
    371
    +    ref = repo.create(os.path.join(project, 'repofiles'))
    
    372
    +
    
    373
    +    gitsource = repo.source_config(ref=ref)
    
    374
    +
    
    375
    +    # Overwrite the track value to the added branch
    
    376
    +    gitsource['track'] = 'foo'
    
    377
    +
    
    378
    +    # Write out our test target
    
    379
    +    element = {
    
    380
    +        'kind': 'import',
    
    381
    +        'sources': [
    
    382
    +            gitsource
    
    383
    +        ]
    
    384
    +    }
    
    385
    +    _yaml.dump(element, os.path.join(project, 'target.bst'))
    
    386
    +
    
    387
    +    # Assert that build raises an error here,
    
    388
    +    # as there's no branch foo containing ref
    
    389
    +    result = cli.run(project=project, args=['build', 'target.bst'])
    
    390
    +    result.assert_main_error(ErrorDomain.STREAM, None)
    
    391
    +    result.assert_task_error(ErrorDomain.SOURCE, 'ref-not-in-track')



  • [Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]