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 if pre_clean {
219 cmd.env("MAGI_PRE_CLEAN_ACTIVE", "1");
220 for key in crate::worker::INVOCATION_ENV {
221 cmd.env_remove(key);
222 }
223 }
224 crate::incremental::configure(&mut cmd, wt)?;
225 let mut running = crate::seat_process::Running::spawn_operation(cmd, lease, outputs)
226 .with_context(|| format!("spawn `{}` for `{command}`", shell[0]))?;
227 let waited = tokio::time::timeout(timeout, running.child().wait()).await;
228 running.finish().await;
232 let body = || {
233 use std::io::{Read, Seek, SeekFrom};
234 let mut bytes = Vec::new();
235 if let Ok(mut f) = std::fs::File::open(&log) {
236 let _ = f.seek(SeekFrom::End(-(TAIL as i64)));
237 let _ = f.take(TAIL as u64).read_to_end(&mut bytes);
238 }
239 tail(&String::from_utf8_lossy(&bytes)).to_owned()
240 };
241 let result = match waited {
242 Ok(Ok(status)) if status.success() => Ok(()),
243 Ok(Ok(status)) => Err(anyhow::anyhow!(
244 "`{command}` exited with {status}\n{}",
245 body()
246 )),
247 Ok(Err(e)) => Err(anyhow::anyhow!("`{command}` could not be awaited: {e}")),
248 Err(_) => Err(anyhow::anyhow!(
249 "`{command}` timed out after {}s\n{}",
250 timeout.as_secs(),
251 body()
252 )),
253 };
254 std::fs::remove_file(&log).ok();
255 result
256}
257
258async fn git_dir(wt: &Path) -> Result<PathBuf> {
259 Ok(PathBuf::from(
260 git::git(wt, &["rev-parse", "--absolute-git-dir"])
261 .await?
262 .trim(),
263 ))
264}
265
266pub async fn withheld_paths(wt: &Path) -> Vec<String> {
268 let Ok(dir) = git_dir(wt).await else {
269 return Vec::new();
270 };
271 let Ok(raw) = tokio::fs::read(dir.join(RECORD)).await else {
272 return Vec::new();
273 };
274 raw.split(|b| *b == 0)
275 .filter(|p| !p.is_empty())
276 .map(|p| String::from_utf8_lossy(p).into_owned())
277 .collect()
278}
279
280pub async fn unstage_products(wt: &Path) -> Result<()> {
282 let paths = withheld_paths(wt).await;
283 if paths.is_empty() {
284 return Ok(());
285 }
286 let mut args = vec!["reset", "-q", "--"];
287 args.extend(paths.iter().map(String::as_str));
288 git::git(wt, &args).await.map(|_| ())
289}
290
291async fn seal(wt: &Path, copied: &[String]) -> Result<()> {
294 let tracked = git::git(wt, &["diff", "--name-only", "HEAD"]).await?;
295 if !tracked.trim().is_empty() {
296 bail!(
297 "setup must not change tracked files (every seat would carry the same diff), \
298 but it changed:\n{}",
299 tail(&tracked)
300 );
301 }
302 let out = git::git(wt, &["ls-files", "-z", "--others", "--exclude-standard"]).await?;
303 let mut paths: Vec<String> = withheld_paths(wt).await;
304 for p in copied
308 .iter()
309 .map(String::as_str)
310 .chain(out.split('\0').filter(|p| !p.is_empty()))
311 {
312 if !paths.iter().any(|q| q == p) {
313 paths.push(p.to_owned());
314 }
315 }
316 if paths.is_empty() {
317 return Ok(());
318 }
319 let dir = git_dir(wt).await?;
320 let mut raw = Vec::new();
321 for p in &paths {
322 raw.extend_from_slice(p.as_bytes());
323 raw.push(0);
324 }
325 tokio::fs::write(dir.join(RECORD), raw).await?;
326 hide(wt, &dir, &paths).await
327}
328
329fn exclude_line(p: &str) -> String {
330 let mut s = String::from("/");
331 for c in p.chars() {
332 if matches!(c, '*' | '?' | '[' | ']' | '\\' | '#' | '!' | ' ') {
333 s.push('\\');
334 }
335 s.push(c);
336 }
337 s
338}
339
340async fn hide(wt: &Path, dir: &Path, paths: &[String]) -> Result<()> {
346 let ext = git::git_raw(wt, &["config", "--get", "extensions.worktreeConfig"]).await?;
347 if !(ext.ok() && ext.stdout.trim() == "true") {
348 bail!(
349 "setup left untracked files ({}) but extensions.worktreeConfig is off, so they \
350 cannot be hidden from `git add` per worktree",
351 paths.join(", ")
352 );
353 }
354 let file = dir.join(EXCLUDE);
355 let file_s = file.to_string_lossy().replace('\\', "/");
356 let current = git::git_raw(wt, &["config", "--get", "core.excludesFile"])
357 .await
358 .ok()
359 .filter(|o| o.ok())
360 .map(|o| o.stdout.trim().to_owned())
361 .filter(|s| !s.is_empty());
362 let mut body = match current {
363 Some(c) if c == file_s => std::fs::read_to_string(&file).unwrap_or_default(),
364 Some(c) => {
365 let c = match c.strip_prefix("~/") {
366 Some(rest) => std::env::var_os("HOME")
367 .map(|h| PathBuf::from(h).join(rest))
368 .unwrap_or_else(|| PathBuf::from(&c)),
369 None => PathBuf::from(&c),
370 };
371 std::fs::read_to_string(c).unwrap_or_default()
372 }
373 None => String::new(),
374 };
375 if !body.is_empty() && !body.ends_with('\n') {
376 body.push('\n');
377 }
378 for p in paths {
379 let line = exclude_line(p);
380 if !body.lines().any(|l| l == line) {
381 body.push_str(&line);
382 body.push('\n');
383 }
384 }
385 std::fs::write(&file, body).with_context(|| format!("write {}", file.display()))?;
386 git::git(wt, &["config", "--worktree", "core.excludesFile", &file_s])
387 .await
388 .map(|_| ())
389}
390
391impl SetupStep {
392 pub fn describe(&self) -> String {
394 match (&self.copy, &self.run) {
395 (Some(c), _) => format!("copy {c}"),
396 (_, Some(r)) => format!("run {r}"),
397 _ => "empty".to_owned(),
398 }
399 }
400}
401
402#[cfg(test)]
403mod tests {
404 use super::*;
405 use crate::config::SetupStep;
406
407 fn copy(spec: &str) -> SetupStep {
408 SetupStep {
409 copy: Some(spec.to_owned()),
410 ..Default::default()
411 }
412 }
413
414 fn run(cmd: &str) -> SetupStep {
415 SetupStep {
416 run: Some(cmd.to_owned()),
417 ..Default::default()
418 }
419 }
420
421 fn cfg(steps: Vec<SetupStep>) -> Config {
422 let mut c = Config::default();
423 c.worktree.setup = steps;
424 c.verify.shell = Some(vec!["sh".to_owned(), "-c".to_owned()]);
425 c
426 }
427
428 async fn scratch() -> (tempfile::TempDir, PathBuf, PathBuf) {
430 let tmp = tempfile::tempdir().unwrap();
431 let repo = tmp.path().join("repo");
432 std::fs::create_dir_all(&repo).unwrap();
433 for args in [
434 vec!["init", "-b", "main"],
435 vec!["config", "user.name", "t"],
436 vec!["config", "user.email", "t@example.com"],
437 ] {
438 git::git(&repo, &args).await.unwrap();
439 }
440 std::fs::write(repo.join("a.txt"), "a\n").unwrap();
441 git::git(&repo, &["add", "-A"]).await.unwrap();
442 git::git(&repo, &["commit", "-m", "init"]).await.unwrap();
443 let wt = tmp.path().join("wt");
444 git::worktree_add_detached(&repo, &wt, "HEAD")
445 .await
446 .unwrap();
447 git::acquire_worktree_config(&repo).await.unwrap();
449 (tmp, repo, wt)
450 }
451
452 #[tokio::test]
453 async fn default_is_a_no_op() {
454 let (_t, repo, wt) = scratch().await;
455 prepare(&Config::default(), &repo, &wt).await.unwrap();
456 assert!(withheld_paths(&wt).await.is_empty());
457 }
458
459 #[tokio::test]
460 async fn copy_then_run_in_order() {
461 let (_t, repo, wt) = scratch().await;
462 std::fs::write(repo.join(".env.example"), "KEY=1\n").unwrap();
463 let c = cfg(vec![
464 copy(".env.example -> .env"),
465 run("cat .env > seen.txt && echo ran >> seen.txt"),
466 ]);
467 prepare(&c, &repo, &wt).await.unwrap();
468 assert_eq!(
469 std::fs::read_to_string(wt.join("seen.txt")).unwrap(),
470 "KEY=1\nran\n"
471 );
472 let mut got = withheld_paths(&wt).await;
473 got.sort();
474 assert_eq!(got, [".env", "seen.txt"]);
475 }
476
477 #[tokio::test]
478 async fn a_failing_step_stops_and_names_step_and_output() {
479 let (_t, repo, wt) = scratch().await;
480 let c = cfg(vec![run("echo boom >&2; exit 3"), run("touch after.txt")]);
481 let err = format!("{:#}", prepare(&c, &repo, &wt).await.unwrap_err());
482 assert!(err.contains("step 1/2"), "{err}");
483 assert!(err.contains("boom"), "{err}");
484 assert!(!wt.join("after.txt").exists());
485 }
486
487 #[tokio::test]
488 async fn a_timeout_keeps_the_partial_output() {
489 let (_t, repo, wt) = scratch().await;
490 let mut step = run("echo partial; sleep 30");
491 step.timeout_secs = Some(1);
492 let err = format!(
493 "{:#}",
494 prepare(&cfg(vec![step]), &repo, &wt).await.unwrap_err()
495 );
496 assert!(err.contains("timed out"), "{err}");
497 assert!(err.contains("partial"), "{err}");
498 }
499
500 #[tokio::test]
501 async fn copy_never_overwrites() {
502 let (_t, repo, wt) = scratch().await;
503 std::fs::write(repo.join("src.txt"), "new\n").unwrap();
504 std::fs::write(wt.join("dst.txt"), "mine\n").unwrap();
505 prepare(&cfg(vec![copy("src.txt -> dst.txt")]), &repo, &wt)
506 .await
507 .unwrap();
508 assert_eq!(
509 std::fs::read_to_string(wt.join("dst.txt")).unwrap(),
510 "mine\n"
511 );
512 }
513
514 #[tokio::test]
515 async fn a_missing_source_is_an_error_unless_optional() {
516 let (_t, repo, wt) = scratch().await;
517 let err = format!(
518 "{:#}",
519 prepare(&cfg(vec![copy("nope")]), &repo, &wt)
520 .await
521 .unwrap_err()
522 );
523 assert!(err.contains("step 1/1") && err.contains("nope"), "{err}");
524 let mut opt = copy("nope");
525 opt.optional = true;
526 prepare(&cfg(vec![opt]), &repo, &wt).await.unwrap();
527 assert!(!wt.join("nope").exists());
528 }
529
530 #[tokio::test]
531 async fn setup_may_not_edit_tracked_files() {
532 let (_t, repo, wt) = scratch().await;
533 let err = format!(
534 "{:#}",
535 prepare(&cfg(vec![run("echo x >> a.txt")]), &repo, &wt)
536 .await
537 .unwrap_err()
538 );
539 assert!(err.contains("tracked") && err.contains("a.txt"), "{err}");
540 }
541
542 #[tokio::test]
543 async fn the_command_does_not_see_the_run_identity() {
544 let (_t, repo, wt) = scratch().await;
545 unsafe {
547 std::env::set_var("MAGI_RUN", "r1");
548 std::env::set_var("MAGI_NODE", "n1");
549 }
550 prepare(
551 &cfg(vec![run("echo \"[$MAGI_RUN][$MAGI_NODE]\" > ids.txt")]),
552 &repo,
553 &wt,
554 )
555 .await
556 .unwrap();
557 assert_eq!(
558 std::fs::read_to_string(wt.join("ids.txt")).unwrap(),
559 "[][]\n"
560 );
561 }
562
563 #[tokio::test]
564 async fn products_are_kept_out_of_both_commit_paths() {
565 let (_t, repo, wt) = scratch().await;
566 std::fs::write(repo.join("secret"), "s\n").unwrap();
567 prepare(&cfg(vec![copy("secret")]), &repo, &wt)
568 .await
569 .unwrap();
570
571 git::git(&wt, &["add", "-A"]).await.unwrap();
573 assert!(
574 git::git(&wt, &["diff", "--cached", "--name-only"])
575 .await
576 .unwrap()
577 .is_empty()
578 );
579
580 assert!(!git::commit_all(&wt, "rescue").await.unwrap());
582 let r = git::rescue_commit(&wt, "rescue").await.unwrap();
583 assert!(!r.committed);
584
585 std::fs::write(wt.join("work.txt"), "w\n").unwrap();
587 assert!(git::commit_all(&wt, "rescue").await.unwrap());
588 let files = git::git(&wt, &["show", "--name-only", "--format=", "HEAD"])
589 .await
590 .unwrap();
591 assert_eq!(files.trim(), "work.txt");
592 }
593
594 #[tokio::test]
595 async fn products_without_worktree_config_fail_loudly_and_leave_shared_config_alone() {
596 let (_t, repo, wt) = scratch().await;
597 git::release_worktree_config(&repo).await.unwrap();
598 std::fs::write(repo.join("secret"), "s\n").unwrap();
599 let err = format!(
600 "{:#}",
601 prepare(&cfg(vec![copy("secret")]), &repo, &wt)
602 .await
603 .unwrap_err()
604 );
605 assert!(err.contains("worktreeConfig"), "{err}");
606 let shared = git::git_raw(&repo, &["config", "--get", "core.excludesFile"])
607 .await
608 .unwrap();
609 assert!(!shared.ok(), "shared config was written: {}", shared.stdout);
610 }
611
612 #[tokio::test]
613 async fn an_ignored_copy_is_still_withheld_from_commits() {
614 let (_t, repo, wt) = scratch().await;
615 std::fs::write(wt.join(".gitignore"), ".env\n").unwrap();
616 git::git(&wt, &["add", ".gitignore"]).await.unwrap();
617 git::git(&wt, &["commit", "-m", "ignore"]).await.unwrap();
618 std::fs::write(repo.join(".env"), "s\n").unwrap();
619 prepare(&cfg(vec![copy(".env")]), &repo, &wt).await.unwrap();
620 assert_eq!(withheld_paths(&wt).await, [".env"]);
621 git::git(&wt, &["add", "-f", ".env"]).await.unwrap();
623 std::fs::write(wt.join("work.txt"), "w\n").unwrap();
624 assert!(git::commit_all(&wt, "rescue").await.unwrap());
625 let files = git::git(&wt, &["show", "--name-only", "--format=", "HEAD"])
626 .await
627 .unwrap();
628 assert_eq!(files.trim(), "work.txt");
629 }
630
631 #[test]
632 fn destinations_must_stay_inside() {
633 for bad in ["/etc/x", "../x", "a/../../x", ".git/hooks/x", "C:/x", ""] {
634 assert!(valid_destination(bad).is_err(), "{bad}");
635 }
636 assert!(valid_destination("a/b.env").is_ok());
637 }
638}