1use std::path::{Component, Path, PathBuf};
22use std::time::Duration;
23
24use anyhow::{Context, Result, bail};
25
26use crate::config::{Config, SetupStep};
27use crate::git;
28use crate::proc::Quiet as _;
29
30pub const RECORD: &str = "magi-setup-paths";
32const EXCLUDE: &str = "magi-setup-exclude";
34const TAIL: usize = 4000;
36
37pub async fn prepare(cfg: &Config, repo: &Path, wt: &Path) -> Result<()> {
42 let steps = &cfg.worktree.setup;
43 if steps.is_empty() {
44 return Ok(());
45 }
46 let total = steps.len();
47 let mut copied: Vec<String> = Vec::new();
48 for (i, step) in steps.iter().enumerate() {
49 let n = i + 1;
50 let what = step.describe();
51 match (&step.copy, &step.run) {
52 (Some(spec), None) => {
53 let made = copy_step(repo, wt, spec, step.optional)
54 .await
55 .with_context(|| format!("worktree setup step {n}/{total} ({what}) failed"))?;
56 copied.extend(made);
57 }
58 (None, Some(command)) => {
59 let secs = step
60 .timeout_secs
61 .unwrap_or_else(|| cfg.graph.verify_timeout());
62 run_step(&cfg.shell(), command, wt, Duration::from_secs(secs))
63 .await
64 .with_context(|| format!("worktree setup step {n}/{total} ({what}) failed"))?;
65 }
66 _ => bail!("worktree setup step {n}/{total} must set exactly one of copy / run"),
67 }
68 }
69 seal(wt, &copied).await.context("worktree setup")
70}
71
72fn split_copy(spec: &str) -> (&str, &str) {
74 match spec.split_once("->") {
75 Some((a, b)) => (a.trim(), b.trim()),
76 None => (spec.trim(), spec.trim()),
77 }
78}
79
80pub fn valid_destination(p: &str) -> Result<()> {
82 if p.is_empty() {
83 bail!("destination is empty");
84 }
85 let path = Path::new(p);
86 if path.is_absolute() || p.starts_with('/') || p.starts_with('\\') || p.contains(':') {
87 bail!("destination `{p}` must be relative to the worktree");
88 }
89 let mut first = true;
90 for c in path.components() {
91 match c {
92 Component::Normal(s) => {
93 if first && s.eq_ignore_ascii_case(".git") {
94 bail!("destination `{p}` is inside .git");
95 }
96 first = false;
97 }
98 Component::CurDir => {}
99 _ => bail!("destination `{p}` must not contain `..`"),
100 }
101 }
102 Ok(())
103}
104
105pub fn validate_copy(spec: &str) -> Result<()> {
107 let (src, dst) = split_copy(spec);
108 if src.is_empty() {
109 bail!("copy `{spec}` has no source");
110 }
111 valid_destination(dst)
112}
113
114async fn copy_step(repo: &Path, wt: &Path, spec: &str, optional: bool) -> Result<Option<String>> {
116 let (src, dst) = split_copy(spec);
117 validate_copy(spec)?;
118 let src_path = if Path::new(src).is_absolute() {
119 PathBuf::from(src)
120 } else {
121 repo.join(src)
122 };
123 let dst_path = wt.join(dst);
124 let mut probe = wt.to_path_buf();
126 for c in Path::new(dst).components() {
127 if let Component::Normal(s) = c {
128 probe.push(s);
129 if let Ok(meta) = tokio::fs::symlink_metadata(&probe).await
130 && meta.file_type().is_symlink()
131 {
132 bail!("destination `{dst}` goes through a symlink");
133 }
134 }
135 }
136 let data = match tokio::fs::read(&src_path).await {
137 Ok(d) => d,
138 Err(e) if e.kind() == std::io::ErrorKind::NotFound && optional => return Ok(None),
139 Err(e) => bail!("cannot read source {}: {e}", src_path.display()),
140 };
141 if let Some(parent) = dst_path.parent() {
142 tokio::fs::create_dir_all(parent)
143 .await
144 .with_context(|| format!("create {}", parent.display()))?;
145 }
146 use tokio::io::AsyncWriteExt as _;
148 match tokio::fs::OpenOptions::new()
149 .write(true)
150 .create_new(true)
151 .open(&dst_path)
152 .await
153 {
154 Ok(mut f) => {
155 f.write_all(&data).await?;
156 f.flush().await?;
157 #[cfg(unix)]
158 if let Ok(meta) = tokio::fs::metadata(&src_path).await {
159 tokio::fs::set_permissions(&dst_path, meta.permissions())
160 .await
161 .ok();
162 }
163 Ok(Some(dst.trim_start_matches("./").to_owned()))
164 }
165 Err(e) if e.kind() == std::io::ErrorKind::AlreadyExists => Ok(None),
166 Err(e) => bail!("cannot create {}: {e}", dst_path.display()),
167 }
168}
169
170fn tail(s: &str) -> &str {
171 let s = s.trim();
172 if s.len() <= TAIL {
173 return s;
174 }
175 let mut at = s.len() - TAIL;
176 while !s.is_char_boundary(at) {
177 at += 1;
178 }
179 &s[at..]
180}
181
182async fn run_step(shell: &[String], command: &str, wt: &Path, timeout: Duration) -> Result<()> {
183 run_command(shell, command, wt, timeout, None, Vec::new(), false).await
184}
185
186pub(crate) async fn run_command(
188 shell: &[String],
189 command: &str,
190 wt: &Path,
191 timeout: Duration,
192 lease: Option<crate::repo_lock::SeatLease>,
193 outputs: Vec<crate::retention::OutputLease>,
194 pre_clean: bool,
195) -> Result<()> {
196 static SEQ: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
197 let log = std::env::temp_dir().join(format!(
198 "magi-setup-{}-{}.out",
199 std::process::id(),
200 SEQ.fetch_add(1, std::sync::atomic::Ordering::Relaxed)
201 ));
202 if pre_clean {
203 tracing::info!(cwd = %wt.display(), output = %log.display(), command, "pre-clean command output (retained on interruption)");
204 }
205 let out = std::fs::File::create(&log).context("create output file")?;
206 let err = out.try_clone()?;
207 let mut cmd = tokio::process::Command::new(&shell[0]);
208 cmd.quiet();
209 cmd.args(&shell[1..])
210 .arg(command)
211 .current_dir(wt)
212 .env_remove("MAGI_RUN")
213 .env_remove("MAGI_NODE")
214 .stdin(std::process::Stdio::null())
215 .stdout(out)
216 .stderr(err)
217 .kill_on_drop(true);
218 crate::chat_control::bridge::clear_environment(&mut cmd);
219 if pre_clean {
220 cmd.env("MAGI_PRE_CLEAN_ACTIVE", "1");
221 for key in crate::worker::INVOCATION_ENV {
222 cmd.env_remove(key);
223 }
224 }
225 crate::incremental::configure(&mut cmd, wt)?;
226 let mut running = crate::seat_process::Running::spawn_operation(cmd, lease, outputs)
227 .with_context(|| format!("spawn `{}` for `{command}`", shell[0]))?;
228 let waited = tokio::time::timeout(timeout, running.child().wait()).await;
229 running.finish().await;
233 let body = || {
234 use std::io::{Read, Seek, SeekFrom};
235 let mut bytes = Vec::new();
236 if let Ok(mut f) = std::fs::File::open(&log) {
237 let _ = f.seek(SeekFrom::End(-(TAIL as i64)));
238 let _ = f.take(TAIL as u64).read_to_end(&mut bytes);
239 }
240 tail(&String::from_utf8_lossy(&bytes)).to_owned()
241 };
242 let result = match waited {
243 Ok(Ok(status)) if status.success() => Ok(()),
244 Ok(Ok(status)) => Err(anyhow::anyhow!(
245 "`{command}` exited with {status}\n{}",
246 body()
247 )),
248 Ok(Err(e)) => Err(anyhow::anyhow!("`{command}` could not be awaited: {e}")),
249 Err(_) => Err(anyhow::anyhow!(
250 "`{command}` timed out after {}s\n{}",
251 timeout.as_secs(),
252 body()
253 )),
254 };
255 std::fs::remove_file(&log).ok();
256 result
257}
258
259async fn git_dir(wt: &Path) -> Result<PathBuf> {
260 Ok(PathBuf::from(
261 git::git(wt, &["rev-parse", "--absolute-git-dir"])
262 .await?
263 .trim(),
264 ))
265}
266
267pub async fn withheld_paths(wt: &Path) -> Vec<String> {
269 let Ok(dir) = git_dir(wt).await else {
270 return Vec::new();
271 };
272 let Ok(raw) = tokio::fs::read(dir.join(RECORD)).await else {
273 return Vec::new();
274 };
275 raw.split(|b| *b == 0)
276 .filter(|p| !p.is_empty())
277 .map(|p| String::from_utf8_lossy(p).into_owned())
278 .collect()
279}
280
281pub async fn unstage_products(wt: &Path) -> Result<()> {
283 let paths = withheld_paths(wt).await;
284 if paths.is_empty() {
285 return Ok(());
286 }
287 let mut args = vec!["reset", "-q", "--"];
288 args.extend(paths.iter().map(String::as_str));
289 git::git(wt, &args).await.map(|_| ())
290}
291
292async fn seal(wt: &Path, copied: &[String]) -> Result<()> {
295 let tracked = git::git(wt, &["diff", "--name-only", "HEAD"]).await?;
296 if !tracked.trim().is_empty() {
297 bail!(
298 "setup must not change tracked files (every seat would carry the same diff), \
299 but it changed:\n{}",
300 tail(&tracked)
301 );
302 }
303 let out = git::git(wt, &["ls-files", "-z", "--others", "--exclude-standard"]).await?;
304 let mut paths: Vec<String> = withheld_paths(wt).await;
305 for p in copied
309 .iter()
310 .map(String::as_str)
311 .chain(out.split('\0').filter(|p| !p.is_empty()))
312 {
313 if !paths.iter().any(|q| q == p) {
314 paths.push(p.to_owned());
315 }
316 }
317 if paths.is_empty() {
318 return Ok(());
319 }
320 let dir = git_dir(wt).await?;
321 let mut raw = Vec::new();
322 for p in &paths {
323 raw.extend_from_slice(p.as_bytes());
324 raw.push(0);
325 }
326 tokio::fs::write(dir.join(RECORD), raw).await?;
327 hide(wt, &dir, &paths).await
328}
329
330fn exclude_line(p: &str) -> String {
331 let mut s = String::from("/");
332 for c in p.chars() {
333 if matches!(c, '*' | '?' | '[' | ']' | '\\' | '#' | '!' | ' ') {
334 s.push('\\');
335 }
336 s.push(c);
337 }
338 s
339}
340
341async fn hide(wt: &Path, dir: &Path, paths: &[String]) -> Result<()> {
347 let ext = git::git_raw(wt, &["config", "--get", "extensions.worktreeConfig"]).await?;
348 if !(ext.ok() && ext.stdout.trim() == "true") {
349 bail!(
350 "setup left untracked files ({}) but extensions.worktreeConfig is off, so they \
351 cannot be hidden from `git add` per worktree",
352 paths.join(", ")
353 );
354 }
355 let file = dir.join(EXCLUDE);
356 let file_s = file.to_string_lossy().replace('\\', "/");
357 let current = git::git_raw(wt, &["config", "--get", "core.excludesFile"])
358 .await
359 .ok()
360 .filter(|o| o.ok())
361 .map(|o| o.stdout.trim().to_owned())
362 .filter(|s| !s.is_empty());
363 let mut body = match current {
364 Some(c) if c == file_s => std::fs::read_to_string(&file).unwrap_or_default(),
365 Some(c) => {
366 let c = match c.strip_prefix("~/") {
367 Some(rest) => std::env::var_os("HOME")
368 .map(|h| PathBuf::from(h).join(rest))
369 .unwrap_or_else(|| PathBuf::from(&c)),
370 None => PathBuf::from(&c),
371 };
372 std::fs::read_to_string(c).unwrap_or_default()
373 }
374 None => String::new(),
375 };
376 if !body.is_empty() && !body.ends_with('\n') {
377 body.push('\n');
378 }
379 for p in paths {
380 let line = exclude_line(p);
381 if !body.lines().any(|l| l == line) {
382 body.push_str(&line);
383 body.push('\n');
384 }
385 }
386 std::fs::write(&file, body).with_context(|| format!("write {}", file.display()))?;
387 git::git(wt, &["config", "--worktree", "core.excludesFile", &file_s])
388 .await
389 .map(|_| ())
390}
391
392impl SetupStep {
393 pub fn describe(&self) -> String {
395 match (&self.copy, &self.run) {
396 (Some(c), _) => format!("copy {c}"),
397 (_, Some(r)) => format!("run {r}"),
398 _ => "empty".to_owned(),
399 }
400 }
401}
402
403#[cfg(test)]
404mod tests {
405 use super::*;
406 use crate::config::SetupStep;
407
408 fn copy(spec: &str) -> SetupStep {
409 SetupStep {
410 copy: Some(spec.to_owned()),
411 ..Default::default()
412 }
413 }
414
415 fn run(cmd: &str) -> SetupStep {
416 SetupStep {
417 run: Some(cmd.to_owned()),
418 ..Default::default()
419 }
420 }
421
422 fn cfg(steps: Vec<SetupStep>) -> Config {
423 let mut c = Config::default();
424 c.worktree.setup = steps;
425 c.verify.shell = Some(vec!["sh".to_owned(), "-c".to_owned()]);
426 c
427 }
428
429 async fn scratch() -> (tempfile::TempDir, PathBuf, PathBuf) {
431 let tmp = tempfile::tempdir().unwrap();
432 let repo = tmp.path().join("repo");
433 std::fs::create_dir_all(&repo).unwrap();
434 for args in [
435 vec!["init", "-b", "main"],
436 vec!["config", "user.name", "t"],
437 vec!["config", "user.email", "t@example.com"],
438 ] {
439 git::git(&repo, &args).await.unwrap();
440 }
441 std::fs::write(repo.join("a.txt"), "a\n").unwrap();
442 git::git(&repo, &["add", "-A"]).await.unwrap();
443 git::git(&repo, &["commit", "-m", "init"]).await.unwrap();
444 let wt = tmp.path().join("wt");
445 git::worktree_add_detached(&repo, &wt, "HEAD")
446 .await
447 .unwrap();
448 git::acquire_worktree_config(&repo).await.unwrap();
450 (tmp, repo, wt)
451 }
452
453 #[tokio::test]
454 async fn default_is_a_no_op() {
455 let (_t, repo, wt) = scratch().await;
456 prepare(&Config::default(), &repo, &wt).await.unwrap();
457 assert!(withheld_paths(&wt).await.is_empty());
458 }
459
460 #[tokio::test]
461 async fn copy_then_run_in_order() {
462 let (_t, repo, wt) = scratch().await;
463 std::fs::write(repo.join(".env.example"), "KEY=1\n").unwrap();
464 let c = cfg(vec![
465 copy(".env.example -> .env"),
466 run("cat .env > seen.txt && echo ran >> seen.txt"),
467 ]);
468 prepare(&c, &repo, &wt).await.unwrap();
469 assert_eq!(
470 std::fs::read_to_string(wt.join("seen.txt")).unwrap(),
471 "KEY=1\nran\n"
472 );
473 let mut got = withheld_paths(&wt).await;
474 got.sort();
475 assert_eq!(got, [".env", "seen.txt"]);
476 }
477
478 #[tokio::test]
479 async fn a_failing_step_stops_and_names_step_and_output() {
480 let (_t, repo, wt) = scratch().await;
481 let c = cfg(vec![run("echo boom >&2; exit 3"), run("touch after.txt")]);
482 let err = format!("{:#}", prepare(&c, &repo, &wt).await.unwrap_err());
483 assert!(err.contains("step 1/2"), "{err}");
484 assert!(err.contains("boom"), "{err}");
485 assert!(!wt.join("after.txt").exists());
486 }
487
488 #[tokio::test]
489 async fn a_timeout_keeps_the_partial_output() {
490 let (_t, repo, wt) = scratch().await;
491 let mut step = run("echo partial; sleep 30");
492 step.timeout_secs = Some(1);
493 let err = format!(
494 "{:#}",
495 prepare(&cfg(vec![step]), &repo, &wt).await.unwrap_err()
496 );
497 assert!(err.contains("timed out"), "{err}");
498 assert!(err.contains("partial"), "{err}");
499 }
500
501 #[tokio::test]
502 async fn copy_never_overwrites() {
503 let (_t, repo, wt) = scratch().await;
504 std::fs::write(repo.join("src.txt"), "new\n").unwrap();
505 std::fs::write(wt.join("dst.txt"), "mine\n").unwrap();
506 prepare(&cfg(vec![copy("src.txt -> dst.txt")]), &repo, &wt)
507 .await
508 .unwrap();
509 assert_eq!(
510 std::fs::read_to_string(wt.join("dst.txt")).unwrap(),
511 "mine\n"
512 );
513 }
514
515 #[tokio::test]
516 async fn a_missing_source_is_an_error_unless_optional() {
517 let (_t, repo, wt) = scratch().await;
518 let err = format!(
519 "{:#}",
520 prepare(&cfg(vec![copy("nope")]), &repo, &wt)
521 .await
522 .unwrap_err()
523 );
524 assert!(err.contains("step 1/1") && err.contains("nope"), "{err}");
525 let mut opt = copy("nope");
526 opt.optional = true;
527 prepare(&cfg(vec![opt]), &repo, &wt).await.unwrap();
528 assert!(!wt.join("nope").exists());
529 }
530
531 #[tokio::test]
532 async fn setup_may_not_edit_tracked_files() {
533 let (_t, repo, wt) = scratch().await;
534 let err = format!(
535 "{:#}",
536 prepare(&cfg(vec![run("echo x >> a.txt")]), &repo, &wt)
537 .await
538 .unwrap_err()
539 );
540 assert!(err.contains("tracked") && err.contains("a.txt"), "{err}");
541 }
542
543 #[tokio::test]
544 async fn the_command_does_not_see_the_run_identity() {
545 let (_t, repo, wt) = scratch().await;
546 unsafe {
548 std::env::set_var("MAGI_RUN", "r1");
549 std::env::set_var("MAGI_NODE", "n1");
550 }
551 prepare(
552 &cfg(vec![run("echo \"[$MAGI_RUN][$MAGI_NODE]\" > ids.txt")]),
553 &repo,
554 &wt,
555 )
556 .await
557 .unwrap();
558 assert_eq!(
559 std::fs::read_to_string(wt.join("ids.txt")).unwrap(),
560 "[][]\n"
561 );
562 }
563
564 #[tokio::test]
565 async fn products_are_kept_out_of_both_commit_paths() {
566 let (_t, repo, wt) = scratch().await;
567 std::fs::write(repo.join("secret"), "s\n").unwrap();
568 prepare(&cfg(vec![copy("secret")]), &repo, &wt)
569 .await
570 .unwrap();
571
572 git::git(&wt, &["add", "-A"]).await.unwrap();
574 assert!(
575 git::git(&wt, &["diff", "--cached", "--name-only"])
576 .await
577 .unwrap()
578 .is_empty()
579 );
580
581 assert!(!git::commit_all(&wt, "rescue").await.unwrap());
583 let r = git::rescue_commit(&wt, "rescue").await.unwrap();
584 assert!(!r.committed);
585
586 std::fs::write(wt.join("work.txt"), "w\n").unwrap();
588 assert!(git::commit_all(&wt, "rescue").await.unwrap());
589 let files = git::git(&wt, &["show", "--name-only", "--format=", "HEAD"])
590 .await
591 .unwrap();
592 assert_eq!(files.trim(), "work.txt");
593 }
594
595 #[tokio::test]
596 async fn products_without_worktree_config_fail_loudly_and_leave_shared_config_alone() {
597 let (_t, repo, wt) = scratch().await;
598 git::release_worktree_config(&repo).await.unwrap();
599 std::fs::write(repo.join("secret"), "s\n").unwrap();
600 let err = format!(
601 "{:#}",
602 prepare(&cfg(vec![copy("secret")]), &repo, &wt)
603 .await
604 .unwrap_err()
605 );
606 assert!(err.contains("worktreeConfig"), "{err}");
607 let shared = git::git_raw(&repo, &["config", "--get", "core.excludesFile"])
608 .await
609 .unwrap();
610 assert!(!shared.ok(), "shared config was written: {}", shared.stdout);
611 }
612
613 #[tokio::test]
614 async fn an_ignored_copy_is_still_withheld_from_commits() {
615 let (_t, repo, wt) = scratch().await;
616 std::fs::write(wt.join(".gitignore"), ".env\n").unwrap();
617 git::git(&wt, &["add", ".gitignore"]).await.unwrap();
618 git::git(&wt, &["commit", "-m", "ignore"]).await.unwrap();
619 std::fs::write(repo.join(".env"), "s\n").unwrap();
620 prepare(&cfg(vec![copy(".env")]), &repo, &wt).await.unwrap();
621 assert_eq!(withheld_paths(&wt).await, [".env"]);
622 git::git(&wt, &["add", "-f", ".env"]).await.unwrap();
624 std::fs::write(wt.join("work.txt"), "w\n").unwrap();
625 assert!(git::commit_all(&wt, "rescue").await.unwrap());
626 let files = git::git(&wt, &["show", "--name-only", "--format=", "HEAD"])
627 .await
628 .unwrap();
629 assert_eq!(files.trim(), "work.txt");
630 }
631
632 #[test]
633 fn destinations_must_stay_inside() {
634 for bad in ["/etc/x", "../x", "a/../../x", ".git/hooks/x", "C:/x", ""] {
635 assert!(valid_destination(bad).is_err(), "{bad}");
636 }
637 assert!(valid_destination("a/b.env").is_ok());
638 }
639}