diff options
Diffstat (limited to 'pkgs/populate/src/populate.rs')
| -rw-r--r-- | pkgs/populate/src/populate.rs | 80 |
1 files changed, 40 insertions, 40 deletions
diff --git a/pkgs/populate/src/populate.rs b/pkgs/populate/src/populate.rs index 668053a..cab1a91 100644 --- a/pkgs/populate/src/populate.rs +++ b/pkgs/populate/src/populate.rs @@ -2,13 +2,13 @@ use crate::flake; use crate::flake_input; +use crate::probe; use crate::record; use crate::rsync::{self, Config}; use crate::run::{self, Context, Failure, Result}; use crate::sh; use crate::spec::{Source, Spec}; use serde_json::{json, Map, Value}; -use std::collections::BTreeMap; use std::fs; use std::path::{Path, PathBuf}; use std::process::{Command, Stdio}; @@ -57,7 +57,7 @@ pub fn populate(context: &Context, spec: &Spec) -> Result<()> { if spec.backup { context.run(&sh::backup(), &[dir])?; } - let listings = target_listings(context, spec)?; + let probed = probe::probe(context, spec, probed)?; for (name, source) in &spec.source { let path = format!("{dir}/{name}"); eprintln!("populating '{}'...", context.name(&path)); @@ -78,7 +78,7 @@ pub fn populate(context: &Context, spec: &Spec) -> Result<()> { source, path: &path, workdir, - listing: listings.get(name).and_then(Option::as_ref), + target: probed.get(name), }; context.note(json!({ "begin": name })); let outcome = kind.deliver(&delivery)?; @@ -191,9 +191,9 @@ struct Delivery<'a> { path: &'a str, /// Where to prepare files on the deploying machine. workdir: PathBuf, - /// The listing of the target path, for a file source whose transfer may - /// find nothing to do, see target_listings. - listing: Option<&'a rsync::Listing>, + /// What the probe found at the target path, for a source whose delivery + /// may find nothing to do, see probed. + target: Option<&'a probe::Target>, } impl Delivery<'_> { @@ -302,39 +302,25 @@ fn file_config(source: &Source, source_path: &str) -> Config { } } -/// The listings of the target paths of the file sources whose transfer may -/// find nothing to do, by name, see File::deliver: of directories, that -/// rsync compares by size and time, not checksum, and that the flakes step -/// doesn't rewrite a lock file in. One command on the target lists them -/// all. -fn target_listings( - context: &Context, - spec: &Spec, -) -> Result<BTreeMap<String, Option<rsync::Listing>>> { - let dir = &spec.target.path; - let names: Vec<&String> = spec - .source - .iter() - .filter(|(_, source)| { - source.kind == "file" - && in_place_lock(source).is_none() - && string(&source.settings, "path").is_ok_and(|path| { - Path::new(path).is_dir() && !file_config(source, path).use_checksum - }) - }) - .map(|(name, _)| name) - .collect(); - if names.is_empty() { - return Ok(BTreeMap::new()); +/// What the probe looks for at a source's target path, see probe::probe: +/// the listing of a file source that's a directory, that rsync compares by +/// size and time, not checksum, the symlink of a symlink source, and the +/// work tree of a git source with a commit as ref. Not where the flakes +/// step rewrites a lock file in place, which delivering brings back. +fn probed(_name: &str, source: &Source) -> Option<&'static str> { + if in_place_lock(source).is_some() { + return None; + } + match source.kind.as_str() { + "file" => string(&source.settings, "path") + .is_ok_and(|path| Path::new(path).is_dir() && !file_config(source, path).use_checksum) + .then_some("F"), + "symlink" => Some("L"), + "git" => (string(&source.settings, "ref").is_ok_and(probe::is_commit) + && source.settings.get("fetchAlways").and_then(Value::as_bool) != Some(true)) + .then_some("G"), + _ => None, } - let paths: Vec<String> = names.iter().map(|name| format!("{dir}/{name}")).collect(); - let args: Vec<&str> = paths.iter().map(String::as_str).collect(); - let mut found = rsync::found(&context.output(sh::LISTINGS, &args)?); - Ok(names - .into_iter() - .zip(&paths) - .map(|(name, path)| (name.clone(), found.remove(path).flatten())) - .collect()) } impl Kind for File { @@ -343,8 +329,9 @@ impl Kind for File { let config = file_config(d.source, source_path); // Nothing to do if what rsync would transfer and what's at the // target path are the same as far as rsync's quick check goes. - let prepared = rsync::prepare(&config, source_path, d.listing.is_some())?; - let changed = if prepared.listing.is_some() && prepared.listing.as_ref() == d.listing { + let listing = d.target.and_then(|target| target.listing.as_ref()); + let prepared = rsync::prepare(&config, source_path, listing.is_some())?; + let changed = if prepared.listing.is_some() && prepared.listing.as_ref() == listing { Vec::new() } else { rsync::transfer_prepared(d.context, &config, &prepared, d.path)? @@ -447,6 +434,15 @@ impl Kind for Git { .into_iter() .flatten() .filter_map(Value::as_str); + // Nothing to do if the work tree there, which git_problems found + // nothing wrong with, has the commit checked out, and nothing that + // git reset, git clean, or updating submodules would change. + if let Some(git) = d.target.and_then(|probed| probed.git.as_ref()) { + if git.unchanged(d.string("url")?, d.string("ref")?) { + d.context.git_checked(d.path); + return Ok(Outcome::changed(false).with("commit", git.head.as_str())); + } + } let name = d.context.name(d.path); let mut args = vec![ d.path, @@ -542,6 +538,10 @@ struct Symlink; impl Kind for Symlink { fn deliver(&self, d: &Delivery) -> Result<Outcome> { let target = d.string("target")?; + // Nothing to do if the symlink there points to target already. + if d.target.and_then(|probed| probed.link.as_deref()) == Some(target) { + return Ok(Outcome::changed(false).with("target", target)); + } let output = d.context.output(&sh::symlink(), &[d.path, target])?; Ok(Outcome::changed(output.contains("changed")).with("target", target)) } |
