diff --git a/CHANGELOG.md b/CHANGELOG.md index b3e08d70a6..a78ec18b57 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -107,6 +107,7 @@ ### Template +- New pre-commit hooks (large files, merge conflicts) in pipeline template ([#3935](https://github.com/nf-core/tools/pull/3935)) - switch to uv and prek for pipeline linting workflow ([#3942](https://github.com/nf-core/tools/pull/3942)) - add schema to devcontainer.json ([#3908](https://github.com/nf-core/tools/pull/3908)) diff --git a/nf_core/pipeline-template/.hooks/block_pipeline_outdir.sh b/nf_core/pipeline-template/.hooks/block_pipeline_outdir.sh new file mode 100755 index 0000000000..fbd2419181 --- /dev/null +++ b/nf_core/pipeline-template/.hooks/block_pipeline_outdir.sh @@ -0,0 +1,30 @@ +#!/usr/bin/env bash +# This hook is used to block commits if they include staged files inside a directory +# which also contains a subdirectory called `pipeline_info`. The purpose of this is to +# prevent users from inadvertently committing output from pipeline test runs inside the +# development directory. + +set -e + +# list staged files +staged_files="$(git diff --cached --name-only)" + +status=0 + +for file in $staged_files; do + file_dir=$(dirname "$file") + + # Walk up the directory tree and check if the current directory contains a subdirectory called `pipeline_info` + # or the staged file is itself inside a directory called `pipeline_info`. + while [ "$file_dir" != "." ] && [ "$file_dir" != "/" ]; do + if [ $(basename "$file_dir") == "pipeline_info" ] || [ -d "$file_dir/pipeline_info" ]; then + echo "❌ Commit blocked: Please do not commit output from pipeline test runs to the pipeline code itself." + echo "Use 'git restore --staged ...' to remove the output files from the staging area before proceeding." + status=1 + break + fi + file_dir=$(dirname "$file_dir") + done +done + +exit "$status" diff --git a/nf_core/pipeline-template/.pre-commit-config.yaml b/nf_core/pipeline-template/.pre-commit-config.yaml index b1e5f80e51..c487da7072 100644 --- a/nf_core/pipeline-template/.pre-commit-config.yaml +++ b/nf_core/pipeline-template/.pre-commit-config.yaml @@ -25,9 +25,26 @@ repos: subworkflows/(?!local/).*| .*\.snap$ )$ + - id: check-added-large-files + args: [--maxkb=5000] + exclude: | + (?x)^( + .*ro-crate-metadata.json$| + .*\.snap$| + lib/nfcore_external_java_deps.jar$| + docs/.*\.(svg|pdf)$| + assets/.*$ + )$ + - id: check-merge-conflict - repo: https://github.com/seqeralabs/nf-lint-pre-commit rev: v0.3.0 hooks: - id: nextflow-lint files: '\.nf$|nextflow\.config$' args: ["-output", "json"] + - repo: local + hooks: + - id: block-pipeline-outdir + name: Prevent committing output from pipeline test runs to the pipeline code itself + entry: ./.hooks/block_pipeline_outdir.sh + language: script diff --git a/nf_core/pipelines/create/template_features.yml b/nf_core/pipelines/create/template_features.yml index c158263157..19b451fb88 100644 --- a/nf_core/pipelines/create/template_features.yml +++ b/nf_core/pipelines/create/template_features.yml @@ -434,6 +434,7 @@ code_quality: - ".prettierignore" - ".prettierrc.yml" - ".github/workflows/fix-linting.yml" + - ".hooks/block_pipeline_outdir.sh" short_description: "Use code linters" description: "The pipeline will include code linters and CI tests to lint your code: pre-commit, editor-config and prettier." help_text: |