diff --git a/CHANGELOG.md b/CHANGELOG.md index 66a77dd299..c4e97a5c88 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,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)) ### Linting 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 d06777a8f7..9ee191a0ef 100644 --- a/nf_core/pipeline-template/.pre-commit-config.yaml +++ b/nf_core/pipeline-template/.pre-commit-config.yaml @@ -25,3 +25,20 @@ repos: subworkflows/nf-core/.*| .*\.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: 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 0801f1ff75..63a019b3f9 100644 --- a/nf_core/pipelines/create/template_features.yml +++ b/nf_core/pipelines/create/template_features.yml @@ -438,6 +438,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: |