diff options
Diffstat (limited to 'pkgs/populate/src')
| -rw-r--r-- | pkgs/populate/src/sh.rs | 119 |
1 files changed, 79 insertions, 40 deletions
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...: |
