diff options
| author | tv <tv@krebsco.de> | 2026-12-02 01:35:46 +0000 |
|---|---|---|
| committer | tv <tv@krebsco.de> | 2026-12-02 16:33:24 +0100 |
| commit | c8fbbee154adfd7c312c7575c87fb25890168956 (patch) | |
| tree | 90ca3ff17d3501f28deb940adf7073a9fcb32da4 /pkgs | |
| parent | 5874ad295e3ada5d6db880253c140c2107fdd655 (diff) | |
Looking for a .git below a work tree takes find walking all of it,
which takes longer than git status: for nixpkgs, 0.375 s and 0.275 s,
of 0.65 s per git source and run with nothing to do. Without
submodules, which the index tells, Git follows no .git below the work
tree, so the probe runs git status first. If it finds anything
untracked or ignored, the source gets delivered, and the git script
checks the work tree as before; else there's no .git below it, except
where Git doesn't list one, e.g. a .git file in a tracked directory,
which the next run with something to deliver refuses. With
submodules, the probe looks for nested .git first, as before.
git_problems is git_dir_problems and git_nested_problems now.
Diffstat (limited to 'pkgs')
| -rw-r--r-- | pkgs/populate/docs/design.md | 8 | ||||
| -rw-r--r-- | pkgs/populate/src/sh.rs | 119 |
2 files changed, 86 insertions, 41 deletions
diff --git a/pkgs/populate/docs/design.md b/pkgs/populate/docs/design.md index 0e284a0..33148f4 100644 --- a/pkgs/populate/docs/design.md +++ b/pkgs/populate/docs/design.md @@ -146,7 +146,13 @@ processors: checked out, the URL is the source's, nothing is modified, untracked, or ignored, and the submodules are at their commits, nothing runs, and the flakes step takes the work tree for checked, as after the git - script. + script. Looking for a `.git` below the work tree takes find walking + all of it, longer than `git status`, so without submodules, where Git + follows none, `git status` comes first: if it finds anything untracked + or ignored, the work tree gets delivered, which checks it, and else + there's no `.git` below it, except in places Git doesn't list, e.g. a + `.git` file in a tracked directory, which the next run with something + to deliver refuses. ## Development diff --git a/pkgs/populate/src/sh.rs b/pkgs/populate/src/sh.rs index aabf328..a51e3f5 100644 --- a/pkgs/populate/src/sh.rs +++ b/pkgs/populate/src/sh.rs @@ -356,8 +356,9 @@ fi /// with a .git directory that git_problems finds nothing wrong with, C, H /// and HEAD, U and the origin's URL, D and what git status prints for /// anything modified, untracked, or ignored, and, with submodules, M and -/// what git submodule status prints for them, and S and an exit status, -/// all separated by NULs. The pairs get probed in parallel, as many at a +/// what git submodule status prints for them, but nothing for a work tree +/// without submodules that has anything untracked or ignored, see +/// probe_git, and S and an exit status, all separated by NULs. The pairs get probed in parallel, as many at a /// time as there are processors, and printed in order. Git runs like the /// git script runs it, without hooks and file system monitors, and without /// writing the index. @@ -390,31 +391,56 @@ probe_one() { G) if ! test -L "$path" && test -d "$path" && ! test -L "$path"/.git && test -d "$path"/.git && - problems=$(git_problems "$path") && test -z "$problems" + problems=$(git_dir_problems "$path") && test -z "$problems" then - printf 'C\0\0' - printf 'H\0%s\0' "$(git -C "$path" rev-parse --verify --quiet HEAD)" - printf 'U\0%s\0' "$(git -C "$path" config remote.origin.url)" - # Captured first: printf doesn't fail if what it prints did. - if dirty=$(git -C "$path" status --porcelain --ignored \ - --untracked-files=all) - then - printf 'D\0%s\0' "$dirty" - else + if ! probe_git "$path"; then status=1 fi - if test -e "$path"/.gitmodules; then - if submodules=$(git -C "$path" submodule status --recursive); then - printf 'M\0%s\0' "$submodules" - else - status=1 - fi - fi fi ;; esac printf 'S\0%s\0' "$status" } +# probe_git PATH: C, H, U, D, and M for the Git work tree at PATH, whose +# .git directory git_dir_problems found nothing wrong with, if +# git_nested_problems finds nothing wrong below it either, failing if a +# command does. +# +# git_nested_problems looks through all of the work tree with find, which +# takes longer than git status does. Git only looks into what a .git below +# the work tree names with submodules, so if the index has none, nor +# anything named .git, git status may run first: if it finds nothing +# untracked or ignored, there's no .git below the work tree, and if it +# does, the work tree needs delivering anyway, which checks it. +probe_git() { + # Captured first: grep doesn't fail if what it reads did. Names with + # special characters come quoted, on one line. + tracked=$(git -C "$1" ls-files --stage) || return 1 + if printf '%s\n' "$tracked" | grep -qE '^160000 |(/|[[:space:]]|")\.git"?$'; then + problems=$(git_nested_problems "$1") || return 1 + if test -n "$problems"; then + return 0 + fi + dirty=$(git -C "$1" status --porcelain --ignored --untracked-files=all) || + return 1 + elif test $? -eq 1; then + dirty=$(git -C "$1" status --porcelain --ignored --untracked-files=all) || + return 1 + if test -n "$dirty"; then + return 0 + fi + else + return 1 + fi + printf 'C\0\0' + printf 'H\0%s\0' "$(git -C "$1" rev-parse --verify --quiet HEAD)" + printf 'U\0%s\0' "$(git -C "$1" config remote.origin.url)" + printf 'D\0%s\0' "$dirty" + if test -e "$1"/.gitmodules; then + submodules=$(git -C "$1" submodule status --recursive) || return 1 + printf 'M\0%s\0' "$submodules" + fi +} # In waves of as many as there are processors, each into a file of its # own in a private directory, printed in order afterwards. A probe that # dies on the way prints no S, which leaves its path unprobed. @@ -485,9 +511,18 @@ fi /// and submodules, whose repositories have their work tree in the work /// tree. Hooks don't matter: populate's Git doesn't run them. And each /// .git below the work tree, which Git uses for a submodule there, is a -/// file naming a repository in .git/modules. +/// file naming a repository in .git/modules. git_dir_problems PATH checks +/// the .git directory only, and git_nested_problems PATH the .git below the +/// work tree only, which takes looking through all of it. pub const GIT_PROBLEMS: &str = r#" git_problems() ( + problems=$(git_dir_problems "$1") || exit + if test -z "$problems"; then + problems=$(git_nested_problems "$1") || exit + fi + printf %s "$problems" +) +git_dir_problems() ( path=$1 git_dir=$1/.git links=$(find "$git_dir" -type l | head -n 3 | paste -s -d ' ' -) @@ -504,26 +539,6 @@ git_problems() ( printf '%s has linked work trees' "$git_dir" exit fi - nested=$( - find "$path" -path "$git_dir" -prune -o -name .git -exec /bin/sh -c ' - modules=$1 - shift - for git; do - if ! test -L "$git" && test -f "$git"; then - gitdir=$(sed -n "s/^gitdir: //p" "$git") - resolved=$(cd "${git%/.git}" && cd "$gitdir" 2>/dev/null && pwd -P) || : - case $resolved in - "$modules"/?*) continue ;; - esac - fi - printf "%s\n" "$git" - done - ' sh "$(cd "$git_dir" && pwd -P)/modules" {} + | head -n 1 - ) - if test -n "$nested"; then - printf '%s is not a file naming a repository in %s/modules' "$nested" "$git_dir" - exit - fi find "$git_dir" -name config -type f | while IFS= read -r config; do command git config --file "$config" --list --name-only | while IFS= read -r key; do @@ -558,6 +573,30 @@ git_problems() ( done || exit 1 done || : ) +git_nested_problems() ( + path=$1 + git_dir=$1/.git + nested=$( + find "$path" -path "$git_dir" -prune -o -name .git -exec /bin/sh -c ' + modules=$1 + shift + for git; do + if ! test -L "$git" && test -f "$git"; then + gitdir=$(sed -n "s/^gitdir: //p" "$git") + resolved=$(cd "${git%/.git}" && cd "$gitdir" 2>/dev/null && pwd -P) || : + case $resolved in + "$modules"/?*) continue ;; + esac + fi + printf "%s\n" "$git" + done + ' sh "$(cd "$git_dir" && pwd -P)/modules" {} + | head -n 1 + ) + if test -n "$nested"; then + printf '%s is not a file naming a repository in %s/modules' "$nested" "$git_dir" + exit + fi +) "#; /// git PATH NAME URL REF SHALLOW FETCH_ALWAYS RECLONE LOCK EXCLUDE...: |
