forked from vhaudiquet/rhema-claw
fix(git-identity): read and write git's global scope, not a guessed file
The provisioning resolved its target itself: GIT_CONFIG_GLOBAL when set, `.gitconfig` in the home otherwise. That guess is wrong on half the machines git runs on — git's global scope spans both `.gitconfig` and the XDG `~/.config/git/config`, and when the user's identity lives in the XDG file, writing `.gitconfig` shadows it: the daemon's values win silently over the user's, and the guard — which read only the file it was about to write — never saw the existing identity it was built to respect. On this machine the daemon then booted once during testing and the next commit was authored claw <claw@k6>, and pushed. The scope is git's now. Reads and writes run through `git config --global`, so the guard sees whatever a commit would actually carry, from whichever file holds it; writes land in the file git itself picks — the established XDG config when that is the one, `.gitconfig` otherwise. A scope given explicitly pins `GIT_CONFIG_GLOBAL` on the child process only, which keeps the tests isolated without touching the caller's environment. Verified on the machine that was bitten: booting the daemon bare now resolves the defaults and then keeps the user's identity — no file written, the XDG values untouched.
This commit is contained in:
1 parent
163fdbf6c9
commit
fa135acbc6
1 file changed
+91
-78
@@ -14,12 +14,17 @@
|
||||
//! inits itself all inherit them.
|
||||
//!
|
||||
//! The write is guarded. The settings store remembers the last pair
|
||||
//! the daemon wrote; boot rewrites a field only when the config file
|
||||
//! still holds that pair, or nothing at all. A value anyone else set
|
||||
//! wins outright — the daemon is a tenant of its user's machine, not
|
||||
//! the owner of its git config. An ssh sandbox runs git on another
|
||||
//! host, so it stays out of scope here: those workspaces remain
|
||||
//! unprovisioned until a later story pushes the values remotely.
|
||||
//! the daemon wrote; boot rewrites a field only when git's global
|
||||
//! scope still holds that pair, or nothing at all. A value anyone
|
||||
//! else set wins outright — the daemon is a tenant of its user's
|
||||
//! machine, not the owner of its git config. The scope is git's own:
|
||||
//! reads and writes run through `git config --global`, so whatever
|
||||
//! file the user's git actually reads — `.gitconfig`, the XDG
|
||||
//! `~/.config/git/config`, or the `GIT_CONFIG_GLOBAL` override — is
|
||||
//! what the guard sees, not a guessed path that shadows it. An ssh
|
||||
//! sandbox runs git on another host, so it stays out of scope here:
|
||||
//! those workspaces remain unprovisioned until a later story pushes
|
||||
//! the values remotely.
|
||||
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::{Command, Stdio};
|
||||
@@ -98,7 +103,13 @@ pub fn resolve(
|
||||
/// failure becomes the line the caller prints.
|
||||
pub fn boot(config: &ClawConfig) -> String {
|
||||
let outcome = (|| -> Result<String, String> {
|
||||
let file = global_file().ok_or("no home directory known, nowhere to write")?;
|
||||
// The scope rides the environment override when the caller set
|
||||
// one — the tests' isolation — and is otherwise git's own
|
||||
// global scope, resolved by git, not guessed here.
|
||||
let scope = std::env::var("GIT_CONFIG_GLOBAL")
|
||||
.ok()
|
||||
.filter(|path| !path.trim().is_empty())
|
||||
.map(PathBuf::from);
|
||||
let db = config.db_path().map_err(|err| err.to_string())?;
|
||||
let settings = Arc::new(SettingsStore::open(&db).map_err(|err| err.to_string())?);
|
||||
let dir = db
|
||||
@@ -108,40 +119,40 @@ pub fn boot(config: &ClawConfig) -> String {
|
||||
let who = IdentityStore::new(settings.clone(), dir)
|
||||
.get()
|
||||
.map_err(|err| err.to_string())?;
|
||||
Ok(provision_at(&file, config.git.as_ref(), &who, &settings))
|
||||
Ok(provision(
|
||||
scope.as_deref(),
|
||||
config.git.as_ref(),
|
||||
&who,
|
||||
&settings,
|
||||
))
|
||||
})();
|
||||
outcome.unwrap_or_else(|reason| format!("git identity not provisioned: {reason}"))
|
||||
}
|
||||
|
||||
/// The global git config the daemon user's `git` reads: the
|
||||
/// `GIT_CONFIG_GLOBAL` override when set (git's own precedence), else
|
||||
/// `.gitconfig` in the home the family's XDG discovery resolves.
|
||||
pub fn global_file() -> Option<PathBuf> {
|
||||
if let Ok(path) = std::env::var("GIT_CONFIG_GLOBAL")
|
||||
&& !path.trim().is_empty()
|
||||
{
|
||||
return Some(PathBuf::from(path));
|
||||
}
|
||||
rhema_config::Home::discover()
|
||||
.ok()
|
||||
.map(|home| home.home.join(".gitconfig"))
|
||||
}
|
||||
|
||||
/// Provisions both fields against one git config file, and returns the
|
||||
/// log line. The guard is per field: a hand-edited name beside an
|
||||
/// email the daemon wrote is updated on the email side and left alone
|
||||
/// on the name side.
|
||||
pub fn provision_at(
|
||||
file: &Path,
|
||||
/// Provisions both fields into the git global scope — `scope`, when
|
||||
/// given, pins `GIT_CONFIG_GLOBAL` for the child `git` calls (the
|
||||
/// tests' isolation); otherwise git resolves its own. Returns the log
|
||||
/// line. The guard is per field: a hand-edited name beside an email
|
||||
/// the daemon wrote is updated on the email side and left alone on
|
||||
/// the name side.
|
||||
pub fn provision(
|
||||
scope: Option<&Path>,
|
||||
git: Option<&GitIdentity>,
|
||||
identity: &AgentIdentity,
|
||||
settings: &SettingsStore,
|
||||
) -> String {
|
||||
let (name, email) = resolve(git, identity, &crate::environment::hostname());
|
||||
let mut parts = Vec::new();
|
||||
field(file, "user.name", WRITTEN_NAME, &name, settings, &mut parts);
|
||||
field(
|
||||
file,
|
||||
scope,
|
||||
"user.name",
|
||||
WRITTEN_NAME,
|
||||
&name,
|
||||
settings,
|
||||
&mut parts,
|
||||
);
|
||||
field(
|
||||
scope,
|
||||
"user.email",
|
||||
WRITTEN_EMAIL,
|
||||
&email,
|
||||
@@ -159,7 +170,7 @@ pub fn provision_at(
|
||||
/// - it is unset, or it is exactly what the daemon wrote last time →
|
||||
/// written, and the marker follows.
|
||||
fn field(
|
||||
file: &Path,
|
||||
scope: Option<&Path>,
|
||||
key: &str,
|
||||
setting_key: &str,
|
||||
want: &ResolvedField,
|
||||
@@ -176,7 +187,7 @@ fn field(
|
||||
))
|
||||
};
|
||||
let written = settings.get(setting_key).ok().flatten();
|
||||
match read(file, key) {
|
||||
match read(scope, key) {
|
||||
Err(reason) => summary(format!("unreadable ({reason}), left alone")),
|
||||
Ok(Some(current))
|
||||
if Some(current.as_str()) != written.as_deref() && current != want.value =>
|
||||
@@ -189,7 +200,7 @@ fn field(
|
||||
}
|
||||
summary("already current".to_owned())
|
||||
}
|
||||
Ok(_) => match write(file, key, &want.value) {
|
||||
Ok(_) => match write(scope, key, &want.value) {
|
||||
Ok(()) => {
|
||||
record(settings, setting_key, &want.value);
|
||||
summary("wrote".to_owned())
|
||||
@@ -215,9 +226,15 @@ enum GitRun {
|
||||
TimedOut,
|
||||
}
|
||||
|
||||
fn git(args: &[&str]) -> GitRun {
|
||||
let mut child = match Command::new("git")
|
||||
.args(args)
|
||||
fn git_scoped(scope: Option<&Path>, args: &[&str]) -> GitRun {
|
||||
let mut command = Command::new("git");
|
||||
command.args(args);
|
||||
if let Some(path) = scope {
|
||||
// The child only: the caller's environment stays untouched,
|
||||
// and parallel scopes in tests cannot see each other.
|
||||
command.env("GIT_CONFIG_GLOBAL", path);
|
||||
}
|
||||
let mut child = match command
|
||||
.stdin(Stdio::null())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::piped())
|
||||
@@ -254,12 +271,14 @@ fn git(args: &[&str]) -> GitRun {
|
||||
GitRun::Completed { code, stdout }
|
||||
}
|
||||
|
||||
/// Reads one key from the config file: `Some` when set, `None` when
|
||||
/// unset (git exits 1 for that), an error when the read cannot be
|
||||
/// Reads one key from the git global scope: `Some` when set, `None`
|
||||
/// when unset (git exits 1 for that), an error when the read cannot be
|
||||
/// trusted (a missing git, a multi-valued key) — and an untrusted read
|
||||
/// is never written over.
|
||||
fn read(file: &Path, key: &str) -> Result<Option<String>, String> {
|
||||
match git(&["config", "--file", &file.to_string_lossy(), "--get", key]) {
|
||||
/// is never written over. Git resolves the scope itself, so the value
|
||||
/// seen is the value a commit would carry, from whichever file holds
|
||||
/// it.
|
||||
fn read(scope: Option<&Path>, key: &str) -> Result<Option<String>, String> {
|
||||
match git_scoped(scope, &["config", "--global", "--get", key]) {
|
||||
GitRun::Completed { code: 0, stdout } => {
|
||||
Ok(Some(stdout.trim().to_owned()).filter(|value| !value.is_empty()))
|
||||
}
|
||||
@@ -270,18 +289,13 @@ fn read(file: &Path, key: &str) -> Result<Option<String>, String> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes one key into the config file, replacing every value the key
|
||||
/// carries (`--replace-all` keeps the field single-valued even over a
|
||||
/// strange starting file).
|
||||
fn write(file: &Path, key: &str, value: &str) -> Result<(), String> {
|
||||
match git(&[
|
||||
"config",
|
||||
"--file",
|
||||
&file.to_string_lossy(),
|
||||
"--replace-all",
|
||||
key,
|
||||
value,
|
||||
]) {
|
||||
/// Writes one key into the git global scope, replacing every value
|
||||
/// the key carries (`--replace-all` keeps the field single-valued even
|
||||
/// over a strange starting file). Git picks the file — its own write
|
||||
/// rule, the XDG config when that is the established one, `.gitconfig`
|
||||
/// otherwise, the `GIT_CONFIG_GLOBAL` the scope pins.
|
||||
fn write(scope: Option<&Path>, key: &str, value: &str) -> Result<(), String> {
|
||||
match git_scoped(scope, &["config", "--global", "--replace-all", key, value]) {
|
||||
GitRun::Completed { code: 0, .. } => Ok(()),
|
||||
GitRun::Completed { code, .. } => Err(format!("exit {code}")),
|
||||
GitRun::NotFound => Err("git not found".to_owned()),
|
||||
@@ -295,8 +309,9 @@ mod tests {
|
||||
use crate::config::GitIdentity;
|
||||
use crate::identity::IdentityTexts;
|
||||
|
||||
/// The fixture: a temp config file, an in-memory settings store,
|
||||
/// and a fresh store handle per test.
|
||||
/// The fixture: a temp file standing in as the git global scope
|
||||
/// (`GIT_CONFIG_GLOBAL` for the child `git` calls), an in-memory
|
||||
/// settings store, and a fresh store handle per test.
|
||||
struct Fixture {
|
||||
file: PathBuf,
|
||||
settings: SettingsStore,
|
||||
@@ -314,6 +329,11 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// Provisions inside the fixture's scope.
|
||||
fn run(fix: &Fixture, git: Option<&GitIdentity>, who: &AgentIdentity) -> String {
|
||||
provision(Some(&fix.file), git, who, &fix.settings)
|
||||
}
|
||||
|
||||
fn identity(name: Option<&str>, email: Option<&str>) -> AgentIdentity {
|
||||
let store_settings = crate::settings::SettingsStore::in_memory().expect("the store opens");
|
||||
let images = tempfile::tempdir().expect("tempdir");
|
||||
@@ -338,14 +358,14 @@ mod tests {
|
||||
}
|
||||
|
||||
fn value_of(file: &Path, key: &str) -> Option<String> {
|
||||
read(file, key).expect("the key reads")
|
||||
read(Some(file), key).expect("the key reads")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn defaults_land_when_nothing_is_set() {
|
||||
let fix = fixture();
|
||||
let who = identity(None, None);
|
||||
let line = provision_at(&fix.file, None, &who, &fix.settings);
|
||||
let line = run(&fix, None, &who);
|
||||
assert_eq!(value_of(&fix.file, "user.name").as_deref(), Some("claw"));
|
||||
assert_eq!(
|
||||
value_of(&fix.file, "user.email").as_deref(),
|
||||
@@ -359,12 +379,7 @@ mod tests {
|
||||
fn config_beats_identity_beats_default() {
|
||||
let fix = fixture();
|
||||
let who = identity(Some("Kosmos"), Some("kosmos@example.net"));
|
||||
provision_at(
|
||||
&fix.file,
|
||||
config_git(Some("Bot"), None).as_ref(),
|
||||
&who,
|
||||
&fix.settings,
|
||||
);
|
||||
run(&fix, config_git(Some("Bot"), None).as_ref(), &who);
|
||||
assert_eq!(value_of(&fix.file, "user.name").as_deref(), Some("Bot"));
|
||||
assert_eq!(
|
||||
value_of(&fix.file, "user.email").as_deref(),
|
||||
@@ -373,7 +388,7 @@ mod tests {
|
||||
);
|
||||
|
||||
let other = fixture();
|
||||
provision_at(&other.file, None, &who, &other.settings);
|
||||
run(&other, None, &who);
|
||||
assert_eq!(
|
||||
value_of(&other.file, "user.name").as_deref(),
|
||||
Some("Kosmos")
|
||||
@@ -383,9 +398,9 @@ mod tests {
|
||||
#[test]
|
||||
fn a_hand_edited_value_wins_and_is_recorded_as_foreign() {
|
||||
let fix = fixture();
|
||||
write(&fix.file, "user.name", "The Operator").expect("the seed writes");
|
||||
write(Some(&fix.file), "user.name", "The Operator").expect("the seed writes");
|
||||
let who = identity(Some("Kosmos"), None);
|
||||
let line = provision_at(&fix.file, None, &who, &fix.settings);
|
||||
let line = run(&fix, None, &who);
|
||||
assert_eq!(
|
||||
value_of(&fix.file, "user.name").as_deref(),
|
||||
Some("The Operator")
|
||||
@@ -401,9 +416,9 @@ mod tests {
|
||||
fn reruns_update_what_the_daemon_wrote() {
|
||||
let fix = fixture();
|
||||
let first = identity(Some("Kosmos"), None);
|
||||
provision_at(&fix.file, None, &first, &fix.settings);
|
||||
run(&fix, None, &first);
|
||||
let second = identity(Some("Kosmos Prime"), None);
|
||||
let line = provision_at(&fix.file, None, &second, &fix.settings);
|
||||
let line = run(&fix, None, &second);
|
||||
assert_eq!(
|
||||
value_of(&fix.file, "user.name").as_deref(),
|
||||
Some("Kosmos Prime"),
|
||||
@@ -413,7 +428,7 @@ mod tests {
|
||||
|
||||
// And a rerun over an already-current pair is a no-op.
|
||||
let before = std::fs::read_to_string(&fix.file).expect("the file reads");
|
||||
let line = provision_at(&fix.file, None, &second, &fix.settings);
|
||||
let line = run(&fix, None, &second);
|
||||
let after = std::fs::read_to_string(&fix.file).expect("the file reads");
|
||||
assert_eq!(before, after, "nothing to do, nothing written");
|
||||
assert!(line.contains("already current"), "{line}");
|
||||
@@ -423,17 +438,15 @@ mod tests {
|
||||
fn a_config_change_flows_through_on_the_next_boot() {
|
||||
let fix = fixture();
|
||||
let who = identity(None, None);
|
||||
provision_at(
|
||||
&fix.file,
|
||||
run(
|
||||
&fix,
|
||||
config_git(Some("Bot"), Some("bot@example.net")).as_ref(),
|
||||
&who,
|
||||
&fix.settings,
|
||||
);
|
||||
provision_at(
|
||||
&fix.file,
|
||||
run(
|
||||
&fix,
|
||||
config_git(Some("Renamed"), Some("moved@example.net")).as_ref(),
|
||||
&who,
|
||||
&fix.settings,
|
||||
);
|
||||
assert_eq!(value_of(&fix.file, "user.name").as_deref(), Some("Renamed"));
|
||||
assert_eq!(
|
||||
@@ -446,11 +459,11 @@ mod tests {
|
||||
fn a_hand_edit_that_matches_the_resolution_counts_as_the_daemons() {
|
||||
let fix = fixture();
|
||||
let who = identity(Some("Kosmos"), None);
|
||||
write(&fix.file, "user.name", "Kosmos").expect("the seed writes");
|
||||
provision_at(&fix.file, None, &who, &fix.settings);
|
||||
write(Some(&fix.file), "user.name", "Kosmos").expect("the seed writes");
|
||||
run(&fix, None, &who);
|
||||
// The marker was aligned, so a later resolution updates the field.
|
||||
let renamed = identity(Some("Kosmos II"), None);
|
||||
provision_at(&fix.file, None, &renamed, &fix.settings);
|
||||
run(&fix, None, &renamed);
|
||||
assert_eq!(
|
||||
value_of(&fix.file, "user.name").as_deref(),
|
||||
Some("Kosmos II")
|
||||
|
||||
Reference in new issue
Block a user