1use std::path::{Path, PathBuf};
7use std::process::Stdio;
8
9use crate::proc::Quiet as _;
10use anyhow::{Context as _, Result, bail};
11use tokio::process::Command;
12
13#[derive(Debug)]
15pub struct GitOut {
16 pub code: Option<i32>,
18 pub stdout: String,
20 pub stderr: String,
22}
23
24impl GitOut {
25 pub fn ok(&self) -> bool {
27 self.code == Some(0)
28 }
29}
30
31pub async fn git_raw(cwd: &Path, args: &[&str]) -> Result<GitOut> {
34 let out = Command::new("git")
35 .args(args)
36 .current_dir(cwd)
37 .quiet()
38 .env("GIT_TERMINAL_PROMPT", "0")
41 .env("GIT_EDITOR", "true")
42 .stdin(Stdio::null())
43 .output()
44 .await
45 .with_context(|| format!("spawn git {}", args.join(" ")))?;
46 Ok(GitOut {
47 code: out.status.code(),
48 stdout: String::from_utf8_lossy(&out.stdout).trim_end().to_owned(),
49 stderr: String::from_utf8_lossy(&out.stderr).trim_end().to_owned(),
50 })
51}
52
53pub async fn git(cwd: &Path, args: &[&str]) -> Result<String> {
55 let out = git_raw(cwd, args).await?;
56 if !out.ok() {
57 bail!(
58 "git {} failed in {} (exit {:?}): {}",
59 args.join(" "),
60 cwd.display(),
61 out.code,
62 if out.stderr.is_empty() {
63 out.stdout.as_str()
64 } else {
65 out.stderr.as_str()
66 }
67 );
68 }
69 Ok(out.stdout)
70}
71
72pub async fn toplevel(path: &Path) -> Result<PathBuf> {
74 let out = git(path, &["rev-parse", "--show-toplevel"]).await?;
75 Ok(PathBuf::from(out))
76}
77
78pub async fn rev_parse(repo: &Path, rev: &str) -> Result<String> {
80 git(repo, &["rev-parse", rev]).await
81}
82
83pub async fn current_branch(repo: &Path) -> Result<Option<String>> {
85 let out = git_raw(repo, &["symbolic-ref", "--quiet", "--short", "HEAD"]).await?;
86 Ok(if out.ok() && !out.stdout.is_empty() {
87 Some(out.stdout)
88 } else {
89 None
90 })
91}
92
93pub async fn is_clean(repo: &Path) -> Result<bool> {
95 Ok(git(repo, &["status", "--porcelain"]).await?.is_empty())
96}
97
98pub async fn status_porcelain(repo: &Path) -> Result<String> {
100 git(repo, &["status", "--porcelain"]).await
101}
102
103pub async fn worktree_add_branch(repo: &Path, path: &Path, branch: &str, base: &str) -> Result<()> {
105 if let Some(parent) = path.parent() {
106 tokio::fs::create_dir_all(parent).await.ok();
107 }
108 let path_s = path.to_string_lossy().to_string();
109 git(repo, &["worktree", "add", "-b", branch, &path_s, base])
110 .await
111 .map(|_| ())
112}
113
114pub async fn worktree_add_detached(repo: &Path, path: &Path, rev: &str) -> Result<()> {
116 if let Some(parent) = path.parent() {
117 tokio::fs::create_dir_all(parent).await.ok();
118 }
119 let path_s = path.to_string_lossy().to_string();
120 git(repo, &["worktree", "add", "--detach", &path_s, rev])
121 .await
122 .map(|_| ())
123}
124
125pub async fn reset_detached(worktree: &Path, rev: &str) -> Result<()> {
127 git(worktree, &["checkout", "--detach", rev]).await?;
128 git(worktree, &["reset", "--hard", rev]).await?;
129 git(worktree, &["clean", "-fdx"]).await?;
130 Ok(())
131}
132
133pub async fn worktree_holding(repo: &Path, branch: &str) -> Result<Option<PathBuf>> {
138 let listing = git(repo, &["worktree", "list", "--porcelain"]).await?;
139 Ok(parse_worktree_holder(&listing, branch))
140}
141
142fn parse_worktree_holder(listing: &str, branch: &str) -> Option<PathBuf> {
143 let want = format!("refs/heads/{branch}");
144 let mut path: Option<PathBuf> = None;
145 for line in listing.lines() {
146 if let Some(p) = line.strip_prefix("worktree ") {
147 path = Some(PathBuf::from(p));
148 } else if line.strip_prefix("branch ") == Some(want.as_str()) {
149 return path;
150 }
151 }
152 None
153}
154
155pub async fn worktree_remove(repo: &Path, path: &Path) -> Result<bool> {
158 let path_s = path.to_string_lossy().to_string();
159 let out = git_raw(repo, &["worktree", "remove", "--force", &path_s]).await?;
160 if out.ok() {
161 return Ok(true);
162 }
163 git_raw(repo, &["worktree", "prune"]).await?;
165 Ok(false)
166}
167
168pub async fn worktree_remove_clean(repo: &Path, path: &Path) -> Result<bool> {
172 let path_s = path.to_string_lossy().to_string();
173 Ok(git_raw(repo, &["worktree", "remove", &path_s]).await?.ok())
174}
175
176pub async fn remove_worktree_from_linked(dir: &Path) {
185 let Ok(link) = std::fs::read_to_string(dir.join(".git")) else {
186 return;
187 };
188 let Some(admin) = link.strip_prefix("gitdir:").map(str::trim) else {
189 return;
190 };
191 let admin = Path::new(admin);
194 let Some(common) = admin.parent().and_then(Path::parent) else {
195 return;
196 };
197 let common_s = common.to_string_lossy();
198 let _ = git_raw(dir, &["--git-dir", &common_s, "worktree", "prune"]).await;
199}
200
201pub async fn worktree_prune(repo: &Path) -> Result<()> {
210 git(repo, &["worktree", "prune"]).await.map(|_| ())
211}
212
213pub async fn branch_delete(repo: &Path, branch: &str) -> Result<bool> {
215 Ok(git_raw(repo, &["branch", "-D", branch]).await?.ok())
216}
217
218pub async fn branch_exists(repo: &Path, branch: &str) -> Result<bool> {
220 let refname = format!("refs/heads/{branch}");
221 Ok(
222 git_raw(repo, &["show-ref", "--verify", "--quiet", &refname])
223 .await?
224 .ok(),
225 )
226}
227
228pub async fn diff(worktree: &Path, base: &str, head: &str) -> Result<String> {
230 let range = format!("{base}...{head}");
231 git(
232 worktree,
233 &["diff", "--no-color", "--no-ext-diff", "-M", &range],
234 )
235 .await
236}
237
238pub async fn diff_stat(worktree: &Path, base: &str, head: &str) -> Result<String> {
240 let range = format!("{base}...{head}");
241 git(worktree, &["diff", "--no-color", "--stat", &range]).await
242}
243
244pub async fn changed_files(worktree: &Path, base: &str, head: &str) -> Result<Vec<String>> {
246 let range = format!("{base}...{head}");
247 let out = git(worktree, &["diff", "--name-only", &range]).await?;
248 Ok(out.lines().map(str::to_owned).collect())
249}
250
251pub async fn log_oneline(worktree: &Path, base: &str, head: &str) -> Result<String> {
253 let range = format!("{base}..{head}");
254 git(
255 worktree,
256 &["log", "--reverse", "--format=%s%n%b%n--", &range],
257 )
258 .await
259}
260
261pub async fn commits_ahead(worktree: &Path, base: &str, head: &str) -> Result<usize> {
263 let range = format!("{base}..{head}");
264 let out = git(worktree, &["rev-list", "--count", &range]).await?;
265 Ok(out.trim().parse().unwrap_or(0))
266}
267
268pub async fn commit_all(worktree: &Path, message: &str) -> Result<bool> {
275 if git(worktree, &["status", "--porcelain"]).await?.is_empty() {
276 return Ok(false);
277 }
278 git(worktree, &["add", "-A"]).await?;
279 let out = git_raw(
280 worktree,
281 &[
282 "-c",
283 "user.name=magi candidate",
284 "-c",
285 "user.email=magi@localhost",
286 "commit",
287 "--no-verify",
288 "-m",
289 message,
290 ],
291 )
292 .await?;
293 if !out.ok() {
294 bail!("rescue commit failed: {}", out.stderr);
295 }
296 Ok(true)
297}
298
299#[derive(Debug, Clone, PartialEq, Eq)]
302pub struct Stray {
303 pub path: String,
305 pub manager: String,
307 pub kept_by: String,
310}
311
312#[derive(Debug, Default)]
314pub struct Rescue {
315 pub committed: bool,
317 pub withheld: Vec<Stray>,
319}
320
321fn lock_kind(name: &str) -> Option<(&'static str, &'static str)> {
323 Some(match name {
324 "package-lock.json" | "npm-shrinkwrap.json" => ("node", "npm"),
325 "yarn.lock" => ("node", "yarn"),
326 "pnpm-lock.yaml" => ("node", "pnpm"),
327 "bun.lock" | "bun.lockb" => ("node", "bun"),
328 "poetry.lock" => ("python", "poetry"),
329 "uv.lock" => ("python", "uv"),
330 "Pipfile.lock" => ("python", "pipenv"),
331 "pdm.lock" => ("python", "pdm"),
332 "Cargo.lock" => ("rust", "cargo"),
333 _ => return None,
334 })
335}
336
337fn split_dir(path: &str) -> (&str, &str) {
338 path.rsplit_once('/').unwrap_or(("", path))
339}
340
341pub fn stray_lockfiles(untracked: &[String], tracked: &[String]) -> Vec<Stray> {
349 let mut out = Vec::new();
350 for path in untracked {
351 let (dir, name) = split_dir(path);
352 let Some((eco, manager)) = lock_kind(name) else {
353 continue;
354 };
355 let beside = |other: &String| split_dir(other).0 == dir;
356 let kept_by = if manager == "cargo" {
357 let has_manifest = tracked
358 .iter()
359 .chain(untracked)
360 .any(|p| beside(p) && split_dir(p).1 == "Cargo.toml");
361 if has_manifest {
362 continue;
363 }
364 "no Cargo.toml in the directory".to_owned()
365 } else {
366 let Some(other) = tracked.iter().find(|p| {
367 beside(p)
368 && lock_kind(split_dir(p).1).is_some_and(|(e, m)| e == eco && m != manager)
369 }) else {
370 continue;
371 };
372 other.clone()
373 };
374 out.push(Stray {
375 path: path.clone(),
376 manager: manager.to_owned(),
377 kept_by,
378 });
379 }
380 out
381}
382
383async fn nul_list(worktree: &Path, args: &[&str]) -> Result<Vec<String>> {
384 let out = git(worktree, args).await?;
385 Ok(out
386 .split('\0')
387 .filter(|s| !s.is_empty())
388 .map(str::to_owned)
389 .collect())
390}
391
392pub async fn rescue_commit(worktree: &Path, message: &str) -> Result<Rescue> {
398 if git(worktree, &["status", "--porcelain"]).await?.is_empty() {
399 return Ok(Rescue::default());
400 }
401 let untracked = nul_list(
402 worktree,
403 &["ls-files", "-z", "--others", "--exclude-standard"],
404 )
405 .await?;
406 let tracked = nul_list(worktree, &["ls-files", "-z"]).await?;
407 let withheld = stray_lockfiles(&untracked, &tracked);
408
409 git(worktree, &["add", "-A"]).await?;
410 if !withheld.is_empty() {
411 let mut args = vec!["reset", "-q", "--"];
412 args.extend(withheld.iter().map(|s| s.path.as_str()));
413 git(worktree, &args).await?;
414 }
415 if git_raw(worktree, &["diff", "--cached", "--quiet"])
416 .await?
417 .ok()
418 {
419 return Ok(Rescue {
420 committed: false,
421 withheld,
422 });
423 }
424 let out = git_raw(
425 worktree,
426 &[
427 "-c",
428 "user.name=magi candidate",
429 "-c",
430 "user.email=magi@localhost",
431 "commit",
432 "--no-verify",
433 "-m",
434 message,
435 ],
436 )
437 .await?;
438 if !out.ok() {
439 bail!("rescue commit failed: {}", out.stderr);
440 }
441 Ok(Rescue {
442 committed: true,
443 withheld,
444 })
445}
446
447pub async fn enable_worktree_config(repo: &Path) -> Result<bool> {
452 let out = git_raw(repo, &["config", "--get", "extensions.worktreeConfig"]).await?;
453 if out.ok() && out.stdout.trim() == "true" {
454 return Ok(false);
455 }
456 git(repo, &["config", "extensions.worktreeConfig", "true"]).await?;
457 Ok(true)
458}
459
460pub async fn disable_worktree_config(repo: &Path) -> Result<()> {
462 git_raw(repo, &["config", "--unset", "extensions.worktreeConfig"]).await?;
463 Ok(())
464}
465
466struct WorktreeConfigRef {
469 count: usize,
471 we_enabled: bool,
477}
478
479static WORKTREE_CONFIG: std::sync::LazyLock<
486 std::sync::Mutex<
487 std::collections::HashMap<PathBuf, std::sync::Arc<tokio::sync::Mutex<WorktreeConfigRef>>>,
488 >,
489> = std::sync::LazyLock::new(|| std::sync::Mutex::new(std::collections::HashMap::new()));
490
491fn worktree_config_slot(repo: &Path) -> std::sync::Arc<tokio::sync::Mutex<WorktreeConfigRef>> {
493 let mut map = WORKTREE_CONFIG
494 .lock()
495 .unwrap_or_else(std::sync::PoisonError::into_inner);
496 map.entry(repo.to_path_buf())
497 .or_insert_with(|| {
498 std::sync::Arc::new(tokio::sync::Mutex::new(WorktreeConfigRef {
499 count: 0,
500 we_enabled: false,
501 }))
502 })
503 .clone()
504}
505
506pub async fn acquire_worktree_config(repo: &Path) -> Result<()> {
523 let slot = worktree_config_slot(repo);
524 let mut entry = slot.lock().await;
525 entry.count += 1;
526 if entry.count == 1 {
527 entry.we_enabled = enable_worktree_config(repo).await?;
528 }
529 Ok(())
530}
531
532pub async fn release_worktree_config(repo: &Path) -> Result<()> {
538 let slot = worktree_config_slot(repo);
539 let mut entry = slot.lock().await;
540 entry.count = entry.count.saturating_sub(1);
541 if entry.count == 0 && entry.we_enabled {
542 disable_worktree_config(repo).await?;
543 entry.we_enabled = false;
544 }
545 Ok(())
546}
547
548pub async fn set_worktree_hooks_path(worktree: &Path, hooks_dir: &Path) -> Result<()> {
554 let dir = hooks_dir.to_string_lossy().replace('\\', "/");
555 git(worktree, &["config", "--worktree", "core.hooksPath", &dir])
556 .await
557 .map(|_| ())
558}
559
560pub async fn local_exclude(worktree: &Path, pattern: &str) -> Result<()> {
562 let git_dir = git(worktree, &["rev-parse", "--git-path", "info/exclude"]).await?;
563 let path = worktree.join(git_dir);
564 if let Some(parent) = path.parent() {
565 tokio::fs::create_dir_all(parent).await.ok();
566 }
567 let mut body = tokio::fs::read_to_string(&path).await.unwrap_or_default();
568 if body.lines().any(|l| l.trim() == pattern) {
569 return Ok(());
570 }
571 if !body.is_empty() && !body.ends_with('\n') {
572 body.push('\n');
573 }
574 body.push_str(pattern);
575 body.push('\n');
576 tokio::fs::write(&path, body)
577 .await
578 .with_context(|| format!("write {}", path.display()))?;
579 Ok(())
580}
581
582pub async fn merge_no_ff(repo: &Path, branch: &str, message: &str) -> Result<GitOut> {
588 git_raw(
589 repo,
590 &["merge", "--no-ff", "--no-edit", "-m", message, branch],
591 )
592 .await
593}
594
595pub async fn merge_squash(repo: &Path, branch: &str, message: &str) -> Result<GitOut> {
606 let staged = git_raw(repo, &["merge", "--squash", branch]).await?;
607 if !staged.ok() {
608 return Ok(staged);
609 }
610 git_raw(repo, &["commit", "-m", message]).await
611}
612
613pub async fn merge_ff_only(repo: &Path, branch: &str) -> Result<GitOut> {
623 git_raw(repo, &["merge", "--ff-only", branch]).await
624}
625
626pub async fn push(repo: &Path, remote: &str, branch: &str) -> Result<GitOut> {
628 git_raw(repo, &["push", "-u", remote, branch]).await
629}
630
631pub async fn push_rewritten(repo: &Path, remote: &str, branch: &str) -> Result<GitOut> {
639 git_raw(repo, &["push", "--force-with-lease", remote, branch]).await
640}
641
642pub async fn push_pinned(
651 repo: &Path,
652 remote: &str,
653 branch: &str,
654 expected: &str,
655) -> Result<GitOut> {
656 let lease = format!("--force-with-lease=refs/heads/{branch}:{expected}");
657 let refspec = format!("refs/heads/{branch}:refs/heads/{branch}");
658 git_raw(repo, &["push", &lease, remote, &refspec]).await
659}
660
661pub async fn cherry(repo: &Path, upstream: &str, head: &str) -> Result<(Vec<String>, Vec<String>)> {
664 let out = git(repo, &["cherry", upstream, head]).await?;
665 let (mut unmatched, mut matched) = (Vec::new(), Vec::new());
666 for line in out.lines() {
667 if let Some(sha) = line.strip_prefix("+ ") {
668 unmatched.push(sha.trim().to_owned());
669 } else if let Some(sha) = line.strip_prefix("- ") {
670 matched.push(sha.trim().to_owned());
671 }
672 }
673 Ok((unmatched, matched))
674}
675
676pub async fn rebase_branch_in_temp(
688 repo: &Path,
689 scratch: &Path,
690 branch: &str,
691 onto: &str,
692) -> Result<Option<String>> {
693 worktree_remove(repo, scratch).await.ok();
696 git_raw(
697 repo,
698 &[
699 "worktree",
700 "add",
701 "--force",
702 &scratch.to_string_lossy(),
703 branch,
704 ],
705 )
706 .await?;
707
708 let out = git_raw(scratch, &["rebase", onto]).await?;
709 if out.ok() {
710 worktree_remove(repo, scratch).await.ok();
711 return Ok(None);
712 }
713 git_raw(scratch, &["rebase", "--abort"]).await.ok();
715 let why = if out.stderr.trim().is_empty() {
716 out.stdout.trim().to_owned()
717 } else {
718 out.stderr.trim().to_owned()
719 };
720 worktree_remove(repo, scratch).await.ok();
721 Ok(Some(why))
722}
723
724pub async fn sync_to_head(worktree: &Path) -> Result<()> {
737 git(worktree, &["reset", "--hard", "HEAD"]).await?;
738 git(worktree, &["clean", "-fdx"]).await?;
739 Ok(())
740}
741
742pub async fn fetch(repo: &Path, remote: &str, branch: &str) -> Result<GitOut> {
761 let refspec = format!("+refs/heads/{branch}:refs/remotes/{remote}/{branch}");
762 git_raw(repo, &["fetch", "--quiet", remote, &refspec]).await
763}
764
765pub async fn is_ancestor(repo: &Path, ancestor: &str, of: &str) -> bool {
767 git_raw(repo, &["merge-base", "--is-ancestor", ancestor, of])
768 .await
769 .is_ok_and(|o| o.ok())
770}
771
772pub async fn tree_of(repo: &Path, rev: &str) -> Result<String> {
774 git(repo, &["rev-parse", &format!("{rev}^{{tree}}")]).await
775}
776
777pub async fn rev_exists(repo: &Path, rev: &str) -> bool {
779 git_raw(repo, &["rev-parse", "--verify", "--quiet", rev])
780 .await
781 .is_ok_and(|o| o.ok())
782}
783
784#[cfg(test)]
785mod tests {
786 use super::*;
787
788 #[test]
789 fn worktree_holder_is_read_from_the_porcelain_listing() {
790 let listing = "worktree /repo\nHEAD aaa\nbranch refs/heads/main\n\n\
791 worktree /wt/cand-A\nHEAD bbb\nbranch refs/heads/magi/f82f/A\n\n\
792 worktree /wt/detached\nHEAD ccc\ndetached\n";
793 assert_eq!(
794 parse_worktree_holder(listing, "magi/f82f/A"),
795 Some(PathBuf::from("/wt/cand-A"))
796 );
797 assert_eq!(parse_worktree_holder(listing, "magi/f82f"), None);
798 assert_eq!(parse_worktree_holder(listing, "other"), None);
799 }
800
801 async fn scratch() -> (tempfile::TempDir, PathBuf) {
802 let dir = tempfile::tempdir().unwrap();
803 let repo = dir.path().join("repo");
804 tokio::fs::create_dir_all(&repo).await.unwrap();
805 git(&repo, &["init", "-b", "main"]).await.unwrap();
806 git(&repo, &["config", "user.name", "test"]).await.unwrap();
807 git(&repo, &["config", "user.email", "test@example.com"])
808 .await
809 .unwrap();
810 tokio::fs::write(repo.join("a.txt"), "one\n").await.unwrap();
811 git(&repo, &["add", "-A"]).await.unwrap();
812 git(&repo, &["commit", "-m", "init"]).await.unwrap();
813 (dir, repo)
814 }
815
816 #[tokio::test]
817 async fn a_branch_rebases_onto_a_moved_base_and_says_when_it_cannot() {
818 let (_g, repo) = scratch().await;
819
820 git(&repo, &["checkout", "-b", "side"]).await.unwrap();
822 tokio::fs::write(repo.join("b.txt"), "side\n")
823 .await
824 .unwrap();
825 git(&repo, &["add", "-A"]).await.unwrap();
826 git(&repo, &["commit", "-m", "side work"]).await.unwrap();
827
828 git(&repo, &["checkout", "main"]).await.unwrap();
831 tokio::fs::write(repo.join("c.txt"), "main\n")
832 .await
833 .unwrap();
834 git(&repo, &["add", "-A"]).await.unwrap();
835 git(&repo, &["commit", "-m", "main moved"]).await.unwrap();
836
837 let scratch_tree = repo.parent().unwrap().join("rebase-scratch");
838 let clean = rebase_branch_in_temp(&repo, &scratch_tree, "side", "main")
839 .await
840 .unwrap();
841 assert!(clean.is_none(), "a disjoint change rebases: {clean:?}");
842 assert_eq!(
843 commits_ahead(&repo, "main", "side").await.unwrap(),
844 1,
845 "one commit, replayed onto the new base"
846 );
847 assert!(
848 !scratch_tree.exists(),
849 "the throwaway worktree is not left behind"
850 );
851
852 git(&repo, &["checkout", "-b", "clash"]).await.unwrap();
854 tokio::fs::write(repo.join("a.txt"), "clash\n")
855 .await
856 .unwrap();
857 git(&repo, &["add", "-A"]).await.unwrap();
858 git(&repo, &["commit", "-m", "clash"]).await.unwrap();
859 git(&repo, &["checkout", "main"]).await.unwrap();
860 tokio::fs::write(repo.join("a.txt"), "main edit\n")
861 .await
862 .unwrap();
863 git(&repo, &["add", "-A"]).await.unwrap();
864 git(&repo, &["commit", "-m", "main edit"]).await.unwrap();
865
866 let before = rev_parse(&repo, "clash").await.unwrap();
867 let why = rebase_branch_in_temp(&repo, &scratch_tree, "clash", "main")
868 .await
869 .unwrap()
870 .expect("a same-line clash cannot be rebased silently");
871 assert!(
872 why.to_lowercase().contains("conflict"),
873 "the reason is what git said, which is what a person needs: {why}"
874 );
875 assert_eq!(
876 rev_parse(&repo, "clash").await.unwrap(),
877 before,
878 "a failed rebase leaves the branch exactly where it was"
879 );
880 assert!(!scratch_tree.exists(), "and cleans up after itself");
881 }
882
883 #[tokio::test]
884 async fn merge_squash_folds_the_branch_into_one_commit_under_the_given_message() {
885 let (_g, repo) = scratch().await;
886 git(&repo, &["checkout", "-b", "side"]).await.unwrap();
887 for name in ["b.txt", "c.txt"] {
888 tokio::fs::write(repo.join(name), "side\n").await.unwrap();
889 git(&repo, &["add", "-A"]).await.unwrap();
890 git(
891 &repo,
892 &["commit", "-m", "magi: candidate A (uncommitted work)"],
893 )
894 .await
895 .unwrap();
896 }
897 git(&repo, &["checkout", "main"]).await.unwrap();
898 let before = rev_parse(&repo, "main").await.unwrap();
899
900 let out = merge_squash(&repo, "side", "an explicit subject")
901 .await
902 .unwrap();
903 assert!(out.ok(), "{}", out.stderr);
904 assert_eq!(
905 commits_ahead(&repo, &before, "main").await.unwrap(),
906 1,
907 "squash adds exactly one commit onto the tip, not one per candidate commit"
908 );
909 let subject = git(&repo, &["log", "-1", "--format=%s"]).await.unwrap();
910 assert_eq!(
911 subject, "an explicit subject",
912 "the candidate's own placeholder subject must not survive: {subject}"
913 );
914 }
915
916 #[tokio::test]
917 async fn merge_ff_only_fast_forwards_a_branch_already_rebased_onto_the_tip() {
918 let (_g, repo) = scratch().await;
919 git(&repo, &["checkout", "-b", "side"]).await.unwrap();
920 tokio::fs::write(repo.join("b.txt"), "side\n")
921 .await
922 .unwrap();
923 git(&repo, &["add", "-A"]).await.unwrap();
924 git(&repo, &["commit", "-m", "side work"]).await.unwrap();
925 git(&repo, &["checkout", "main"]).await.unwrap();
926
927 let before = rev_parse(&repo, "side").await.unwrap();
928 let out = merge_ff_only(&repo, "side").await.unwrap();
929 assert!(out.ok(), "{}", out.stderr);
930 assert_eq!(
931 rev_parse(&repo, "main").await.unwrap(),
932 before,
933 "a fast-forward moves the base tip to the branch, no merge commit"
934 );
935 }
936
937 #[tokio::test]
938 async fn merge_ff_only_refuses_to_write_a_merge_commit() {
939 let (_g, repo) = scratch().await;
940 git(&repo, &["checkout", "-b", "side"]).await.unwrap();
941 tokio::fs::write(repo.join("b.txt"), "side\n")
942 .await
943 .unwrap();
944 git(&repo, &["add", "-A"]).await.unwrap();
945 git(&repo, &["commit", "-m", "side work"]).await.unwrap();
946
947 git(&repo, &["checkout", "main"]).await.unwrap();
949 tokio::fs::write(repo.join("c.txt"), "main\n")
950 .await
951 .unwrap();
952 git(&repo, &["add", "-A"]).await.unwrap();
953 git(&repo, &["commit", "-m", "main moved"]).await.unwrap();
954
955 let before = rev_parse(&repo, "main").await.unwrap();
956 let out = merge_ff_only(&repo, "side").await.unwrap();
957 assert!(!out.ok(), "a divergent branch cannot fast-forward");
958 assert_eq!(
959 rev_parse(&repo, "main").await.unwrap(),
960 before,
961 "a refused fast-forward must not touch main"
962 );
963 }
964
965 #[tokio::test]
966 async fn a_sibling_worktree_stays_stale_after_a_rebase_until_synced() {
967 let (guard, repo) = scratch().await;
968
969 git(&repo, &["branch", "side"]).await.unwrap();
973 let side_wt = guard.path().join("side-wt");
974 git(
975 &repo,
976 &["worktree", "add", &side_wt.to_string_lossy(), "side"],
977 )
978 .await
979 .unwrap();
980 tokio::fs::write(side_wt.join("b.txt"), "candidate\n")
981 .await
982 .unwrap();
983 git(&side_wt, &["add", "-A"]).await.unwrap();
984 git(&side_wt, &["commit", "-m", "side work"]).await.unwrap();
985
986 git(&repo, &["checkout", "main"]).await.unwrap();
988 tokio::fs::write(repo.join("c.txt"), "main\n")
989 .await
990 .unwrap();
991 git(&repo, &["add", "-A"]).await.unwrap();
992 git(&repo, &["commit", "-m", "main moved"]).await.unwrap();
993
994 let scratch_tree = guard.path().join("rebase-scratch");
996 let clean = rebase_branch_in_temp(&repo, &scratch_tree, "side", "main")
997 .await
998 .unwrap();
999 assert!(clean.is_none());
1000
1001 assert_eq!(
1005 rev_parse(&side_wt, "HEAD").await.unwrap(),
1006 rev_parse(&repo, "side").await.unwrap(),
1007 "HEAD follows the moved ref"
1008 );
1009 assert!(
1010 !side_wt.join("c.txt").exists(),
1011 "stale until synced: main's new file has not reached this worktree's disk"
1012 );
1013
1014 sync_to_head(&side_wt).await.unwrap();
1015 assert!(side_wt.join("c.txt").is_file(), "synced now");
1016 assert!(
1017 side_wt.join("b.txt").is_file(),
1018 "the worktree's own committed work survives the sync"
1019 );
1020 assert!(is_clean(&side_wt).await.unwrap());
1021 }
1022
1023 #[tokio::test]
1024 async fn clean_repo_reports_clean_then_dirty() {
1025 let (_g, repo) = scratch().await;
1026 assert!(is_clean(&repo).await.unwrap());
1027 tokio::fs::write(repo.join("a.txt"), "two\n").await.unwrap();
1028 assert!(!is_clean(&repo).await.unwrap());
1029 }
1030
1031 async fn track(repo: &Path, name: &str, body: &str) {
1032 let p = repo.join(name);
1033 if let Some(d) = p.parent() {
1034 tokio::fs::create_dir_all(d).await.unwrap();
1035 }
1036 tokio::fs::write(&p, body).await.unwrap();
1037 git(repo, &["add", name]).await.unwrap();
1038 git(
1039 repo,
1040 &[
1041 "-c",
1042 "user.name=t",
1043 "-c",
1044 "user.email=t@localhost",
1045 "commit",
1046 "-q",
1047 "-m",
1048 "seed",
1049 ],
1050 )
1051 .await
1052 .unwrap();
1053 }
1054
1055 #[tokio::test]
1056 async fn rescue_withholds_a_foreign_lockfile() {
1057 let (_g, repo) = scratch().await;
1058 track(&repo, "web/bun.lock", "a\n").await;
1059 tokio::fs::write(repo.join("web/pnpm-lock.yaml"), "x\n")
1060 .await
1061 .unwrap();
1062 tokio::fs::write(repo.join("web/app.ts"), "real\n")
1063 .await
1064 .unwrap();
1065
1066 let r = rescue_commit(&repo, "rescue").await.unwrap();
1067 assert!(r.committed);
1068 assert_eq!(
1069 r.withheld,
1070 [Stray {
1071 path: "web/pnpm-lock.yaml".to_owned(),
1072 manager: "pnpm".to_owned(),
1073 kept_by: "web/bun.lock".to_owned(),
1074 }]
1075 );
1076 let files = git(&repo, &["show", "--name-only", "--format=", "HEAD"])
1077 .await
1078 .unwrap();
1079 assert!(files.contains("web/app.ts"), "{files}");
1080 assert!(!files.contains("pnpm-lock"), "{files}");
1081 assert!(repo.join("web/pnpm-lock.yaml").is_file(), "not deleted");
1082 }
1083
1084 #[tokio::test]
1085 async fn rescue_with_only_a_stray_commits_nothing() {
1086 let (_g, repo) = scratch().await;
1087 track(&repo, "bun.lock", "a\n").await;
1088 tokio::fs::write(repo.join("yarn.lock"), "x\n")
1089 .await
1090 .unwrap();
1091 let r = rescue_commit(&repo, "rescue").await.unwrap();
1092 assert!(!r.committed);
1093 assert_eq!(r.withheld.len(), 1);
1094 }
1095
1096 #[tokio::test]
1097 async fn rescue_keeps_a_same_manager_lockfile_update() {
1098 let (_g, repo) = scratch().await;
1099 track(&repo, "bun.lock", "a\n").await;
1100 tokio::fs::write(repo.join("bun.lock"), "b\n")
1101 .await
1102 .unwrap();
1103 let r = rescue_commit(&repo, "rescue").await.unwrap();
1104 assert!(r.committed);
1105 assert!(r.withheld.is_empty());
1106 let files = git(&repo, &["show", "--name-only", "--format=", "HEAD"])
1107 .await
1108 .unwrap();
1109 assert_eq!(files, "bun.lock");
1110 }
1111
1112 #[tokio::test]
1113 async fn rescue_keeps_the_first_lockfile_in_a_bare_directory() {
1114 let (_g, repo) = scratch().await;
1115 track(&repo, "other/bun.lock", "a\n").await;
1116 tokio::fs::create_dir_all(repo.join("web")).await.unwrap();
1117 tokio::fs::write(repo.join("web/package-lock.json"), "{}\n")
1118 .await
1119 .unwrap();
1120 let r = rescue_commit(&repo, "rescue").await.unwrap();
1121 assert!(r.committed);
1122 assert!(r.withheld.is_empty());
1123 }
1124
1125 #[test]
1126 fn a_cargo_lock_is_foreign_only_without_a_cargo_toml() {
1127 let s = |v: &[&str]| v.iter().map(|x| (*x).to_owned()).collect::<Vec<_>>();
1128 assert_eq!(stray_lockfiles(&s(&["a/Cargo.lock"]), &s(&[])).len(), 1);
1129 assert!(stray_lockfiles(&s(&["a/Cargo.lock"]), &s(&["a/Cargo.toml"])).is_empty());
1130 assert!(stray_lockfiles(&s(&["a/Cargo.lock", "a/Cargo.toml"]), &s(&[])).is_empty());
1131 assert_eq!(
1133 stray_lockfiles(&s(&["a/Cargo.lock"]), &s(&["Cargo.toml"])).len(),
1134 1
1135 );
1136 }
1137
1138 #[tokio::test]
1139 async fn worktree_lifecycle_and_diff() {
1140 let (guard, repo) = scratch().await;
1141 let base = rev_parse(&repo, "HEAD").await.unwrap();
1142 let wt = guard.path().join("wt-a");
1143 worktree_add_branch(&repo, &wt, "magi/test/a", &base)
1144 .await
1145 .unwrap();
1146 tokio::fs::write(wt.join("b.txt"), "candidate\n")
1147 .await
1148 .unwrap();
1149
1150 assert!(commit_all(&wt, "candidate work").await.unwrap());
1151 assert!(!commit_all(&wt, "nothing left").await.unwrap());
1152
1153 assert_eq!(commits_ahead(&wt, &base, "HEAD").await.unwrap(), 1);
1154 let patch = diff(&wt, &base, "HEAD").await.unwrap();
1155 assert!(patch.contains("b.txt"), "patch was: {patch}");
1156 assert_eq!(
1157 changed_files(&wt, &base, "HEAD").await.unwrap(),
1158 ["b.txt".to_owned()]
1159 );
1160
1161 let author = git(&wt, &["log", "-1", "--format=%an <%ae>"])
1163 .await
1164 .unwrap();
1165 assert_eq!(author, "magi candidate <magi@localhost>");
1166
1167 assert!(worktree_remove(&repo, &wt).await.unwrap());
1168 assert!(branch_exists(&repo, "magi/test/a").await.unwrap());
1169 assert!(branch_delete(&repo, "magi/test/a").await.unwrap());
1170 assert!(!branch_exists(&repo, "magi/test/a").await.unwrap());
1171 }
1172
1173 #[tokio::test]
1174 async fn worktree_scoped_hooks_path_does_not_leak_to_primary() {
1175 let (guard, repo) = scratch().await;
1176 let base = rev_parse(&repo, "HEAD").await.unwrap();
1177 let wt = guard.path().join("wt-h");
1178 worktree_add_branch(&repo, &wt, "magi/test/h", &base)
1179 .await
1180 .unwrap();
1181 let hooks = guard.path().join("hooks");
1182 tokio::fs::create_dir_all(&hooks).await.unwrap();
1183
1184 assert!(enable_worktree_config(&repo).await.unwrap());
1185 set_worktree_hooks_path(&wt, &hooks).await.unwrap();
1186
1187 let in_wt = git(&wt, &["config", "--get", "core.hooksPath"])
1188 .await
1189 .unwrap();
1190 assert!(!in_wt.is_empty());
1191 let in_primary = git_raw(&repo, &["config", "--get", "core.hooksPath"])
1192 .await
1193 .unwrap();
1194 assert!(
1195 !in_primary.ok(),
1196 "primary worktree must keep its own hooks: {in_primary:?}"
1197 );
1198
1199 disable_worktree_config(&repo).await.unwrap();
1200 }
1201
1202 #[tokio::test]
1203 async fn worktree_config_stays_on_while_a_sibling_run_still_holds_it() {
1204 let (_g, repo) = scratch().await;
1205
1206 acquire_worktree_config(&repo).await.unwrap();
1209 acquire_worktree_config(&repo).await.unwrap();
1210
1211 let on = git(&repo, &["config", "--get", "extensions.worktreeConfig"])
1212 .await
1213 .unwrap();
1214 assert_eq!(on, "true");
1215
1216 release_worktree_config(&repo).await.unwrap();
1220 let still_on = git(&repo, &["config", "--get", "extensions.worktreeConfig"])
1221 .await
1222 .unwrap();
1223 assert_eq!(
1224 still_on, "true",
1225 "a sibling run's release must not disable the setting for the one still working"
1226 );
1227
1228 release_worktree_config(&repo).await.unwrap();
1230 let after = git_raw(&repo, &["config", "--get", "extensions.worktreeConfig"])
1231 .await
1232 .unwrap();
1233 assert!(
1234 !after.ok(),
1235 "the last release must turn the setting back off: {after:?}"
1236 );
1237 }
1238
1239 #[tokio::test]
1240 async fn worktree_config_already_on_before_magi_touched_it_is_left_alone() {
1241 let (_g, repo) = scratch().await;
1242 git(&repo, &["config", "extensions.worktreeConfig", "true"])
1243 .await
1244 .unwrap();
1245
1246 acquire_worktree_config(&repo).await.unwrap();
1251 release_worktree_config(&repo).await.unwrap();
1252
1253 let still_on = git(&repo, &["config", "--get", "extensions.worktreeConfig"])
1254 .await
1255 .unwrap();
1256 assert_eq!(still_on, "true");
1257 }
1258
1259 #[tokio::test]
1260 async fn local_exclude_is_idempotent() {
1261 let (_g, repo) = scratch().await;
1262 local_exclude(&repo, "/.magi/").await.unwrap();
1263 local_exclude(&repo, "/.magi/").await.unwrap();
1264 let path = repo.join(".git/info/exclude");
1265 let body = tokio::fs::read_to_string(&path).await.unwrap();
1266 assert_eq!(body.matches("/.magi/").count(), 1);
1267 }
1268}