diff --git a/CHANGELOG.md b/CHANGELOG.md index b36802c145..d7e1afb421 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,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 diff --git a/nf_core/create.py b/nf_core/create.py index 457adc3266..bd00dd1517 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__( @@ -53,6 +55,7 @@ def __init__( 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 @@ -82,6 +85,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"]) @@ -528,24 +532,27 @@ 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 + 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: - 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" + "'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" + "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( 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() diff --git a/tests/test_sync.py b/tests/test_sync.py index 075690b2c0..0cedf51698 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -26,12 +26,14 @@ def setUp(self): """Create a new pipeline to test""" self.tmp_dir = tempfile.mkdtemp() self.pipeline_dir = os.path.join(self.tmp_dir, "testpipeline") + 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.active_branch.rename(default_branch) def tearDown(self): if os.path.exists(self.tmp_dir):