Check for 'detached' state and invalid branches passed
Add a check for if the tree is in a detached state, and also improve the test on whether any references given as arguments to the constructor are invalid with logging for all invalid refs not just the first detected. Change-Id: Ieebc25337d842ee2cffddd03481da36877d2c27e
This commit is contained in:
@@ -41,26 +41,32 @@ class ImportUpstream(LogDedentMixin, GitMixin):
|
||||
# any computation
|
||||
super(ImportUpstream, self).__init__(*args, **kwargs)
|
||||
|
||||
# test that we can use this git repo
|
||||
if not self.is_detached():
|
||||
raise ImportUpstreamError("In 'detached HEAD' state")
|
||||
|
||||
if self.repo.bare:
|
||||
raise ImportUpstreamError("Cannot perform imports in bare repos")
|
||||
|
||||
if self.branch == 'HEAD':
|
||||
self._branch = self.repo.active_branch
|
||||
|
||||
branches = {
|
||||
'upstream': self.upstream,
|
||||
'branch': self.branch
|
||||
}
|
||||
# validate branches exist and log all failures
|
||||
branches = [
|
||||
self.branch,
|
||||
self.upstream
|
||||
]
|
||||
branches.extend(self.extra_branches)
|
||||
|
||||
if self._extra_branches != []:
|
||||
branches.update({'extra branch %d' % idx: value
|
||||
for (idx, value) in enumerate(self.extra_branches, 1)})
|
||||
|
||||
for branch_type, branch in branches.iteritems():
|
||||
invalid_ref = False
|
||||
for branch in branches:
|
||||
if not any(head for head in self.repo.heads if head.name == branch):
|
||||
msg = "Specified %s not found: '%s'"
|
||||
self.log.error(msg, branch_type, branch)
|
||||
raise ImportUpstreamError(msg % (branch_type, branch))
|
||||
msg = "Specified ref does not exist: '%s'"
|
||||
self.log.error(msg, branch)
|
||||
invalid_ref = True
|
||||
|
||||
if invalid_ref:
|
||||
raise ImportUpstreamError("Invalid ref")
|
||||
|
||||
@property
|
||||
def branch(self):
|
||||
|
||||
@@ -31,3 +31,6 @@ class GitMixin(object):
|
||||
@property
|
||||
def git(self):
|
||||
return self.__git
|
||||
|
||||
def is_detached(self):
|
||||
return self.git.symbolic_ref("HEAD", q=True, with_exceptions=False)
|
||||
|
||||
Reference in New Issue
Block a user