diff options
Diffstat (limited to 'pkgs/populate/src')
| -rw-r--r-- | pkgs/populate/src/flake.rs | 14 | ||||
| -rw-r--r-- | pkgs/populate/src/sh.rs | 64 |
2 files changed, 53 insertions, 25 deletions
diff --git a/pkgs/populate/src/flake.rs b/pkgs/populate/src/flake.rs index f992231..22b49e8 100644 --- a/pkgs/populate/src/flake.rs +++ b/pkgs/populate/src/flake.rs @@ -203,7 +203,8 @@ struct DerivedState<'a> { target_kind: &'a str, /// A line of sha256sum for each patch. patch_sums: &'a [String], - /// The hash of each source drawn from, or empty if it's in the store. + /// What tells whether each source drawn from changed: git and its + /// commit, its hash, or empty if it's in the store. hashes: &'a [String], } @@ -230,10 +231,11 @@ impl<'a> DerivedState<'a> { } } -/// The text whose hash is the key of a derived copy, like the shell -/// implementation computes it: the original's path, the spec, the patches' -/// sums, and, for a copy drawing inputs, nix flake lock's arguments and the -/// hashes of the sources drawn from that aren't in the store. +/// The text whose hash is the key of a derived copy: the original's path, +/// the spec, the patches' sums, and, for a copy drawing inputs, nix flake +/// lock's arguments and what tells whether the sources drawn from that +/// aren't in the store changed: the commit of a Git work tree that Nix gets +/// as git+file:, and else a hash of the source. fn key_text( original: &str, spec: &Value, @@ -535,7 +537,7 @@ impl<'a> Flakes<'a> { /// the patches, or what inputs get drawn from which sources changes, /// and others also if the sources they draw inputs from change. The /// key of a copy, in .populate/derived/<name>/key, is the hash of all - /// that. + /// that, see key_text. fn derive(&self, name: &str) -> Result<()> { let settings = &self.sources[name].settings; let materialize = bool_setting(settings, "materialize", false); diff --git a/pkgs/populate/src/sh.rs b/pkgs/populate/src/sh.rs index 70efd3f..9ae920b 100644 --- a/pkgs/populate/src/sh.rs +++ b/pkgs/populate/src/sh.rs @@ -759,8 +759,8 @@ nix eval --impure --raw --expr ' /// the definitions of prelude and DEFINITIONS. Populate decides what to /// do, from what these send back. pub mod draw { - /// Defines own_lock DIR, apply_patches PATCH_DIR NAME, and write_key - /// KEY_FILE KEY. + /// Defines own_lock DIR, apply_patches PATCH_DIR NAME, write_key + /// KEY_FILE KEY, git_flake PATH, and clean_git_rev PATH. /// /// Nix rewrites lock files in place, which also changes what else is a /// hard link to the lock file, e.g. in a backup, or what it is a @@ -774,6 +774,17 @@ pub mod draw { /// /// write_key writes KEY to a new file, which then replaces KEY_FILE, so /// nothing at its place gets written through, e.g. a symlink. + /// + /// git_flake PATH succeeds if Nix gets the directory at PATH as a + /// git+file: flake: if it has a .git directory that 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. + /// + /// clean_git_rev PATH prints the commit HEAD of the Git work tree at + /// PATH points to, if git_flake PATH, 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. pub const DEFINITIONS: &str = r#" own_lock() { if test -e "$1"/flake.lock; then @@ -796,6 +807,24 @@ write_key() { printf '%s\n' "$2" > "$key_file" mv -f "$key_file" "$1" } +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 +} +clean_git_rev() ( + git_flake "$1" || exit 1 + cd "$1" + git() { + command git -c core.hooksPath=/dev/null -c core.fsmonitor=false "$@" + } + changes=$(git status --porcelain --untracked-files=no 2>/dev/null) || exit 1 + test -z "$changes" || exit 1 + git rev-parse --verify --quiet HEAD +) "#; /// use_state SENTINEL NAME... @@ -805,25 +834,14 @@ use_state "$@" /// resolve PATH...: prints, for each PATH, what's there and where it /// resolves to, as two fields, each ending in a null byte. The first - /// holds d if PATH is a directory, g if it has a .git directory that - /// git_problems finds nothing wrong with, and that has no hooks, s if - /// it has .gitmodules, and f if it has flake.nix. Nix runs Git in a - /// git+file: flake, with the repository's configuration and hooks, so - /// it only gets such ones. + /// holds d if PATH is a directory, g if git_flake PATH, s if it has + /// .gitmodules, and f if it has flake.nix. pub const RESOLVE: &str = r#" for path; do resolved=$(readlink -f "$path") || resolved= flags= if test -d "$path"; then flags=${flags}d; fi - if ! test -L "$path"/.git && test -d "$path"/.git; then - problems=$(git_problems "$path" 2>/dev/null) || problems=failed - if test -d "$path"/.git/hooks; then - hooks=$(find "$path"/.git/hooks -mindepth 1 ! -name '*.sample' | head -n 1) - else - hooks= - fi - if test -z "$problems" && test -z "$hooks"; then flags=${flags}g; fi - fi + if git_flake "$path"; then flags=${flags}g; fi if test -e "$path"/.gitmodules; then flags=${flags}s; fi if test -e "$path"/flake.nix; then flags=${flags}f; fi printf '%s\0%s\0' "$flags" "$resolved" @@ -870,8 +888,10 @@ nix flake lock "$@" "$url" >&2 /// ending in null bytes: where DIR/original resolves to, the key of the /// copy, the store path DIR/result links to, if the store has it, link /// or directory, if TARGET_PATH is one, a line of sha256sum for each - /// patch in PATCH_DIR, an empty field, and the hash of each DRAWN path, - /// or nothing if it resolves into the store. + /// patch in PATCH_DIR, an empty field, and what tells whether each + /// DRAWN path changed: nothing if it resolves into the store, git and + /// the commit, if clean_git_rev gives one, or else its hash, which + /// takes reading all of it. pub const DERIVED_STATE: &str = r#" dir=$1 target_path=$2 patch_dir=$3 shift 3 @@ -905,7 +925,13 @@ for drawn; do drawn=$(readlink -f "$drawn") case $drawn in /nix/store/*) printf '\0' ;; - *) printf '%s\0' "$(nix-hash --type sha256 "$drawn")" ;; + *) + if rev=$(clean_git_rev "$drawn") && test -n "$rev"; then + printf 'git %s\0' "$rev" + else + printf '%s\0' "$(nix-hash --type sha256 "$drawn")" + fi + ;; esac done "#; |
