From 7dfacd6437fe361b59d142901cd8343266c50bd8 Mon Sep 17 00:00:00 2001 From: tv Date: Sat, 14 Nov 2026 02:07:26 +0000 Subject: populate: git sources reset the work tree only if it differs from the commit Resetting a work tree that is at the commit already rewrites Git's index, after comparing each tracked file with it, which takes long for big work trees like nixpkgs, and comparing alone takes about a third of that. So git sources reset only if HEAD isn't the commit, or a tracked file differs from it, as git status tells, which also covers a lock file Nix created and staged. The git scenarios also change a tracked file, which redeploying resets. --- pkgs/populate/src/sh.rs | 12 +++++++++--- pkgs/populate/tests/scenarios.nix | 2 ++ 2 files changed, 11 insertions(+), 3 deletions(-) (limited to 'pkgs/populate') diff --git a/pkgs/populate/src/sh.rs b/pkgs/populate/src/sh.rs index 9ae920b..0411edd 100644 --- a/pkgs/populate/src/sh.rs +++ b/pkgs/populate/src/sh.rs @@ -414,9 +414,15 @@ else fetch "$ref" hash=$(git rev-parse --verify 'FETCH_HEAD^{commit}') fi -# Also if HEAD is there already: rewriting a lock file in place, e.g., -# changes the work tree, and stages the lock file if Nix created it. -git reset --hard "$hash" >&2 +# Also if HEAD is there already, unless no tracked file differs from it: +# rewriting a lock file in place, e.g., changes the work tree, and stages +# the lock file if Nix created it. +head=$(git rev-parse --verify --quiet HEAD) || head= +commit=$(git rev-parse --verify --quiet "$hash^{commit}") || commit= +changes=$(git status --porcelain --untracked-files=no) || changes=unknown +if test -z "$head" || test "$head" != "$commit" || test -n "$changes"; then + git reset --hard "$hash" >&2 +fi git submodule update --init --recursive n=$# while test "$n" -gt 0; do diff --git a/pkgs/populate/tests/scenarios.nix b/pkgs/populate/tests/scenarios.nix index 47adea7..35645d4 100644 --- a/pkgs/populate/tests/scenarios.nix +++ b/pkgs/populate/tests/scenarios.nix @@ -305,11 +305,13 @@ with import ./builder.nix args; mutate = '' echo junk > "$T"/x/junk echo keepme > "$T"/x/keepme + echo changed > "$T"/x/two ''; } { sources = sources gitHead; check = check + '' + expect_eq two "$(cat "$T"/x/two)" two test ! -e "$T"/x/junk || fail "junk not cleaned" ${if flags.cleanExclude then '' test -e "$T"/x/keepme || fail "keepme cleaned" -- cgit v1.3.1