brain: Accept Git revisions in JJ workspaces
64d6b29163aa3220e8882c81311022e47e072da3
Running `brain diff HEAD^^^ HEAD` in a colocated Jujutsu workspace failed with only an exit status because Jujutsu parsed Git revision syntax and discarded its command diagnostics. Resolve selectors through Jujutsu with a Git fallback, pass resolved object IDs to Jujutsu range queries, and retain stderr from failed Git and Jujutsu commands with explicit backend labels. Assisted-by: Codex:gpt-5.6-sol
1 parent
e7679e75
crates/brain/src/backend.rs
+18 -8
| 1753 | 1753 | jj, |
|
| 1754 | 1754 | repository, |
|
| 1755 | 1755 | review: ReviewIdentity::new(base, target), |
|
| 1756 | 1756 | }) |
|
| 1757 | 1757 | } |
|
| 1758 | + | ||
| 1759 | + | /// Resolves either a Jujutsu revset or a Git revision. |
|
| 1760 | + | fn resolve(&self, revision: &Rev) -> Result<Oid, Error> { |
|
| 1761 | + | match self.jj.resolve(revision) { |
|
| 1762 | + | Ok(commit) => Ok(commit), |
|
| 1763 | + | Err(_) => Ok(self.repository.commit_id(revision)?), |
|
| 1764 | + | } |
|
| 1765 | + | } |
|
| 1758 | 1766 | } |
|
| 1759 | 1767 | ||
| 1760 | 1768 | impl Backend for JjBackend { |
|
| 1761 | 1769 | fn range_label(&self) -> String { |
|
| 1762 | 1770 | range_label(&self.review) |
|
| 1763 | 1771 | } |
|
| 1764 | 1772 | ||
| 1765 | 1773 | fn commits(&self) -> Result<Vec<Commit>, Error> { |
|
| 1766 | - | let commits = self.jj.commits(&self.review.base, &self.review.target)?; |
|
| 1774 | + | let base = Rev::from(self.resolve(&self.review.base)?); |
|
| 1775 | + | let target = Rev::from(self.resolve(&self.review.target)?); |
|
| 1776 | + | let commits = self.jj.commits(&base, &target)?; |
|
| 1767 | 1777 | let bookmarks = self.jj.bookmarks()?; |
|
| 1768 | 1778 | Commit::with_stats(&self.repository, commits, bookmarks) |
|
| 1769 | 1779 | } |
|
| 1770 | 1780 | ||
| 1771 | 1781 | fn commit_header(&self, commit: Oid) -> Result<CommitHeader, Error> { |
| 1780 | 1790 | let bytes = self.jj.commit_diff(commit)?; |
|
| 1781 | 1791 | Ok(DiffDocument::decode_bytes(&bytes)?.files) |
|
| 1782 | 1792 | } |
|
| 1783 | 1793 | ||
| 1784 | 1794 | fn brain(&self) -> Result<Brain, Error> { |
|
| 1785 | - | let base = self.jj.resolve(&self.review.base)?; |
|
| 1786 | - | let target = self.jj.resolve(&self.review.target)?; |
|
| 1795 | + | let base = self.resolve(&self.review.base)?; |
|
| 1796 | + | let target = self.resolve(&self.review.target)?; |
|
| 1787 | 1797 | Brain::load(self.repository.clone(), base, target, &self.review) |
|
| 1788 | 1798 | } |
|
| 1789 | 1799 | ||
| 1790 | 1800 | fn reset_brain(&self) -> Result<Brain, Error> { |
|
| 1791 | - | let base = self.jj.resolve(&self.review.base)?; |
|
| 1792 | - | let target = self.jj.resolve(&self.review.target)?; |
|
| 1801 | + | let base = self.resolve(&self.review.base)?; |
|
| 1802 | + | let target = self.resolve(&self.review.target)?; |
|
| 1793 | 1803 | Brain::reset(self.repository.clone(), base, target, &self.review) |
|
| 1794 | 1804 | } |
|
| 1795 | 1805 | ||
| 1796 | 1806 | fn rebase_brain(&self) -> Result<Brain, Error> { |
|
| 1797 | - | let base = self.jj.resolve(&self.review.base)?; |
|
| 1798 | - | let target = self.jj.resolve(&self.review.target)?; |
|
| 1807 | + | let base = self.resolve(&self.review.base)?; |
|
| 1808 | + | let target = self.resolve(&self.review.target)?; |
|
| 1799 | 1809 | let commits = self.commits()?; |
|
| 1800 | 1810 | let documents = commits |
|
| 1801 | 1811 | .iter() |
|
| 1802 | 1812 | .map(|commit| Ok(self.diff_document(commit.id)?.split_hunks())) |
|
| 1803 | 1813 | .collect::<Result<Vec<_>, Error>>()?; |
| 2194 | 2204 | fixture.git(&["branch", "-m", "topic"]); |
|
| 2195 | 2205 | fixture.git(&["commit", "-q", "--allow-empty", "-m", "topic"]); |
|
| 2196 | 2206 | ||
| 2197 | 2207 | let error = statuses(fixture.path()).unwrap_err(); |
|
| 2198 | 2208 | ||
| 2199 | - | assert_eq!(error.to_string(), "no trunk or master branch found"); |
|
| 2209 | + | assert_eq!(error.to_string(), "git: no trunk or master branch found"); |
|
| 2200 | 2210 | } |
|
| 2201 | 2211 | ||
| 2202 | 2212 | #[test] |
|
| 2203 | 2213 | fn statuses_match_symbolic_target_to_branch_commit() { |
|
| 2204 | 2214 | let fixture = TestRepository::new(); |
f
+0 -0
new.rs
+0 -0
image.png
+0 -0
new.txt
+0 -0
crates/brain/src/error.rs
+5 -5
| 8 | 8 | /// Top-level error type for the binary. |
|
| 9 | 9 | #[derive(Debug)] |
|
| 10 | 10 | pub enum Error { |
|
| 11 | 11 | /// I/O failed. |
|
| 12 | 12 | Io(io::Error), |
|
| 13 | - | /// A backend command exited with a failing status code. |
|
| 14 | - | Backend(i32), |
|
| 15 | 13 | /// A Git plumbing command failed, carrying its diagnostic. |
|
| 16 | 14 | Git(String), |
|
| 15 | + | /// A Jujutsu command failed, carrying its diagnostic. |
|
| 16 | + | Jj(String), |
|
| 17 | 17 | /// A hidden-tree merge encountered a content conflict. |
|
| 18 | 18 | MergeConflict(String), |
|
| 19 | 19 | /// The persisted review state belongs to another resolved range. |
|
| 20 | 20 | StaleReviewState, |
|
| 21 | 21 | /// Persisted review state is not valid. |
| 50 | 50 | ||
| 51 | 51 | impl From<RepoError> for Error { |
|
| 52 | 52 | fn from(error: RepoError) -> Self { |
|
| 53 | 53 | match error { |
|
| 54 | 54 | RepoError::Io(e) => Self::Io(e), |
|
| 55 | - | RepoError::Status(code) => Self::Backend(code), |
|
| 56 | 55 | RepoError::Git(msg) => Self::Git(msg), |
|
| 56 | + | RepoError::Jj(msg) => Self::Jj(msg), |
|
| 57 | 57 | RepoError::MergeConflict(msg) => Self::MergeConflict(msg), |
|
| 58 | 58 | RepoError::PatchApply(msg) => Self::Git(msg), |
|
| 59 | 59 | } |
|
| 60 | 60 | } |
|
| 61 | 61 | } |
|
| 62 | 62 | ||
| 63 | 63 | impl std::fmt::Display for Error { |
|
| 64 | 64 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|
| 65 | 65 | match self { |
|
| 66 | 66 | Self::Io(error) => write!(f, "{error}"), |
|
| 67 | - | Self::Backend(code) => write!(f, "backend command exited with status {code}"), |
|
| 68 | - | Self::Git(message) => write!(f, "{message}"), |
|
| 67 | + | Self::Git(message) => write!(f, "git: {message}"), |
|
| 68 | + | Self::Jj(message) => write!(f, "jj: {message}"), |
|
| 69 | 69 | Self::MergeConflict(message) => write!(f, "merge conflict: {message}"), |
|
| 70 | 70 | Self::StaleReviewState => { |
|
| 71 | 71 | f.write_str("stored review state is for a different resolved range") |
|
| 72 | 72 | } |
|
| 73 | 73 | Self::InvalidReviewState => { |
crates/repo/src/git.rs
+1 -1
| 957 | 957 | .arg(context) |
|
| 958 | 958 | .arg("--") |
|
| 959 | 959 | .args(paths) |
|
| 960 | 960 | .output()?; |
|
| 961 | 961 | if !output.status.success() { |
|
| 962 | - | return Err(RepoError::Status(output.status.code().unwrap_or(1))); |
|
| 962 | + | return Err(RepoError::git(&output)); |
|
| 963 | 963 | } |
|
| 964 | 964 | Ok(output.stdout) |
|
| 965 | 965 | } |
|
| 966 | 966 | } |
|
| 967 | 967 |
root\nnew file mode"));
+0 -0
file\n"));
+0 -0
file\n"),
+0 -0
file\n"),
+0 -0
a\n--- a/a\n+++ b/a\n@@ -1 +1 @@\n-old\n+new\n".to_owned(),
+0 -0
b\n--- a/b\n+++ b/b\n@@ -0,0 +1 @@\n+line\n".to_owned(),
+0 -0
crates/repo/src/jj.rs
+10 -10
| 28 | 28 | "-r", |
|
| 29 | 29 | &revset, |
|
| 30 | 30 | "-T", |
|
| 31 | 31 | "commit_id ++ \" \" ++ description.first_line() ++ \"\\n\"", |
|
| 32 | 32 | ])?; |
|
| 33 | - | let text = String::from_utf8(stdout).map_err(|e| RepoError::Git(e.to_string()))?; |
|
| 33 | + | let text = String::from_utf8(stdout).map_err(|e| RepoError::Jj(e.to_string()))?; |
|
| 34 | 34 | let mut commits = Vec::new(); |
|
| 35 | 35 | for line in text.lines() { |
|
| 36 | 36 | let Some((id, summary)) = line.split_once(' ') else { |
|
| 37 | - | return Err(RepoError::Git( |
|
| 37 | + | return Err(RepoError::Jj( |
|
| 38 | 38 | "malformed commit line from jj log".to_owned(), |
|
| 39 | 39 | )); |
|
| 40 | 40 | }; |
|
| 41 | 41 | let id = id |
|
| 42 | 42 | .parse() |
|
| 43 | - | .map_err(|_| RepoError::Git("invalid oid from jj log".to_owned()))?; |
|
| 43 | + | .map_err(|_| RepoError::Jj("invalid oid from jj log".to_owned()))?; |
|
| 44 | 44 | commits.push((id, summary.trim_start().to_owned())); |
|
| 45 | 45 | } |
|
| 46 | 46 | Ok(commits) |
|
| 47 | 47 | } |
|
| 48 | 48 | ||
| 49 | 49 | /// Returns the fork point between `target` and the configured trunk. |
|
| 50 | 50 | pub fn branch_base(&self, target: &Rev) -> Result<Oid, RepoError> { |
|
| 51 | 51 | let revset = format!("fork_point(({}) | trunk())", target.as_str()); |
|
| 52 | 52 | let revset = Rev::try_from(revset.as_str()) |
|
| 53 | - | .map_err(|error| RepoError::Git(format!("invalid branch base revset: {error}")))?; |
|
| 53 | + | .map_err(|error| RepoError::Jj(format!("invalid branch base revset: {error}")))?; |
|
| 54 | 54 | self.resolve(&revset) |
|
| 55 | 55 | } |
|
| 56 | 56 | ||
| 57 | 57 | /// Returns local bookmark names paired with their target commits. |
|
| 58 | 58 | pub fn bookmarks(&self) -> Result<Vec<(Oid, String)>, RepoError> { |
| 62 | 62 | "-r", |
|
| 63 | 63 | "bookmarks()", |
|
| 64 | 64 | "-T", |
|
| 65 | 65 | "commit_id ++ \"\\t\" ++ local_bookmarks.map(|bookmark| bookmark.name()).join(\"\\t\") ++ \"\\n\"", |
|
| 66 | 66 | ])?; |
|
| 67 | - | let text = String::from_utf8(stdout).map_err(|e| RepoError::Git(e.to_string()))?; |
|
| 67 | + | let text = String::from_utf8(stdout).map_err(|e| RepoError::Jj(e.to_string()))?; |
|
| 68 | 68 | let mut bookmarks = Vec::new(); |
|
| 69 | 69 | for line in text.lines() { |
|
| 70 | 70 | let mut fields = line.split('\t'); |
|
| 71 | 71 | let id = fields |
|
| 72 | 72 | .next() |
|
| 73 | - | .ok_or_else(|| RepoError::Git("malformed bookmark line from jj log".to_owned()))? |
|
| 73 | + | .ok_or_else(|| RepoError::Jj("malformed bookmark line from jj log".to_owned()))? |
|
| 74 | 74 | .parse() |
|
| 75 | - | .map_err(|_| RepoError::Git("invalid oid from jj log".to_owned()))?; |
|
| 75 | + | .map_err(|_| RepoError::Jj("invalid oid from jj log".to_owned()))?; |
|
| 76 | 76 | for name in fields { |
|
| 77 | 77 | bookmarks.push((id, name.to_owned())); |
|
| 78 | 78 | } |
|
| 79 | 79 | } |
|
| 80 | 80 | Ok(bookmarks) |
| 102 | 102 | "-T", |
|
| 103 | 103 | "commit_id", |
|
| 104 | 104 | ])?; |
|
| 105 | 105 | text.trim() |
|
| 106 | 106 | .parse() |
|
| 107 | - | .map_err(|_| RepoError::Git("invalid commit id from jj".to_owned())) |
|
| 107 | + | .map_err(|_| RepoError::Jj("invalid commit id from jj".to_owned())) |
|
| 108 | 108 | } |
|
| 109 | 109 | ||
| 110 | 110 | /// Returns the path of the embedded Git repository. |
|
| 111 | 111 | fn git_root(&self) -> Result<PathBuf, RepoError> { |
|
| 112 | 112 | let text = self.jj_text(&["git", "root"])?; |
| 120 | 120 | .current_dir(&self.cwd) |
|
| 121 | 121 | .output()?; |
|
| 122 | 122 | if output.status.success() { |
|
| 123 | 123 | Ok(output.stdout) |
|
| 124 | 124 | } else { |
|
| 125 | - | Err(RepoError::Status(output.status.code().unwrap_or(1))) |
|
| 125 | + | Err(RepoError::jj(&output)) |
|
| 126 | 126 | } |
|
| 127 | 127 | } |
|
| 128 | 128 | ||
| 129 | 129 | /// Runs `jj` and returns its stdout as UTF-8 text on success. |
|
| 130 | 130 | fn jj_text(&self, args: &[&str]) -> Result<String, RepoError> { |
|
| 131 | 131 | let bytes = self.jj_bytes(args)?; |
|
| 132 | - | String::from_utf8(bytes).map_err(|e| RepoError::Git(e.to_string())) |
|
| 132 | + | String::from_utf8(bytes).map_err(|e| RepoError::Jj(e.to_string())) |
|
| 133 | 133 | } |
|
| 134 | 134 | } |
crates/repo/src/lib.rs
+8 -4
| 282 | 282 | /// A failure from a Git-backed repository operation. |
|
| 283 | 283 | #[derive(Debug)] |
|
| 284 | 284 | pub enum RepoError { |
|
| 285 | 285 | /// Spawning or waiting on a process failed. |
|
| 286 | 286 | Io(io::Error), |
|
| 287 | - | /// A backend command exited with a failing status code. |
|
| 288 | - | Status(i32), |
|
| 289 | 287 | /// A Git plumbing command failed, carrying its diagnostic. |
|
| 290 | 288 | Git(String), |
|
| 289 | + | /// A Jujutsu command failed, carrying its diagnostic. |
|
| 290 | + | Jj(String), |
|
| 291 | 291 | /// A three-way merge produced conflicts. |
|
| 292 | 292 | MergeConflict(String), |
|
| 293 | 293 | /// A patch did not apply to the requested tree, carrying its diagnostic. |
|
| 294 | 294 | PatchApply(String), |
|
| 295 | 295 | } |
| 297 | 297 | impl RepoError { |
|
| 298 | 298 | /// Builds a Git error from a failed command's output. |
|
| 299 | 299 | pub(crate) fn git(output: &Output) -> Self { |
|
| 300 | 300 | Self::Git(Self::diagnostic(output)) |
|
| 301 | 301 | } |
|
| 302 | + | /// Builds a Jujutsu error from a failed command's output. |
|
| 303 | + | pub(crate) fn jj(output: &Output) -> Self { |
|
| 304 | + | Self::Jj(Self::diagnostic(output)) |
|
| 305 | + | } |
|
| 302 | 306 | ||
| 303 | 307 | /// Builds a patch-apply error from a failed command's output. |
|
| 304 | 308 | pub(crate) fn patch_apply(output: &Output) -> Self { |
|
| 305 | 309 | Self::PatchApply(Self::diagnostic(output)) |
|
| 306 | 310 | } |
| 322 | 326 | ||
| 323 | 327 | impl std::fmt::Display for RepoError { |
|
| 324 | 328 | fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
|
| 325 | 329 | match self { |
|
| 326 | 330 | Self::Io(error) => write!(f, "{error}"), |
|
| 327 | - | Self::Status(code) => write!(f, "command exited with status {code}"), |
|
| 328 | - | Self::Git(message) => write!(f, "{message}"), |
|
| 331 | + | Self::Git(message) => write!(f, "git: {message}"), |
|
| 332 | + | Self::Jj(message) => write!(f, "jj: {message}"), |
|
| 329 | 333 | Self::MergeConflict(message) => write!(f, "merge conflict: {message}"), |
|
| 330 | 334 | Self::PatchApply(message) => write!(f, "patch does not apply: {message}"), |
|
| 331 | 335 | } |
|
| 332 | 336 | } |
|
| 333 | 337 | } |