Radish alpha
h
Radicle Heartwood Protocol & Stack
Radicle
Git (anonymous pull)
Log in to clone via SSH
radicle: Detect current repository using `jj`
✗ CI failure Lorenz Leutgeb committed 7 months ago
commit 34c3bc7134a3d473482b3d8b7b573ddf384482cb
parent 5fea9ac05c7296dce3f8dec363b7442bec929c55
1 passed 1 failed (2 total) View logs
1 file changed +39 -2
modified crates/radicle/src/rad.rs
@@ -372,6 +372,15 @@ pub fn remove_remote(repo: &git2::Repository) -> Result<(), RemoteError> {
    Ok(())
}

+
#[derive(Error, Debug)]
+
pub enum CwdError {
+
    #[error(transparent)]
+
    Remote(#[from] RemoteError),
+

+
    #[error(transparent)]
+
    Jujutsu(#[from] JujutsuGitRootError),
+
}
+

/// Get the RID of the repository in current working directory
///
/// It will atempt to search parent directories if `path` did not find
@@ -382,8 +391,8 @@ pub fn remove_remote(repo: &git2::Repository) -> Result<(), RemoteError> {
/// This function should only perform read operations since we do not
/// want to modify the wrong repository in the case that it found a
/// Git repository that is not a Radicle repository.
-
pub fn cwd() -> Result<(git2::Repository, RepoId), RemoteError> {
-
    let repo = repo()?;
+
pub fn cwd() -> Result<(git2::Repository, RepoId), CwdError> {
+
    let repo = repo().or_else(|_| jj_git_root())?;
    let (_, id) = remote(&repo)?;

    Ok((repo, id))
@@ -411,6 +420,34 @@ pub fn repo() -> Result<git2::Repository, git2::Error> {
    Ok(repo)
}

+
#[derive(Error, Debug)]
+
pub enum JujutsuGitRootError {
+
    #[error("git: {0}")]
+
    Git(#[from] git2::Error),
+

+
    #[error("i/o: {0}")]
+
    Io(#[from] io::Error),
+

+
    #[error("jj exited with status {status}")]
+
    CommandFailure { status: std::process::ExitStatus },
+
}
+

+
/// Get the Git repo backing the current Jujutsu repository, if possible.
+
pub fn jj_git_root() -> Result<git2::Repository, JujutsuGitRootError> {
+
    let output = std::process::Command::new("jj")
+
        .args(["git", "root"])
+
        .output()?;
+

+
    if !output.status.success() {
+
        return Err(JujutsuGitRootError::CommandFailure {
+
            status: output.status,
+
        });
+
    }
+

+
    let path = std::path::PathBuf::from(String::from_utf8_lossy(&output.stdout).to_string().trim());
+
    Ok(git2::Repository::open(path)?)
+
}
+

/// Setup patch upstream branch such that `git push` updates the patch.
pub fn setup_patch_upstream<'a>(
    patch: &ObjectId,