//! Jujutsu plumbing handle for reading workspace state. use std::path::PathBuf; use std::process::Command; use crate::{Oid, RepoError, Rev, git}; /// Jujutsu repository command context. #[derive(Clone, Debug)] pub struct Repository { /// Workspace directory used for `jj` invocations. cwd: PathBuf, } impl Repository { /// Creates a new Jujutsu repository context rooted at `cwd`. pub fn new(cwd: impl Into) -> Self { Self { cwd: cwd.into() } } /// Returns commits in `base..target`, oldest first, as `(id, summary)` pairs. pub fn commits(&self, base: &Rev, target: &Rev) -> Result, RepoError> { let revset = format!("{}..{}", base.as_str(), target.as_str()); let stdout = self.jj_bytes(&[ "log", "--no-graph", "--reversed", "-r", &revset, "-T", "commit_id ++ \" \" ++ description.first_line() ++ \"\\n\"", ])?; let text = String::from_utf8(stdout).map_err(|e| RepoError::Jj(e.to_string()))?; let mut commits = Vec::new(); for line in text.lines() { let Some((id, summary)) = line.split_once(' ') else { return Err(RepoError::Jj( "malformed commit line from jj log".to_owned(), )); }; let id = id .parse() .map_err(|_| RepoError::Jj("invalid oid from jj log".to_owned()))?; commits.push((id, summary.trim_start().to_owned())); } Ok(commits) } /// Returns the fork point between `target` and the configured trunk. pub fn branch_base(&self, target: &Rev) -> Result { let revset = format!("fork_point(({}) | trunk())", target.as_str()); let revset = Rev::try_from(revset.as_str()) .map_err(|error| RepoError::Jj(format!("invalid branch base revset: {error}")))?; self.resolve(&revset) } /// Returns local bookmark names paired with their target commits. pub fn bookmarks(&self) -> Result, RepoError> { let stdout = self.jj_bytes(&[ "log", "--no-graph", "-r", "bookmarks()", "-T", "commit_id ++ \"\\t\" ++ local_bookmarks.map(|bookmark| bookmark.name()).join(\"\\t\") ++ \"\\n\"", ])?; let text = String::from_utf8(stdout).map_err(|e| RepoError::Jj(e.to_string()))?; let mut bookmarks = Vec::new(); for line in text.lines() { let mut fields = line.split('\t'); let id = fields .next() .ok_or_else(|| RepoError::Jj("malformed bookmark line from jj log".to_owned()))? .parse() .map_err(|_| RepoError::Jj("invalid oid from jj log".to_owned()))?; for name in fields { bookmarks.push((id, name.to_owned())); } } Ok(bookmarks) } /// Returns `commit`'s diff in unified Git format. pub fn commit_diff(&self, commit: Oid) -> Result, RepoError> { let commit = commit.to_string(); self.jj_bytes(&["diff", "--git", "-r", &commit]) } /// Returns a [`git::Repository`] for the embedded Git repository in this workspace. pub fn git_repository(&self) -> Result { let root = self.git_root()?; git::Repository::from_git_dir(self.cwd.clone(), root) } /// Resolves `revision` to a full commit hash. pub fn resolve(&self, revision: &Rev) -> Result { let text = self.jj_text(&[ "log", "--no-graph", "-r", revision.as_str(), "-T", "commit_id", ])?; text.trim() .parse() .map_err(|_| RepoError::Jj("invalid commit id from jj".to_owned())) } /// Returns the path of the embedded Git repository. fn git_root(&self) -> Result { let text = self.jj_text(&["git", "root"])?; Ok(PathBuf::from(text.trim())) } /// Runs `jj` and returns its stdout bytes on success. fn jj_bytes(&self, args: &[&str]) -> Result, RepoError> { let output = Command::new("jj") .args(args) .current_dir(&self.cwd) .output()?; if output.status.success() { Ok(output.stdout) } else { Err(RepoError::jj(&output)) } } /// Runs `jj` and returns its stdout as UTF-8 text on success. fn jj_text(&self, args: &[&str]) -> Result { let bytes = self.jj_bytes(args)?; String::from_utf8(bytes).map_err(|e| RepoError::Jj(e.to_string())) } }