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:
Darragh Bailey
2013-08-02 06:07:11 +01:00
parent 900295c370
commit 318934c357
2 changed files with 21 additions and 12 deletions

View File

@@ -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):

View File

@@ -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)