|
2 | 2 | let
|
3 | 3 | inherit (lib)
|
4 | 4 | boolToString
|
| 5 | + concatMapStrings |
5 | 6 | concatStringsSep
|
6 |
| - compare |
7 | 7 | filterAttrs
|
8 | 8 | literalExample
|
9 | 9 | mapAttrsToList
|
10 | 10 | mkOption
|
| 11 | + optionalString |
11 | 12 | types
|
12 | 13 | remove
|
13 | 14 | ;
|
|
91 | 92 | nativeBuildInputs = enabledExtraPackages
|
92 | 93 | ++ lib.optional (config.settings.rust.check.cargoDeps != null) cargoSetupHook;
|
93 | 94 | cargoDeps = config.settings.rust.check.cargoDeps;
|
94 |
| - buildPhase = '' |
95 |
| - set +e |
96 |
| - # Set HOME to a temporary directory for pre-commit to create its cache files in. |
97 |
| - HOME=$(mktemp -d) |
98 |
| - ln -fs ${cfg.configFile} ${cfg.configPath} |
99 |
| - git init -q |
100 |
| - git add . |
101 |
| - git config --global user.email "[email protected]" |
102 |
| - git config --global user.name "Your Name" |
103 |
| - git commit -m "init" -q |
104 |
| - if [[ ${toString (compare cfg.installStages [ "manual" ])} -eq 0 ]] |
105 |
| - then |
106 |
| - echo "Running: $ pre-commit run --hook-stage manual --all-files" |
107 |
| - pre-commit run -c ${cfg.configPath} --hook-stage manual --all-files |
108 |
| - else |
109 |
| - echo "Running: $ pre-commit run --all-files" |
110 |
| - pre-commit run -c ${cfg.configPath} --all-files |
111 |
| - fi |
112 |
| - exitcode=$? |
113 |
| - git --no-pager diff --color |
114 |
| - # Derivations must produce an output |
115 |
| - mkdir $out |
116 |
| - [ $? -eq 0 ] && exit $exitcode |
117 |
| - ''; |
| 95 | + buildPhase = |
| 96 | + let |
| 97 | + cmd = "pre-commit run -c ${cfg.configPath}${optionalString (install_stages == [ "manual" ]) " --hook-stage manual"} --all-files"; |
| 98 | + in |
| 99 | + '' |
| 100 | + set +e |
| 101 | + # Set HOME to a temporary directory for pre-commit to create its cache files in. |
| 102 | + HOME=$(mktemp -d) |
| 103 | + ln -fs ${configFile} ${cfg.configPath} |
| 104 | + git init -q |
| 105 | + git add . |
| 106 | + git config --global user.email "[email protected]" |
| 107 | + git config --global user.name "Your Name" |
| 108 | + git commit -m "init" -q |
| 109 | + echo "Running: $ ${cmd}" |
| 110 | + ${cfg.package}/bin/${cmd} |
| 111 | + exitcode=$? |
| 112 | + # Derivations must produce an output |
| 113 | + git --no-pager diff --color |
| 114 | + mkdir $out |
| 115 | + [ $? -eq 0 ] && exit $exitcode |
| 116 | + ''; |
118 | 117 | };
|
119 | 118 |
|
120 | 119 | failedAssertions = builtins.map (x: x.message) (builtins.filter (x: !x.assertion) config.assertions);
|
|
423 | 422 | if ! ${cfg.gitPackage}/bin/git rev-parse --git-dir &> /dev/null; then
|
424 | 423 | echo 1>&2 "WARNING: git-hooks.nix: .git not found; skipping installation."
|
425 | 424 | else
|
426 |
| - GIT_WC=`${cfg.gitPackage}/bin/git rev-parse --show-toplevel` |
| 425 | + GIT_WC=$(${cfg.gitPackage}/bin/git rev-parse --show-toplevel) |
427 | 426 |
|
428 | 427 | # These update procedures compare before they write, to avoid
|
429 | 428 | # filesystem churn. This improves performance with watch tools like lorri
|
|
449 | 448 | ln -fs ${cfg.configFile} "''${GIT_WC}/${cfg.configPath}"
|
450 | 449 | fi
|
451 | 450 | # Remove any previously installed hooks (since pre-commit itself has no convergent design)
|
452 |
| - hooks="${concatStringsSep " " (remove "manual" supportedHooksLib.supportedHooks )}" |
453 |
| - for hook in $hooks; do |
454 |
| - pre-commit uninstall -t $hook |
455 |
| - done |
| 451 | + pre-commit uninstall${concatMapStrings (hook: " --hook-type ${hook}") (remove "manual" supportedHooksLib.supportedHooks)} |
456 | 452 | ${cfg.gitPackage}/bin/git config --local core.hooksPath ""
|
457 |
| - # Add hooks for configured stages (only) ... |
458 |
| - if [ ! -z "${concatStringsSep " " install_stages}" ]; then |
459 |
| - for stage in ${concatStringsSep " " install_stages}; do |
460 |
| - case $stage in |
461 |
| - manual) |
462 |
| - ;; |
463 |
| - # if you amend these switches please also review $hooks above |
464 |
| - commit | merge-commit | push) |
465 |
| - stage="pre-"$stage |
466 |
| - pre-commit install -c ${cfg.configPath} -t $stage |
467 |
| - ;; |
468 |
| - ${concatStringsSep "|" supportedHooksLib.supportedHooks}) |
469 |
| - pre-commit install -c ${cfg.configPath} -t $stage |
470 |
| - ;; |
471 |
| - *) |
472 |
| - echo 1>&2 "ERROR: git-hooks.nix: either $stage is not a valid stage or git-hooks.nix doesn't yet support it." |
473 |
| - exit 1 |
474 |
| - ;; |
475 |
| - esac |
476 |
| - done |
477 |
| - # ... or default 'pre-commit' hook |
478 |
| - else |
479 |
| - pre-commit install -c ${cfg.configPath} |
480 |
| - fi |
| 453 | + # Add hooks for configured stages |
| 454 | + pre-commit install -c ${cfg.configPath}${concatMapStrings (stage: " --hook-type ${if builtins.elem stage [ "commit" "merge-commit" "push" ] then "pre-${stage}" else stage}") (remove "manual" install_stages)} |
481 | 455 |
|
482 | 456 | # Fetch the absolute path to the git common directory. This will normally point to $GIT_WC/.git.
|
483 | 457 | common_dir=''$(${cfg.gitPackage}/bin/git rev-parse --path-format=absolute --git-common-dir)
|
484 | 458 |
|
485 | 459 | # Convert the absolute path to a path relative to the toplevel working directory.
|
486 |
| - common_dir=''${common_dir#''$GIT_WC/} |
| 460 | + common_dir=''${common_dir#"''$GIT_WC"/} |
487 | 461 |
|
488 | 462 | ${cfg.gitPackage}/bin/git config --local core.hooksPath "''$common_dir/hooks"
|
489 | 463 | fi
|
|
0 commit comments