use crate::index::Index;
use crate::{Config, IndexError};
pub(super) fn run_bounded_auto_update(index: &Index, config: &Config, quiet: bool) -> bool {
if !config.auto_update {
return false;
}
use crate::index::freshness::{self, UpdateLimits, UpdateOutcome};
let limits = UpdateLimits {
max_files: Some(config.auto_update_max_files),
budget_ms: Some(config.auto_update_budget_ms),
};
match index.update_from_git(limits) {
Ok(outcome) => {
let git = crate::git_util::resolve_git_binary();
freshness::maybe_print_fsmonitor_tip(
&index.canonical_root,
&git,
&config.index_dir,
outcome.detect_elapsed_ms(),
config.auto_update_budget_ms,
);
match &outcome {
UpdateOutcome::BudgetExceeded {
files_behind_estimate: n,
..
}
| UpdateOutcome::TooManyFiles {
files_behind: n, ..
}
| UpdateOutcome::OverlayFull {
files_behind: n, ..
} => {
if !quiet && *n > 0 {
eprintln!(
"st: index is ~{n} files behind; \
searching stale (run 'st update')"
);
}
true
}
_ => false,
}
}
Err(IndexError::LockConflict(_)) => {
false
}
Err(IndexError::OverlayFull { .. }) => {
if !quiet {
eprintln!("st: overlay full; searching stale (run 'st update')");
}
true
}
Err(_) => {
false
}
}
}
pub(super) fn maybe_spawn_async_catchup(config: &Config) {
if !config.auto_update_async_catchup {
return;
}
let stamp = config.index_dir.join("async-catchup-stamp");
if let Ok(meta) = std::fs::metadata(&stamp) {
if meta
.modified()
.ok()
.and_then(|m| m.elapsed().ok())
.is_some_and(|e| e < std::time::Duration::from_secs(5))
{
return; }
}
let _ = std::fs::write(&stamp, b"");
let Ok(exe) = std::env::current_exe() else {
return;
};
let _ = std::process::Command::new(exe)
.arg("update")
.arg("--quiet")
.arg("--index-dir")
.arg(&config.index_dir)
.arg("--repo-root")
.arg(&config.repo_root)
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.spawn();
}