Skip to content

Commit e687b08

Browse files
authored
fix(cli): prevent --template-ref from being set without --template-source in renku init (#2146)
* fix(cli): prevent --template-ref from being set without --template-source in renku init * fix template clone whithout main branch
1 parent 634f2b3 commit e687b08

File tree

3 files changed

+25
-13
lines changed

3 files changed

+25
-13
lines changed

renku/cli/init.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ def check_git_user_config():
250250
@click.option("-i", "--template-index", help="Provide the index number of the template to use.", type=int)
251251
@click.option("-s", "--template-source", help="Provide the templates repository url or path.")
252252
@click.option(
253-
"-r", "--template-ref", default="master", help="Specify the reference to checkout on remote template repository."
253+
"-r", "--template-ref", default=None, help="Specify the reference to checkout on remote template repository."
254254
)
255255
@click.option(
256256
"-p",
@@ -297,6 +297,10 @@ def init(
297297
"\tgit config --global --add user.email "
298298
299299
)
300+
301+
if template_ref and not template_source:
302+
raise errors.ParameterError("Can't use '--template-ref' without specifying '--template-source'")
303+
300304
communicator = ClickCallback()
301305
init_command().with_communicator(communicator).build().execute(
302306
ctx=ctx,

renku/core/commands/init.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ def init_command():
334334
return Command().command(_init)
335335

336336

337-
def fetch_template_from_git(source, ref="master", tempdir=None):
337+
def fetch_template_from_git(source, ref=None, tempdir=None):
338338
"""Fetch the template from a git repository and checkout the relevant data.
339339
340340
:param source: url or full path of the templates repository
@@ -351,17 +351,22 @@ def fetch_template_from_git(source, ref="master", tempdir=None):
351351
except git.exc.GitCommandError as e:
352352
raise errors.GitError("Cannot clone repo from {0}".format(source)) from e
353353

354-
try:
355-
# fetch ref and set the HEAD
356-
template_repo.remotes.origin.fetch(ref)
354+
if ref:
357355
try:
358-
template_repo.head.reset(template_repo.commit(ref))
359-
except git.exc.BadName:
360-
ref = "origin/{0}".format(ref)
361-
template_repo.head.reset(template_repo.commit(ref))
356+
# fetch ref and set the HEAD
357+
template_repo.remotes.origin.fetch(ref)
358+
try:
359+
template_repo.head.reset(template_repo.commit(ref))
360+
except git.exc.BadName:
361+
ref = "origin/{0}".format(ref)
362+
template_repo.head.reset(template_repo.commit(ref))
363+
git_repo = git.Git(str(tempdir))
364+
except git.exc.GitCommandError as e:
365+
raise errors.GitError("Cannot fetch and checkout reference {0}".format(ref)) from e
366+
else:
367+
template_repo.remotes.origin.fetch()
368+
template_repo.head.reset(template_repo.commit())
362369
git_repo = git.Git(str(tempdir))
363-
except git.exc.GitCommandError as e:
364-
raise errors.GitError("Cannot fetch and checkout reference {0}".format(ref)) from e
365370

366371
# checkout the manifest
367372
try:
@@ -380,14 +385,17 @@ def fetch_template(template_source, template_ref):
380385
:return: tuple of (template manifest, template folder, template source, template version)
381386
"""
382387
if template_source and template_source != "renku":
383-
communication.echo("Fetching template from {0}@{1}... ".format(template_source, template_ref))
388+
communication.echo("Fetching template from {0}@{1}... ".format(template_source, template_ref or ""))
384389
template_folder = Path(mkdtemp())
385390
_, template_version = fetch_template_from_git(template_source, template_ref, template_folder)
386391
template_manifest = read_template_manifest(template_folder, checkout=True)
387392
communication.echo("OK")
388393
else:
389394
from renku import __version__
390395

396+
if template_ref:
397+
raise errors.ParameterError("Templates included in renku don't support specifying a template_ref")
398+
391399
template_folder = Path(pkg_resources.resource_filename("renku", "templates"))
392400
template_manifest = read_template_manifest(template_folder)
393401
template_source = "renku"

tests/cli/test_init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ def test_default_init_parameters(isolated_runner, mocker, project_init, template
412412
"__sanitized_project_name__",
413413
} <= set(metadata.keys())
414414
assert metadata["__template_source__"] == "renku"
415-
assert metadata["__template_ref__"] == "master"
415+
assert metadata["__template_ref__"] is None
416416
assert metadata["__template_id__"] == template["id"]
417417
assert metadata["__namespace__"] == ""
418418
assert metadata["__repository__"] == ""

0 commit comments

Comments
 (0)