-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworktree.rs
More file actions
538 lines (500 loc) · 19.7 KB
/
Copy pathworktree.rs
File metadata and controls
538 lines (500 loc) · 19.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
//! Git worktree helpers for parallel subagent isolation.
//!
//! When `worktree: true` is set on a parallel subagent (or goal deploy with
//! concurrency > 1 for mutating agents), each task gets a linked worktree under
//! `.catalyst-code/worktrees/<run_id>/` so writers cannot clobber each other.
//! Non-git workspaces cannot isolate this way — callers should error clearly.
//!
//! All add/remove calls are serialized: concurrent `git worktree add` races on
//! `.git` locks and can fail spuriously (observed as uniform goal-step failures).
//!
//! Goal waves: `promote_worktree` copies wt→main; the next wave's `add_worktree`
//! checks out HEAD only, so call `seed_worktree_from_main` (main→wt) so dependents
//! see uncommitted promoted files from prior waves.
use crate::protocol::{emit, Event};
use serde_json::json;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Mutex;
/// Process-wide lock so concurrent goal/parallel steps don't race `git worktree`.
static WORKTREE_LOCK: Mutex<()> = Mutex::new(());
/// True when `workspace` is inside a git working tree.
pub fn is_git_repo(workspace: &Path) -> bool {
Command::new("git")
.args(["rev-parse", "--is-inside-work-tree"])
.current_dir(workspace)
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false)
}
fn worktrees_root(workspace: &Path) -> PathBuf {
workspace.join(".catalyst-code").join("worktrees")
}
fn git_out(workspace: &Path, args: &[&str]) -> Result<String, String> {
let out = Command::new("git")
.args(args)
.current_dir(workspace)
.output()
.map_err(|e| format!("git {} failed to spawn: {e}", args.first().unwrap_or(&"")))?;
if !out.status.success() {
let err = String::from_utf8_lossy(&out.stderr);
return Err(format!("git {} failed: {}", args.join(" "), err.trim()));
}
Ok(String::from_utf8_lossy(&out.stdout).trim().to_string())
}
/// Create a linked worktree for `run_id`. Returns the absolute worktree path.
pub fn add_worktree(workspace: &Path, run_id: &str) -> Result<PathBuf, String> {
let _guard = WORKTREE_LOCK.lock().unwrap_or_else(|e| e.into_inner());
add_worktree_locked(workspace, run_id)
}
fn add_worktree_locked(workspace: &Path, run_id: &str) -> Result<PathBuf, String> {
if !is_git_repo(workspace) {
return Err(
"worktree isolation requires a git repository; use checkpoints instead of worktree:true"
.into(),
);
}
let root = worktrees_root(workspace);
std::fs::create_dir_all(&root).map_err(|e| format!("create worktrees dir: {e}"))?;
// Sanitize run_id for a branch/path segment.
let safe: String = run_id
.chars()
.map(|c| {
if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
c
} else {
'-'
}
})
.collect();
let path = root.join(&safe);
if path.exists() {
let _ = remove_worktree_locked(workspace, &path);
}
let branch = format!("catcode/wt/{safe}");
// Prefer creating from HEAD; detach if branch already exists.
let head = git_out(workspace, &["rev-parse", "HEAD"])?;
let add = Command::new("git")
.args([
"worktree",
"add",
"-B",
&branch,
path.to_str().unwrap_or("."),
&head,
])
.current_dir(workspace)
.output()
.map_err(|e| format!("git worktree add spawn: {e}"))?;
if !add.status.success() {
let err = String::from_utf8_lossy(&add.stderr);
return Err(format!("git worktree add failed: {}", err.trim()));
}
emit(
&Event::new("worktree_ready")
.with("run_id", json!(run_id))
.with("path", json!(path.display().to_string()))
.with("branch", json!(branch)),
);
Ok(path)
}
/// Remove a linked worktree (best-effort).
pub fn remove_worktree(workspace: &Path, wt_path: &Path) -> Result<(), String> {
let _guard = WORKTREE_LOCK.lock().unwrap_or_else(|e| e.into_inner());
remove_worktree_locked(workspace, wt_path)
}
fn remove_worktree_locked(workspace: &Path, wt_path: &Path) -> Result<(), String> {
let path_str = wt_path.display().to_string();
let out = Command::new("git")
.args(["worktree", "remove", "--force", &path_str])
.current_dir(workspace)
.output()
.map_err(|e| format!("git worktree remove spawn: {e}"))?;
if !out.status.success() {
// Fall back to plain rm if git refuses (already pruned, etc.).
let _ = std::fs::remove_dir_all(wt_path);
let _ = Command::new("git")
.args(["worktree", "prune"])
.current_dir(workspace)
.status();
}
emit(&Event::new("worktree_cleaned").with("path", json!(path_str)));
Ok(())
}
/// Copy changed tracked+untracked files from a worktree back into the main
/// workspace (shadow promote). Skips `.git` and `.catalyst-code`.
/// Also removes from main any tracked paths deleted in the worktree vs HEAD
/// (`git diff --diff-filter=D`). Does **not** full-mirror-delete (would wipe
/// sibling promotions on concurrent waves).
pub fn promote_worktree(main_ws: &Path, wt_path: &Path) -> Result<Vec<String>, String> {
let _guard = WORKTREE_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut promoted = Vec::new();
copy_tree_diff(wt_path, main_ws, wt_path, &mut promoted)?;
sync_git_deletions(wt_path, main_ws, &mut promoted)?;
Ok(promoted)
}
/// After `add_worktree` (checkout HEAD only), mirror main's working tree into
/// the new worktree so dependent goal steps see prior-wave output — including
/// deletions (full mirror under skip rules). Memory: promote is wt→main; seed
/// is main→wt — without seed, next-wave workers miss uncommitted promotes.
pub fn seed_worktree_from_main(main_ws: &Path, wt_path: &Path) -> Result<Vec<String>, String> {
let _guard = WORKTREE_LOCK.lock().unwrap_or_else(|e| e.into_inner());
let mut seeded = Vec::new();
copy_tree_diff(main_ws, wt_path, main_ws, &mut seeded)?;
// Full mirror deletes: drop wt paths that no longer exist on main.
remove_dest_missing_from_src(main_ws, wt_path, wt_path, &mut seeded)?;
Ok(seeded)
}
fn should_skip_name(name: &str) -> bool {
name == ".git" || name == ".catalyst-code"
}
/// Recursively copy files under `dir` (within `src_root`) into `dest_root` when
/// content differs or the dest file is missing. Skips `.git` and `.catalyst-code`.
fn copy_tree_diff(
src_root: &Path,
dest_root: &Path,
dir: &Path,
out_paths: &mut Vec<String>,
) -> Result<(), String> {
let rd = std::fs::read_dir(dir).map_err(|e| format!("read {}: {e}", dir.display()))?;
for ent in rd.flatten() {
let p = ent.path();
let name = ent.file_name();
let name_s = name.to_string_lossy();
if should_skip_name(&name_s) {
continue;
}
let rel = p
.strip_prefix(src_root)
.map_err(|_| "strip prefix".to_string())?;
let dest = dest_root.join(rel);
let ft = ent.file_type().map_err(|e| e.to_string())?;
if ft.is_dir() {
std::fs::create_dir_all(&dest).map_err(|e| e.to_string())?;
copy_tree_diff(src_root, dest_root, &p, out_paths)?;
} else if ft.is_file() {
if let Some(parent) = dest.parent() {
std::fs::create_dir_all(parent).map_err(|e| e.to_string())?;
}
let src_bytes = std::fs::read(&p).map_err(|e| e.to_string())?;
let same = std::fs::read(&dest)
.ok()
.map(|b| b == src_bytes)
.unwrap_or(false);
if !same {
std::fs::write(&dest, &src_bytes).map_err(|e| e.to_string())?;
out_paths.push(rel.display().to_string());
}
}
}
Ok(())
}
/// Remove paths under `dest_dir` that have no counterpart under `src_root`.
/// Used by seed to full-mirror main→worktree (including deletions).
fn remove_dest_missing_from_src(
src_root: &Path,
dest_root: &Path,
dest_dir: &Path,
out_paths: &mut Vec<String>,
) -> Result<(), String> {
let rd = match std::fs::read_dir(dest_dir) {
Ok(rd) => rd,
Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(()),
Err(e) => return Err(format!("read {}: {e}", dest_dir.display())),
};
for ent in rd.flatten() {
let p = ent.path();
let name = ent.file_name();
let name_s = name.to_string_lossy();
if should_skip_name(&name_s) {
continue;
}
let rel = p
.strip_prefix(dest_root)
.map_err(|_| "strip prefix".to_string())?;
let src = src_root.join(rel);
let ft = ent.file_type().map_err(|e| e.to_string())?;
if ft.is_dir() {
remove_dest_missing_from_src(src_root, dest_root, &p, out_paths)?;
if src.is_dir() {
continue;
}
if std::fs::read_dir(&p)
.map(|mut i| i.next().is_none())
.unwrap_or(false)
{
let _ = std::fs::remove_dir(&p);
out_paths.push(format!("deleted:{}", rel.display()));
} else if !src.exists() {
std::fs::remove_dir_all(&p).map_err(|e| e.to_string())?;
out_paths.push(format!("deleted:{}", rel.display()));
}
} else if ft.is_file() && !src.exists() {
std::fs::remove_file(&p).map_err(|e| e.to_string())?;
out_paths.push(format!("deleted:{}", rel.display()));
}
}
Ok(())
}
/// Apply tracked deletions from `src_repo` (vs HEAD) onto `dest_root`.
/// Safe for promote under concurrency — only paths the worktree deleted.
fn sync_git_deletions(
src_repo: &Path,
dest_root: &Path,
out_paths: &mut Vec<String>,
) -> Result<(), String> {
if !is_git_repo(src_repo) {
return Ok(());
}
let out = match git_out(
src_repo,
&["diff", "--name-only", "--diff-filter=D", "HEAD"],
) {
Ok(s) => s,
Err(_) => return Ok(()),
};
for line in out.lines() {
let rel = line.trim();
if rel.is_empty() {
continue;
}
let dest = dest_root.join(rel);
if !dest.exists() {
continue;
}
if dest.is_dir() {
std::fs::remove_dir_all(&dest).map_err(|e| e.to_string())?;
} else {
std::fs::remove_file(&dest).map_err(|e| e.to_string())?;
}
out_paths.push(format!("deleted:{rel}"));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn is_git_repo_detects_this_workspace() {
let here = PathBuf::from(env!("CARGO_MANIFEST_DIR"));
assert!(is_git_repo(&here));
}
#[test]
fn add_worktree_errors_outside_git() {
let dir = std::env::temp_dir().join("catcode-nongit-wt");
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
let err = add_worktree(&dir, "t1").unwrap_err();
assert!(err.contains("git repository"));
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn is_git_repo_false_in_temp_dir() {
let dir = std::env::temp_dir().join(format!("catcode-wt-nongit-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
assert!(!is_git_repo(&dir), "empty temp dir must not be a git repo");
let _ = std::fs::remove_dir_all(&dir);
}
#[test]
fn promote_dir_copies_changed_file() {
let stamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos();
let main = std::env::temp_dir().join(format!("catcode-promote-main-{stamp}"));
let wt = std::env::temp_dir().join(format!("catcode-promote-wt-{stamp}"));
let _ = std::fs::remove_dir_all(&main);
let _ = std::fs::remove_dir_all(&wt);
std::fs::create_dir_all(&main).unwrap();
std::fs::create_dir_all(&wt).unwrap();
// Write a file only in the worktree.
std::fs::write(wt.join("new.txt"), b"wt content").unwrap();
let promoted = promote_worktree(&main, &wt).unwrap();
assert!(promoted.iter().any(|p| p.contains("new.txt")));
assert_eq!(
std::fs::read_to_string(main.join("new.txt")).unwrap(),
"wt content"
);
let _ = std::fs::remove_dir_all(&main);
let _ = std::fs::remove_dir_all(&wt);
}
#[test]
fn seed_worktree_from_main_copies_uncommitted_files() {
// promote copies wt→main; add_worktree checks out HEAD only — seed must
// bring uncommitted main files into the new worktree for dependent waves.
let stamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos();
let main = std::env::temp_dir().join(format!("catcode-seed-main-{stamp}"));
let _ = std::fs::remove_dir_all(&main);
std::fs::create_dir_all(&main).unwrap();
// Init a real git repo with committed file A.
for (args, msg) in [
(vec!["init"], "init"),
(vec!["config", "user.email", "test@example.com"], "email"),
(vec!["config", "user.name", "test"], "name"),
] {
let st = Command::new("git")
.args(&args)
.current_dir(&main)
.status()
.unwrap_or_else(|e| panic!("{msg}: {e}"));
assert!(st.success(), "git {msg} failed");
}
// Detach from any template/default branch naming quirks.
let _ = Command::new("git")
.args(["checkout", "-b", "main"])
.current_dir(&main)
.status();
std::fs::write(main.join("A.txt"), b"committed").unwrap();
assert!(Command::new("git")
.args(["add", "A.txt"])
.current_dir(&main)
.status()
.unwrap()
.success());
assert!(Command::new("git")
.args(["commit", "-m", "add A"])
.current_dir(&main)
.status()
.unwrap()
.success());
// Dirty uncommitted file B in main (simulates prior-wave promote).
std::fs::write(main.join("B.txt"), b"promoted dirty").unwrap();
let wt = add_worktree(&main, &format!("seed-{stamp}")).unwrap();
// Fresh worktree has A (from HEAD) but not dirty B until seeded.
assert!(wt.join("A.txt").exists());
assert!(
!wt.join("B.txt").exists(),
"add_worktree alone must not see uncommitted B"
);
let seeded = seed_worktree_from_main(&main, &wt).unwrap();
assert!(seeded.iter().any(|p| p.contains("B.txt")), "{seeded:?}");
assert_eq!(
std::fs::read_to_string(wt.join("B.txt")).unwrap(),
"promoted dirty"
);
let _ = remove_worktree(&main, &wt);
let _ = std::fs::remove_dir_all(&main);
}
#[test]
fn seed_worktree_from_main_removes_deleted_files() {
let stamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos();
let main = std::env::temp_dir().join(format!("catcode-seed-del-main-{stamp}"));
let _ = std::fs::remove_dir_all(&main);
std::fs::create_dir_all(&main).unwrap();
for (args, msg) in [
(vec!["init"], "init"),
(vec!["config", "user.email", "test@example.com"], "email"),
(vec!["config", "user.name", "test"], "name"),
] {
let st = Command::new("git")
.args(&args)
.current_dir(&main)
.status()
.unwrap_or_else(|e| panic!("{msg}: {e}"));
assert!(st.success(), "git {msg} failed");
}
let _ = Command::new("git")
.args(["checkout", "-b", "main"])
.current_dir(&main)
.status();
std::fs::write(main.join("A.txt"), b"committed").unwrap();
assert!(Command::new("git")
.args(["add", "A.txt"])
.current_dir(&main)
.status()
.unwrap()
.success());
assert!(Command::new("git")
.args(["commit", "-m", "add A"])
.current_dir(&main)
.status()
.unwrap()
.success());
// Simulate prior-wave deletion on main (still present at HEAD).
std::fs::remove_file(main.join("A.txt")).unwrap();
let wt = add_worktree(&main, &format!("seed-del-{stamp}")).unwrap();
assert!(wt.join("A.txt").exists(), "fresh wt still has HEAD A.txt");
let seeded = seed_worktree_from_main(&main, &wt).unwrap();
assert!(!wt.join("A.txt").exists(), "seed must delete A.txt from wt");
assert!(
seeded.iter().any(|p| p.contains("deleted:A.txt")),
"expected deleted:A.txt in {seeded:?}"
);
let _ = remove_worktree(&main, &wt);
let _ = std::fs::remove_dir_all(&main);
}
#[test]
fn promote_worktree_removes_tracked_deletions() {
let stamp = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_nanos();
let main = std::env::temp_dir().join(format!("catcode-promote-del-main-{stamp}"));
let _ = std::fs::remove_dir_all(&main);
std::fs::create_dir_all(&main).unwrap();
for (args, msg) in [
(vec!["init"], "init"),
(vec!["config", "user.email", "test@example.com"], "email"),
(vec!["config", "user.name", "test"], "name"),
] {
let st = Command::new("git")
.args(&args)
.current_dir(&main)
.status()
.unwrap_or_else(|e| panic!("{msg}: {e}"));
assert!(st.success(), "git {msg} failed");
}
let _ = Command::new("git")
.args(["checkout", "-b", "main"])
.current_dir(&main)
.status();
std::fs::write(main.join("A.txt"), b"committed").unwrap();
assert!(Command::new("git")
.args(["add", "A.txt"])
.current_dir(&main)
.status()
.unwrap()
.success());
assert!(Command::new("git")
.args(["commit", "-m", "add A"])
.current_dir(&main)
.status()
.unwrap()
.success());
let wt = add_worktree(&main, &format!("promote-del-{stamp}")).unwrap();
assert!(wt.join("A.txt").exists());
std::fs::remove_file(wt.join("A.txt")).unwrap();
let promoted = promote_worktree(&main, &wt).unwrap();
assert!(
!main.join("A.txt").exists(),
"promote must delete A.txt on main"
);
assert!(
promoted.iter().any(|p| p.contains("deleted:A.txt")),
"expected deleted:A.txt in {promoted:?}"
);
let _ = remove_worktree(&main, &wt);
let _ = std::fs::remove_dir_all(&main);
}
#[test]
fn remove_worktree_handles_nonexistent_path() {
// Should not panic for a path that doesn't exist.
let dir = std::env::temp_dir().join(format!("catcode-rmwt-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&dir);
std::fs::create_dir_all(&dir).unwrap();
// Drive-by best-effort: even if git fails, the function shouldn't panic.
let result = remove_worktree(&dir, &dir.join("nonexistent"));
// We just assert it doesn't panic; result may be Ok or Err.
let _ = result;
let _ = std::fs::remove_dir_all(&dir);
}
}