.cargo/
crates/
brain/
diff/
files/
highlight/
repo/
src/
git/
git.rs
54.7 KiB
jj.rs
4.7 KiB
lib.rs
12.2 KiB
oid.rs
3.9 KiB
Cargo.toml
330 B
signals/
tui/
tui-core/
tui-termion/
tui-widgets/
scripts/
src/
.gitignore
30 B
.gitsigners
198 B
AGENTS.md
1.9 KiB
CONTRIBUTING
1.7 KiB
Cargo.lock
5.4 KiB
Cargo.toml
1.9 KiB
LICENSE
1.1 KiB
README.md
245 B
STYLE
3.7 KiB
clippy.toml
213 B
crates/repo/src/jj.rs
raw
| 1 | //! Jujutsu plumbing handle for reading workspace state. |
| 2 | |
| 3 | use std::path::PathBuf; |
| 4 | use std::process::Command; |
| 5 | |
| 6 | use crate::{Oid, RepoError, Rev, git}; |
| 7 | |
| 8 | /// Jujutsu repository command context. |
| 9 | #[derive(Clone, Debug)] |
| 10 | pub struct Repository { |
| 11 | /// Workspace directory used for `jj` invocations. |
| 12 | cwd: PathBuf, |
| 13 | } |
| 14 | |
| 15 | impl Repository { |
| 16 | /// Creates a new Jujutsu repository context rooted at `cwd`. |
| 17 | pub fn new(cwd: impl Into<PathBuf>) -> Self { |
| 18 | Self { cwd: cwd.into() } |
| 19 | } |
| 20 | |
| 21 | /// Returns commits in `base..target`, oldest first, as `(id, summary)` pairs. |
| 22 | pub fn commits(&self, base: &Rev, target: &Rev) -> Result<Vec<(Oid, String)>, RepoError> { |
| 23 | let revset = format!("{}..{}", base.as_str(), target.as_str()); |
| 24 | let stdout = self.jj_bytes(&[ |
| 25 | "log", |
| 26 | "--no-graph", |
| 27 | "--reversed", |
| 28 | "-r", |
| 29 | &revset, |
| 30 | "-T", |
| 31 | "commit_id ++ \" \" ++ description.first_line() ++ \"\\n\"", |
| 32 | ])?; |
| 33 | let text = String::from_utf8(stdout).map_err(|e| RepoError::Jj(e.to_string()))?; |
| 34 | let mut commits = Vec::new(); |
| 35 | for line in text.lines() { |
| 36 | let Some((id, summary)) = line.split_once(' ') else { |
| 37 | return Err(RepoError::Jj( |
| 38 | "malformed commit line from jj log".to_owned(), |
| 39 | )); |
| 40 | }; |
| 41 | let id = id |
| 42 | .parse() |
| 43 | .map_err(|_| RepoError::Jj("invalid oid from jj log".to_owned()))?; |
| 44 | commits.push((id, summary.trim_start().to_owned())); |
| 45 | } |
| 46 | Ok(commits) |
| 47 | } |
| 48 | |
| 49 | /// Returns the fork point between `target` and the configured trunk. |
| 50 | pub fn branch_base(&self, target: &Rev) -> Result<Oid, RepoError> { |
| 51 | let revset = format!("fork_point(({}) | trunk())", target.as_str()); |
| 52 | let revset = Rev::try_from(revset.as_str()) |
| 53 | .map_err(|error| RepoError::Jj(format!("invalid branch base revset: {error}")))?; |
| 54 | self.resolve(&revset) |
| 55 | } |
| 56 | |
| 57 | /// Returns local bookmark names paired with their target commits. |
| 58 | pub fn bookmarks(&self) -> Result<Vec<(Oid, String)>, RepoError> { |
| 59 | let stdout = self.jj_bytes(&[ |
| 60 | "log", |
| 61 | "--no-graph", |
| 62 | "-r", |
| 63 | "bookmarks()", |
| 64 | "-T", |
| 65 | "commit_id ++ \"\\t\" ++ local_bookmarks.map(|bookmark| bookmark.name()).join(\"\\t\") ++ \"\\n\"", |
| 66 | ])?; |
| 67 | let text = String::from_utf8(stdout).map_err(|e| RepoError::Jj(e.to_string()))?; |
| 68 | let mut bookmarks = Vec::new(); |
| 69 | for line in text.lines() { |
| 70 | let mut fields = line.split('\t'); |
| 71 | let id = fields |
| 72 | .next() |
| 73 | .ok_or_else(|| RepoError::Jj("malformed bookmark line from jj log".to_owned()))? |
| 74 | .parse() |
| 75 | .map_err(|_| RepoError::Jj("invalid oid from jj log".to_owned()))?; |
| 76 | for name in fields { |
| 77 | bookmarks.push((id, name.to_owned())); |
| 78 | } |
| 79 | } |
| 80 | Ok(bookmarks) |
| 81 | } |
| 82 | |
| 83 | /// Returns `commit`'s diff in unified Git format. |
| 84 | pub fn commit_diff(&self, commit: Oid) -> Result<Vec<u8>, RepoError> { |
| 85 | let commit = commit.to_string(); |
| 86 | self.jj_bytes(&["diff", "--git", "-r", &commit]) |
| 87 | } |
| 88 | |
| 89 | /// Returns a [`git::Repository`] for the embedded Git repository in this workspace. |
| 90 | pub fn git_repository(&self) -> Result<git::Repository, RepoError> { |
| 91 | let root = self.git_root()?; |
| 92 | git::Repository::from_git_dir(self.cwd.clone(), root) |
| 93 | } |
| 94 | |
| 95 | /// Resolves `revision` to a full commit hash. |
| 96 | pub fn resolve(&self, revision: &Rev) -> Result<Oid, RepoError> { |
| 97 | let text = self.jj_text(&[ |
| 98 | "log", |
| 99 | "--no-graph", |
| 100 | "-r", |
| 101 | revision.as_str(), |
| 102 | "-T", |
| 103 | "commit_id", |
| 104 | ])?; |
| 105 | text.trim() |
| 106 | .parse() |
| 107 | .map_err(|_| RepoError::Jj("invalid commit id from jj".to_owned())) |
| 108 | } |
| 109 | |
| 110 | /// Returns the path of the embedded Git repository. |
| 111 | fn git_root(&self) -> Result<PathBuf, RepoError> { |
| 112 | let text = self.jj_text(&["git", "root"])?; |
| 113 | Ok(PathBuf::from(text.trim())) |
| 114 | } |
| 115 | |
| 116 | /// Runs `jj` and returns its stdout bytes on success. |
| 117 | fn jj_bytes(&self, args: &[&str]) -> Result<Vec<u8>, RepoError> { |
| 118 | let output = Command::new("jj") |
| 119 | .args(args) |
| 120 | .current_dir(&self.cwd) |
| 121 | .output()?; |
| 122 | if output.status.success() { |
| 123 | Ok(output.stdout) |
| 124 | } else { |
| 125 | Err(RepoError::jj(&output)) |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | /// Runs `jj` and returns its stdout as UTF-8 text on success. |
| 130 | fn jj_text(&self, args: &[&str]) -> Result<String, RepoError> { |
| 131 | let bytes = self.jj_bytes(args)?; |
| 132 | String::from_utf8(bytes).map_err(|e| RepoError::Jj(e.to_string())) |
| 133 | } |
| 134 | } |