vcs_core/lib.rs
1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![deny(rustdoc::broken_intra_doc_links)]
3//! `vcs-core` — write code against "the repository" without caring whether it's
4//! git or jj.
5//!
6//! You hold one handle, [`Repo`], that auto-detects whether a directory is a git or
7//! a jj checkout and runs whatever operations *both* tools support — handing back
8//! plain result types ([`RepoSnapshot`], [`FileChange`], [`MergeProbe`], …) that
9//! don't mention the backend (whether the repo is git or jj). Async, structured
10//! errors, and every subprocess
11//! inherits the underlying client's OS-**job** containment (an OS-level container
12//! that kills the whole process tree if your program exits, via [`processkit`]) so
13//! no `git`/`jj` tree is orphaned.
14//!
15//! # What you can do
16//!
17//! From one [`Repo`] handle: read the current branch and a batched status
18//! [`snapshot`](Repo::snapshot) · list & diff changed files · commit paths · fetch
19//! / push / checkout / rebase · probe a merge for conflicts
20//! ([`try_merge`](Repo::try_merge)) · drive in-progress merge/rebase state · manage
21//! worktrees. Open one and read a prompt line:
22//!
23//! ```no_run
24//! use vcs_core::Repo;
25//! # async fn demo() -> vcs_core::Result<()> {
26//! let repo = Repo::open(".")?; // detects git vs jj
27//! let s = repo.snapshot().await?; // a few spawns, not a call per field
28//! let branch = s.branch.as_deref().unwrap_or("(detached)");
29//! println!("{branch} {}", if s.dirty { "*" } else { "" });
30//! # Ok(()) }
31//! ```
32//!
33//! **It's a thin common layer, not a god-object.** The shared surface carries only
34//! what unifies *without lying*; the few operations the two tools model too
35//! differently (a full `merge`, jj's `op restore`, range/revset queries) stay on
36//! the raw `git`/`jj` handle rather than being faked (see
37//! [below](#whats-deliberately-not-unified)). Reach for the unified handle when code
38//! must work on both backends; drop to the raw client when you need power only one
39//! of them offers.
40//!
41//! # Mental model (engineering reference)
42//!
43//! The surface is three layers, narrowing from "which tool is this?" to "do the
44//! thing":
45//!
46//! - **[`detect`]** — walk up from a directory to the filesystem root for a
47//! `.git`/`.jj` repo (jj wins when colocated — it's the tool driving the working
48//! copy). Pure filesystem probing, no subprocess; yields a [`Located`]
49//! ([`BackendKind`] + worktree root).
50//! - **[`Repo`]** — the cwd-bound facade handle, the thing you hold. Open one with
51//! [`Repo::open`] (real job-backed runner) or build it over an explicit client
52//! with [`Repo::from_git`] / [`Repo::from_jj`] (the test seam). Re-anchor it to
53//! another directory cheaply with [`Repo::at`] — the backend is shared behind an
54//! `Arc`, so threading work across worktrees never re-detects or rebuilds the
55//! client. Inspect it with [`kind`](Repo::kind) / [`root`](Repo::root) /
56//! [`cwd`](Repo::cwd).
57//! - **[`VcsRepo`]** — the same common surface as an object-safe trait, so a
58//! consumer can hold a `Box<dyn VcsRepo>` / `&dyn VcsRepo` without naming the
59//! [`ProcessRunner`] generic. Every method mirrors the like-named inherent method
60//! on [`Repo`]; it adds nothing but the abstraction boundary.
61//!
62//! ## The common operations
63//!
64//! All on [`Repo`] (and [`VcsRepo`]), dir-free, dispatched per backend:
65//!
66//! - **Refs** — [`current_branch`](Repo::current_branch),
67//! [`trunk`](Repo::trunk), [`local_branches`](Repo::local_branches),
68//! [`branch_exists`](Repo::branch_exists),
69//! [`delete_branch`](Repo::delete_branch),
70//! [`rename_branch`](Repo::rename_branch) (branch on git, bookmark on jj).
71//! - **Status** — [`changed_files`](Repo::changed_files),
72//! [`diff_stat`](Repo::diff_stat),
73//! [`has_uncommitted_changes`](Repo::has_uncommitted_changes),
74//! [`has_tracked_changes`](Repo::has_tracked_changes),
75//! [`conflicted_files`](Repo::conflicted_files), and
76//! [`snapshot`](Repo::snapshot) — a **batched** prompt/status-bar read of the
77//! lot in one or two spawns.
78//! - **Mutations** — [`commit_paths`](Repo::commit_paths) (partial commit),
79//! [`fetch`](Repo::fetch) / [`fetch_from`](Repo::fetch_from) /
80//! [`fetch_branch`](Repo::fetch_branch) /
81//! [`push`](Repo::push), [`checkout`](Repo::checkout),
82//! [`rebase`](Repo::rebase).
83//! - **Merge & operation state** — [`try_merge`](Repo::try_merge) (a
84//! trace-free conflict probe → [`MergeProbe`]),
85//! [`in_progress_state`](Repo::in_progress_state) /
86//! [`abort_in_progress`](Repo::abort_in_progress) /
87//! [`continue_in_progress`](Repo::continue_in_progress) → [`OperationState`].
88//! - **Worktrees / workspaces** — [`list_worktrees`](Repo::list_worktrees),
89//! [`create_worktree`](Repo::create_worktree),
90//! [`remove_worktree`](Repo::remove_worktree), and the **synchronous**
91//! [`cleanup_worktree_blocking`](Repo::cleanup_worktree_blocking) for a `Drop`
92//! guard that cannot `.await`.
93//!
94//! Because the backends genuinely diverge in places, several common methods carry
95//! a documented asymmetry (e.g. `upstream`/`ahead`/`behind` are always `None` on
96//! jj; [`diff_stat`](Repo::diff_stat) excludes untracked files on git but not jj;
97//! [`in_progress_state`](Repo::in_progress_state) never returns `Conflict` on git).
98//! The method docs spell each one out — the facade unifies the *shape*, not away
99//! the truth.
100//!
101//! ## The escape hatches
102//!
103//! Tool-specific work reaches the underlying typed clients without adding
104//! `vcs-git`/`vcs-jj` as separate dependencies (both are re-exported):
105//! [`git_at`](Repo::git_at) / [`jj_at`](Repo::jj_at) hand out a cwd-bound view
106//! ([`GitAt`] / [`JjAt`], `dir` dropped); the raw
107//! [`git`](Repo::git) / [`jj`](Repo::jj) hand out a borrow of the client itself.
108//! Each returns `None` for the other backend.
109//!
110//! ## What's deliberately *not* unified
111//!
112//! Three families stay off the common surface because no honest single shape
113//! exists — reach them through the bound handles:
114//!
115//! - **Full `merge`** — jj composes `new` + `squash` + bookmark moves; git runs a
116//! single command. Only the *conflict probe* unifies, as
117//! [`try_merge`](Repo::try_merge).
118//! - **Operation rollback** — jj's `op restore` has no faithful git analogue; use
119//! [`Jj::transaction`](vcs_jj::Jj::transaction) on the jj client.
120//! - **Range / revset queries** — commit counts and diff stats over a range: git's
121//! `a..b` and jj's revsets aren't interchangeable, so neither is forced onto a
122//! shared signature.
123//!
124//! # Recipes
125//!
126//! Probe a merge for conflicts (trace-free), or spin up a worktree:
127//!
128//! ```no_run
129//! use std::path::Path;
130//! use vcs_core::{MergeProbe, Repo};
131//! # async fn demo(repo: &Repo) -> vcs_core::Result<()> {
132//! match repo.try_merge("feature").await? {
133//! MergeProbe::Clean => println!("merges cleanly"),
134//! MergeProbe::Conflicts(paths) => println!("would conflict in {paths:?}"),
135//! _ => {} // #[non_exhaustive]
136//! }
137//! let wt = repo.create_worktree(Path::new("/tmp/feat"), "feature", "main").await?;
138//! # let _ = wt;
139//! # Ok(()) }
140//! ```
141//!
142//! # Testing
143//!
144//! There is **no mock feature** on the facade traits — the runner is the seam.
145//! Build a [`Repo`] over a fake [`ProcessRunner`] with [`Repo::from_git`] /
146//! [`Repo::from_jj`] (e.g. a [`ScriptedRunner`](processkit::testing::ScriptedRunner)
147//! replying to canned argv), so the *real* per-backend dispatch, argv-building and
148//! parsing run against canned output — exactly what a mocked `VcsRepo` would skip.
149//! The cross-cutting patterns live in
150//! [vcs-testkit's guide](https://docs.rs/vcs-testkit/latest/vcs_testkit/guide/testing/).
151//!
152//! ```no_run
153//! use processkit::testing::{Reply, ScriptedRunner};
154//! use vcs_core::{vcs_git::Git, Repo};
155//! # async fn demo() -> vcs_core::Result<()> {
156//! let runner = ScriptedRunner::new().on(["git", "status"], Reply::ok(" M a.rs\0"));
157//! let repo = Repo::from_git("/repo", "/repo", Git::with_runner(runner));
158//! assert!(repo.has_uncommitted_changes().await?);
159//! # Ok(()) }
160//! ```
161//!
162//! # In-depth guide
163//!
164//! Beyond this page, this crate ships a full how-to guide — rendered on docs.rs
165//! from `docs/`. See the [`guide`] module, which walks every operation in depth
166//! and hosts the cross-cutting sub-guides: a [`cookbook`](guide::cookbook) of
167//! end-to-end flows, the [`process_model`](guide::process_model) (job containment,
168//! errors, cancellation), [`positioning`](guide::positioning) (facade-vs-raw-client
169//! and the three call shapes), and the [`stability`](guide::stability) contract.
170
171use std::path::{Path, PathBuf};
172use std::sync::Arc;
173
174use processkit::{JobRunner, ProcessRunner};
175use vcs_git::{Git, GitAt};
176use vcs_jj::{Jj, JjAt};
177
178mod dto;
179mod error;
180mod git_backend;
181mod jj_backend;
182
183pub use dto::{
184 BackendKind, BranchDelete, ChangeKind, CreateOutcome, DiffStat, FileChange, MergeProbe,
185 OperationState, RepoSnapshot, UpstreamTracking, WorktreeInfo, WorktreeRemove,
186};
187pub use error::{Error, Result};
188
189// Re-export the underlying typed clients so a consumer depending only on
190// `vcs-core` can still reach raw, tool-specific operations — and their types
191// (`GitApi`, `JjApi`, `WorktreeAdd`, `JjFileset`, …) — without adding `vcs-git`
192// / `vcs-jj` as separate dependencies. [`Repo::git`] / [`Repo::jj`] hand out
193// borrows of these clients; the consumer decides, per call, whether to go
194// through the facade or straight to the tool.
195pub use vcs_git;
196pub use vcs_jj;
197// Re-export `processkit` itself so a `vcs-core`-only consumer can name the
198// wrapped error directly — `match err { Error::Vcs(vcs_core::processkit::Error::
199// Timeout { .. }) => … }` — and reach `Outcome`/`CancellationToken`/… without
200// adding `processkit` as a separate dependency. (`Error::Vcs` carries a
201// `processkit::Error`; the classifiers below cover the common branches.)
202pub use processkit;
203// Also surfaced at the crate root so the token a `default_cancel_on` client takes
204// (built via `Git`/`Jj`, then passed to `Repo::from_git`/`from_jj`) is one name
205// away. (Cancellation is core in processkit 0.10 — always available, no feature.)
206pub use processkit::CancellationToken;
207
208/// The result of [`detect`]: which backend, and the repository root it was found
209/// at.
210#[derive(Debug, Clone, PartialEq, Eq)]
211#[non_exhaustive]
212pub struct Located {
213 /// The detected backend.
214 pub kind: BackendKind,
215 /// The directory holding `.git`/`.jj` — the worktree root.
216 pub root: PathBuf,
217}
218
219/// Walk up from `start` to the filesystem root looking for a repository. A `.jj`
220/// directory wins over `.git` (colocated repos are driven through jj); `.git` may
221/// be a directory or a gitlink file (a linked worktree/submodule). Pure
222/// filesystem probing — no subprocess.
223///
224/// `start` is walked exactly as given via [`Path::parent`], so pass an **absolute**
225/// path to search ancestors — a relative path like `"."` has no ancestor chain
226/// and only its own directory is checked. ([`Repo::open`] absolutises for you.)
227pub fn detect(start: &Path) -> Option<Located> {
228 let mut current = Some(start);
229 while let Some(dir) = current {
230 if is_jj_marker(&dir.join(".jj")) {
231 return Some(Located {
232 kind: BackendKind::Jj,
233 root: dir.to_path_buf(),
234 });
235 }
236 if is_git_marker(&dir.join(".git")) {
237 return Some(Located {
238 kind: BackendKind::Git,
239 root: dir.to_path_buf(),
240 });
241 }
242 current = dir.parent();
243 }
244 None
245}
246
247/// Whether `path` (a candidate `.jj`) is a real jj repository marker — a `.jj`
248/// **directory** that contains a **`repo`** entry (the store: a *directory* in a
249/// repo's main workspace / a colocated repo, a *file* pointer in a secondary
250/// workspace). A stray/empty directory merely *named* `.jj` (e.g. a leftover
251/// `mkdir .jj`) has no `repo` entry, so it can't shadow a healthy `.git` repo in the
252/// same or a higher directory (M19). Symmetric with [`is_git_marker`]: both require a
253/// *valid* marker, not mere existence.
254fn is_jj_marker(path: &Path) -> bool {
255 path.is_dir() && path.join("repo").exists()
256}
257
258/// Whether `path` (a candidate `.git`) is a real git repository marker — a `.git`
259/// **directory**, or a **gitlink file** (a linked worktree / submodule) whose
260/// content starts with `gitdir:`. A stray/garbage file merely *named* `.git` is
261/// rejected, so it can't shadow a real repository higher up the tree, and a binary
262/// or unreadable file is rejected too (the read fails → `false`). Symmetric with
263/// [`is_jj_marker`]: both require a *valid* marker, not mere existence.
264fn is_git_marker(path: &Path) -> bool {
265 use std::io::Read;
266 match std::fs::metadata(path) {
267 Ok(meta) if meta.is_dir() => true,
268 Ok(meta) if meta.is_file() => {
269 // A gitlink file is tiny (`gitdir: <path>\n`), so read only a small
270 // prefix: `detect` walks *up to the filesystem root*, so a huge/garbage
271 // file merely named `.git` in an ancestor we don't own must not force an
272 // unbounded read. `read_to_end` loops over short reads (unlike a single
273 // `read`, which the `Read` contract lets return fewer bytes), and
274 // `from_utf8_lossy` tolerates a binary file or a multibyte char split at
275 // the cap — the `gitdir:` marker is ASCII and within the first bytes.
276 let Ok(file) = std::fs::File::open(path) else {
277 return false;
278 };
279 let mut buf = Vec::new();
280 let _ = file.take(32).read_to_end(&mut buf);
281 String::from_utf8_lossy(&buf)
282 .trim_start()
283 .starts_with("gitdir:")
284 }
285 _ => false,
286 }
287}
288
289/// The per-tool client behind a [`Repo`]. Shared via `Arc` so [`Repo::at`] can
290/// re-anchor the cwd cheaply without rebuilding the client.
291enum Backend<R: ProcessRunner> {
292 Git(Arc<Git<R>>),
293 Jj(Arc<Jj<R>>),
294}
295
296impl<R: ProcessRunner> Backend<R> {
297 fn shared(&self) -> Self {
298 match self {
299 Backend::Git(g) => Backend::Git(Arc::clone(g)),
300 Backend::Jj(j) => Backend::Jj(Arc::clone(j)),
301 }
302 }
303}
304
305/// A cwd-bound, backend-agnostic VCS handle. Operations run against the bound
306/// directory ([`cwd`](Repo::cwd)); use [`at`](Repo::at) to get a sibling handle
307/// bound elsewhere.
308pub struct Repo<R: ProcessRunner = JobRunner> {
309 root: PathBuf,
310 cwd: PathBuf,
311 backend: Backend<R>,
312}
313
314impl Repo<JobRunner> {
315 /// Detect the repository at or above `dir` and open a handle bound to `dir`,
316 /// using the real job-backed runner. Errors with
317 /// [`Error::NotARepository`] when no `.git`/`.jj` is found.
318 pub fn open(dir: impl AsRef<Path>) -> Result<Self> {
319 // Absolutise first: `detect` walks parents, and a relative path like "."
320 // has no real ancestor chain (`Path::new(".").parent()` is `""`, then
321 // `None`), so a relative input would never find a repo above the cwd.
322 let dir = std::path::absolute(dir.as_ref())?;
323 let located = detect(&dir).ok_or_else(|| Error::NotARepository(dir.clone()))?;
324 let backend = match located.kind {
325 BackendKind::Git => Backend::Git(Arc::new(Git::new())),
326 BackendKind::Jj => Backend::Jj(Arc::new(Jj::new())),
327 };
328 Ok(Repo {
329 root: located.root,
330 cwd: dir,
331 backend,
332 })
333 }
334}
335
336impl<R: ProcessRunner> Repo<R> {
337 /// Build a git-backed handle from an explicit client — for a custom runner
338 /// (e.g. a test seam) or a pre-configured [`Git`].
339 pub fn from_git(root: impl Into<PathBuf>, cwd: impl Into<PathBuf>, client: Git<R>) -> Self {
340 Repo {
341 root: root.into(),
342 cwd: cwd.into(),
343 backend: Backend::Git(Arc::new(client)),
344 }
345 }
346
347 /// Build a jj-backed handle from an explicit client.
348 pub fn from_jj(root: impl Into<PathBuf>, cwd: impl Into<PathBuf>, client: Jj<R>) -> Self {
349 Repo {
350 root: root.into(),
351 cwd: cwd.into(),
352 backend: Backend::Jj(Arc::new(client)),
353 }
354 }
355
356 /// Which backend drives this handle.
357 pub fn kind(&self) -> BackendKind {
358 match &self.backend {
359 Backend::Git(_) => BackendKind::Git,
360 Backend::Jj(_) => BackendKind::Jj,
361 }
362 }
363
364 /// The repository root detected at open time.
365 pub fn root(&self) -> &Path {
366 &self.root
367 }
368
369 /// The directory operations run against.
370 pub fn cwd(&self) -> &Path {
371 &self.cwd
372 }
373
374 /// A sibling handle bound to `dir`, sharing this handle's client and root.
375 pub fn at(&self, dir: impl Into<PathBuf>) -> Self {
376 Repo {
377 root: self.root.clone(),
378 cwd: dir.into(),
379 backend: self.backend.shared(),
380 }
381 }
382
383 /// The underlying [`Git`] client, or `None` when jj-backed — an escape hatch
384 /// to git-only operations not on the common surface.
385 pub fn git(&self) -> Option<&Git<R>> {
386 match &self.backend {
387 Backend::Git(g) => Some(g.as_ref()),
388 Backend::Jj(_) => None,
389 }
390 }
391
392 /// The underlying [`Jj`] client, or `None` when git-backed.
393 pub fn jj(&self) -> Option<&Jj<R>> {
394 match &self.backend {
395 Backend::Jj(j) => Some(j.as_ref()),
396 Backend::Git(_) => None,
397 }
398 }
399
400 /// The git client bound to this handle's [`cwd`](Repo::cwd) — a [`GitAt`] whose
401 /// methods omit the `dir` argument — or `None` when jj-backed. The dir-free
402 /// counterpart of [`git`](Repo::git): `repo.git_at()?.merge_continue().await?`.
403 ///
404 /// The returned view borrows `self`. To work in another worktree, **bind the
405 /// re-anchored handle first** (the view can't outlive a temporary
406 /// [`at`](Repo::at)):
407 ///
408 /// ```no_run
409 /// # async fn f(repo: vcs_core::Repo, wt: &std::path::Path) -> vcs_core::Result<()> {
410 /// let wt = repo.at(wt); // owns the re-anchored handle
411 /// let git = wt.git_at().unwrap();
412 /// git.fetch().await?;
413 /// # Ok(()) }
414 /// ```
415 pub fn git_at(&self) -> Option<GitAt<'_, R>> {
416 match &self.backend {
417 Backend::Git(g) => Some(g.at(&self.cwd)),
418 Backend::Jj(_) => None,
419 }
420 }
421
422 /// The jj client bound to this handle's [`cwd`](Repo::cwd) — a [`JjAt`] whose
423 /// methods omit the `dir` argument — or `None` when git-backed. The dir-free
424 /// counterpart of [`jj`](Repo::jj). For another workspace, bind the re-anchored
425 /// handle first (`let ws = repo.at(path); ws.jj_at()…`) — see [`git_at`](Repo::git_at).
426 pub fn jj_at(&self) -> Option<JjAt<'_, R>> {
427 match &self.backend {
428 Backend::Jj(j) => Some(j.at(&self.cwd)),
429 Backend::Git(_) => None,
430 }
431 }
432
433 /// The current branch (git) or bookmark (jj). On jj this is the nearest
434 /// bookmark reachable from the working copy (`heads(::@ & bookmarks())`),
435 /// so it stays set across a `jj describe`/`jj new`/`jj commit` — which leave
436 /// the bookmark on the described parent while the new change carries none —
437 /// matching git's "still on my branch" reporting. When several bookmarks are
438 /// equally near `@`, the lexicographically-smallest name is returned
439 /// (deterministic). `None` only when detached / no bookmark on or above `@`.
440 pub async fn current_branch(&self) -> Result<Option<String>> {
441 match &self.backend {
442 Backend::Git(g) => git_backend::current_branch(g, &self.cwd).await,
443 Backend::Jj(j) => jj_backend::current_branch(j, &self.cwd).await,
444 }
445 }
446
447 /// The trunk branch/bookmark. Resolution order: the backend's own notion
448 /// (git's `origin/HEAD`, jj's `trunk()` revset), then a fallback to a local
449 /// `main`, then `master`; `None` when none of those resolve.
450 pub async fn trunk(&self) -> Result<Option<String>> {
451 let native = match &self.backend {
452 Backend::Git(g) => git_backend::trunk(g, &self.cwd).await?,
453 Backend::Jj(j) => jj_backend::trunk(j, &self.cwd).await?,
454 };
455 if native.is_some() {
456 return Ok(native);
457 }
458 for candidate in ["main", "master"] {
459 if self.branch_exists(candidate).await? {
460 return Ok(Some(candidate.to_string()));
461 }
462 }
463 Ok(None)
464 }
465
466 /// Local branch (git) / bookmark (jj) names.
467 ///
468 /// Backend divergence: on **jj**, a bookmark deleted locally but still **tracked**
469 /// on a remote lingers as a *tombstone* row (jj keeps it so the deletion can be
470 /// propagated) until the deletion is pushed — so this can list a name a
471 /// `delete_branch` just removed, unlike git. (The tombstone is not filtered here
472 /// because jj renders it and a *conflicted* bookmark identically — filtering would
473 /// also hide a real, conflicted bookmark; M21.)
474 pub async fn local_branches(&self) -> Result<Vec<String>> {
475 match &self.backend {
476 Backend::Git(g) => git_backend::local_branches(g, &self.cwd).await,
477 Backend::Jj(j) => jj_backend::local_branches(j, &self.cwd).await,
478 }
479 }
480
481 /// Whether a local branch/bookmark named `name` exists. See
482 /// [`local_branches`](Repo::local_branches) for the jj deleted-but-tracked
483 /// *tombstone* divergence (a just-deleted tracked bookmark can still read as
484 /// existing until the deletion is pushed).
485 pub async fn branch_exists(&self, name: &str) -> Result<bool> {
486 match &self.backend {
487 Backend::Git(g) => git_backend::branch_exists(g, &self.cwd, name).await,
488 Backend::Jj(j) => jj_backend::branch_exists(j, &self.cwd, name).await,
489 }
490 }
491
492 /// Whether the working copy has uncommitted changes (git: a non-empty
493 /// `status`; jj: a non-empty working-copy change `@`).
494 pub async fn has_uncommitted_changes(&self) -> Result<bool> {
495 match &self.backend {
496 Backend::Git(g) => git_backend::has_uncommitted_changes(g, &self.cwd).await,
497 Backend::Jj(j) => jj_backend::has_uncommitted_changes(j, &self.cwd).await,
498 }
499 }
500
501 /// Whether the working copy has uncommitted changes to *tracked* files.
502 ///
503 /// Backend nuance: git ignores untracked files here
504 /// (`status --untracked-files=no`); jj auto-tracks new files, so there is no
505 /// untracked concept and this equals
506 /// [`has_uncommitted_changes`](Self::has_uncommitted_changes).
507 pub async fn has_tracked_changes(&self) -> Result<bool> {
508 match &self.backend {
509 Backend::Git(g) => git_backend::has_tracked_changes(g, &self.cwd).await,
510 Backend::Jj(j) => jj_backend::has_uncommitted_changes(j, &self.cwd).await,
511 }
512 }
513
514 /// Paths with unresolved merge conflicts in the working copy, repo-relative
515 /// with `/` separators (git `diff --diff-filter=U` / jj `resolve --list -r @`).
516 /// Empty when there are none.
517 pub async fn conflicted_files(&self) -> Result<Vec<String>> {
518 match &self.backend {
519 Backend::Git(g) => git_backend::conflicted_files(g, &self.cwd).await,
520 Backend::Jj(j) => jj_backend::conflicted_files(j, &self.cwd).await,
521 }
522 }
523
524 /// Delete a local branch (git) / bookmark (jj). The [`BranchDelete`] spec's
525 /// [`force`](BranchDelete::force) applies to git only (`branch -D` vs `-d`); jj
526 /// has no force and ignores it.
527 pub async fn delete_branch(&self, spec: BranchDelete) -> Result<()> {
528 match &self.backend {
529 Backend::Git(g) => {
530 git_backend::delete_branch(g, &self.cwd, &spec.name, spec.force).await
531 }
532 Backend::Jj(j) => jj_backend::delete_branch(j, &self.cwd, &spec.name).await,
533 }
534 }
535
536 /// Rename a local branch (git) / bookmark (jj).
537 pub async fn rename_branch(&self, old: &str, new: &str) -> Result<()> {
538 match &self.backend {
539 Backend::Git(g) => git_backend::rename_branch(g, &self.cwd, old, new).await,
540 Backend::Jj(j) => jj_backend::rename_branch(j, &self.cwd, old, new).await,
541 }
542 }
543
544 /// The working-copy changes (git `status` / jj `diff -r @ --summary`).
545 pub async fn changed_files(&self) -> Result<Vec<FileChange>> {
546 match &self.backend {
547 Backend::Git(g) => git_backend::changed_files(g, &self.cwd).await,
548 Backend::Jj(j) => jj_backend::changed_files(j, &self.cwd).await,
549 }
550 }
551
552 /// Aggregate insertion/deletion counts for the working copy.
553 ///
554 /// Backend nuance: git counts the working tree against `HEAD` (`git diff`,
555 /// which **excludes untracked files**), while jj counts the `@` change against
556 /// its parent (which **includes** newly-added files). So on git a brand-new
557 /// file shows in [`changed_files`](Self::changed_files) but not here, whereas
558 /// on jj it shows in both. On an unborn git repo (no commits yet) the count is
559 /// taken against the empty tree, so a pre-first-commit working tree stats
560 /// instead of erroring.
561 pub async fn diff_stat(&self) -> Result<DiffStat> {
562 match &self.backend {
563 Backend::Git(g) => git_backend::diff_stat(g, &self.cwd).await,
564 Backend::Jj(j) => jj_backend::diff_stat(j, &self.cwd).await,
565 }
566 }
567
568 /// A batched [`RepoSnapshot`] of the common repo state — branch, upstream,
569 /// ahead/behind, dirtiness, change count, and operation state — in a **small
570 /// fixed** number of spawns instead of a call per field (git: `status
571 /// --porcelain=v2 --branch` + the in-progress probe; jj: a `log -r @`
572 /// template for head/empty/conflict, a `reachable_bookmarks` query for
573 /// `branch`, and a change count only when dirty). Built for prompt/status-bar/
574 /// TUI refreshes. Note the asymmetry: [`tracking`](RepoSnapshot::tracking)
575 /// (the upstream ref + ahead/behind) is always `None` on jj, which has no
576 /// git-style upstream tracking.
577 pub async fn snapshot(&self) -> Result<RepoSnapshot> {
578 match &self.backend {
579 Backend::Git(g) => git_backend::snapshot(g, &self.cwd).await,
580 Backend::Jj(j) => jj_backend::snapshot(j, &self.cwd).await,
581 }
582 }
583
584 /// Commit exactly `paths` with `message` (git `commit --only`, jj
585 /// `commit <filesets>`). Paths are repo-relative. `paths` must be non-empty:
586 /// an empty set is refused up front, because the backends would diverge
587 /// dangerously — git errors out, while jj's `commit` with no filesets would
588 /// silently commit the **entire** working copy.
589 pub async fn commit_paths(&self, paths: &[String], message: &str) -> Result<()> {
590 if paths.is_empty() {
591 return Err(Error::Io(std::io::Error::new(
592 std::io::ErrorKind::InvalidInput,
593 "commit_paths requires at least one path: an empty set would error \
594 on git but commit the entire working copy on jj",
595 )));
596 }
597 match &self.backend {
598 Backend::Git(g) => git_backend::commit_paths(g, &self.cwd, paths, message).await,
599 Backend::Jj(j) => jj_backend::commit_paths(j, &self.cwd, paths, message).await,
600 }
601 }
602
603 /// Fetch from the default remote (git `fetch` / jj `git fetch`).
604 pub async fn fetch(&self) -> Result<()> {
605 match &self.backend {
606 Backend::Git(g) => git_backend::fetch(g, &self.cwd).await,
607 Backend::Jj(j) => jj_backend::fetch(j, &self.cwd).await,
608 }
609 }
610
611 /// Fetch from a *named* remote (git `fetch <remote>` / jj
612 /// `git fetch --remote <remote>`). Transient network failures are retried by
613 /// the underlying client.
614 pub async fn fetch_from(&self, remote: &str) -> Result<()> {
615 match &self.backend {
616 Backend::Git(g) => git_backend::fetch_from(g, &self.cwd, remote).await,
617 Backend::Jj(j) => jj_backend::fetch_from(j, &self.cwd, remote).await,
618 }
619 }
620
621 /// Fetch a single branch/bookmark from `origin` into its remote-tracking ref
622 /// (git `fetch_branch` / jj `git fetch -b`). Transient network failures
623 /// are retried by the underlying client.
624 pub async fn fetch_branch(&self, branch: &str) -> Result<()> {
625 match &self.backend {
626 Backend::Git(g) => git_backend::fetch_branch(g, &self.cwd, branch).await,
627 Backend::Jj(j) => jj_backend::fetch_branch(j, &self.cwd, branch).await,
628 }
629 }
630
631 /// Push `branch` to `origin` (git `push -u origin <branch>` / jj
632 /// `git push -b <branch>`).
633 ///
634 /// The branch (jj: bookmark) must already exist locally. The two backends
635 /// honestly differ in what "push" means: git pushes the *ref* and records
636 /// the upstream (`-u`; idempotent on repeat pushes), while jj pushes the
637 /// *bookmark's state* — including deleting the remote branch if the
638 /// bookmark was deleted locally. Renamed refspecs (`local:remote`) and
639 /// non-`origin` remotes are git-only concepts; use the
640 /// [`git()`](Repo::git) escape hatch ([`vcs_git::GitPush`]) for those.
641 pub async fn push(&self, branch: &str) -> Result<()> {
642 match &self.backend {
643 Backend::Git(g) => git_backend::push(g, &self.cwd, branch).await,
644 Backend::Jj(j) => jj_backend::push(j, &self.cwd, branch).await,
645 }
646 }
647
648 /// Switch the working copy to `reference` (git `checkout` / jj `edit`).
649 ///
650 /// ⚠ **Backend divergence — this is not "detach and build on top" on jj.** On
651 /// **git**, a subsequent commit *appends* on top of `reference` (its tip is
652 /// untouched). On **jj**, `checkout` maps to `jj edit`, which makes `reference`'s
653 /// commit *itself* the working-copy change — so a following
654 /// [`commit_paths`](Repo::commit_paths) (or any edit) **rewrites that commit in
655 /// place** (a new change-id, a replaced
656 /// description), silently amending a possibly-already-pushed commit rather than
657 /// adding a new one.
658 ///
659 /// So backend-agnostic "start fresh work on top of `main`" code must **not** rely
660 /// on `checkout` alone. If you want git-like append-on-top semantics on both
661 /// backends, start a new child change explicitly — on jj that is `jj new
662 /// <reference>` via the raw [`jj`](Repo::jj) client (a first-class `new_child`
663 /// facade primitive is planned); on git, `checkout` already appends.
664 pub async fn checkout(&self, reference: &str) -> Result<()> {
665 match &self.backend {
666 Backend::Git(g) => git_backend::checkout(g, &self.cwd, reference).await,
667 Backend::Jj(j) => jj_backend::checkout(j, &self.cwd, reference).await,
668 }
669 }
670
671 /// Rebase the current work onto `onto` (git `rebase` / jj `rebase -d`). The
672 /// `onto` is a branch/bookmark name or revision the backend understands.
673 pub async fn rebase(&self, onto: &str) -> Result<()> {
674 match &self.backend {
675 Backend::Git(g) => git_backend::rebase(g, &self.cwd, onto).await,
676 Backend::Jj(j) => jj_backend::rebase(j, &self.cwd, onto).await,
677 }
678 }
679
680 /// Probe whether merging `source` into the current work would conflict,
681 /// **without leaving any trace**: the probe is rolled back before returning
682 /// (git: `merge --no-commit --no-ff` then `merge --abort`; jj: a merge
683 /// change probed and undone via `op restore`).
684 ///
685 /// Preconditions/behaviour:
686 /// - git: requires a clean-enough working tree — a dirty-tree refusal
687 /// propagates as a plain error, not as [`MergeProbe::Conflicts`].
688 /// - A failing rollback **propagates as an error** rather than returning a
689 /// result that misdescribes the on-disk state.
690 /// - **Cancellation caveat:** the rollback runs on the same client, so if the
691 /// client carries a `default_cancel_on` token (the `cancellation` feature)
692 /// that fires during the probe, the rollback command is cancelled too and the
693 /// probe change may be left behind (`Error::Cancelled` surfaces). Re-probe and
694 /// reset with an un-cancelled client if you need a clean tree.
695 pub async fn try_merge(&self, source: &str) -> Result<MergeProbe> {
696 match &self.backend {
697 Backend::Git(g) => git_backend::try_merge(g, &self.cwd, source).await,
698 Backend::Jj(j) => jj_backend::try_merge(j, &self.cwd, source).await,
699 }
700 }
701
702 /// Abort the in-progress operation, if any (git: `merge --abort` /
703 /// `rebase --abort`; jj: a no-op — there are no paused operations, roll back
704 /// explicitly via `Jj::transaction` / `op_restore`). Returns the fresh
705 /// *post-call* [`OperationState`]; `Clear` when nothing was (or remains) in
706 /// progress.
707 pub async fn abort_in_progress(&self) -> Result<OperationState> {
708 match &self.backend {
709 Backend::Git(g) => git_backend::abort_in_progress(g, &self.cwd).await,
710 Backend::Jj(j) => jj_backend::abort_in_progress(j, &self.cwd).await,
711 }
712 }
713
714 /// Continue the in-progress operation after conflict resolution (git:
715 /// `commit --no-edit` for a merge / `rebase --continue`; jj: a no-op —
716 /// resolving the files *is* the continuation). Returns the fresh *post-call*
717 /// [`OperationState`]:
718 /// - `Conflict` when unresolved paths still block continuing (also on git —
719 /// unlike [`in_progress_state`](Self::in_progress_state), this method
720 /// *does* report `Conflict` for git), or when a continued rebase stops on
721 /// the next patch's conflict.
722 /// - `Clear` when the operation finished.
723 pub async fn continue_in_progress(&self) -> Result<OperationState> {
724 match &self.backend {
725 Backend::Git(g) => git_backend::continue_in_progress(g, &self.cwd).await,
726 Backend::Jj(j) => jj_backend::continue_in_progress(j, &self.cwd).await,
727 }
728 }
729
730 /// Whether the working copy is mid-operation or conflicted — see
731 /// [`OperationState`]. Lets a caller decide between abort/continue without
732 /// knowing the backend's model. Note the asymmetry: *this method* reports
733 /// `Merge`/`Rebase` (never `Conflict`) on git — a git conflict *is* that
734 /// paused state, and the conflict itself surfaces on the failed op via
735 /// [`Error::is_merge_conflict`] (or as `Conflict` from
736 /// [`continue_in_progress`](Self::continue_in_progress)) — while jj has no
737 /// paused op and reports `Conflict` directly.
738 pub async fn in_progress_state(&self) -> Result<OperationState> {
739 match &self.backend {
740 Backend::Git(g) => git_backend::in_progress_state(g, &self.cwd).await,
741 Backend::Jj(j) => jj_backend::in_progress_state(j, &self.cwd).await,
742 }
743 }
744
745 /// List attached worktrees (git) / workspaces (jj).
746 pub async fn list_worktrees(&self) -> Result<Vec<WorktreeInfo>> {
747 match &self.backend {
748 Backend::Git(g) => git_backend::list_worktrees(g, &self.cwd).await,
749 Backend::Jj(j) => jj_backend::list_worktrees(j, &self.cwd).await,
750 }
751 }
752
753 /// Create a worktree/workspace at `path` on a **new** `branch` based on
754 /// `base`. Always [`CreateOutcome::Plain`]; a copy-on-write strategy stays in
755 /// the consumer.
756 ///
757 /// `branch` must not already exist. The jj path is two steps (`workspace add`
758 /// then `bookmark create`) and is not atomic, but a failed bookmark step
759 /// **rolls back**: the workspace directory is removed only when `workspace add`
760 /// created it (a pre-existing directory the caller already had is left intact),
761 /// the workspace is forgotten best-effort, and the original error is surfaced —
762 /// so a failed call doesn't leak a half-made worktree.
763 pub async fn create_worktree(
764 &self,
765 path: &Path,
766 branch: &str,
767 base: &str,
768 ) -> Result<CreateOutcome> {
769 match &self.backend {
770 Backend::Git(g) => git_backend::create_worktree(g, &self.cwd, path, branch, base).await,
771 Backend::Jj(j) => jj_backend::create_worktree(j, &self.cwd, path, branch, base).await,
772 }
773 }
774
775 /// Remove the worktree/workspace at `path`. For jj this resolves the
776 /// workspace name by matching `path`, deletes the directory, then forgets it;
777 /// a `path` that matches no attached jj workspace returns
778 /// [`Error::WorktreeNotFound`]. (For the best-effort, never-erroring variant,
779 /// see [`cleanup_worktree_blocking`](Self::cleanup_worktree_blocking).)
780 ///
781 /// The [`WorktreeRemove`] spec's [`force`](WorktreeRemove::force) mirrors git's
782 /// `worktree remove`: without it a worktree that still has **uncommitted changes**
783 /// is refused (`Err`) rather than deleted, so a stray edit isn't silently lost —
784 /// build `WorktreeRemove::new(path).force()` to remove it anyway. On **jj** the
785 /// changes are snapshotted into the op log before the check, so a refusal keeps
786 /// them recoverable; note that checking spawns a jj command in the target
787 /// workspace, so a genuinely stale working copy can surface an error without
788 /// `force` (use `.force()` there). The repository's **main** workspace is always
789 /// refused (it can't be removed without destroying the repo), regardless of `force`.
790 pub async fn remove_worktree(&self, spec: WorktreeRemove) -> Result<()> {
791 match &self.backend {
792 Backend::Git(g) => {
793 git_backend::remove_worktree(g, &self.cwd, &spec.path, spec.force).await
794 }
795 Backend::Jj(j) => {
796 jj_backend::remove_worktree(j, &self.cwd, &spec.path, spec.force).await
797 }
798 }
799 }
800
801 /// **Synchronous** worktree cleanup for a context that cannot `.await` —
802 /// chiefly a `Drop` guard. Force-removes the worktree at `path` (git:
803 /// `worktree remove --force`; jj: resolve the workspace name by `path`, delete
804 /// the directory, then `workspace forget`). Best-effort and short-lived: it
805 /// shells out directly (no job-containment); a jj `path` that matches no
806 /// workspace is a no-op (`Ok`). Like the async
807 /// [`remove_worktree`](Self::remove_worktree), it **refuses the repository's
808 /// main workspace** (whose directory is the main working copy) — deleting it
809 /// would wipe the repo — even on this force-by-contract path.
810 pub fn cleanup_worktree_blocking(&self, path: &Path) -> Result<()> {
811 match &self.backend {
812 Backend::Git(_) => {
813 vcs_git::blocking::worktree_remove(&self.cwd, path, true).map_err(Error::Io)
814 }
815 Backend::Jj(_) => {
816 // jj resolves a relative worktree path against the repo dir (its
817 // cwd), so resolve it the same way here — the lookup and the dir
818 // removal must target the location jj used, not one under the process
819 // cwd (which may differ from `self.cwd`).
820 let abs_path = self.cwd.join(path);
821 match vcs_jj::blocking::workspace_name_for_path(&self.cwd, &abs_path) {
822 Some(name) => {
823 // Same main-workspace guard as the async `remove_worktree`
824 // (jj_backend.rs): never `remove_dir_all` the repository's
825 // main working copy — its directory owns the object store, so
826 // deleting it wipes the whole repo. The `default` name and the
827 // store-owning `.jj/repo` *directory* (a secondary's is a file
828 // pointer) both flag it, so a `jj workspace rename` can't
829 // bypass it. Force is implied on this Drop path, but this guard
830 // is unconditional — a repo-wipe is never the intent.
831 if name == "default" || abs_path.join(".jj").join("repo").is_dir() {
832 return Err(Error::Io(std::io::Error::new(
833 std::io::ErrorKind::InvalidInput,
834 "refusing to remove the repository's main workspace",
835 )));
836 }
837 // Delete the on-disk dir first (jj `forget` leaves it), then
838 // drop jj's record of the workspace.
839 let _ = std::fs::remove_dir_all(&abs_path);
840 vcs_jj::blocking::workspace_forget(&self.cwd, &name).map_err(Error::Io)
841 }
842 None => Ok(()),
843 }
844 }
845 }
846 }
847}
848
849/// Generate a facade trait from one signature table: the `#[async_trait]` trait
850/// declaration *and* the delegating `impl … for $Ty<R>`, so the two can never drift
851/// out of sync (a hazard when each is hand-maintained). Every generated body is a
852/// trivial delegation to the like-named inherent method — which method resolution
853/// prefers, so this never recurses; the real backend-`match` dispatch stays
854/// hand-written on the inherent `impl`. `async` methods doc-link to their inherent
855/// twin; `sync` methods carry an explicit doc string (their docs aren't uniform).
856///
857/// A near-identical copy lives in `vcs-forge`; the two are deliberately not shared
858/// (separate crates, ~40-line macro — duplication beats a new dependency).
859///
860/// Signatures only: each entry is a bare `&self` (or sync) method — no method-level
861/// generics, no `&mut self`, no default bodies (a new method shaped that way needs a
862/// grammar tweak, not just a table row).
863///
864/// No `mockall::automock`: a Wave-S spike proved it can't process a trait whose
865/// signatures come from `macro_rules!`. Captured `$_:ty` fragments reach `automock`
866/// as opaque nonterminal token groups; its `syn` parser rejects them ("unsupported
867/// type in this position"), whereas `#[async_trait]` tolerates them. So the facade
868/// traits stay test-seam-tested (build a handle over a fake runner — see the trait
869/// docs), which is also what their docs already recommend over mocking.
870macro_rules! facade_trait {
871 (
872 $(#[doc = $tdoc:expr])*
873 trait $Trait:ident for $Ty:ident;
874 sync {
875 $( #[doc = $sdoc:expr] fn $sn:ident( $($sa:ident: $sat:ty),* $(,)? ) -> $sr:ty; )*
876 }
877 async {
878 $( fn $an:ident( $($aa:ident: $aat:ty),* $(,)? ) -> $ar:ty; )*
879 }
880 ) => {
881 $(#[doc = $tdoc])*
882 #[async_trait::async_trait]
883 pub trait $Trait: Send + Sync {
884 $(
885 #[doc = $sdoc]
886 fn $sn(&self, $($sa: $sat),*) -> $sr;
887 )*
888 $(
889 #[doc = concat!("See [`", stringify!($Ty), "::", stringify!($an), "`].")]
890 async fn $an(&self, $($aa: $aat),*) -> $ar;
891 )*
892 }
893
894 // Delegates to the inherent methods, which method resolution prefers — so
895 // these bodies dispatch through the concrete type's real implementations,
896 // not back into the trait.
897 #[async_trait::async_trait]
898 impl<R: ProcessRunner> $Trait for $Ty<R> {
899 $(
900 fn $sn(&self, $($sa: $sat),*) -> $sr {
901 self.$sn($($sa),*)
902 }
903 )*
904 $(
905 async fn $an(&self, $($aa: $aat),*) -> $ar {
906 self.$an($($aa),*).await
907 }
908 )*
909 }
910 };
911}
912
913facade_trait! {
914 /// The backend-agnostic common surface of [`Repo`], as a trait — so a consumer can
915 /// hold a `Box<dyn VcsRepo>` / `&dyn VcsRepo` and code against the operations
916 /// without naming the [`ProcessRunner`] generic or wrapping `Repo` themselves.
917 ///
918 /// Every method mirrors the like-named inherent method on [`Repo`]; the trait adds
919 /// nothing but the abstraction boundary. Tool-specific operations stay off it (see
920 /// the crate docs) — reach those through the concrete [`Repo`] and its bound
921 /// handles. For hermetic tests, build a `Repo` over a fake runner with
922 /// [`Repo::from_git`] / [`Repo::from_jj`] rather than mocking this trait.
923 trait VcsRepo for Repo;
924 sync {
925 #[doc = "Which backend drives this handle."]
926 fn kind() -> BackendKind;
927 #[doc = "The repository root detected at open time."]
928 fn root() -> &Path;
929 #[doc = "The directory operations run against."]
930 fn cwd() -> &Path;
931 #[doc = "See [`Repo::cleanup_worktree_blocking`]."]
932 fn cleanup_worktree_blocking(path: &Path) -> Result<()>;
933 }
934 async {
935 fn current_branch() -> Result<Option<String>>;
936 fn trunk() -> Result<Option<String>>;
937 fn local_branches() -> Result<Vec<String>>;
938 fn branch_exists(name: &str) -> Result<bool>;
939 fn has_uncommitted_changes() -> Result<bool>;
940 fn has_tracked_changes() -> Result<bool>;
941 fn conflicted_files() -> Result<Vec<String>>;
942 fn delete_branch(spec: BranchDelete) -> Result<()>;
943 fn rename_branch(old: &str, new: &str) -> Result<()>;
944 fn changed_files() -> Result<Vec<FileChange>>;
945 fn diff_stat() -> Result<DiffStat>;
946 fn snapshot() -> Result<RepoSnapshot>;
947 fn commit_paths(paths: &[String], message: &str) -> Result<()>;
948 fn fetch() -> Result<()>;
949 fn fetch_from(remote: &str) -> Result<()>;
950 fn fetch_branch(branch: &str) -> Result<()>;
951 fn push(branch: &str) -> Result<()>;
952 fn checkout(reference: &str) -> Result<()>;
953 fn rebase(onto: &str) -> Result<()>;
954 fn try_merge(source: &str) -> Result<MergeProbe>;
955 fn abort_in_progress() -> Result<OperationState>;
956 fn continue_in_progress() -> Result<OperationState>;
957 fn in_progress_state() -> Result<OperationState>;
958 fn list_worktrees() -> Result<Vec<WorktreeInfo>>;
959 fn create_worktree(path: &Path, branch: &str, base: &str) -> Result<CreateOutcome>;
960 fn remove_worktree(spec: WorktreeRemove) -> Result<()>;
961 }
962}
963
964#[cfg(test)]
965mod tests {
966 use super::*;
967 use processkit::testing::{Reply, ScriptedRunner};
968 // The shared sandbox fixture — a unique temp dir removed on drop. Using the
969 // testkit's one impl instead of a private copy means the wrappers/facades
970 // don't each carry a fixture that could drift.
971 use vcs_testkit::TempDir;
972
973 // --- detect ------------------------------------------------------------
974
975 #[test]
976 fn detect_finds_git_and_jj_and_prefers_jj() {
977 let tmp = TempDir::new("detect");
978 let root = tmp.path();
979
980 // Plain git repo.
981 std::fs::create_dir_all(root.join(".git")).unwrap();
982 let located = detect(root).expect("git detected");
983 assert_eq!(located.kind, BackendKind::Git);
984 assert_eq!(located.root, root);
985
986 // Colocated: adding a *valid* .jj (with its `repo` store) makes jj win.
987 std::fs::create_dir_all(root.join(".jj").join("repo")).unwrap();
988 assert_eq!(detect(root).unwrap().kind, BackendKind::Jj);
989 }
990
991 // M19: a stray/empty `.jj` directory (no `repo` store — e.g. a leftover
992 // `mkdir .jj`) is NOT a jj marker and must not shadow a healthy `.git` repo in the
993 // same directory. A valid `.jj` (with `repo`, dir or file) still wins.
994 #[test]
995 fn detect_ignores_a_dotjj_without_a_repo_store() {
996 let tmp = TempDir::new("stray-jj");
997 let root = tmp.path();
998 std::fs::create_dir_all(root.join(".git")).unwrap();
999 std::fs::create_dir_all(root.join(".jj")).unwrap(); // empty — no `repo`
1000 assert_eq!(
1001 detect(root).expect("git still detected").kind,
1002 BackendKind::Git,
1003 "an empty .jj must not shadow a real .git"
1004 );
1005
1006 // A secondary workspace's `.jj/repo` is a *file* pointer — still valid.
1007 let sec = TempDir::new("jj-secondary");
1008 std::fs::create_dir_all(sec.path().join(".jj")).unwrap();
1009 std::fs::write(sec.path().join(".jj").join("repo"), b"/path/to/store\n").unwrap();
1010 assert_eq!(detect(sec.path()).unwrap().kind, BackendKind::Jj);
1011 }
1012
1013 #[test]
1014 fn detect_walks_up_to_ancestor() {
1015 let tmp = TempDir::new("walkup");
1016 let root = tmp.path();
1017 std::fs::create_dir_all(root.join(".git")).unwrap();
1018 let nested = root.join("a").join("b");
1019 std::fs::create_dir_all(&nested).unwrap();
1020 let located = detect(&nested).expect("found via ancestor walk");
1021 assert_eq!(located.kind, BackendKind::Git);
1022 assert_eq!(located.root, root);
1023 }
1024
1025 #[test]
1026 fn detect_returns_none_outside_repo() {
1027 let tmp = TempDir::new("norepo");
1028 assert!(detect(tmp.path()).is_none());
1029 }
1030
1031 // A gitlink `.git` *file* (a linked worktree / submodule) is a valid git marker;
1032 // a stray file merely named `.git` is NOT — so it can't shadow a real repo above.
1033 #[test]
1034 fn detect_validates_dotgit_file_is_a_gitlink() {
1035 let tmp = TempDir::new("gitlink");
1036 let root = tmp.path();
1037
1038 // A gitlink file → detected as a git repo at this dir.
1039 std::fs::write(root.join(".git"), "gitdir: /somewhere/.git/worktrees/wt\n").unwrap();
1040 assert_eq!(
1041 detect(root).expect("gitlink detected").kind,
1042 BackendKind::Git
1043 );
1044
1045 // A garbage file named `.git` (not a gitlink) is rejected — and must NOT
1046 // shadow a real `.git` directory in the parent.
1047 let parent = TempDir::new("gitlink-parent");
1048 std::fs::create_dir_all(parent.path().join(".git")).unwrap();
1049 let child = parent.path().join("sub");
1050 std::fs::create_dir_all(&child).unwrap();
1051 std::fs::write(child.join(".git"), "not a gitlink, just noise\n").unwrap();
1052 let located = detect(&child).expect("walks up past the bogus .git file");
1053 assert_eq!(located.root, parent.path(), "the real repo is the parent");
1054
1055 // An empty `.git` file is not a marker.
1056 let empty = TempDir::new("gitlink-empty");
1057 std::fs::write(empty.path().join(".git"), "").unwrap();
1058 assert!(detect(empty.path()).is_none(), "empty .git is not a repo");
1059
1060 // Leading whitespace before `gitdir:` is tolerated (the `trim_start`).
1061 let spaced = TempDir::new("gitlink-spaced");
1062 std::fs::write(
1063 spaced.path().join(".git"),
1064 " gitdir: /x/.git/worktrees/w\n",
1065 )
1066 .unwrap();
1067 assert_eq!(
1068 detect(spaced.path()).expect("spaced gitlink detected").kind,
1069 BackendKind::Git
1070 );
1071 }
1072
1073 // --- dispatch (hermetic, ScriptedRunner-backed) ------------------------
1074
1075 fn git_repo(runner: ScriptedRunner) -> Repo<ScriptedRunner> {
1076 Repo::from_git("/repo", "/repo", Git::with_runner(runner))
1077 }
1078
1079 fn jj_repo(runner: ScriptedRunner) -> Repo<ScriptedRunner> {
1080 Repo::from_jj("/repo", "/repo", Jj::with_runner(runner))
1081 }
1082
1083 // --- snapshot ----------------------------------------------------------
1084
1085 // git: one porcelain-v2 call + a git-dir probe → a combined RepoSnapshot.
1086 #[tokio::test]
1087 async fn git_snapshot_combines_v2_status_and_op_state() {
1088 let v2 = concat!(
1089 "# branch.oid abc123\0",
1090 "# branch.head main\0",
1091 "# branch.upstream origin/main\0",
1092 "# branch.ab +2 -0\0",
1093 "1 .M N... 100644 100644 100644 1 2 a.rs\0",
1094 "? new.txt\0",
1095 );
1096 // An empty git dir → no MERGE_HEAD / rebase dir → Clear.
1097 let gitdir = TempDir::new("snap-git");
1098 let repo = git_repo(
1099 ScriptedRunner::new()
1100 .on(["git", "status", "--porcelain=v2"], Reply::ok(v2))
1101 .on(
1102 ["git", "rev-parse", "--git-dir"],
1103 Reply::ok(gitdir.path().to_str().unwrap()),
1104 ),
1105 );
1106 let s = repo.snapshot().await.unwrap();
1107 assert_eq!(s.branch.as_deref(), Some("main"));
1108 let tracking = s.tracking.as_ref().expect("upstream tracking");
1109 assert_eq!(tracking.branch, "origin/main");
1110 assert_eq!((tracking.ahead, tracking.behind), (Some(2), Some(0)));
1111 assert!(s.dirty);
1112 assert_eq!(s.change_count, 2, "1 tracked + 1 untracked");
1113 assert!(!s.conflicted);
1114 assert_eq!(s.operation, OperationState::Clear);
1115 }
1116
1117 // M20 (whole-solution): `snapshot()` has its OWN operation probe (separate from
1118 // `in_progress_state`); it too must report a `git am` as `ApplyMailbox`, not
1119 // `Rebase` — otherwise the new variant is dead on the snapshot → watch → mcp path.
1120 #[tokio::test]
1121 async fn git_snapshot_reports_git_am_as_apply_mailbox() {
1122 let v2 = concat!("# branch.oid abc\0", "# branch.head main\0");
1123 let gitdir = TempDir::new("snap-git-am");
1124 // A `git am` in progress: `rebase-apply/` WITH the `applying` marker.
1125 let apply = gitdir.path().join("rebase-apply");
1126 std::fs::create_dir_all(&apply).unwrap();
1127 std::fs::write(apply.join("applying"), b"").unwrap();
1128 let repo = git_repo(
1129 ScriptedRunner::new()
1130 .on(["git", "status", "--porcelain=v2"], Reply::ok(v2))
1131 .on(
1132 ["git", "rev-parse", "--git-dir"],
1133 Reply::ok(gitdir.path().to_str().unwrap()),
1134 ),
1135 );
1136 let s = repo.snapshot().await.unwrap();
1137 assert_eq!(
1138 s.operation,
1139 OperationState::ApplyMailbox,
1140 "a git am must not read as Rebase in snapshot()"
1141 );
1142 }
1143
1144 // git with NO upstream configured: porcelain v2 omits the `# branch.upstream`
1145 // and `# branch.ab` lines, so `tracking` is None (the all-or-nothing invariant —
1146 // git is the only backend that can produce either) — mirrors the jj None case.
1147 #[tokio::test]
1148 async fn git_snapshot_without_upstream_has_no_tracking() {
1149 let v2 = concat!("# branch.oid abc123\0", "# branch.head main\0");
1150 let gitdir = TempDir::new("snap-git-noup");
1151 let repo = git_repo(
1152 ScriptedRunner::new()
1153 .on(["git", "status", "--porcelain=v2"], Reply::ok(v2))
1154 .on(
1155 ["git", "rev-parse", "--git-dir"],
1156 Reply::ok(gitdir.path().to_str().unwrap()),
1157 ),
1158 );
1159 let s = repo.snapshot().await.unwrap();
1160 assert_eq!(s.branch.as_deref(), Some("main"));
1161 assert!(s.tracking.is_none(), "no upstream → no tracking");
1162 }
1163
1164 // M17: an upstream that is SET but GONE (deleted on the remote, or not yet
1165 // fetched) — porcelain v2 emits `# branch.upstream` but OMITS `# branch.ab`, so the
1166 // counts are uncountable. `tracking` must be `Some { branch, ahead: None, behind:
1167 // None }` (tracking configured but uncountable), NOT a fabricated in-sync `0`/`0`.
1168 #[tokio::test]
1169 async fn git_snapshot_upstream_set_but_gone_is_uncountable() {
1170 let v2 = concat!(
1171 "# branch.oid abc123\0",
1172 "# branch.head main\0",
1173 "# branch.upstream origin/main\0", // upstream named…
1174 // …but no `# branch.ab` line — it doesn't resolve.
1175 );
1176 let gitdir = TempDir::new("snap-git-gone");
1177 let repo = git_repo(
1178 ScriptedRunner::new()
1179 .on(["git", "status", "--porcelain=v2"], Reply::ok(v2))
1180 .on(
1181 ["git", "rev-parse", "--git-dir"],
1182 Reply::ok(gitdir.path().to_str().unwrap()),
1183 ),
1184 );
1185 let s = repo.snapshot().await.unwrap();
1186 let tracking = s.tracking.as_ref().expect("upstream is set");
1187 assert_eq!(tracking.branch, "origin/main");
1188 assert_eq!(
1189 (tracking.ahead, tracking.behind),
1190 (None, None),
1191 "a gone upstream is uncountable, not in-sync 0/0"
1192 );
1193 }
1194
1195 // jj: one template row + a status count; a conflicted @ maps to Conflict; no
1196 // git-style upstream/ahead/behind.
1197 #[tokio::test]
1198 async fn jj_snapshot_dirty_with_change_count() {
1199 let repo = jj_repo(
1200 ScriptedRunner::new()
1201 // snapshot template (`jj log -r @`): commit_id \t empty \t conflict
1202 .on(["jj", "log", "-r", "@"], Reply::ok("deadbeef\t0\t1\n")) // empty=0 dirty, conflict=1
1203 // `branch` via `current_branch` → `reachable_bookmarks`
1204 // (`jj log -r heads(::@ & bookmarks())`): bookmarks \t commit
1205 .on(
1206 ["jj", "log", "-r", "heads(::@ & bookmarks())"],
1207 Reply::ok("main\tdeadbeef\n"),
1208 )
1209 .on(["jj", "diff"], Reply::ok("M a.rs\nA b.rs\n")), // status -r @ --summary → 2
1210 );
1211 let s = repo.snapshot().await.unwrap();
1212 assert_eq!(s.head.as_deref(), Some("deadbeef"));
1213 assert_eq!(s.branch.as_deref(), Some("main"));
1214 assert!(s.dirty);
1215 assert_eq!(s.change_count, 2);
1216 assert!(s.conflicted);
1217 assert_eq!(s.operation, OperationState::Conflict);
1218 assert!(s.tracking.is_none(), "jj has no upstream tracking");
1219 }
1220
1221 // jj: a clean `@` (empty=1) skips the change-count spawn entirely — the test
1222 // scripts NO `diff` rule, so calling `status` would error.
1223 #[tokio::test]
1224 async fn jj_snapshot_clean_skips_change_count() {
1225 let repo = jj_repo(
1226 ScriptedRunner::new()
1227 .on(["jj", "log", "-r", "@"], Reply::ok("c0ffee\t1\t0\n"))
1228 .on(
1229 ["jj", "log", "-r", "heads(::@ & bookmarks())"],
1230 Reply::ok(""),
1231 ),
1232 );
1233 let s = repo.snapshot().await.unwrap();
1234 assert_eq!(s.head.as_deref(), Some("c0ffee"));
1235 assert_eq!(s.branch, None, "no bookmark");
1236 assert!(!s.dirty);
1237 assert_eq!(s.change_count, 0);
1238 assert!(!s.conflicted);
1239 assert_eq!(s.operation, OperationState::Clear);
1240 }
1241
1242 // jj: a conflicted `@` that jj marks `empty` (conflict but no net content change)
1243 // is still reported `dirty` — the conflict is uncommitted state needing
1244 // resolution — so the count runs and the snapshot is coherent (no
1245 // `conflicted: true` next to `dirty: false`), mirroring git's conflict handling.
1246 #[tokio::test]
1247 async fn jj_snapshot_conflicted_empty_change_is_dirty() {
1248 let repo = jj_repo(
1249 ScriptedRunner::new()
1250 .on(["jj", "log", "-r", "@"], Reply::ok("c0ffee\t1\t1\n")) // empty=1, conflict=1
1251 .on(
1252 ["jj", "log", "-r", "heads(::@ & bookmarks())"],
1253 Reply::ok(""),
1254 ) // no bookmark
1255 .on(["jj", "diff"], Reply::ok("M conflicted.rs\n")), // status → 1
1256 );
1257 let s = repo.snapshot().await.unwrap();
1258 assert!(s.conflicted);
1259 assert!(s.dirty, "a conflicted change is a dirty working copy");
1260 assert_eq!(s.change_count, 1);
1261 assert_eq!(s.operation, OperationState::Conflict);
1262 }
1263
1264 // jj `list_worktrees` resolves each workspace's root via the batched
1265 // `workspace_roots` fan-out (one `workspace root --name <n>` per `workspace
1266 // list` row), then builds a `WorktreeInfo` per workspace. Hermetic: scripts the
1267 // template rows + the per-name root replies — the backend glue that the
1268 // `#[ignore]` integration tests otherwise cover only with a real `jj`.
1269 #[tokio::test]
1270 async fn jj_list_worktrees_batches_root_lookups() {
1271 let repo = jj_repo(
1272 ScriptedRunner::new()
1273 .on(
1274 ["jj", "workspace", "list"],
1275 Reply::ok("default\tc0ffee\tmain\nws1\tdecaf0\t\n"),
1276 )
1277 .on(
1278 ["jj", "workspace", "root", "--name", "default"],
1279 Reply::ok("/repo\n"),
1280 )
1281 .on(
1282 ["jj", "workspace", "root", "--name", "ws1"],
1283 Reply::ok("/repo/ws1\n"),
1284 ),
1285 );
1286 let worktrees = repo.list_worktrees().await.expect("list_worktrees");
1287 assert_eq!(worktrees.len(), 2);
1288 assert_eq!(worktrees[0].path, Path::new("/repo"));
1289 assert_eq!(worktrees[0].branch.as_deref(), Some("main"));
1290 assert_eq!(worktrees[1].path, Path::new("/repo/ws1"));
1291 assert_eq!(worktrees[1].branch, None);
1292 }
1293
1294 // A workspace whose `workspace root` lookup errors is skipped (no useful path),
1295 // mirroring the old sequential loop — the batch maps that slot to `Err`.
1296 #[tokio::test]
1297 async fn jj_list_worktrees_skips_unresolvable_root() {
1298 let repo = jj_repo(
1299 ScriptedRunner::new()
1300 .on(
1301 ["jj", "workspace", "list"],
1302 Reply::ok("default\tc0ffee\tmain\ngone\tdecaf0\t\n"),
1303 )
1304 .on(
1305 ["jj", "workspace", "root", "--name", "default"],
1306 Reply::ok("/repo\n"),
1307 )
1308 .on(
1309 ["jj", "workspace", "root", "--name", "gone"],
1310 Reply::fail(1, "Error: No such workspace"),
1311 ),
1312 );
1313 let worktrees = repo.list_worktrees().await.expect("list_worktrees");
1314 assert_eq!(worktrees.len(), 1, "the unresolvable workspace is skipped");
1315 assert_eq!(worktrees[0].path, Path::new("/repo"));
1316 }
1317
1318 // remove_worktree surfaces a `workspace forget` failure rather than swallowing
1319 // it — name resolution already proved the workspace is registered, so a forget
1320 // error is a real dangling-registration the caller should see.
1321 #[tokio::test]
1322 async fn jj_remove_worktree_surfaces_forget_error() {
1323 let repo = jj_repo(
1324 ScriptedRunner::new()
1325 .on(["jj", "workspace", "list"], Reply::ok("ws1\tc0ffee\t\n"))
1326 .on(
1327 ["jj", "workspace", "root", "--name", "ws1"],
1328 Reply::ok("/repo/ws1\n"),
1329 )
1330 .on(
1331 ["jj", "workspace", "forget"],
1332 Reply::fail(1, "Error: cannot forget workspace"),
1333 ),
1334 );
1335 // `/repo/ws1` does not exist on disk, so the dir-removal step is skipped and
1336 // the forget error is the sole outcome.
1337 let res = repo.remove_worktree(WorktreeRemove::new("/repo/ws1")).await;
1338 assert!(res.is_err(), "a forget failure is surfaced, not swallowed");
1339 }
1340
1341 // C1: the default workspace resolves at the repo root; removing it would wipe
1342 // the whole repository, so it is refused even with force = true and WITHOUT
1343 // running `workspace forget` (no such cassette rule — a miss would also error,
1344 // so we assert the *refusal* message to prove the guard, not a fallthrough).
1345 #[tokio::test]
1346 async fn jj_remove_worktree_refuses_the_main_workspace() {
1347 let repo = jj_repo(
1348 ScriptedRunner::new()
1349 .on(
1350 ["jj", "workspace", "list"],
1351 Reply::ok("default\tc0ffee\t\n"),
1352 )
1353 .on(
1354 ["jj", "workspace", "root", "--name", "default"],
1355 Reply::ok("/repo\n"),
1356 ),
1357 );
1358 let err = repo
1359 .remove_worktree(WorktreeRemove::new("/repo").force())
1360 .await
1361 .expect_err("the main workspace must be refused");
1362 assert!(
1363 err.to_string().contains("main workspace"),
1364 "refusal message, not a cassette miss: {err}"
1365 );
1366 }
1367
1368 // C1: a secondary workspace with un-snapshotted edits (`current_change` reports
1369 // non-empty) is refused under force = false, and its directory is NOT deleted.
1370 #[tokio::test]
1371 async fn jj_remove_worktree_refuses_dirty_workspace_without_force() {
1372 let tmp = TempDir::new("rmw-dirty");
1373 let root = tmp.path().to_string_lossy().into_owned();
1374 let repo = Repo::from_jj(
1375 &root,
1376 &root,
1377 Jj::with_runner(
1378 ScriptedRunner::new()
1379 .on(["jj", "workspace", "list"], Reply::ok("ws1\tc0ffee\t\n"))
1380 .on(
1381 ["jj", "workspace", "root", "--name", "ws1"],
1382 Reply::ok(format!("{root}\n")),
1383 )
1384 // `current_change` → 3rd field `false` = not empty = dirty.
1385 .on(["jj", "log"], Reply::ok("aaa\tbbb\tfalse\twork\n")),
1386 ),
1387 );
1388 let err = repo
1389 .remove_worktree(WorktreeRemove::new(tmp.path()))
1390 .await
1391 .expect_err("a dirty workspace must be refused without force");
1392 assert!(
1393 err.to_string().contains("uncommitted changes"),
1394 "refusal message: {err}"
1395 );
1396 assert!(
1397 tmp.path().exists(),
1398 "the workspace directory must survive a refusal"
1399 );
1400 }
1401
1402 // C1: force = true skips the dirty check and removes the directory (no
1403 // `current_change` rule is scripted, proving the check is bypassed).
1404 #[tokio::test]
1405 async fn jj_remove_worktree_with_force_removes_the_dir() {
1406 let tmp = TempDir::new("rmw-force");
1407 let ws = tmp.path().join("ws1");
1408 std::fs::create_dir_all(&ws).expect("mkdir ws");
1409 let root = tmp.path().to_string_lossy().into_owned();
1410 let ws_str = ws.to_string_lossy().into_owned();
1411 let repo = Repo::from_jj(
1412 &root,
1413 &root,
1414 Jj::with_runner(
1415 ScriptedRunner::new()
1416 .on(["jj", "workspace", "list"], Reply::ok("ws1\tc0ffee\t\n"))
1417 .on(
1418 ["jj", "workspace", "root", "--name", "ws1"],
1419 Reply::ok(format!("{ws_str}\n")),
1420 )
1421 .on(["jj", "workspace", "forget"], Reply::ok("")),
1422 ),
1423 );
1424 repo.remove_worktree(WorktreeRemove::new(ws.clone()).force())
1425 .await
1426 .expect("force removes a dirty worktree");
1427 assert!(!ws.exists(), "the worktree directory was removed");
1428 }
1429
1430 // C1: the main-workspace guard's store-directory branch — a workspace whose
1431 // name was changed away from `default` (via `jj workspace rename`) still owns
1432 // the object store (`.jj/repo` is a *directory*, not a secondary's file
1433 // pointer), so removal is refused even with force = true, and the dir survives.
1434 // Exercises the `|| .jj/repo.is_dir()` half of the guard (the name is not
1435 // `default`), which the name-based test can't reach.
1436 #[tokio::test]
1437 async fn jj_remove_worktree_refuses_renamed_store_owning_workspace() {
1438 let tmp = TempDir::new("rmw-store");
1439 std::fs::create_dir_all(tmp.path().join(".jj").join("repo")).expect("mk .jj/repo dir");
1440 let root = tmp.path().to_string_lossy().into_owned();
1441 let repo = Repo::from_jj(
1442 &root,
1443 &root,
1444 Jj::with_runner(
1445 ScriptedRunner::new()
1446 .on(["jj", "workspace", "list"], Reply::ok("mainws\tc0ffee\t\n"))
1447 .on(
1448 ["jj", "workspace", "root", "--name", "mainws"],
1449 Reply::ok(format!("{root}\n")),
1450 ),
1451 ),
1452 );
1453 let err = repo
1454 .remove_worktree(WorktreeRemove::new(tmp.path()).force())
1455 .await
1456 .expect_err("a renamed store-owning workspace is still refused");
1457 assert!(
1458 err.to_string().contains("main workspace"),
1459 "refusal message: {err}"
1460 );
1461 assert!(
1462 tmp.path().exists(),
1463 "the store-owning directory must not be deleted"
1464 );
1465 }
1466
1467 #[tokio::test]
1468 async fn kind_and_escape_hatches_reflect_backend() {
1469 let repo = git_repo(ScriptedRunner::new());
1470 assert_eq!(repo.kind(), BackendKind::Git);
1471 assert!(repo.git().is_some());
1472 assert!(repo.jj().is_none());
1473 }
1474
1475 // The cwd-bound views mirror the backend, and `at` re-binds them to another
1476 // directory without a separate client.
1477 #[tokio::test]
1478 async fn bound_views_reflect_backend_and_cwd() {
1479 let git = git_repo(ScriptedRunner::new());
1480 assert!(git.git_at().is_some());
1481 assert!(git.jj_at().is_none());
1482 // A sibling handle bound elsewhere yields a view rooted at that dir.
1483 assert_eq!(git.at("/repo/wt").cwd(), Path::new("/repo/wt"));
1484
1485 let jj = jj_repo(ScriptedRunner::new());
1486 assert!(jj.jj_at().is_some());
1487 assert!(jj.git_at().is_none());
1488 }
1489
1490 #[tokio::test]
1491 async fn current_branch_maps_detached_head_to_none() {
1492 // git's `current_branch` now runs `symbolic-ref --quiet --short HEAD`:
1493 // exit 0 → the branch name, exit 1 → detached HEAD → None.
1494 let named =
1495 git_repo(ScriptedRunner::new().on(["git", "symbolic-ref"], Reply::ok("main\n")));
1496 assert_eq!(
1497 named.current_branch().await.unwrap().as_deref(),
1498 Some("main")
1499 );
1500 let detached =
1501 git_repo(ScriptedRunner::new().on(["git", "symbolic-ref"], Reply::fail(1, "")));
1502 assert!(detached.current_branch().await.unwrap().is_none());
1503 }
1504
1505 #[tokio::test]
1506 async fn changed_files_maps_git_status() {
1507 let repo = git_repo(ScriptedRunner::new().on(
1508 ["git", "status"],
1509 Reply::ok(" M a.rs\0?? b.rs\0R new.rs\0old.rs\0"),
1510 ));
1511 let changes = repo.changed_files().await.unwrap();
1512 assert_eq!(changes.len(), 3);
1513 assert_eq!(changes[0].kind, ChangeKind::Modified);
1514 assert_eq!(changes[1].kind, ChangeKind::Added);
1515 assert_eq!(changes[2].kind, ChangeKind::Renamed);
1516 assert_eq!(changes[2].old_path.as_deref(), Some("old.rs"));
1517 }
1518
1519 #[tokio::test]
1520 async fn local_branches_maps_git_branch_output() {
1521 let repo =
1522 git_repo(ScriptedRunner::new().on(["git", "branch"], Reply::ok("* main\n feat\n")));
1523 assert_eq!(repo.local_branches().await.unwrap(), ["main", "feat"]);
1524 }
1525
1526 #[tokio::test]
1527 async fn branch_exists_reads_show_ref_exit() {
1528 let yes = git_repo(ScriptedRunner::new().on(["git", "show-ref"], Reply::ok("")));
1529 assert!(yes.branch_exists("main").await.unwrap());
1530 let no = git_repo(ScriptedRunner::new().on(["git", "show-ref"], Reply::fail(1, "")));
1531 assert!(!no.branch_exists("nope").await.unwrap());
1532 }
1533
1534 #[tokio::test]
1535 async fn has_uncommitted_changes_reflects_status() {
1536 let dirty = git_repo(ScriptedRunner::new().on(["git", "status"], Reply::ok(" M a.rs\0")));
1537 assert!(dirty.has_uncommitted_changes().await.unwrap());
1538 let clean = git_repo(ScriptedRunner::new().on(["git", "status"], Reply::ok("")));
1539 assert!(!clean.has_uncommitted_changes().await.unwrap());
1540 }
1541
1542 #[tokio::test]
1543 async fn at_rebinds_cwd_and_shares_backend() {
1544 let repo = git_repo(ScriptedRunner::new());
1545 let moved = repo.at("/repo/sub");
1546 assert_eq!(moved.cwd(), Path::new("/repo/sub"));
1547 assert_eq!(moved.root(), Path::new("/repo"));
1548 assert_eq!(moved.kind(), BackendKind::Git);
1549 }
1550
1551 // --- dispatch: jj backend (hermetic) -----------------------------------
1552
1553 #[tokio::test]
1554 async fn jj_kind_and_escape_hatches_reflect_backend() {
1555 let repo = jj_repo(ScriptedRunner::new());
1556 assert_eq!(repo.kind(), BackendKind::Jj);
1557 assert!(repo.jj().is_some() && repo.git().is_none());
1558 }
1559
1560 #[tokio::test]
1561 async fn jj_current_branch_reads_bookmark() {
1562 // current_branch derives from `reachable_bookmarks`, whose template is
1563 // `<bookmarks space-joined>\t<commit>` — distinct from the strict
1564 // `current_bookmark(@)` comma-joined template.
1565 let repo = jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("main\t53e4e879\n")));
1566 assert_eq!(
1567 repo.current_branch().await.unwrap().as_deref(),
1568 Some("main")
1569 );
1570 }
1571
1572 #[tokio::test]
1573 async fn jj_current_branch_persists_across_commit() {
1574 // After a jj commit the new working-copy change carries no bookmark, but
1575 // the described parent does. `reachable_bookmarks` resolves the nearest
1576 // bookmarked ancestor, so the facade still reports it — git-like "I'm
1577 // still on my branch". Under the old strict `current_bookmark(@)` rule
1578 // this returned `None`; feeding the reachable template (`feat\t…`,
1579 // unparseable as a comma-joined bookmark name) pins the new derivation.
1580 let repo = jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("feat\tc8d49332\n")));
1581 assert_eq!(
1582 repo.current_branch().await.unwrap().as_deref(),
1583 Some("feat")
1584 );
1585 }
1586
1587 #[tokio::test]
1588 async fn jj_current_branch_tie_break_is_deterministic() {
1589 // `heads(::@ & bookmarks())` can yield several equally-near bookmarks —
1590 // a merge of two bookmarked lines (one row each) or one commit carrying
1591 // several (one row, space-joined). current_branch returns the
1592 // lexicographically-smallest name regardless of jj's row order, so the
1593 // result is stable. Here: rows `zeta` then `alpha beta` ⇒ `alpha`.
1594 let repo = jj_repo(ScriptedRunner::new().on(
1595 ["jj", "log"],
1596 Reply::ok("zeta\tabc1234\nalpha beta\tdef5678\n"),
1597 ));
1598 assert_eq!(
1599 repo.current_branch().await.unwrap().as_deref(),
1600 Some("alpha")
1601 );
1602 }
1603
1604 #[tokio::test]
1605 async fn jj_local_branches_maps_bookmark_list() {
1606 // BOOKMARK_LIST_TEMPLATE rows: `name\t<commit>`.
1607 let repo = jj_repo(ScriptedRunner::new().on(
1608 ["jj", "bookmark", "list"],
1609 Reply::ok("main\tcmt\nfeat\tm2\n"),
1610 ));
1611 assert_eq!(repo.local_branches().await.unwrap(), ["main", "feat"]);
1612 }
1613
1614 #[tokio::test]
1615 async fn jj_branch_exists_scans_bookmarks() {
1616 let repo =
1617 jj_repo(ScriptedRunner::new().on(["jj", "bookmark", "list"], Reply::ok("main\tcmt\n")));
1618 assert!(repo.branch_exists("main").await.unwrap());
1619 let repo2 =
1620 jj_repo(ScriptedRunner::new().on(["jj", "bookmark", "list"], Reply::ok("main\tcmt\n")));
1621 assert!(!repo2.branch_exists("missing").await.unwrap());
1622 }
1623
1624 #[tokio::test]
1625 async fn jj_has_uncommitted_changes_reads_empty_flag() {
1626 // CHANGE_TEMPLATE row: change_id \t commit_id \t empty \t description
1627 let dirty =
1628 jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("kz\t38\tfalse\twip\n")));
1629 assert!(dirty.has_uncommitted_changes().await.unwrap());
1630 let clean = jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("kz\t38\ttrue\t\n")));
1631 assert!(!clean.has_uncommitted_changes().await.unwrap());
1632 }
1633
1634 // M18: a conflicted-but-**empty** `@` is uncommitted state (it needs resolution),
1635 // so `has_uncommitted_changes` returns true — agreeing with `snapshot().dirty`,
1636 // which already treats `conflict ⇒ dirty`. First `jj log` = current_change (empty),
1637 // second = is_conflicted (`"1"`).
1638 #[tokio::test]
1639 async fn jj_has_uncommitted_changes_true_when_conflicted_even_if_empty() {
1640 let repo = jj_repo(ScriptedRunner::new().on_sequence(
1641 ["jj", "log"],
1642 [
1643 Reply::ok("kz\t38\ttrue\t\n"), // current_change: empty = true
1644 Reply::ok("1\n"), // is_conflicted: conflicted
1645 ],
1646 ));
1647 assert!(
1648 repo.has_uncommitted_changes().await.unwrap(),
1649 "a conflicted empty @ is dirty"
1650 );
1651 }
1652
1653 #[tokio::test]
1654 async fn jj_changed_files_maps_diff_summary() {
1655 let repo = jj_repo(
1656 ScriptedRunner::new().on(["jj", "diff"], Reply::ok("M src/a.rs\nA b.rs\nD gone.rs\n")),
1657 );
1658 let changes = repo.changed_files().await.unwrap();
1659 assert_eq!(changes.len(), 3);
1660 assert_eq!(changes[0].kind, ChangeKind::Modified);
1661 assert_eq!(changes[1].kind, ChangeKind::Added);
1662 assert_eq!(changes[2].kind, ChangeKind::Deleted);
1663 assert!(changes.iter().all(|c| c.old_path.is_none()));
1664 }
1665
1666 // jj DOES supply the rename's original path (its `{old => new}` summary
1667 // form) — `old_path` is populated on both backends, as the DTO documents.
1668 #[tokio::test]
1669 async fn jj_changed_files_populates_rename_old_path() {
1670 let repo = jj_repo(
1671 ScriptedRunner::new().on(["jj", "diff"], Reply::ok("R src/{old.rs => new.rs}\n")),
1672 );
1673 let changes = repo.changed_files().await.unwrap();
1674 assert_eq!(changes.len(), 1);
1675 assert_eq!(changes[0].kind, ChangeKind::Renamed);
1676 assert_eq!(changes[0].path, "src/new.rs");
1677 assert_eq!(changes[0].old_path.as_deref(), Some("src/old.rs"));
1678 }
1679
1680 // `commit_paths(&[])` is refused up front on BOTH backends: the runners have
1681 // no rules, so reaching the CLI would error differently — the guard must trip
1682 // first (on jj an empty fileset would otherwise commit the whole working
1683 // copy; on git it would exit 128).
1684 #[tokio::test]
1685 async fn commit_paths_refuses_an_empty_path_set() {
1686 for repo in [
1687 git_repo(ScriptedRunner::new()),
1688 jj_repo(ScriptedRunner::new()),
1689 ] {
1690 let err = repo
1691 .commit_paths(&[], "msg")
1692 .await
1693 .expect_err("empty paths must be refused");
1694 assert!(
1695 err.to_string().contains("at least one path"),
1696 "unexpected error: {err}"
1697 );
1698 }
1699 }
1700
1701 #[tokio::test]
1702 async fn jj_rename_branch_builds_bookmark_rename() {
1703 use processkit::testing::RecordingRunner;
1704 let rec = RecordingRunner::replying(Reply::ok(""));
1705 let repo = Repo::from_jj("/repo", "/repo", Jj::with_runner(&rec));
1706 repo.rename_branch("old", "new").await.unwrap();
1707 assert_eq!(
1708 rec.only_call().args_str(),
1709 ["bookmark", "rename", "old", "new", "--color", "never"]
1710 );
1711 }
1712
1713 // The widened common surface dispatches `checkout` to each backend's verb:
1714 // git `checkout`, jj `edit`.
1715 #[tokio::test]
1716 async fn checkout_dispatches_per_backend() {
1717 use processkit::testing::RecordingRunner;
1718 let grec = RecordingRunner::replying(Reply::ok(""));
1719 Repo::from_git("/repo", "/repo", Git::with_runner(&grec))
1720 .checkout("feat")
1721 .await
1722 .unwrap();
1723 // Trailing `--` so a path-like ref can't fall into pathspec mode (C2).
1724 assert_eq!(grec.only_call().args_str(), ["checkout", "feat", "--"]);
1725
1726 let jrec = RecordingRunner::replying(Reply::ok(""));
1727 Repo::from_jj("/repo", "/repo", Jj::with_runner(&jrec))
1728 .checkout("feat")
1729 .await
1730 .unwrap();
1731 assert_eq!(
1732 jrec.only_call().args_str(),
1733 ["edit", "feat", "--color", "never"]
1734 );
1735 }
1736
1737 // A1: `delete_branch` takes a `BranchDelete` spec; `.force()` threads through to
1738 // git's `-D` (vs `-d`), and jj ignores it (its `bookmark delete` has no force).
1739 #[tokio::test]
1740 async fn delete_branch_spec_threads_force_to_git_only() {
1741 use processkit::testing::RecordingRunner;
1742 let forced = RecordingRunner::replying(Reply::ok(""));
1743 Repo::from_git("/repo", "/repo", Git::with_runner(&forced))
1744 .delete_branch(BranchDelete::new("feat").force())
1745 .await
1746 .unwrap();
1747 assert!(
1748 forced.only_call().args_str().iter().any(|a| a == "-D"),
1749 "force → branch -D"
1750 );
1751
1752 let unforced = RecordingRunner::replying(Reply::ok(""));
1753 Repo::from_git("/repo", "/repo", Git::with_runner(&unforced))
1754 .delete_branch(BranchDelete::new("feat"))
1755 .await
1756 .unwrap();
1757 assert!(
1758 unforced.only_call().args_str().iter().any(|a| a == "-d"),
1759 "no force → branch -d"
1760 );
1761
1762 let jj = RecordingRunner::replying(Reply::ok(""));
1763 Repo::from_jj("/repo", "/repo", Jj::with_runner(&jj))
1764 .delete_branch(BranchDelete::new("feat").force())
1765 .await
1766 .unwrap();
1767 assert!(
1768 !jj.only_call()
1769 .args_str()
1770 .iter()
1771 .any(|a| a == "-D" || a == "--force"),
1772 "jj bookmark delete has no force flag"
1773 );
1774 }
1775
1776 #[tokio::test]
1777 async fn fetch_branch_dispatches_per_backend() {
1778 use processkit::testing::RecordingRunner;
1779 let grec = RecordingRunner::replying(Reply::ok(""));
1780 Repo::from_git("/repo", "/repo", Git::with_runner(&grec))
1781 .fetch_branch("main")
1782 .await
1783 .unwrap();
1784 assert!(
1785 grec.only_call()
1786 .args_str()
1787 .starts_with(&["fetch".to_string()])
1788 );
1789
1790 let jrec = RecordingRunner::replying(Reply::ok(""));
1791 Repo::from_jj("/repo", "/repo", Jj::with_runner(&jrec))
1792 .fetch_branch("main")
1793 .await
1794 .unwrap();
1795 let args = jrec.only_call().args_str();
1796 assert_eq!(&args[..2], &["git", "fetch"]);
1797 }
1798
1799 // The facade push is the honest LCD: git pushes the ref with `-u origin`,
1800 // jj pushes the bookmark's state with `-b`. Argv pinned on both backends.
1801 #[tokio::test]
1802 async fn push_dispatches_per_backend() {
1803 use processkit::testing::RecordingRunner;
1804 let grec = RecordingRunner::replying(Reply::ok(""));
1805 Repo::from_git("/repo", "/repo", Git::with_runner(&grec))
1806 .push("feature")
1807 .await
1808 .unwrap();
1809 assert_eq!(
1810 grec.only_call().args_str(),
1811 ["push", "-u", "origin", "feature"]
1812 );
1813
1814 let jrec = RecordingRunner::replying(Reply::ok(""));
1815 Repo::from_jj("/repo", "/repo", Jj::with_runner(&jrec))
1816 .push("feature")
1817 .await
1818 .unwrap();
1819 let args = jrec.only_call().args_str();
1820 // `exact:` disables jj's glob matching so a `*` can't push every bookmark (H1).
1821 assert_eq!(&args[..4], &["git", "push", "-b", "exact:feature"]);
1822 }
1823
1824 // The two backends handle a flag-like branch per the documented guard
1825 // convention: git rejects it BEFORE spawning (the branch lands in GitPush's
1826 // bare-positional refspec slot, where `--force` would otherwise be parsed
1827 // as a flag); jj passes it verbatim in the `-b` flag-VALUE slot, where jj
1828 // reads it as a bookmark name and errors itself — no flag injection is
1829 // possible there, so no pre-spawn guard exists (same as rebase/fetch_from).
1830 #[tokio::test]
1831 async fn push_flag_like_branch_follows_guard_convention() {
1832 use processkit::testing::RecordingRunner;
1833 let grec = RecordingRunner::replying(Reply::ok(""));
1834 let err = Repo::from_git("/repo", "/repo", Git::with_runner(&grec))
1835 .push("--force")
1836 .await
1837 .unwrap_err();
1838 assert!(
1839 matches!(err, Error::Vcs(processkit::Error::Spawn { .. })),
1840 "got: {err:?}"
1841 );
1842 assert_eq!(grec.calls().len(), 0, "no process must have spawned");
1843
1844 let jrec = RecordingRunner::replying(Reply::ok(""));
1845 Repo::from_jj("/repo", "/repo", Jj::with_runner(&jrec))
1846 .push("--force")
1847 .await
1848 .expect("jj path spawns; the value rides -b verbatim");
1849 assert_eq!(
1850 &jrec.only_call().args_str()[..4],
1851 &["git", "push", "-b", "exact:--force"],
1852 "the flag-like value must ride the -b flag-VALUE slot, not become argv"
1853 );
1854 }
1855
1856 #[tokio::test]
1857 async fn fetch_from_names_the_remote_on_both_backends() {
1858 use processkit::testing::RecordingRunner;
1859 let grec = RecordingRunner::replying(Reply::ok(""));
1860 Repo::from_git("/repo", "/repo", Git::with_runner(&grec))
1861 .fetch_from("upstream")
1862 .await
1863 .unwrap();
1864 assert_eq!(
1865 grec.only_call().args_str(),
1866 ["fetch", "--quiet", "upstream"]
1867 );
1868
1869 let jrec = RecordingRunner::replying(Reply::ok(""));
1870 Repo::from_jj("/repo", "/repo", Jj::with_runner(&jrec))
1871 .fetch_from("upstream")
1872 .await
1873 .unwrap();
1874 let args = jrec.only_call().args_str();
1875 // `exact:` disables jj's glob matching on the remote name (H1).
1876 assert_eq!(&args[..4], &["git", "fetch", "--remote", "exact:upstream"]);
1877 }
1878
1879 // git: untracked files count as uncommitted but not as *tracked* changes.
1880 #[tokio::test]
1881 async fn git_has_tracked_changes_ignores_untracked() {
1882 let dirty = git_repo(ScriptedRunner::new().on(["git", "status"], Reply::ok(" M a.rs\0")));
1883 assert!(dirty.has_tracked_changes().await.unwrap());
1884 // `--untracked-files=no` means git itself omits `??` entries; an empty
1885 // reply is what a tracked-clean tree returns.
1886 let clean = git_repo(ScriptedRunner::new().on(["git", "status"], Reply::ok("")));
1887 assert!(!clean.has_tracked_changes().await.unwrap());
1888 }
1889
1890 // jj has no untracked concept — `has_tracked_changes` follows `@`'s emptiness.
1891 #[tokio::test]
1892 async fn jj_has_tracked_changes_follows_working_copy() {
1893 let dirty =
1894 jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("kz\t38\tfalse\twip\n")));
1895 assert!(dirty.has_tracked_changes().await.unwrap());
1896 }
1897
1898 #[tokio::test]
1899 async fn conflicted_files_dispatches_per_backend() {
1900 let git =
1901 git_repo(ScriptedRunner::new().on(["git", "diff"], Reply::ok("a.rs\0b dir/c.rs\0")));
1902 assert_eq!(
1903 git.conflicted_files().await.unwrap(),
1904 ["a.rs", "b dir/c.rs"]
1905 );
1906
1907 let jj = jj_repo(
1908 ScriptedRunner::new().on(["jj", "resolve"], Reply::ok("a.rs 2-sided conflict\n")),
1909 );
1910 assert_eq!(jj.conflicted_files().await.unwrap(), ["a.rs"]);
1911 // The benign "no conflicts" non-zero exit still reads as an empty list.
1912 let clean = jj_repo(ScriptedRunner::new().on(
1913 ["jj", "resolve"],
1914 Reply::fail(2, "Error: No conflicts found at this revision"),
1915 ));
1916 assert!(clean.conflicted_files().await.unwrap().is_empty());
1917 }
1918
1919 #[test]
1920 fn merge_probe_is_clean() {
1921 assert!(MergeProbe::Clean.is_clean());
1922 assert!(!MergeProbe::Conflicts(vec!["a.rs".into()]).is_clean());
1923 }
1924
1925 // git try_merge, clean: probe merge, no MERGE_HEAD afterwards (the scripted
1926 // git-dir doesn't exist) → no abort, `Clean`.
1927 #[tokio::test]
1928 async fn git_try_merge_reports_clean_and_skips_needless_abort() {
1929 use processkit::testing::RecordingRunner;
1930 let rec = RecordingRunner::new(
1931 ScriptedRunner::new()
1932 .on(["git", "merge"], Reply::ok("Already up to date.\n"))
1933 .on(["git", "rev-parse"], Reply::ok("/vcs-core-no-such-git-dir")),
1934 );
1935 let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
1936 assert_eq!(repo.try_merge("other").await.unwrap(), MergeProbe::Clean);
1937 assert!(
1938 rec.calls()
1939 .iter()
1940 .all(|c| !c.args_str().contains(&"--abort".to_string())),
1941 "no merge to abort"
1942 );
1943 }
1944
1945 // git try_merge, conflict: conflicted paths are read BEFORE the abort (abort
1946 // clears the unmerged index), then the merge is aborted.
1947 #[tokio::test]
1948 async fn git_try_merge_collects_conflicts_then_aborts() {
1949 use processkit::testing::RecordingRunner;
1950 let rec = RecordingRunner::new(
1951 ScriptedRunner::new()
1952 // Order matters: ["merge","--abort"] must outrank the ["merge"] rule.
1953 .on(["git", "merge", "--abort"], Reply::ok(""))
1954 .on(
1955 ["git", "merge"],
1956 Reply::fail(1, "CONFLICT (content): Merge conflict in a.rs"),
1957 )
1958 .on(["git", "diff"], Reply::ok("a.rs\0")),
1959 );
1960 let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
1961 assert_eq!(
1962 repo.try_merge("other").await.unwrap(),
1963 MergeProbe::Conflicts(vec!["a.rs".to_string()])
1964 );
1965 let calls = rec.calls();
1966 let diff_pos = calls.iter().position(|c| c.args_str()[0] == "diff");
1967 let abort_pos = calls
1968 .iter()
1969 .position(|c| c.args_str().contains(&"--abort".to_string()));
1970 assert!(diff_pos.unwrap() < abort_pos.unwrap(), "{calls:?}");
1971 }
1972
1973 // git try_merge: a failing rollback must propagate, not be reported as a
1974 // clean/conflicted probe.
1975 #[tokio::test]
1976 async fn git_try_merge_propagates_abort_failure() {
1977 let tmp = TempDir::new("probe-abort");
1978 std::fs::write(tmp.path().join("MERGE_HEAD"), "deadbeef\n").unwrap();
1979 let repo = git_repo(
1980 ScriptedRunner::new()
1981 .on(
1982 ["git", "merge", "--abort"],
1983 Reply::fail(128, "fatal: cannot abort"),
1984 )
1985 .on(["git", "merge"], Reply::ok(""))
1986 .on(
1987 ["git", "rev-parse"],
1988 Reply::ok(tmp.path().to_str().unwrap()),
1989 ),
1990 );
1991 assert!(repo.try_merge("other").await.is_err());
1992 }
1993
1994 // jj try_merge: op head captured first, probe runs, op restore always runs.
1995 #[tokio::test]
1996 async fn jj_try_merge_probes_and_restores() {
1997 use processkit::testing::RecordingRunner;
1998 let rec = RecordingRunner::new(
1999 ScriptedRunner::new()
2000 .on(["jj", "op", "log"], Reply::ok("op42\n"))
2001 .on(["jj", "op", "restore"], Reply::ok(""))
2002 .on(["jj", "new"], Reply::ok(""))
2003 .on(["jj", "log"], Reply::ok("1\n")) // is_conflicted → true
2004 .on(["jj", "resolve"], Reply::ok("a.rs 2-sided conflict\n")),
2005 );
2006 let repo = Repo::from_jj("/repo", "/repo", Jj::with_runner(&rec));
2007 assert_eq!(
2008 repo.try_merge("feature").await.unwrap(),
2009 MergeProbe::Conflicts(vec!["a.rs".to_string()])
2010 );
2011 let calls = rec.calls();
2012 assert_eq!(calls[0].args_str()[..2], ["op", "log"]);
2013 assert_eq!(calls[1].args_str()[0], "new");
2014 let last = calls.last().unwrap().args_str();
2015 assert_eq!(last[..3], ["op", "restore", "op42"]);
2016 }
2017
2018 #[tokio::test]
2019 async fn jj_try_merge_clean_and_restore_failure() {
2020 // Conflict-free probe → Clean (no resolve call needed).
2021 let clean = jj_repo(
2022 ScriptedRunner::new()
2023 .on(["jj", "op", "log"], Reply::ok("op42\n"))
2024 .on(["jj", "op", "restore"], Reply::ok(""))
2025 .on(["jj", "new"], Reply::ok(""))
2026 .on(["jj", "log"], Reply::ok("0\n")),
2027 );
2028 assert_eq!(clean.try_merge("feature").await.unwrap(), MergeProbe::Clean);
2029
2030 // A failing op restore breaks the rollback guarantee → error, not Clean.
2031 let broken = jj_repo(
2032 ScriptedRunner::new()
2033 .on(["jj", "op", "log"], Reply::ok("op42\n"))
2034 .on(["jj", "op", "restore"], Reply::fail(1, "op not found"))
2035 .on(["jj", "new"], Reply::ok(""))
2036 .on(["jj", "log"], Reply::ok("0\n")),
2037 );
2038 assert!(broken.try_merge("feature").await.is_err());
2039 }
2040
2041 // continue_in_progress with unresolved paths reports `Conflict` and must NOT
2042 // attempt the continue (git would hard-error).
2043 #[tokio::test]
2044 async fn git_continue_blocked_by_conflicts_does_not_act() {
2045 use processkit::testing::RecordingRunner;
2046 let rec =
2047 RecordingRunner::new(ScriptedRunner::new().on(["git", "diff"], Reply::ok("a.rs\0")));
2048 let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
2049 assert_eq!(
2050 repo.continue_in_progress().await.unwrap(),
2051 OperationState::Conflict
2052 );
2053 assert!(
2054 rec.calls().iter().all(|c| c.args_str()[0] == "diff"),
2055 "only the conflict probe may run: {:?}",
2056 rec.calls()
2057 );
2058 }
2059
2060 // A continued rebase that stops on the NEXT patch's conflict exits non-zero;
2061 // continue_in_progress must report that as `Conflict`, not as an error. The
2062 // first conflict probe must see a clean index (else continue is blocked), the
2063 // post-continue probe must see the new conflict — a stateful predicate
2064 // sequences the two `diff` replies.
2065 #[tokio::test]
2066 async fn git_continue_maps_rebase_re_conflict() {
2067 use std::sync::Arc as StdArc;
2068 use std::sync::atomic::{AtomicBool, Ordering};
2069 let tmp = TempDir::new("rebase-restop");
2070 std::fs::create_dir_all(tmp.path().join("rebase-merge")).unwrap();
2071 let seen_first_diff = StdArc::new(AtomicBool::new(false));
2072 let flag = StdArc::clone(&seen_first_diff);
2073 let repo = git_repo(
2074 ScriptedRunner::new()
2075 .when(
2076 move |cmd| {
2077 cmd.arguments().first().and_then(|a| a.to_str()) == Some("diff")
2078 && flag.swap(true, Ordering::SeqCst)
2079 },
2080 Reply::ok("a.rs\0"),
2081 )
2082 .on(["git", "diff"], Reply::ok(""))
2083 .on(
2084 ["git", "rev-parse"],
2085 Reply::ok(tmp.path().to_str().unwrap()),
2086 )
2087 .on(
2088 ["git", "rebase", "--continue"],
2089 Reply::fail(1, "CONFLICT (content): Merge conflict in a.rs"),
2090 ),
2091 );
2092 assert_eq!(
2093 repo.continue_in_progress().await.unwrap(),
2094 OperationState::Conflict
2095 );
2096 }
2097
2098 // abort_in_progress dispatches to `merge --abort` when MERGE_HEAD is present.
2099 #[tokio::test]
2100 async fn git_abort_dispatches_on_merge_in_progress() {
2101 use processkit::testing::RecordingRunner;
2102 let tmp = TempDir::new("abort");
2103 std::fs::write(tmp.path().join("MERGE_HEAD"), "deadbeef\n").unwrap();
2104 let rec = RecordingRunner::new(
2105 ScriptedRunner::new()
2106 .on(
2107 ["git", "rev-parse"],
2108 Reply::ok(tmp.path().to_str().unwrap()),
2109 )
2110 .on(["git", "merge", "--abort"], Reply::ok("")),
2111 );
2112 let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
2113 repo.abort_in_progress().await.unwrap();
2114 assert!(
2115 rec.calls()
2116 .iter()
2117 .any(|c| c.args_str() == ["merge", "--abort"]),
2118 "{:?}",
2119 rec.calls()
2120 );
2121 }
2122
2123 // git surfaces an interrupted op as on-disk state: in_progress_state returns
2124 // Merge when MERGE_HEAD is present and Rebase when a rebase dir is — the
2125 // documented asymmetry (git's conflict IS that paused state, never `Conflict`
2126 // from this method).
2127 #[tokio::test]
2128 async fn git_in_progress_state_maps_merge_and_rebase() {
2129 let merging = TempDir::new("inprog-merge");
2130 std::fs::write(merging.path().join("MERGE_HEAD"), "deadbeef\n").unwrap();
2131 let merge_repo = Repo::from_git(
2132 "/repo",
2133 "/repo",
2134 Git::with_runner(ScriptedRunner::new().on(
2135 ["git", "rev-parse"],
2136 Reply::ok(merging.path().to_str().unwrap()),
2137 )),
2138 );
2139 assert_eq!(
2140 merge_repo.in_progress_state().await.unwrap(),
2141 OperationState::Merge
2142 );
2143
2144 let rebasing = TempDir::new("inprog-rebase");
2145 std::fs::create_dir_all(rebasing.path().join("rebase-merge")).unwrap();
2146 let rebase_repo = Repo::from_git(
2147 "/repo",
2148 "/repo",
2149 Git::with_runner(ScriptedRunner::new().on(
2150 ["git", "rev-parse"],
2151 Reply::ok(rebasing.path().to_str().unwrap()),
2152 )),
2153 );
2154 assert_eq!(
2155 rebase_repo.in_progress_state().await.unwrap(),
2156 OperationState::Rebase
2157 );
2158 }
2159
2160 // On an unborn git repo (no commits) diff_stat probes is_unborn and stats
2161 // against the empty tree instead of the unresolvable HEAD, so a fresh working
2162 // tree reports its additions rather than erroring.
2163 #[tokio::test]
2164 async fn git_diff_stat_unborn_uses_empty_tree() {
2165 use processkit::testing::RecordingRunner;
2166 let rec = RecordingRunner::new(
2167 ScriptedRunner::new()
2168 .on(["git", "rev-parse"], Reply::fail(1, "")) // HEAD unborn
2169 .on(
2170 ["git", "diff", "--shortstat"],
2171 Reply::ok(" 1 file changed, 2 insertions(+)\n"),
2172 ),
2173 );
2174 let repo = Repo::from_git("/repo", "/repo", Git::with_runner(&rec));
2175 let stat = repo.diff_stat().await.unwrap();
2176 assert_eq!(stat.insertions, 2);
2177 assert!(
2178 rec.calls()
2179 .iter()
2180 .any(|c| c.args_str() == ["diff", "--shortstat", vcs_git::EMPTY_TREE]),
2181 "diff_stat should target the empty tree on an unborn repo: {:?}",
2182 rec.calls()
2183 );
2184 }
2185
2186 // On jj, abort/continue are reporting no-ops (nothing is ever paused).
2187 #[tokio::test]
2188 async fn jj_abort_and_continue_are_reporting_noops() {
2189 let conflicted = jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("1\n")));
2190 assert_eq!(
2191 conflicted.abort_in_progress().await.unwrap(),
2192 OperationState::Conflict
2193 );
2194 let clear = jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("0\n")));
2195 assert_eq!(
2196 clear.continue_in_progress().await.unwrap(),
2197 OperationState::Clear
2198 );
2199 }
2200
2201 // jj records conflicts on the change; the facade maps that to `Conflict`.
2202 #[tokio::test]
2203 async fn jj_in_progress_state_maps_conflict() {
2204 let conflicted = jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("1\n")));
2205 assert_eq!(
2206 conflicted.in_progress_state().await.unwrap(),
2207 OperationState::Conflict
2208 );
2209 let clear = jj_repo(ScriptedRunner::new().on(["jj", "log"], Reply::ok("0\n")));
2210 assert_eq!(
2211 clear.in_progress_state().await.unwrap(),
2212 OperationState::Clear
2213 );
2214 }
2215
2216 // `&dyn VcsRepo` must dispatch through the real inherent methods (a delegating
2217 // body that recursed would stack-overflow here instead of returning).
2218 #[tokio::test]
2219 async fn vcs_repo_trait_object_dispatches() {
2220 let repo = git_repo(
2221 ScriptedRunner::new()
2222 .on(["git", "symbolic-ref"], Reply::ok("main\n"))
2223 .on(["git", "show-ref"], Reply::ok("")),
2224 );
2225 let dynamic: &dyn VcsRepo = &repo;
2226 assert_eq!(dynamic.kind(), BackendKind::Git);
2227 assert_eq!(
2228 dynamic.current_branch().await.unwrap().as_deref(),
2229 Some("main")
2230 );
2231 // Exercise a reference-argument async method through `&dyn` — pins the
2232 // async_trait lifetime capture the macro relies on (no-arg calls don't).
2233 assert!(dynamic.branch_exists("main").await.unwrap());
2234 }
2235
2236 // When the backend has no native trunk (git `origin/HEAD` unset), the facade
2237 // falls back to a local `main`, then `master`.
2238 #[tokio::test]
2239 async fn trunk_falls_back_to_main() {
2240 let repo = git_repo(
2241 ScriptedRunner::new()
2242 .on(["git", "symbolic-ref"], Reply::fail(1, "")) // origin/HEAD unset → None
2243 .on(["git", "show-ref"], Reply::ok("")), // branch_exists("main") → exit 0
2244 );
2245 assert_eq!(repo.trunk().await.unwrap().as_deref(), Some("main"));
2246 }
2247
2248 #[test]
2249 fn error_classifiers_recognise_markers() {
2250 let conflict = Error::Vcs(processkit::Error::Exit {
2251 program: "git".into(),
2252 code: 1,
2253 stdout: "CONFLICT (content): Merge conflict in a.rs".into(),
2254 stderr: String::new(),
2255 });
2256 assert!(conflict.is_merge_conflict());
2257 assert!(!conflict.is_nothing_to_commit());
2258 // A non-Vcs error classifies as none of them.
2259 assert!(!Error::NotARepository("/x".into()).is_merge_conflict());
2260 }
2261}
2262
2263// Long-form how-to guides, rendered from this crate's docs/*.md on docs.rs.
2264#[doc = include_str!("../docs/core.md")]
2265#[allow(rustdoc::broken_intra_doc_links)]
2266pub mod guide {
2267 #[doc = include_str!("../docs/cookbook.md")]
2268 #[allow(rustdoc::broken_intra_doc_links)]
2269 pub mod cookbook {}
2270 #[doc = include_str!("../docs/process-model.md")]
2271 #[allow(rustdoc::broken_intra_doc_links)]
2272 pub mod process_model {}
2273 #[doc = include_str!("../docs/positioning.md")]
2274 #[allow(rustdoc::broken_intra_doc_links)]
2275 pub mod positioning {}
2276 #[doc = include_str!("../docs/stability.md")]
2277 #[allow(rustdoc::broken_intra_doc_links)]
2278 pub mod stability {}
2279}