Skip to content

feat: make git package configurable and default to gitMinimal #529

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

Merged
merged 1 commit into from
Dec 4, 2024
Merged
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
28 changes: 21 additions & 7 deletions modules/pre-commit.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let
remove
;

inherit (pkgs) runCommand git;
inherit (pkgs) runCommand;

cfg = config;
install_stages = lib.unique (builtins.concatLists (lib.mapAttrsToList (_: h: h.stages) enabledHooks));
Expand Down Expand Up @@ -51,7 +51,7 @@ let
);

run =
runCommand "pre-commit-run" { buildInputs = [ git ]; } ''
runCommand "pre-commit-run" { buildInputs = [ cfg.gitPackage ]; } ''
set +e
HOME=$PWD
# Use `chmod +w` instead of `cp --no-preserve=mode` to be able to write and to
Expand Down Expand Up @@ -115,6 +115,20 @@ in
'';
};

gitPackage =
mkOption {
type = types.package;
description =
''
The `git` package to use.
'';
default = pkgs.gitMinimal;
defaultText =
lib.literalExpression or literalExample ''
pkgs.gitMinimal
'';
};

tools =
mkOption {
type = types.lazyAttrsOf (types.nullOr types.package);
Expand Down Expand Up @@ -338,10 +352,10 @@ in
if ! type -t git >/dev/null; then
# This happens in pure shells, including lorri
echo 1>&2 "WARNING: git-hooks.nix: git command not found; skipping installation."
elif ! ${git}/bin/git rev-parse --git-dir &> /dev/null; then
elif ! ${cfg.gitPackage}/bin/git rev-parse --git-dir &> /dev/null; then
echo 1>&2 "WARNING: git-hooks.nix: .git not found; skipping installation."
else
GIT_WC=`${git}/bin/git rev-parse --show-toplevel`
GIT_WC=`${cfg.gitPackage}/bin/git rev-parse --show-toplevel`

# These update procedures compare before they write, to avoid
# filesystem churn. This improves performance with watch tools like lorri
Expand Down Expand Up @@ -369,7 +383,7 @@ in
for hook in $hooks; do
pre-commit uninstall -t $hook
done
${git}/bin/git config --local core.hooksPath ""
${cfg.gitPackage}/bin/git config --local core.hooksPath ""
# Add hooks for configured stages (only) ...
if [ ! -z "${concatStringsSep " " install_stages}" ]; then
for stage in ${concatStringsSep " " install_stages}; do
Expand All @@ -396,12 +410,12 @@ in
fi

# Fetch the absolute path to the git common directory. This will normally point to $GIT_WC/.git.
common_dir=''$(${git}/bin/git rev-parse --path-format=absolute --git-common-dir)
common_dir=''$(${cfg.gitPackage}/bin/git rev-parse --path-format=absolute --git-common-dir)

# Convert the absolute path to a path relative to the toplevel working directory.
common_dir=''${common_dir#''$GIT_WC/}

${git}/bin/git config --local core.hooksPath "''$common_dir/hooks"
${cfg.gitPackage}/bin/git config --local core.hooksPath "''$common_dir/hooks"
fi
fi
fi
Expand Down