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 fetch_origin(cwd: &Path, timeout: std::time::Duration) -> Result<()> {
65 let url = git(cwd, &["remote", "get-url", "origin"]).await?;
66 let fut = Command::new("git")
67 .args([
68 "fetch",
69 "--quiet",
70 "--no-tags",
71 "--no-recurse-submodules",
72 "--no-write-fetch-head",
73 "--",
74 url.as_str(),
75 "+refs/heads/*:refs/remotes/origin/*",
76 ])
77 .current_dir(cwd)
78 .quiet()
79 .env("GIT_TERMINAL_PROMPT", "0")
80 .stdin(Stdio::null())
81 .kill_on_drop(true)
82 .output();
83 let out = tokio::time::timeout(timeout, fut)
84 .await
85 .map_err(|_| anyhow::anyhow!("git fetch timed out after {}s", timeout.as_secs()))?
86 .context("spawn git fetch")?;
87 if !out.status.success() {
88 bail!(
89 "git fetch failed (exit {:?}): {}",
90 out.status.code(),
91 String::from_utf8_lossy(&out.stderr).trim_end()
92 );
93 }
94 Ok(())
95}
96
97pub async fn git(cwd: &Path, args: &[&str]) -> Result<String> {
99 let out = git_raw(cwd, args).await?;
100 if !out.ok() {
101 bail!(
102 "git {} failed in {} (exit {:?}): {}",
103 args.join(" "),
104 cwd.display(),
105 out.code,
106 if out.stderr.is_empty() {
107 out.stdout.as_str()
108 } else {
109 out.stderr.as_str()
110 }
111 );
112 }
113 Ok(out.stdout)
114}
115
116pub async fn toplevel(path: &Path) -> Result<PathBuf> {
118 let out = git(path, &["rev-parse", "--show-toplevel"]).await?;
119 Ok(PathBuf::from(out))
120}
121
122pub async fn rev_parse(repo: &Path, rev: &str) -> Result<String> {
124 git(repo, &["rev-parse", rev]).await
125}
126
127pub async fn current_branch(repo: &Path) -> Result<Option<String>> {
129 let out = git_raw(repo, &["symbolic-ref", "--quiet", "--short", "HEAD"]).await?;
130 Ok(if out.ok() && !out.stdout.is_empty() {
131 Some(out.stdout)
132 } else {
133 None
134 })
135}
136
137pub async fn is_clean(repo: &Path) -> Result<bool> {
139 Ok(git(repo, &["status", "--porcelain"]).await?.is_empty())
140}
141
142pub async fn status_porcelain(repo: &Path) -> Result<String> {
144 git(repo, &["status", "--porcelain"]).await
145}
146
147pub async fn worktree_add_branch(repo: &Path, path: &Path, branch: &str, base: &str) -> Result<()> {
149 if let Some(parent) = path.parent() {
150 tokio::fs::create_dir_all(parent).await.ok();
151 }
152 let path_s = path.to_string_lossy().to_string();
153 git(repo, &["worktree", "add", "-b", branch, &path_s, base])
154 .await
155 .map(|_| ())
156}
157
158pub async fn worktree_add_detached(repo: &Path, path: &Path, rev: &str) -> Result<()> {
160 if let Some(parent) = path.parent() {
161 tokio::fs::create_dir_all(parent).await.ok();
162 }
163 let path_s = path.to_string_lossy().to_string();
164 git(repo, &["worktree", "add", "--detach", &path_s, rev])
165 .await
166 .map(|_| ())
167}
168
169pub async fn reset_detached(worktree: &Path, rev: &str) -> Result<()> {
171 git(worktree, &["checkout", "--detach", rev]).await?;
172 git(worktree, &["reset", "--hard", rev]).await?;
173 git(worktree, &["clean", "-fdx"]).await?;
174 Ok(())
175}
176
177pub async fn worktree_holding(repo: &Path, branch: &str) -> Result<Option<PathBuf>> {
182 let listing = git(repo, &["worktree", "list", "--porcelain"]).await?;
183 Ok(parse_worktree_holder(&listing, branch))
184}
185
186fn parse_worktree_holder(listing: &str, branch: &str) -> Option<PathBuf> {
187 let want = format!("refs/heads/{branch}");
188 let mut path: Option<PathBuf> = None;
189 for line in listing.lines() {
190 if let Some(p) = line.strip_prefix("worktree ") {
191 path = Some(PathBuf::from(p));
192 } else if line.strip_prefix("branch ") == Some(want.as_str()) {
193 return path;
194 }
195 }
196 None
197}
198
199pub async fn worktree_remove(repo: &Path, path: &Path) -> Result<bool> {
202 let path_s = path.to_string_lossy().to_string();
203 let out = git_raw(repo, &["worktree", "remove", "--force", &path_s]).await?;
204 if out.ok() {
205 return Ok(true);
206 }
207 git_raw(repo, &["worktree", "prune"]).await?;
209 Ok(false)
210}
211
212pub async fn worktree_remove_clean(repo: &Path, path: &Path) -> Result<bool> {
216 let path_s = path.to_string_lossy().to_string();
217 Ok(git_raw(repo, &["worktree", "remove", &path_s]).await?.ok())
218}
219
220pub async fn remove_worktree_from_linked(dir: &Path) {
229 let Ok(link) = std::fs::read_to_string(dir.join(".git")) else {
230 return;
231 };
232 let Some(admin) = link.strip_prefix("gitdir:").map(str::trim) else {
233 return;
234 };
235 let admin = Path::new(admin);
238 let Some(common) = admin.parent().and_then(Path::parent) else {
239 return;
240 };
241 let common_s = common.to_string_lossy();
242 let _ = git_raw(dir, &["--git-dir", &common_s, "worktree", "prune"]).await;
243}
244
245pub async fn worktree_prune(repo: &Path) -> Result<()> {
254 git(repo, &["worktree", "prune"]).await.map(|_| ())
255}
256
257pub async fn branch_delete(repo: &Path, branch: &str) -> Result<bool> {
259 Ok(git_raw(repo, &["branch", "-D", branch]).await?.ok())
260}
261
262pub async fn branch_exists(repo: &Path, branch: &str) -> Result<bool> {
264 let refname = format!("refs/heads/{branch}");
265 Ok(
266 git_raw(repo, &["show-ref", "--verify", "--quiet", &refname])
267 .await?
268 .ok(),
269 )
270}
271
272pub async fn remote_has_branch(repo: &Path, remote: &str, branch: &str) -> Result<bool> {
275 let refname = format!("refs/heads/{branch}");
276 let out = git_raw(repo, &["ls-remote", "--exit-code", remote, &refname]).await?;
277 match out.code {
278 Some(0) => Ok(true),
279 Some(2) => Ok(false),
280 code => bail!(
281 "git ls-remote {remote} failed (exit {code:?}): {}",
282 out.stderr
283 ),
284 }
285}
286
287pub async fn diff(worktree: &Path, base: &str, head: &str) -> Result<String> {
289 let range = format!("{base}...{head}");
290 git(
291 worktree,
292 &["diff", "--no-color", "--no-ext-diff", "-M", &range],
293 )
294 .await
295}
296
297pub async fn diff_stat(worktree: &Path, base: &str, head: &str) -> Result<String> {
299 let range = format!("{base}...{head}");
300 git(worktree, &["diff", "--no-color", "--stat", &range]).await
301}
302
303pub async fn changed_files(worktree: &Path, base: &str, head: &str) -> Result<Vec<String>> {
305 let range = format!("{base}...{head}");
306 let out = git(worktree, &["diff", "--name-only", &range]).await?;
307 Ok(out.lines().map(str::to_owned).collect())
308}
309
310pub async fn commit_log(worktree: &Path, base: &str, head: &str) -> Result<Vec<(String, String)>> {
314 let range = format!("{base}..{head}");
315 let out = git(
316 worktree,
317 &[
318 "log",
319 "--reverse",
320 "--no-color",
321 "--format=%s%x1f%b%x00",
322 &range,
323 ],
324 )
325 .await?;
326 Ok(out
327 .split('\0')
328 .filter_map(|rec| {
329 let rec = rec.trim_start_matches('\n');
330 if rec.trim().is_empty() {
331 return None;
332 }
333 let (subject, body) = rec.split_once('\x1f').unwrap_or((rec, ""));
334 Some((subject.trim().to_owned(), body.trim().to_owned()))
335 })
336 .collect())
337}
338
339pub async fn log_oneline(worktree: &Path, base: &str, head: &str) -> Result<String> {
341 let range = format!("{base}..{head}");
342 git(
343 worktree,
344 &["log", "--reverse", "--format=%s%n%b%n--", &range],
345 )
346 .await
347}
348
349pub async fn subjects(worktree: &Path, base: &str, head: &str) -> Result<Vec<String>> {
352 let range = format!("{base}..{head}");
353 let out = git(worktree, &["log", "--reverse", "--format=%s%x00", &range]).await?;
354 let mut parts: Vec<String> = out.split('\0').map(|s| s.trim().to_owned()).collect();
355 parts.pop();
357 Ok(parts)
358}
359
360pub async fn commits_ahead(worktree: &Path, base: &str, head: &str) -> Result<usize> {
362 let range = format!("{base}..{head}");
363 let out = git(worktree, &["rev-list", "--count", &range]).await?;
364 Ok(out.trim().parse().unwrap_or(0))
365}
366
367pub async fn commit_all(worktree: &Path, message: &str) -> Result<bool> {
374 if git(worktree, &["status", "--porcelain"]).await?.is_empty() {
375 return Ok(false);
376 }
377 git(worktree, &["add", "-A"]).await?;
378 let out = git_raw(
379 worktree,
380 &[
381 "-c",
382 "user.name=magi candidate",
383 "-c",
384 "user.email=magi@localhost",
385 "commit",
386 "--no-verify",
387 "-m",
388 message,
389 ],
390 )
391 .await?;
392 if !out.ok() {
393 bail!("rescue commit failed: {}", out.stderr);
394 }
395 Ok(true)
396}
397
398#[derive(Debug, Clone, PartialEq, Eq)]
401pub struct Stray {
402 pub path: String,
404 pub manager: String,
406 pub kept_by: String,
409}
410
411#[derive(Debug, Default)]
413pub struct Rescue {
414 pub committed: bool,
416 pub withheld: Vec<Stray>,
418}
419
420fn lock_kind(name: &str) -> Option<(&'static str, &'static str)> {
422 Some(match name {
423 "package-lock.json" | "npm-shrinkwrap.json" => ("node", "npm"),
424 "yarn.lock" => ("node", "yarn"),
425 "pnpm-lock.yaml" => ("node", "pnpm"),
426 "bun.lock" | "bun.lockb" => ("node", "bun"),
427 "poetry.lock" => ("python", "poetry"),
428 "uv.lock" => ("python", "uv"),
429 "Pipfile.lock" => ("python", "pipenv"),
430 "pdm.lock" => ("python", "pdm"),
431 "Cargo.lock" => ("rust", "cargo"),
432 _ => return None,
433 })
434}
435
436fn split_dir(path: &str) -> (&str, &str) {
437 path.rsplit_once('/').unwrap_or(("", path))
438}
439
440pub fn stray_lockfiles(untracked: &[String], tracked: &[String]) -> Vec<Stray> {
448 let mut out = Vec::new();
449 for path in untracked {
450 let (dir, name) = split_dir(path);
451 let Some((eco, manager)) = lock_kind(name) else {
452 continue;
453 };
454 let beside = |other: &String| split_dir(other).0 == dir;
455 let kept_by = if manager == "cargo" {
456 let has_manifest = tracked
457 .iter()
458 .chain(untracked)
459 .any(|p| beside(p) && split_dir(p).1 == "Cargo.toml");
460 if has_manifest {
461 continue;
462 }
463 "no Cargo.toml in the directory".to_owned()
464 } else {
465 let Some(other) = tracked.iter().find(|p| {
466 beside(p)
467 && lock_kind(split_dir(p).1).is_some_and(|(e, m)| e == eco && m != manager)
468 }) else {
469 continue;
470 };
471 other.clone()
472 };
473 out.push(Stray {
474 path: path.clone(),
475 manager: manager.to_owned(),
476 kept_by,
477 });
478 }
479 out
480}
481
482async fn nul_list(worktree: &Path, args: &[&str]) -> Result<Vec<String>> {
483 let out = git(worktree, args).await?;
484 Ok(out
485 .split('\0')
486 .filter(|s| !s.is_empty())
487 .map(str::to_owned)
488 .collect())
489}
490
491pub async fn rescue_commit(worktree: &Path, message: &str) -> Result<Rescue> {
497 if git(worktree, &["status", "--porcelain"]).await?.is_empty() {
498 return Ok(Rescue::default());
499 }
500 let untracked = nul_list(
501 worktree,
502 &["ls-files", "-z", "--others", "--exclude-standard"],
503 )
504 .await?;
505 let tracked = nul_list(worktree, &["ls-files", "-z"]).await?;
506 let withheld = stray_lockfiles(&untracked, &tracked);
507
508 git(worktree, &["add", "-A"]).await?;
509 if !withheld.is_empty() {
510 let mut args = vec!["reset", "-q", "--"];
511 args.extend(withheld.iter().map(|s| s.path.as_str()));
512 git(worktree, &args).await?;
513 }
514 if git_raw(worktree, &["diff", "--cached", "--quiet"])
515 .await?
516 .ok()
517 {
518 return Ok(Rescue {
519 committed: false,
520 withheld,
521 });
522 }
523 let out = git_raw(
524 worktree,
525 &[
526 "-c",
527 "user.name=magi candidate",
528 "-c",
529 "user.email=magi@localhost",
530 "commit",
531 "--no-verify",
532 "-m",
533 message,
534 ],
535 )
536 .await?;
537 if !out.ok() {
538 bail!("rescue commit failed: {}", out.stderr);
539 }
540 Ok(Rescue {
541 committed: true,
542 withheld,
543 })
544}
545
546pub async fn enable_worktree_config(repo: &Path) -> Result<bool> {
551 let out = git_raw(repo, &["config", "--get", "extensions.worktreeConfig"]).await?;
552 if out.ok() && out.stdout.trim() == "true" {
553 return Ok(false);
554 }
555 git(repo, &["config", "extensions.worktreeConfig", "true"]).await?;
556 Ok(true)
557}
558
559pub async fn disable_worktree_config(repo: &Path) -> Result<()> {
561 git_raw(repo, &["config", "--unset", "extensions.worktreeConfig"]).await?;
562 Ok(())
563}
564
565struct WorktreeConfigRef {
568 count: usize,
570 we_enabled: bool,
576}
577
578static WORKTREE_CONFIG: std::sync::LazyLock<
585 std::sync::Mutex<
586 std::collections::HashMap<PathBuf, std::sync::Arc<tokio::sync::Mutex<WorktreeConfigRef>>>,
587 >,
588> = std::sync::LazyLock::new(|| std::sync::Mutex::new(std::collections::HashMap::new()));
589
590fn worktree_config_slot(repo: &Path) -> std::sync::Arc<tokio::sync::Mutex<WorktreeConfigRef>> {
592 let mut map = WORKTREE_CONFIG
593 .lock()
594 .unwrap_or_else(std::sync::PoisonError::into_inner);
595 map.entry(repo.to_path_buf())
596 .or_insert_with(|| {
597 std::sync::Arc::new(tokio::sync::Mutex::new(WorktreeConfigRef {
598 count: 0,
599 we_enabled: false,
600 }))
601 })
602 .clone()
603}
604
605pub async fn acquire_worktree_config(repo: &Path) -> Result<()> {
622 let slot = worktree_config_slot(repo);
623 let mut entry = slot.lock().await;
624 entry.count += 1;
625 if entry.count == 1 {
626 entry.we_enabled = enable_worktree_config(repo).await?;
627 }
628 Ok(())
629}
630
631pub async fn release_worktree_config(repo: &Path) -> Result<()> {
637 let slot = worktree_config_slot(repo);
638 let mut entry = slot.lock().await;
639 entry.count = entry.count.saturating_sub(1);
640 if entry.count == 0 && entry.we_enabled {
641 disable_worktree_config(repo).await?;
642 entry.we_enabled = false;
643 }
644 Ok(())
645}
646
647pub async fn set_worktree_hooks_path(worktree: &Path, hooks_dir: &Path) -> Result<()> {
653 let dir = hooks_dir.to_string_lossy().replace('\\', "/");
654 git(worktree, &["config", "--worktree", "core.hooksPath", &dir])
655 .await
656 .map(|_| ())
657}
658
659pub async fn local_exclude(worktree: &Path, pattern: &str) -> Result<()> {
661 let git_dir = git(worktree, &["rev-parse", "--git-path", "info/exclude"]).await?;
662 let path = worktree.join(git_dir);
663 if let Some(parent) = path.parent() {
664 tokio::fs::create_dir_all(parent).await.ok();
665 }
666 let mut body = tokio::fs::read_to_string(&path).await.unwrap_or_default();
667 if body.lines().any(|l| l.trim() == pattern) {
668 return Ok(());
669 }
670 if !body.is_empty() && !body.ends_with('\n') {
671 body.push('\n');
672 }
673 body.push_str(pattern);
674 body.push('\n');
675 tokio::fs::write(&path, body)
676 .await
677 .with_context(|| format!("write {}", path.display()))?;
678 Ok(())
679}
680
681pub async fn merge_no_ff(repo: &Path, branch: &str, message: &str) -> Result<GitOut> {
687 git_raw(
688 repo,
689 &["merge", "--no-ff", "--no-edit", "-m", message, branch],
690 )
691 .await
692}
693
694pub async fn merge_squash(repo: &Path, branch: &str, message: &str) -> Result<GitOut> {
705 let staged = git_raw(repo, &["merge", "--squash", branch]).await?;
706 if !staged.ok() {
707 return Ok(staged);
708 }
709 git_raw(repo, &["commit", "-m", message]).await
710}
711
712pub async fn merge_ff_only(repo: &Path, branch: &str) -> Result<GitOut> {
722 git_raw(repo, &["merge", "--ff-only", branch]).await
723}
724
725pub async fn push(repo: &Path, remote: &str, branch: &str) -> Result<GitOut> {
727 git_raw(repo, &["push", "-u", remote, branch]).await
728}
729
730pub async fn push_rewritten(repo: &Path, remote: &str, branch: &str) -> Result<GitOut> {
738 git_raw(repo, &["push", "--force-with-lease", remote, branch]).await
739}
740
741pub async fn push_pinned(
750 repo: &Path,
751 remote: &str,
752 branch: &str,
753 expected: &str,
754) -> Result<GitOut> {
755 let lease = format!("--force-with-lease=refs/heads/{branch}:{expected}");
756 let refspec = format!("refs/heads/{branch}:refs/heads/{branch}");
757 git_raw(repo, &["push", &lease, remote, &refspec]).await
758}
759
760pub async fn cherry(repo: &Path, upstream: &str, head: &str) -> Result<(Vec<String>, Vec<String>)> {
763 let out = git(repo, &["cherry", upstream, head]).await?;
764 let (mut unmatched, mut matched) = (Vec::new(), Vec::new());
765 for line in out.lines() {
766 if let Some(sha) = line.strip_prefix("+ ") {
767 unmatched.push(sha.trim().to_owned());
768 } else if let Some(sha) = line.strip_prefix("- ") {
769 matched.push(sha.trim().to_owned());
770 }
771 }
772 Ok((unmatched, matched))
773}
774
775#[derive(Debug, Clone, PartialEq, Eq)]
779pub struct CommitKey {
780 pub sha: String,
782 pub key: String,
784}
785
786pub async fn commit_keys(repo: &Path, range: &str) -> Result<Vec<CommitKey>> {
788 let out = git(
789 repo,
790 &[
791 "log",
792 "--no-merges",
793 "--reverse",
794 "--date=raw",
795 "--format=%H%x1f%an%x1f%ae%x1f%ad%x1f%s",
796 range,
797 ],
798 )
799 .await?;
800 Ok(out
801 .lines()
802 .filter_map(|l| l.split_once('\u{1f}'))
803 .map(|(sha, key)| CommitKey {
804 sha: sha.to_owned(),
805 key: key.to_owned(),
806 })
807 .collect())
808}
809
810#[derive(Debug, Clone, PartialEq, Eq)]
812pub enum RebaseStart {
813 Applied,
816 Conflicted(String),
819 Failed(String),
822}
823
824pub async fn rebase_start(
839 repo: &Path,
840 scratch: &Path,
841 branch: &str,
842 onto: &str,
843) -> Result<RebaseStart> {
844 worktree_remove(repo, scratch).await.ok();
847 git_raw(
848 repo,
849 &[
850 "worktree",
851 "add",
852 "--force",
853 &scratch.to_string_lossy(),
854 branch,
855 ],
856 )
857 .await?;
858
859 let out = git_raw(scratch, &["rebase", onto]).await?;
860 if out.ok() {
861 worktree_remove(repo, scratch).await.ok();
862 return Ok(RebaseStart::Applied);
863 }
864 let why = if out.stderr.trim().is_empty() {
865 out.stdout.trim().to_owned()
866 } else {
867 out.stderr.trim().to_owned()
868 };
869 if rebase_in_progress(scratch).await {
870 return Ok(RebaseStart::Conflicted(why));
871 }
872 worktree_remove(repo, scratch).await.ok();
873 Ok(RebaseStart::Failed(why))
874}
875
876pub async fn rebase_in_progress(worktree: &Path) -> bool {
878 for name in ["rebase-merge", "rebase-apply"] {
879 let Ok(p) = git(worktree, &["rev-parse", "--git-path", name]).await else {
880 continue;
881 };
882 let p = Path::new(p.trim());
883 let full = if p.is_absolute() {
884 p.to_path_buf()
885 } else {
886 worktree.join(p)
887 };
888 if full.exists() {
889 return true;
890 }
891 }
892 false
893}
894
895pub async fn unmerged_paths(worktree: &Path) -> Result<Vec<String>> {
897 let out = git(worktree, &["diff", "--name-only", "--diff-filter=U"]).await?;
898 Ok(out
899 .lines()
900 .map(str::trim)
901 .filter(|l| !l.is_empty())
902 .map(str::to_owned)
903 .collect())
904}
905
906pub async fn rebase_abort(repo: &Path, scratch: &Path) {
909 git_raw(scratch, &["rebase", "--abort"]).await.ok();
910 worktree_remove(repo, scratch).await.ok();
911}
912
913pub async fn rebase_branch_in_temp(
921 repo: &Path,
922 scratch: &Path,
923 branch: &str,
924 onto: &str,
925) -> Result<Option<String>> {
926 match rebase_start(repo, scratch, branch, onto).await? {
927 RebaseStart::Applied => Ok(None),
928 RebaseStart::Failed(why) => Ok(Some(why)),
929 RebaseStart::Conflicted(why) => {
930 rebase_abort(repo, scratch).await;
931 Ok(Some(why))
932 }
933 }
934}
935
936pub async fn sync_to_head(worktree: &Path) -> Result<()> {
949 git(worktree, &["reset", "--hard", "HEAD"]).await?;
950 git(worktree, &["clean", "-fdx"]).await?;
951 Ok(())
952}
953
954pub async fn fetch(repo: &Path, remote: &str, branch: &str) -> Result<GitOut> {
973 let refspec = format!("+refs/heads/{branch}:refs/remotes/{remote}/{branch}");
974 git_raw(repo, &["fetch", "--quiet", remote, &refspec]).await
975}
976
977pub async fn is_ancestor(repo: &Path, ancestor: &str, of: &str) -> bool {
979 git_raw(repo, &["merge-base", "--is-ancestor", ancestor, of])
980 .await
981 .is_ok_and(|o| o.ok())
982}
983
984pub async fn ancestry(repo: &Path, ancestor: &str, of: &str) -> Result<bool> {
989 let out = git_raw(repo, &["merge-base", "--is-ancestor", ancestor, of]).await?;
990 match out.code {
991 Some(0) => Ok(true),
992 Some(1) => Ok(false),
993 _ => bail!(
994 "git merge-base --is-ancestor {ancestor} {of} failed (exit {:?}): {}",
995 out.code,
996 out.stderr
997 ),
998 }
999}
1000
1001pub async fn commit_of(repo: &Path, rev: &str) -> Option<String> {
1003 let spec = format!("{rev}^{{commit}}");
1004 let out = git_raw(repo, &["rev-parse", "--verify", "--quiet", &spec])
1005 .await
1006 .ok()?;
1007 (out.ok() && !out.stdout.is_empty()).then_some(out.stdout)
1008}
1009
1010pub async fn branches_containing(repo: &Path, commit: &str) -> Result<Vec<String>> {
1012 let out = git(
1013 repo,
1014 &[
1015 "branch",
1016 "-a",
1017 "--contains",
1018 commit,
1019 "--format=%(refname:short)",
1020 ],
1021 )
1022 .await?;
1023 Ok(out
1024 .lines()
1025 .map(str::trim)
1026 .filter(|l| !l.is_empty() && !l.ends_with("/HEAD") && !l.contains("HEAD detached"))
1027 .map(str::to_owned)
1028 .collect())
1029}
1030
1031pub async fn cherry_pick(worktree: &Path, commit: &str) -> Result<()> {
1035 let out = git_raw(
1036 worktree,
1037 &[
1038 "-c",
1039 "user.name=magi candidate",
1040 "-c",
1041 "user.email=magi@localhost",
1042 "cherry-pick",
1043 "-x",
1044 commit,
1045 ],
1046 )
1047 .await?;
1048 if out.ok() {
1049 return Ok(());
1050 }
1051 let _ = git_raw(worktree, &["cherry-pick", "--abort"]).await;
1052 bail!(
1053 "cherry-pick of {commit} failed: {}",
1054 if out.stderr.is_empty() {
1055 out.stdout
1056 } else {
1057 out.stderr
1058 }
1059 )
1060}
1061
1062pub async fn tree_of(repo: &Path, rev: &str) -> Result<String> {
1064 git(repo, &["rev-parse", &format!("{rev}^{{tree}}")]).await
1065}
1066
1067pub async fn rev_exists(repo: &Path, rev: &str) -> bool {
1069 git_raw(repo, &["rev-parse", "--verify", "--quiet", rev])
1070 .await
1071 .is_ok_and(|o| o.ok())
1072}
1073
1074#[cfg(test)]
1075mod tests {
1076 use super::*;
1077
1078 #[test]
1079 fn worktree_holder_is_read_from_the_porcelain_listing() {
1080 let listing = "worktree /repo\nHEAD aaa\nbranch refs/heads/main\n\n\
1081 worktree /wt/cand-A\nHEAD bbb\nbranch refs/heads/magi/f82f/A\n\n\
1082 worktree /wt/detached\nHEAD ccc\ndetached\n";
1083 assert_eq!(
1084 parse_worktree_holder(listing, "magi/f82f/A"),
1085 Some(PathBuf::from("/wt/cand-A"))
1086 );
1087 assert_eq!(parse_worktree_holder(listing, "magi/f82f"), None);
1088 assert_eq!(parse_worktree_holder(listing, "other"), None);
1089 }
1090
1091 async fn scratch() -> (tempfile::TempDir, PathBuf) {
1092 let dir = tempfile::tempdir().unwrap();
1093 let repo = dir.path().join("repo");
1094 tokio::fs::create_dir_all(&repo).await.unwrap();
1095 git(&repo, &["init", "-b", "main"]).await.unwrap();
1096 git(&repo, &["config", "user.name", "test"]).await.unwrap();
1097 git(&repo, &["config", "user.email", "test@example.com"])
1098 .await
1099 .unwrap();
1100 tokio::fs::write(repo.join("a.txt"), "one\n").await.unwrap();
1101 git(&repo, &["add", "-A"]).await.unwrap();
1102 git(&repo, &["commit", "-m", "init"]).await.unwrap();
1103 (dir, repo)
1104 }
1105
1106 #[tokio::test]
1107 async fn a_branch_rebases_onto_a_moved_base_and_says_when_it_cannot() {
1108 let (_g, repo) = scratch().await;
1109
1110 git(&repo, &["checkout", "-b", "side"]).await.unwrap();
1112 tokio::fs::write(repo.join("b.txt"), "side\n")
1113 .await
1114 .unwrap();
1115 git(&repo, &["add", "-A"]).await.unwrap();
1116 git(&repo, &["commit", "-m", "side work"]).await.unwrap();
1117
1118 git(&repo, &["checkout", "main"]).await.unwrap();
1121 tokio::fs::write(repo.join("c.txt"), "main\n")
1122 .await
1123 .unwrap();
1124 git(&repo, &["add", "-A"]).await.unwrap();
1125 git(&repo, &["commit", "-m", "main moved"]).await.unwrap();
1126
1127 let scratch_tree = repo.parent().unwrap().join("rebase-scratch");
1128 let clean = rebase_branch_in_temp(&repo, &scratch_tree, "side", "main")
1129 .await
1130 .unwrap();
1131 assert!(clean.is_none(), "a disjoint change rebases: {clean:?}");
1132 assert_eq!(
1133 commits_ahead(&repo, "main", "side").await.unwrap(),
1134 1,
1135 "one commit, replayed onto the new base"
1136 );
1137 assert!(
1138 !scratch_tree.exists(),
1139 "the throwaway worktree is not left behind"
1140 );
1141
1142 git(&repo, &["checkout", "-b", "clash"]).await.unwrap();
1144 tokio::fs::write(repo.join("a.txt"), "clash\n")
1145 .await
1146 .unwrap();
1147 git(&repo, &["add", "-A"]).await.unwrap();
1148 git(&repo, &["commit", "-m", "clash"]).await.unwrap();
1149 git(&repo, &["checkout", "main"]).await.unwrap();
1150 tokio::fs::write(repo.join("a.txt"), "main edit\n")
1151 .await
1152 .unwrap();
1153 git(&repo, &["add", "-A"]).await.unwrap();
1154 git(&repo, &["commit", "-m", "main edit"]).await.unwrap();
1155
1156 let before = rev_parse(&repo, "clash").await.unwrap();
1157 let why = rebase_branch_in_temp(&repo, &scratch_tree, "clash", "main")
1158 .await
1159 .unwrap()
1160 .expect("a same-line clash cannot be rebased silently");
1161 assert!(
1162 why.to_lowercase().contains("conflict"),
1163 "the reason is what git said, which is what a person needs: {why}"
1164 );
1165 assert_eq!(
1166 rev_parse(&repo, "clash").await.unwrap(),
1167 before,
1168 "a failed rebase leaves the branch exactly where it was"
1169 );
1170 assert!(!scratch_tree.exists(), "and cleans up after itself");
1171 }
1172
1173 #[tokio::test]
1174 async fn merge_squash_folds_the_branch_into_one_commit_under_the_given_message() {
1175 let (_g, repo) = scratch().await;
1176 git(&repo, &["checkout", "-b", "side"]).await.unwrap();
1177 for name in ["b.txt", "c.txt"] {
1178 tokio::fs::write(repo.join(name), "side\n").await.unwrap();
1179 git(&repo, &["add", "-A"]).await.unwrap();
1180 git(
1181 &repo,
1182 &["commit", "-m", "magi: candidate A (uncommitted work)"],
1183 )
1184 .await
1185 .unwrap();
1186 }
1187 git(&repo, &["checkout", "main"]).await.unwrap();
1188 let before = rev_parse(&repo, "main").await.unwrap();
1189
1190 let out = merge_squash(&repo, "side", "an explicit subject")
1191 .await
1192 .unwrap();
1193 assert!(out.ok(), "{}", out.stderr);
1194 assert_eq!(
1195 commits_ahead(&repo, &before, "main").await.unwrap(),
1196 1,
1197 "squash adds exactly one commit onto the tip, not one per candidate commit"
1198 );
1199 let subject = git(&repo, &["log", "-1", "--format=%s"]).await.unwrap();
1200 assert_eq!(
1201 subject, "an explicit subject",
1202 "the candidate's own placeholder subject must not survive: {subject}"
1203 );
1204 }
1205
1206 #[tokio::test]
1207 async fn merge_ff_only_fast_forwards_a_branch_already_rebased_onto_the_tip() {
1208 let (_g, repo) = scratch().await;
1209 git(&repo, &["checkout", "-b", "side"]).await.unwrap();
1210 tokio::fs::write(repo.join("b.txt"), "side\n")
1211 .await
1212 .unwrap();
1213 git(&repo, &["add", "-A"]).await.unwrap();
1214 git(&repo, &["commit", "-m", "side work"]).await.unwrap();
1215 git(&repo, &["checkout", "main"]).await.unwrap();
1216
1217 let before = rev_parse(&repo, "side").await.unwrap();
1218 let out = merge_ff_only(&repo, "side").await.unwrap();
1219 assert!(out.ok(), "{}", out.stderr);
1220 assert_eq!(
1221 rev_parse(&repo, "main").await.unwrap(),
1222 before,
1223 "a fast-forward moves the base tip to the branch, no merge commit"
1224 );
1225 }
1226
1227 #[tokio::test]
1228 async fn merge_ff_only_refuses_to_write_a_merge_commit() {
1229 let (_g, repo) = scratch().await;
1230 git(&repo, &["checkout", "-b", "side"]).await.unwrap();
1231 tokio::fs::write(repo.join("b.txt"), "side\n")
1232 .await
1233 .unwrap();
1234 git(&repo, &["add", "-A"]).await.unwrap();
1235 git(&repo, &["commit", "-m", "side work"]).await.unwrap();
1236
1237 git(&repo, &["checkout", "main"]).await.unwrap();
1239 tokio::fs::write(repo.join("c.txt"), "main\n")
1240 .await
1241 .unwrap();
1242 git(&repo, &["add", "-A"]).await.unwrap();
1243 git(&repo, &["commit", "-m", "main moved"]).await.unwrap();
1244
1245 let before = rev_parse(&repo, "main").await.unwrap();
1246 let out = merge_ff_only(&repo, "side").await.unwrap();
1247 assert!(!out.ok(), "a divergent branch cannot fast-forward");
1248 assert_eq!(
1249 rev_parse(&repo, "main").await.unwrap(),
1250 before,
1251 "a refused fast-forward must not touch main"
1252 );
1253 }
1254
1255 #[tokio::test]
1256 async fn a_sibling_worktree_stays_stale_after_a_rebase_until_synced() {
1257 let (guard, repo) = scratch().await;
1258
1259 git(&repo, &["branch", "side"]).await.unwrap();
1263 let side_wt = guard.path().join("side-wt");
1264 git(
1265 &repo,
1266 &["worktree", "add", &side_wt.to_string_lossy(), "side"],
1267 )
1268 .await
1269 .unwrap();
1270 tokio::fs::write(side_wt.join("b.txt"), "candidate\n")
1271 .await
1272 .unwrap();
1273 git(&side_wt, &["add", "-A"]).await.unwrap();
1274 git(&side_wt, &["commit", "-m", "side work"]).await.unwrap();
1275
1276 git(&repo, &["checkout", "main"]).await.unwrap();
1278 tokio::fs::write(repo.join("c.txt"), "main\n")
1279 .await
1280 .unwrap();
1281 git(&repo, &["add", "-A"]).await.unwrap();
1282 git(&repo, &["commit", "-m", "main moved"]).await.unwrap();
1283
1284 let scratch_tree = guard.path().join("rebase-scratch");
1286 let clean = rebase_branch_in_temp(&repo, &scratch_tree, "side", "main")
1287 .await
1288 .unwrap();
1289 assert!(clean.is_none());
1290
1291 assert_eq!(
1295 rev_parse(&side_wt, "HEAD").await.unwrap(),
1296 rev_parse(&repo, "side").await.unwrap(),
1297 "HEAD follows the moved ref"
1298 );
1299 assert!(
1300 !side_wt.join("c.txt").exists(),
1301 "stale until synced: main's new file has not reached this worktree's disk"
1302 );
1303
1304 sync_to_head(&side_wt).await.unwrap();
1305 assert!(side_wt.join("c.txt").is_file(), "synced now");
1306 assert!(
1307 side_wt.join("b.txt").is_file(),
1308 "the worktree's own committed work survives the sync"
1309 );
1310 assert!(is_clean(&side_wt).await.unwrap());
1311 }
1312
1313 #[tokio::test]
1314 async fn clean_repo_reports_clean_then_dirty() {
1315 let (_g, repo) = scratch().await;
1316 assert!(is_clean(&repo).await.unwrap());
1317 tokio::fs::write(repo.join("a.txt"), "two\n").await.unwrap();
1318 assert!(!is_clean(&repo).await.unwrap());
1319 }
1320
1321 async fn track(repo: &Path, name: &str, body: &str) {
1322 let p = repo.join(name);
1323 if let Some(d) = p.parent() {
1324 tokio::fs::create_dir_all(d).await.unwrap();
1325 }
1326 tokio::fs::write(&p, body).await.unwrap();
1327 git(repo, &["add", name]).await.unwrap();
1328 git(
1329 repo,
1330 &[
1331 "-c",
1332 "user.name=t",
1333 "-c",
1334 "user.email=t@localhost",
1335 "commit",
1336 "-q",
1337 "-m",
1338 "seed",
1339 ],
1340 )
1341 .await
1342 .unwrap();
1343 }
1344
1345 #[tokio::test]
1346 async fn rescue_withholds_a_foreign_lockfile() {
1347 let (_g, repo) = scratch().await;
1348 track(&repo, "web/bun.lock", "a\n").await;
1349 tokio::fs::write(repo.join("web/pnpm-lock.yaml"), "x\n")
1350 .await
1351 .unwrap();
1352 tokio::fs::write(repo.join("web/app.ts"), "real\n")
1353 .await
1354 .unwrap();
1355
1356 let r = rescue_commit(&repo, "rescue").await.unwrap();
1357 assert!(r.committed);
1358 assert_eq!(
1359 r.withheld,
1360 [Stray {
1361 path: "web/pnpm-lock.yaml".to_owned(),
1362 manager: "pnpm".to_owned(),
1363 kept_by: "web/bun.lock".to_owned(),
1364 }]
1365 );
1366 let files = git(&repo, &["show", "--name-only", "--format=", "HEAD"])
1367 .await
1368 .unwrap();
1369 assert!(files.contains("web/app.ts"), "{files}");
1370 assert!(!files.contains("pnpm-lock"), "{files}");
1371 assert!(repo.join("web/pnpm-lock.yaml").is_file(), "not deleted");
1372 }
1373
1374 #[tokio::test]
1375 async fn rescue_with_only_a_stray_commits_nothing() {
1376 let (_g, repo) = scratch().await;
1377 track(&repo, "bun.lock", "a\n").await;
1378 tokio::fs::write(repo.join("yarn.lock"), "x\n")
1379 .await
1380 .unwrap();
1381 let r = rescue_commit(&repo, "rescue").await.unwrap();
1382 assert!(!r.committed);
1383 assert_eq!(r.withheld.len(), 1);
1384 }
1385
1386 #[tokio::test]
1387 async fn rescue_keeps_a_same_manager_lockfile_update() {
1388 let (_g, repo) = scratch().await;
1389 track(&repo, "bun.lock", "a\n").await;
1390 tokio::fs::write(repo.join("bun.lock"), "b\n")
1391 .await
1392 .unwrap();
1393 let r = rescue_commit(&repo, "rescue").await.unwrap();
1394 assert!(r.committed);
1395 assert!(r.withheld.is_empty());
1396 let files = git(&repo, &["show", "--name-only", "--format=", "HEAD"])
1397 .await
1398 .unwrap();
1399 assert_eq!(files, "bun.lock");
1400 }
1401
1402 #[tokio::test]
1403 async fn rescue_keeps_the_first_lockfile_in_a_bare_directory() {
1404 let (_g, repo) = scratch().await;
1405 track(&repo, "other/bun.lock", "a\n").await;
1406 tokio::fs::create_dir_all(repo.join("web")).await.unwrap();
1407 tokio::fs::write(repo.join("web/package-lock.json"), "{}\n")
1408 .await
1409 .unwrap();
1410 let r = rescue_commit(&repo, "rescue").await.unwrap();
1411 assert!(r.committed);
1412 assert!(r.withheld.is_empty());
1413 }
1414
1415 #[test]
1416 fn a_cargo_lock_is_foreign_only_without_a_cargo_toml() {
1417 let s = |v: &[&str]| v.iter().map(|x| (*x).to_owned()).collect::<Vec<_>>();
1418 assert_eq!(stray_lockfiles(&s(&["a/Cargo.lock"]), &s(&[])).len(), 1);
1419 assert!(stray_lockfiles(&s(&["a/Cargo.lock"]), &s(&["a/Cargo.toml"])).is_empty());
1420 assert!(stray_lockfiles(&s(&["a/Cargo.lock", "a/Cargo.toml"]), &s(&[])).is_empty());
1421 assert_eq!(
1423 stray_lockfiles(&s(&["a/Cargo.lock"]), &s(&["Cargo.toml"])).len(),
1424 1
1425 );
1426 }
1427
1428 #[tokio::test]
1429 async fn worktree_lifecycle_and_diff() {
1430 let (guard, repo) = scratch().await;
1431 let base = rev_parse(&repo, "HEAD").await.unwrap();
1432 let wt = guard.path().join("wt-a");
1433 worktree_add_branch(&repo, &wt, "magi/test/a", &base)
1434 .await
1435 .unwrap();
1436 tokio::fs::write(wt.join("b.txt"), "candidate\n")
1437 .await
1438 .unwrap();
1439
1440 assert!(commit_all(&wt, "candidate work").await.unwrap());
1441 assert!(!commit_all(&wt, "nothing left").await.unwrap());
1442
1443 assert_eq!(commits_ahead(&wt, &base, "HEAD").await.unwrap(), 1);
1444 let patch = diff(&wt, &base, "HEAD").await.unwrap();
1445 assert!(patch.contains("b.txt"), "patch was: {patch}");
1446 assert_eq!(
1447 changed_files(&wt, &base, "HEAD").await.unwrap(),
1448 ["b.txt".to_owned()]
1449 );
1450
1451 let author = git(&wt, &["log", "-1", "--format=%an <%ae>"])
1453 .await
1454 .unwrap();
1455 assert_eq!(author, "magi candidate <magi@localhost>");
1456
1457 assert!(worktree_remove(&repo, &wt).await.unwrap());
1458 assert!(branch_exists(&repo, "magi/test/a").await.unwrap());
1459 assert!(branch_delete(&repo, "magi/test/a").await.unwrap());
1460 assert!(!branch_exists(&repo, "magi/test/a").await.unwrap());
1461 }
1462
1463 #[tokio::test]
1464 async fn worktree_scoped_hooks_path_does_not_leak_to_primary() {
1465 let (guard, repo) = scratch().await;
1466 let base = rev_parse(&repo, "HEAD").await.unwrap();
1467 let wt = guard.path().join("wt-h");
1468 worktree_add_branch(&repo, &wt, "magi/test/h", &base)
1469 .await
1470 .unwrap();
1471 let hooks = guard.path().join("hooks");
1472 tokio::fs::create_dir_all(&hooks).await.unwrap();
1473
1474 assert!(enable_worktree_config(&repo).await.unwrap());
1475 set_worktree_hooks_path(&wt, &hooks).await.unwrap();
1476
1477 let in_wt = git(&wt, &["config", "--get", "core.hooksPath"])
1478 .await
1479 .unwrap();
1480 assert!(!in_wt.is_empty());
1481 let in_primary = git_raw(&repo, &["config", "--get", "core.hooksPath"])
1482 .await
1483 .unwrap();
1484 assert!(
1485 !in_primary.ok(),
1486 "primary worktree must keep its own hooks: {in_primary:?}"
1487 );
1488
1489 disable_worktree_config(&repo).await.unwrap();
1490 }
1491
1492 #[tokio::test]
1493 async fn worktree_config_stays_on_while_a_sibling_run_still_holds_it() {
1494 let (_g, repo) = scratch().await;
1495
1496 acquire_worktree_config(&repo).await.unwrap();
1499 acquire_worktree_config(&repo).await.unwrap();
1500
1501 let on = git(&repo, &["config", "--get", "extensions.worktreeConfig"])
1502 .await
1503 .unwrap();
1504 assert_eq!(on, "true");
1505
1506 release_worktree_config(&repo).await.unwrap();
1510 let still_on = git(&repo, &["config", "--get", "extensions.worktreeConfig"])
1511 .await
1512 .unwrap();
1513 assert_eq!(
1514 still_on, "true",
1515 "a sibling run's release must not disable the setting for the one still working"
1516 );
1517
1518 release_worktree_config(&repo).await.unwrap();
1520 let after = git_raw(&repo, &["config", "--get", "extensions.worktreeConfig"])
1521 .await
1522 .unwrap();
1523 assert!(
1524 !after.ok(),
1525 "the last release must turn the setting back off: {after:?}"
1526 );
1527 }
1528
1529 #[tokio::test]
1530 async fn worktree_config_already_on_before_magi_touched_it_is_left_alone() {
1531 let (_g, repo) = scratch().await;
1532 git(&repo, &["config", "extensions.worktreeConfig", "true"])
1533 .await
1534 .unwrap();
1535
1536 acquire_worktree_config(&repo).await.unwrap();
1541 release_worktree_config(&repo).await.unwrap();
1542
1543 let still_on = git(&repo, &["config", "--get", "extensions.worktreeConfig"])
1544 .await
1545 .unwrap();
1546 assert_eq!(still_on, "true");
1547 }
1548
1549 #[tokio::test]
1550 async fn local_exclude_is_idempotent() {
1551 let (_g, repo) = scratch().await;
1552 local_exclude(&repo, "/.magi/").await.unwrap();
1553 local_exclude(&repo, "/.magi/").await.unwrap();
1554 let path = repo.join(".git/info/exclude");
1555 let body = tokio::fs::read_to_string(&path).await.unwrap();
1556 assert_eq!(body.matches("/.magi/").count(), 1);
1557 }
1558}