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