Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions crates/git/src/git.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pub mod blame;
pub mod commit;
mod hosting_provider;
mod jj;
mod remote;
pub mod repository;
pub mod stash;
Expand Down
46 changes: 46 additions & 0 deletions crates/git/src/jj.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use anyhow::{Context as _, Result};

use std::path::Path;
use util::command::new_command;

pub(crate) async fn visible_heads(work_directory: &Path) -> Result<Option<Vec<String>>> {
if !work_directory.join(".jj").is_dir() || !jj_binary_is_available().await {
return Ok(None);
}

let mut command = new_command("jj");
command
.arg("--repository")
.arg(work_directory)
.arg("--ignore-working-copy")
.args(["--color", "never"])
.arg("--quiet")
.arg("--no-pager")
.arg("log")
.args(["-r", "visible_heads()"])
.arg("--no-graph")
.args(["-T", r#"commit_id ++ "\n""#]);

let output = command.output().await?;
anyhow::ensure!(
output.status.success(),
"jj command failed: {}",
String::from_utf8_lossy(&output.stderr)
);

let head_revisions = String::from_utf8(output.stdout)?
.lines()
.map(str::trim)
.filter(|line| !line.is_empty())
.map(ToOwned::to_owned)
.collect::<Vec<_>>();
Ok(Some(head_revisions))
}

async fn jj_binary_is_available() -> bool {
new_command("jj")
.arg("--version")
.output()
.await
.is_ok_and(|output| output.status.success())
}
43 changes: 41 additions & 2 deletions crates/git/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1064,6 +1064,25 @@ impl RealGitRepository {
*self.any_git_binary_help_output.lock() = Some(output.clone());
output
}

async fn resolve_log_source<'a>(
&self,
log_source: &'a LogSource,
) -> Result<(&'a str, Option<Vec<String>>)> {
match log_source {
LogSource::All => match crate::jj::visible_heads(&self.working_directory()?).await {
Ok(Some(head_revisions)) => return Ok(("--stdin", Some(head_revisions))),
Ok(None) => {}
Err(error) => {
log::warn!(
"resolve_log_source: failed to read visible heads from jj: {error:#}"
);
}
},
LogSource::Branch(_) | LogSource::Sha(_) => {}
}
Ok((log_source.get_arg()?, None))
}
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -2874,17 +2893,27 @@ impl GitRepository for RealGitRepository {

async move {
let git = git_binary?;
let (log_source_arg, revisions) = self.resolve_log_source(&log_source).await?;

let mut command = git.build_command(&[
"log",
GRAPH_COMMIT_FORMAT,
log_order.as_arg(),
log_source.get_arg()?,
log_source_arg,
]);
command.stdin(Stdio::piped());
command.stdout(Stdio::piped());
command.stderr(Stdio::piped());

let mut child = command.spawn()?;
if let Some(revisions) = revisions {
let mut stdin = child.stdin.take().context("failed to get stdin")?;
for revision in revisions {
stdin.write_all(revision.as_bytes()).await?;
stdin.write_all(b"\n").await?;
}
stdin.flush().await?;
}
let stdout = child.stdout.take().context("failed to get stdout")?;
let stderr = child.stderr.take().context("failed to get stderr")?;
let mut reader = BufReader::new(stdout);
Expand Down Expand Up @@ -2950,8 +2979,9 @@ impl GitRepository for RealGitRepository {

async move {
let git = git_binary?;
let (log_source_arg, revisions) = self.resolve_log_source(&log_source).await?;

let mut args = vec!["log", SEARCH_COMMIT_FORMAT, log_source.get_arg()?];
let mut args = vec!["log", SEARCH_COMMIT_FORMAT, log_source_arg];

args.push("--fixed-strings");

Expand All @@ -2963,10 +2993,19 @@ impl GitRepository for RealGitRepository {
args.push(search_args.query.as_str());

let mut command = git.build_command(&args);
command.stdin(Stdio::piped());
command.stdout(Stdio::piped());
command.stderr(Stdio::null());

let mut child = command.spawn()?;
if let Some(revisions) = revisions {
let mut stdin = child.stdin.take().context("failed to get stdin")?;
for revision in revisions {
stdin.write_all(revision.as_bytes()).await?;
stdin.write_all(b"\n").await?;
}
stdin.flush().await?;
}
let stdout = child.stdout.take().context("failed to get stdout")?;
let mut reader = BufReader::new(stdout);

Expand Down
Loading