Simplify cloner required parameters
Prior to this change you not only had to always supply zuul-* args or set ZUUL_* env vars but you had to also set zuul params that were unused. Trim down the required zuul params to only those that are used (branch, url, ref) and only require all of them if one is set. zuul-cloner is a useful tool for configuring git repos from a git cache even without zuul params, this change exposes that functionality. In particular this is handy if we ever want to run zuul-cloner outside of a CI context, for example on a developers laptop. Change-Id: I4f285b489df64007a6ec4c14522623f38939c74b
This commit is contained in:
parent
099e7f719c
commit
0c7321b6d8
@ -25,10 +25,6 @@ import zuul.lib.cloner
|
|||||||
|
|
||||||
ZUUL_ENV_SUFFIXES = (
|
ZUUL_ENV_SUFFIXES = (
|
||||||
'branch',
|
'branch',
|
||||||
'change',
|
|
||||||
'patchset',
|
|
||||||
'pipeline',
|
|
||||||
'project',
|
|
||||||
'ref',
|
'ref',
|
||||||
'url',
|
'url',
|
||||||
)
|
)
|
||||||
@ -93,10 +89,11 @@ class Cloner(zuul.cmd.ZuulApp):
|
|||||||
|
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
|
|
||||||
# Validate ZUUL_* arguments
|
# Validate ZUUL_* arguments. If any ZUUL_* argument is set they
|
||||||
|
# must all be set, otherwise fallback to defaults.
|
||||||
zuul_missing = [zuul_opt for zuul_opt, val in vars(args).items()
|
zuul_missing = [zuul_opt for zuul_opt, val in vars(args).items()
|
||||||
if zuul_opt.startswith('zuul') and val is None]
|
if zuul_opt.startswith('zuul') and val is None]
|
||||||
if zuul_missing:
|
if len(zuul_missing) < len(ZUUL_ENV_SUFFIXES):
|
||||||
parser.error(("Some Zuul parameters are not set:\n\t%s\n"
|
parser.error(("Some Zuul parameters are not set:\n\t%s\n"
|
||||||
"Define them either via environment variables or "
|
"Define them either via environment variables or "
|
||||||
"using options above." %
|
"using options above." %
|
||||||
|
@ -136,11 +136,11 @@ class Cloner(object):
|
|||||||
override_zuul_ref = re.sub(self.zuul_branch, indicated_branch,
|
override_zuul_ref = re.sub(self.zuul_branch, indicated_branch,
|
||||||
self.zuul_ref)
|
self.zuul_ref)
|
||||||
|
|
||||||
if repo.hasBranch(indicated_branch):
|
if indicated_branch and repo.hasBranch(indicated_branch):
|
||||||
self.log.debug("upstream repo has branch %s", indicated_branch)
|
self.log.info("upstream repo has branch %s", indicated_branch)
|
||||||
fallback_branch = indicated_branch
|
fallback_branch = indicated_branch
|
||||||
else:
|
else:
|
||||||
self.log.debug("upstream repo is missing branch %s",
|
self.log.info("upstream repo is missing branch %s",
|
||||||
self.branch)
|
self.branch)
|
||||||
# FIXME should be origin HEAD branch which might not be 'master'
|
# FIXME should be origin HEAD branch which might not be 'master'
|
||||||
fallback_branch = 'master'
|
fallback_branch = 'master'
|
||||||
@ -148,10 +148,12 @@ class Cloner(object):
|
|||||||
fallback_zuul_ref = re.sub(self.zuul_branch, fallback_branch,
|
fallback_zuul_ref = re.sub(self.zuul_branch, fallback_branch,
|
||||||
self.zuul_ref)
|
self.zuul_ref)
|
||||||
|
|
||||||
if (self.fetchFromZuul(repo, project, override_zuul_ref)
|
# If we have a non empty zuul_ref to use, use it. Otherwise we fall
|
||||||
or (fallback_zuul_ref != override_zuul_ref and
|
# back to checking out the branch.
|
||||||
self.fetchFromZuul(repo, project, fallback_zuul_ref))
|
if ((override_zuul_ref and
|
||||||
):
|
self.fetchFromZuul(repo, project, override_zuul_ref)) or
|
||||||
|
(fallback_zuul_ref != override_zuul_ref and
|
||||||
|
self.fetchFromZuul(repo, project, fallback_zuul_ref))):
|
||||||
# Work around a bug in GitPython which can not parse FETCH_HEAD
|
# Work around a bug in GitPython which can not parse FETCH_HEAD
|
||||||
gitcmd = git.Git(dest)
|
gitcmd = git.Git(dest)
|
||||||
fetch_head = gitcmd.rev_parse('FETCH_HEAD')
|
fetch_head = gitcmd.rev_parse('FETCH_HEAD')
|
||||||
@ -160,7 +162,7 @@ class Cloner(object):
|
|||||||
project, fetch_head)
|
project, fetch_head)
|
||||||
else:
|
else:
|
||||||
# Checkout branch
|
# Checkout branch
|
||||||
self.log.debug("Falling back to branch %s", fallback_branch)
|
self.log.info("Falling back to branch %s", fallback_branch)
|
||||||
try:
|
try:
|
||||||
repo.checkout('remotes/origin/%s' % fallback_branch)
|
repo.checkout('remotes/origin/%s' % fallback_branch)
|
||||||
except (ValueError, GitCommandError):
|
except (ValueError, GitCommandError):
|
||||||
|
Loading…
Reference in New Issue
Block a user