From c1de099d8d0854b55459c7ef0c7038301127de40 Mon Sep 17 00:00:00 2001 From: fabianegli Date: Wed, 19 Oct 2022 23:24:13 +0200 Subject: [PATCH 01/12] enable setting defaulBranch for pipeline repo creation --- nf_core/create.py | 16 ++++++++++------ tests/test_sync.py | 5 +++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/nf_core/create.py b/nf_core/create.py index 8b028d90af..9f9503e879 100644 --- a/nf_core/create.py +++ b/nf_core/create.py @@ -49,6 +49,7 @@ def __init__( author, version="1.0dev", no_git=False, + default_branch=None, force=False, outdir=None, template_yaml_path=None, @@ -82,6 +83,7 @@ def __init__( self.no_git = ( no_git if self.template_params["github"] else True ) # Set to True if template was configured without github hosting + self.default_branch = default_branch self.force = force if outdir is None: outdir = os.path.join(os.getcwd(), self.template_params["name_noslash"]) @@ -525,19 +527,21 @@ def git_init_pipeline(self): """ # Check that the default branch is not dev try: - default_branch = git.config.GitConfigParser().get_value("init", "defaultBranch") + default_branch = self.default_branch or git.config.GitConfigParser().get_value("init", "defaultBranch") except configparser.Error: default_branch = None log.debug("Could not read init.defaultBranch") - if default_branch == "dev" or default_branch == "TEMPLATE": + if default_branch in ["dev", "TEMPLATE"]: raise UserWarning( - f"Your Git defaultBranch is set to '{default_branch}', which is incompatible with nf-core.\n" - "This can be modified with the command [white on grey23] git config --global init.defaultBranch [/]\n" - "Pipeline git repository is not initialised." + f"Your Git defaultBranch '{default_branch}' is incompatible with nf-core.\n" + "Set the default branch name with " + "[white on grey23] git config --global init.defaultBranch [/]\n" + # Or set the default_branch parameter in this class + "Pipeline git repository will not be initialised." ) # Initialise pipeline log.info("Initialising pipeline git repository") - repo = git.Repo.init(self.outdir) + repo = git.Repo.init(self.outdir, initial_branch=default_branch) repo.git.add(A=True) repo.index.commit(f"initial template build from nf-core/tools, version {nf_core.__version__}") # Add TEMPLATE branch to git repository diff --git a/tests/test_sync.py b/tests/test_sync.py index 4460b56054..cf5d772f1c 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -26,12 +26,13 @@ def setUp(self): """Create a new pipeline to test""" self.tmp_dir = tempfile.mkdtemp() self.pipeline_dir = os.path.join(self.tmp_dir, "test_pipeline") + default_branch = "master" self.create_obj = nf_core.create.PipelineCreate( - "testing", "test pipeline", "tester", outdir=self.pipeline_dir, plain=True + "testing", "test pipeline", "tester", outdir=self.pipeline_dir, plain=True, default_branch=default_branch ) self.create_obj.init_pipeline() self.remote_path = os.path.join(self.tmp_dir, "remote_repo") - self.remote_repo = git.Repo.init(self.remote_path, bare=True) + self.remote_repo = git.Repo.init(self.remote_path, bare=True, initial_branch=default_branch) def tearDown(self): if os.path.exists(self.tmp_dir): From 6dd98742a626d54db41e51c609d202c666fd8acc Mon Sep 17 00:00:00 2001 From: fabianegli Date: Wed, 19 Oct 2022 23:37:14 +0200 Subject: [PATCH 02/12] the default branch can now be specified for new pipeline repos --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cf016b8e63..25ef314297 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ - Update subworkflows install so it installs also imported modules and subworkflows ([#1904](https://github.com/nf-core/tools/pull/1904)) - Improve test coverage of sync.py - `check_up_to_date()` function from `modules_json` also checks for subworkflows. +- The default branch can now be specified when creating a new pipeline repo [#1959](https://github.com/nf-core/tools/pull/1959). ### Modules From 50aa97430d573e8bcdfdeecf99d853c8a30c5a96 Mon Sep 17 00:00:00 2001 From: fabianegli Date: Wed, 19 Oct 2022 23:54:55 +0200 Subject: [PATCH 03/12] only set the initial branch if default banch is given --- nf_core/create.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nf_core/create.py b/nf_core/create.py index 9f9503e879..eb664ad4ec 100644 --- a/nf_core/create.py +++ b/nf_core/create.py @@ -541,7 +541,8 @@ def git_init_pipeline(self): ) # Initialise pipeline log.info("Initialising pipeline git repository") - repo = git.Repo.init(self.outdir, initial_branch=default_branch) + init_kwargs = {"initial_branch": default_branch} if default_branch is not None else {} + repo = git.Repo.init(self.outdir, **init_kwargs) repo.git.add(A=True) repo.index.commit(f"initial template build from nf-core/tools, version {nf_core.__version__}") # Add TEMPLATE branch to git repository From f19288766d0d79952464b01d56e17f3efcb7aae0 Mon Sep 17 00:00:00 2001 From: fabianegli Date: Thu, 20 Oct 2022 08:47:17 +0200 Subject: [PATCH 04/12] move the default_branch to the end of the argument list --- nf_core/create.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nf_core/create.py b/nf_core/create.py index eb664ad4ec..78aa54ed36 100644 --- a/nf_core/create.py +++ b/nf_core/create.py @@ -49,11 +49,11 @@ def __init__( author, version="1.0dev", no_git=False, - default_branch=None, force=False, outdir=None, template_yaml_path=None, plain=False, + default_branch=None, ): self.template_params, skip_paths_keys = self.create_param_dict( name, description, author, version, template_yaml_path, plain From fe5fb7b7bd8e7222169236d7527b9c08fb41ba15 Mon Sep 17 00:00:00 2001 From: fabianegli Date: Thu, 20 Oct 2022 08:55:11 +0200 Subject: [PATCH 05/12] add docstrings for git related arguments --- nf_core/create.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/nf_core/create.py b/nf_core/create.py index 78aa54ed36..183877303f 100644 --- a/nf_core/create.py +++ b/nf_core/create.py @@ -40,6 +40,8 @@ class PipelineCreate(object): May the force be with you. outdir (str): Path to the local output directory. template_yaml (str): Path to template.yml file for pipeline creation settings. + plain (bool): If true the Git repository will be initialized plain. + default_branch (str): Specifies the --initial-branch name. """ def __init__( From c3fe2e52d37e0d977e14303a9fa3a3d3fb5e43d2 Mon Sep 17 00:00:00 2001 From: fabianegli Date: Thu, 20 Oct 2022 09:36:41 +0200 Subject: [PATCH 06/12] use git branch rename as git<2.28 lacks the initial-branch argument --- nf_core/create.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/nf_core/create.py b/nf_core/create.py index 183877303f..31caa44fde 100644 --- a/nf_core/create.py +++ b/nf_core/create.py @@ -543,8 +543,9 @@ def git_init_pipeline(self): ) # Initialise pipeline log.info("Initialising pipeline git repository") - init_kwargs = {"initial_branch": default_branch} if default_branch is not None else {} - repo = git.Repo.init(self.outdir, **init_kwargs) + repo = git.Repo.init(self.outdir) + if default_branch: + repo.active_branch.rename(default_branch) repo.git.add(A=True) repo.index.commit(f"initial template build from nf-core/tools, version {nf_core.__version__}") # Add TEMPLATE branch to git repository From 83b7eaf0cfb665430db5fe39e6da67f8677e32b5 Mon Sep 17 00:00:00 2001 From: fabianegli Date: Thu, 20 Oct 2022 09:59:43 +0200 Subject: [PATCH 07/12] clean up comments --- nf_core/create.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/nf_core/create.py b/nf_core/create.py index 31caa44fde..8fef3fb83b 100644 --- a/nf_core/create.py +++ b/nf_core/create.py @@ -527,7 +527,6 @@ def git_init_pipeline(self): Raises: UserWarning: if Git default branch is set to 'dev' or 'TEMPLATE'. """ - # Check that the default branch is not dev try: default_branch = self.default_branch or git.config.GitConfigParser().get_value("init", "defaultBranch") except configparser.Error: @@ -538,17 +537,16 @@ def git_init_pipeline(self): f"Your Git defaultBranch '{default_branch}' is incompatible with nf-core.\n" "Set the default branch name with " "[white on grey23] git config --global init.defaultBranch [/]\n" - # Or set the default_branch parameter in this class + "Or set the default_branch parameter in this class.\n" "Pipeline git repository will not be initialised." ) - # Initialise pipeline + log.info("Initialising pipeline git repository") repo = git.Repo.init(self.outdir) if default_branch: repo.active_branch.rename(default_branch) repo.git.add(A=True) repo.index.commit(f"initial template build from nf-core/tools, version {nf_core.__version__}") - # Add TEMPLATE branch to git repository repo.git.branch("TEMPLATE") repo.git.branch("dev") log.info( From 9cda484a251d0e8d472f28429e6a5fb4419111bd Mon Sep 17 00:00:00 2001 From: fabianegli Date: Thu, 20 Oct 2022 10:01:28 +0200 Subject: [PATCH 08/12] simplify code --- nf_core/create.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nf_core/create.py b/nf_core/create.py index 8fef3fb83b..6367b397c4 100644 --- a/nf_core/create.py +++ b/nf_core/create.py @@ -527,10 +527,10 @@ def git_init_pipeline(self): Raises: UserWarning: if Git default branch is set to 'dev' or 'TEMPLATE'. """ + default_branch = self.default_branch try: - default_branch = self.default_branch or git.config.GitConfigParser().get_value("init", "defaultBranch") + default_branch = git.config.GitConfigParser().get_value("init", "defaultBranch") except configparser.Error: - default_branch = None log.debug("Could not read init.defaultBranch") if default_branch in ["dev", "TEMPLATE"]: raise UserWarning( From b0a013d2f35dc232cd67b82c979c3baf25c95d2d Mon Sep 17 00:00:00 2001 From: fabianegli Date: Thu, 20 Oct 2022 10:09:54 +0200 Subject: [PATCH 09/12] restore precedence for default_branch --- nf_core/create.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nf_core/create.py b/nf_core/create.py index 6367b397c4..1166c1b0f6 100644 --- a/nf_core/create.py +++ b/nf_core/create.py @@ -529,7 +529,7 @@ def git_init_pipeline(self): """ default_branch = self.default_branch try: - default_branch = git.config.GitConfigParser().get_value("init", "defaultBranch") + default_branch = default_branch or git.config.GitConfigParser().get_value("init", "defaultBranch") except configparser.Error: log.debug("Could not read init.defaultBranch") if default_branch in ["dev", "TEMPLATE"]: From 66dbd4fec07ad572ae2153da4b3d7cfb9cd5e92b Mon Sep 17 00:00:00 2001 From: fabianegli Date: Thu, 20 Oct 2022 11:29:33 +0200 Subject: [PATCH 10/12] add test for defaul_branch argument in pipeline creation --- tests/test_create.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/test_create.py b/tests/test_create.py index 5f8f6546f2..5f732fc180 100644 --- a/tests/test_create.py +++ b/tests/test_create.py @@ -4,6 +4,8 @@ import os import unittest +import git + import nf_core.create from .utils import with_temporary_folder @@ -16,6 +18,7 @@ def setUp(self, tmp_path): self.pipeline_description = "just for 4w3s0m3 tests" self.pipeline_author = "Chuck Norris" self.pipeline_version = "1.0.0" + self.default_branch = "default" self.pipeline = nf_core.create.PipelineCreate( name=self.pipeline_name, @@ -26,6 +29,7 @@ def setUp(self, tmp_path): force=True, outdir=tmp_path, plain=True, + default_branch=self.default_branch, ) def test_pipeline_creation(self): @@ -37,3 +41,4 @@ def test_pipeline_creation(self): def test_pipeline_creation_initiation(self): self.pipeline.init_pipeline() assert os.path.isdir(os.path.join(self.pipeline.outdir, ".git")) + assert f" {self.default_branch}\n" in git.Repo.init(self.pipeline.outdir).git.branch() From 9b7348c9315f064a3b1de2f9ec2fc3edf5f74732 Mon Sep 17 00:00:00 2001 From: fabianegli Date: Thu, 20 Oct 2022 13:15:46 +0200 Subject: [PATCH 11/12] list reserved branch names in error message --- nf_core/create.py | 1 + 1 file changed, 1 insertion(+) diff --git a/nf_core/create.py b/nf_core/create.py index 1166c1b0f6..8124ea21b2 100644 --- a/nf_core/create.py +++ b/nf_core/create.py @@ -535,6 +535,7 @@ def git_init_pipeline(self): if default_branch in ["dev", "TEMPLATE"]: raise UserWarning( f"Your Git defaultBranch '{default_branch}' is incompatible with nf-core.\n" + "'dev' and 'TEMPLATE' can not be used as default branch name.\n" "Set the default branch name with " "[white on grey23] git config --global init.defaultBranch [/]\n" "Or set the default_branch parameter in this class.\n" From 3bbc5583aa7d5c316b4b70e8b43ac028cb3557e9 Mon Sep 17 00:00:00 2001 From: fabianegli Date: Thu, 20 Oct 2022 13:22:12 +0200 Subject: [PATCH 12/12] initial-branch is not available on git<2.28 --- tests/test_sync.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_sync.py b/tests/test_sync.py index cf5d772f1c..b968b64370 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -32,7 +32,8 @@ def setUp(self): ) self.create_obj.init_pipeline() self.remote_path = os.path.join(self.tmp_dir, "remote_repo") - self.remote_repo = git.Repo.init(self.remote_path, bare=True, initial_branch=default_branch) + self.remote_repo = git.Repo.init(self.remote_path, bare=True) + self.remote_repo.active_branch.rename(default_branch) def tearDown(self): if os.path.exists(self.tmp_dir):