Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enable setting defaulBranch for pipeline repo creation #1959

Merged
merged 13 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
25 changes: 16 additions & 9 deletions nf_core/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__(
Expand All @@ -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
Expand Down Expand Up @@ -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"])
Expand Down Expand Up @@ -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 <NAME> [/]\n"
"Pipeline git repository is not initialised."
f"Your Git defaultBranch '{default_branch}' is incompatible with nf-core.\n"
fabianegli marked this conversation as resolved.
Show resolved Hide resolved
"'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 <NAME> [/]\n"
fabianegli marked this conversation as resolved.
Show resolved Hide resolved
"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(
Expand Down
5 changes: 5 additions & 0 deletions tests/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import os
import unittest

import git

import nf_core.create

from .utils import with_temporary_folder
Expand All @@ -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,
Expand All @@ -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):
Expand All @@ -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()
4 changes: 3 additions & 1 deletion tests/test_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down