From f46bb7f2a5dbdcd0098904654f7d8742dc111c01 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 14 Nov 2026 01:49:56 +0000 Subject: populate: tell whether a Git work tree drawn from changed by its commit Whether a derived copy of a flake or flake-input source needs to be made again depends on the sources it draws inputs from. For one that isn't in the store, the key had its hash, so every run read all of it, e.g. all of a nixpkgs work tree, .git included, which takes long even when nothing changed. For a Git work tree that Nix gets as git+file:, whose commit and tracked files are all Nix uses, the key has its commit instead, if no tracked file differs from it, which Git tells quickly. git_flake decides what Nix gets as git+file:, for flake_url as well. Keys of copies drawing from such work trees change once, so those copies get made again on the first run. The keys are the same as krops 2's. rewrite-cache-git checks that such a copy gets made again for a new commit, but not for redeploying, nor for an untracked file. --- pkgs/populate/default.nix | 53 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 9 deletions(-) (limited to 'pkgs') diff --git a/pkgs/populate/default.nix b/pkgs/populate/default.nix index 80cbcd3..e69d64b 100644 --- a/pkgs/populate/default.nix +++ b/pkgs/populate/default.nix @@ -460,9 +460,18 @@ let then "${flakesDir}/${source}" else "${target.path}/${source}" )}) + # By the commit of a Git work tree that Nix gets as + # git+file:, which Git tells quickly, and else by a hash, + # which takes reading all of the source. case $drawn in /nix/store/*) ;; - *) nix-hash --type sha256 "$drawn" ;; + *) + if rev=$(clean_git_rev "$drawn") && test -n "$rev"; then + printf 'git %s\n' "$rev" + else + nix-hash --type sha256 "$drawn" + fi + ;; esac '') (unique (drawnFrom name))} ''} @@ -608,20 +617,46 @@ let } ${gitProblems} - # Prints the flake URL of the source named $1: git+file: if it has a - # .git directory git_problems finds nothing wrong with, and that has - # no hooks, else path:. Nix runs Git in a git+file: flake, with the - # repository's configuration and hooks. + # Succeeds if Nix gets the directory $1 as a git+file: flake: if it + # has a .git directory git_problems finds nothing wrong with, and + # that has no hooks. Nix runs Git in a git+file: flake, with the + # repository's configuration and hooks, so it only gets such ones. + git_flake() { + ! test -L "$1"/.git && test -d "$1"/.git || return 1 + git_flake_problems=$(git_problems "$1" 2>/dev/null) || return 1 + test -z "$git_flake_problems" || return 1 + if test -d "$1"/.git/hooks; then + test -z "$(find "$1"/.git/hooks -mindepth 1 ! -name '*.sample' | head -n 1)" + fi + } + # Prints the commit HEAD of the Git work tree $1 points to, if + # git_flake $1, and none of its tracked files differ from that + # commit, which is all of it Nix uses then. Git runs without hooks + # and file system monitors. + clean_git_rev() ( + git_flake "$1" || exit 1 + cd "$1" + git() { + command git -c core.hooksPath=/dev/null -c core.fsmonitor=false "$@" + } + # Without writing the index: in a copy of a work tree, e.g. in a file + # source, that makes it differ from the original's, so the next run + # copies it again, unless the original's changed in the same second, + # keeping its size, which rsync doesn't notice. Then the work tree + # differs from its index, which Nix refuses to lock. + changes=$(git --no-optional-locks status --porcelain --untracked-files=no 2>/dev/null) || exit 1 + test -z "$changes" || exit 1 + git rev-parse --verify --quiet HEAD + ) + # Prints the flake URL of the source named $1: git+file: if + # git_flake, else path:. flake_url() { path=$(readlink -f ${quote target.path}/"$1") if ! test -d "$path"; then >&2 printf 'error: source %s is not a directory\n' "$1" exit 1 fi - if ! test -L "$path"/.git && test -d "$path"/.git && - problems=$(git_problems "$path" 2>/dev/null) && - test -z "$problems" && - test -z "$(find "$path"/.git/hooks -mindepth 1 ! -name '*.sample' 2>/dev/null | head -n 1)"; then + if git_flake "$path"; then if test -e "$path"/.gitmodules; then printf 'git+file:%s?submodules=1' "$(url_path "$path")" else -- cgit v1.3.1