project: Load diff bases for many buffers in one git process (#67026)

## Summary

Opening a diff loads its base text with
`Repository::load_committed_text` (HEAD and index, for uncommitted
diffs) or `Repository::load_staged_text` (index, for unstaged diffs).
Each call was a separate git job with its own `git cat-file --batch`
process. A repository runs its git jobs one at a time, so opening many
diffs at once queued one process per buffer, and other git work (status,
staging, blame) waited behind them. Project search results open an
uncommitted diff for every buffer they show, and the Uncommitted Changes
view one per changed file. The branch diff view and the `default_branch`
diff base open an unstaged diff per file. Each process reads the index
to resolve `:path`, so the cost grows with the repository.

Loads requested while their job is queued join it and share one process.
Only the most recently queued job takes new loads, which keeps their
order relative to other git jobs. The job owns its batch, so a job
dropped before it runs cancels its loads. If the batch fails, each load
retries in its own process, so an unreadable object fails only its own
diff. The batch decodes its texts on a background thread. Remote
repositories still send one request per buffer.

Diff base reloads already loaded every open buffer's bases in one batch,
and a failed batch cleared the bases of every buffer. Reloads retry the
same way, and a buffer whose own load fails keeps its bases.

`load_revisions` discards git's stderr, which it never read. When a
`refs/tags/HEAD` ref exists, git writes an ambiguity warning for each
`HEAD` lookup, and about 1,700 lookups filled a 64 KiB pipe and blocked
git mid-batch, in opens and reloads alike.

Benchmark: open the first N files of a checkout as buffers in a headless
app with `RealFs`, wait 5 s for the initial git status, then time
opening all of their diffs at once. Binary files don't open as buffers,
hence 983 and 996. Warm cache, Ryzen 7 9800X3D, git 2.53, median of 5
alternating runs with and without this change:

| Checkout | Index | Diff | Diffs | Before | After | Speedup |
|---|---|---|---|---|---|---|
| Zed | 0.5 MB | uncommitted | 100 | 95.0 ms | 29.6 ms | 3.2× |
| Zed | 0.5 MB | uncommitted | 983 | 980 ms | 313 ms | 3.1× |
| Zed | 0.5 MB | unstaged | 100 | 81.8 ms | 17.2 ms | 4.8× |
| Zed | 0.5 MB | unstaged | 983 | 793 ms | 169 ms | 4.7× |
| Linux | 10.5 MB | uncommitted | 100 | 418 ms | 32.6 ms | 13× |
| Linux | 10.5 MB | uncommitted | 1,000 | 4.31 s | 281 ms | 15× |
| Linux | 10.5 MB | unstaged | 100 | 398 ms | 20.0 ms | 20× |
| Linux | 10.5 MB | unstaged | 1,000 | 4.08 s | 155 ms | 26× |
| Firefox | 72 MB | uncommitted | 100 | 2.43 s | 56.1 ms | 43× |
| Firefox | 72 MB | uncommitted | 996 | 25.4 s | 314 ms | 81× |
| Firefox | 72 MB | unstaged | 100 | 2.52 s | 43.5 ms | 58× |
| Firefox | 72 MB | unstaged | 996 | 24.5 s | 183 ms | 134× |

git's CPU time drops by a similar factor, for example from 25.1 s to 95
ms for 996 uncommitted diffs in Firefox.

GitHub-hosted runners, 1,000 diffs, shallow clones, one build that
switches to the old code with an environment variable, median of 5
alternating runs:

| Runner | Checkout | Diff | Before | After | Speedup |
|---|---|---|---|---|---|
| `ubuntu-latest` | Linux | uncommitted | 13.3 s | 598 ms | 22× |
| `ubuntu-latest` | Linux | unstaged | 13.2 s | 294 ms | 45× |
| `ubuntu-latest` | rust-lang/rust | uncommitted | 10.1 s | 638 ms | 16×
|
| `ubuntu-latest` | rust-lang/rust | unstaged | 10.0 s | 354 ms | 28× |
| `windows-latest` | rust-lang/rust | uncommitted | 45.2 s | 1.05 s |
43× |
| `windows-latest` | rust-lang/rust | unstaged | 44.5 s | 444 ms | 100×
|

## Testing

- `test_diffs_opened_together` opens three uncommitted diffs (index
equal to HEAD, index differing from HEAD, untracked) and one unstaged
diff in one batch, and checks each buffer's HEAD and index text.
- `test_diff_base_loads_fail_when_their_job_is_dropped` drops a queued
job and closes the job channel, and checks that its loads and later ones
fail instead of hanging.
- `test_diff_base_loads_join_only_the_newest_queued_job` checks that two
loads share one job, and that a load queued after an index write gets
its own job and sees the new index text.
- `test_diff_base_load_with_unreadable_revision_fails_alone` makes one
HEAD revision unreadable in the fake repository and checks that the
other loads, including the index text of the same file, succeed.
- `test_diff_base_reload_with_unreadable_revision_fails_alone` reloads
three diffs while one HEAD revision is unreadable, and checks that only
that file keeps its old bases.
- `test_load_revisions_with_ambiguous_head` loads 4,000 `HEAD:`
revisions with a `refs/tags/HEAD` ref. Without the stderr change, the
test scheduler reports a deadlock after 15 seconds.
- `cargo test -p project --features test-support`, `cargo test -p git`,
`cargo test -p fs`, `cargo test -p remote_server --lib`, and
`./script/clippy -p project -p git -p fs` pass.

## Self-review

- [x] I've reviewed my diff for quality, security, reliability, and
performance.
- [x] UI changes follow the
[checklist](https://zed.dev/docs/development/ui-checklist).
- [x] Tests cover the new or changed behavior.

Release Notes:

- Improved git diff loading when many files open at once, such as in
project search results and the Uncommitted Changes and branch diff views
This commit is contained in:
Parker Loflin
2026-11-18 17:19:54 +00:00
committed by GitHub
parent 8c514d28ed
commit 5cffb90247
4 changed files with 573 additions and 78 deletions
+17
View File
@@ -76,6 +76,10 @@ pub struct FakeGitRepositoryState {
pub simulated_index_write_error_message: Option<String>,
pub simulated_create_worktree_error: Option<String>,
pub simulated_graph_error: Option<String>,
/// Revisions that fail any `load_revisions` call that includes them.
pub simulated_unreadable_revisions: HashSet<String>,
/// The revisions passed to each `load_revisions` call.
pub load_revisions_calls: Vec<Vec<String>>,
pub branches_requiring_force_delete: HashSet<String>,
pub worktrees_requiring_force_delete: HashSet<PathBuf>,
pub refs: HashMap<String, String>,
@@ -104,6 +108,8 @@ impl FakeGitRepositoryState {
simulated_index_write_error_message: Default::default(),
simulated_create_worktree_error: Default::default(),
simulated_graph_error: None,
simulated_unreadable_revisions: HashSet::default(),
load_revisions_calls: Vec::new(),
branches_requiring_force_delete: Default::default(),
worktrees_requiring_force_delete: Default::default(),
refs: HashMap::from_iter([("HEAD".into(), "abc".into())]),
@@ -385,6 +391,17 @@ impl GitRepository for FakeGitRepository {
revisions: Vec<String>,
) -> BoxFuture<'_, Result<Vec<Option<Vec<u8>>>>> {
let fut = self.with_state_async(false, move |state| {
state.load_revisions_calls.push(revisions.clone());
// Like `git cat-file --batch`, which reads one revision per line.
if let Some(revision) = revisions.iter().find(|revision| revision.contains('\n')) {
bail!("revision spec {revision:?} contains a newline");
}
if let Some(revision) = revisions
.iter()
.find(|revision| state.simulated_unreadable_revisions.contains(*revision))
{
bail!("simulated failure to read {revision}");
}
Ok(revisions
.into_iter()
.map(|rev| {
+30 -1
View File
@@ -1824,11 +1824,13 @@ impl GitRepository for RealGitRepository {
);
}
// `git` blocks once an unread stderr pipe fills, for example with one ambiguity
// warning per `HEAD` lookup when `refs/tags/HEAD` exists.
let mut process = git
.build_command(&["cat-file", "--batch"])
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.stderr(Stdio::null())
.kill_on_drop(true)
.spawn()?;
@@ -5863,6 +5865,33 @@ mod tests {
);
}
#[gpui::test]
async fn test_load_revisions_with_ambiguous_head(cx: &mut TestAppContext) {
disable_git_global_config();
cx.executor().allow_parking();
let repo_dir = tempfile::tempdir().unwrap();
git_init_repo(repo_dir.path());
fs::write(repo_dir.path().join("file"), "contents").unwrap();
git_command(repo_dir.path(), ["add", "file"]);
git_command(repo_dir.path(), ["commit", "-m", "Initial commit"]);
git_command(repo_dir.path(), ["update-ref", "refs/tags/HEAD", "HEAD"]);
let repo = RealGitRepository::new(
&repo_dir.path().join(".git"),
None,
Some("git".into()),
cx.executor(),
)
.unwrap();
// Writes about 150 KB of warnings to stderr, more than a pipe buffer holds.
let results = repo
.load_revisions(vec!["HEAD:file".to_string(); 4000])
.await
.unwrap();
assert_eq!(results, vec![Some(b"contents".to_vec()); 4000]);
}
#[gpui::test]
async fn test_blame_at_revision(cx: &mut TestAppContext) {
disable_git_global_config();
+439 -77
View File
@@ -71,12 +71,14 @@ use settings::{GitDiffBaseSetting, Settings, SettingsLocation, SettingsStore, Wo
use smallvec::SmallVec;
use smol::future::yield_now;
use std::{
cell::RefCell,
cmp::Ordering,
collections::{BTreeSet, HashSet, VecDeque, hash_map::Entry},
future::Future,
mem,
ops::Range,
path::{Path, PathBuf},
rc::{Rc, Weak},
str::FromStr,
sync::{
Arc,
@@ -220,6 +222,160 @@ fn pending_hunks(
.collect()
}
struct DiffBaseLoad {
buffer_id: BufferId,
repo_path: RepoPath,
kind: DiffBaseLoadKind,
}
enum DiffBaseLoadKind {
/// HEAD and index text.
Committed(oneshot::Sender<Result<DiffBasesChange>>),
/// Index text.
Staged(oneshot::Sender<Result<Option<String>>>),
}
impl DiffBaseLoad {
fn revisions(&self) -> Vec<String> {
let path = self.repo_path.as_unix_str();
match self.kind {
DiffBaseLoadKind::Committed(_) => vec![format!("HEAD:{path}"), format!(":{path}")],
DiffBaseLoadKind::Staged(_) => vec![format!(":{path}")],
}
}
/// Sends this load's result, given the result of loading its `revisions`.
fn finish(self, texts: Result<Vec<Option<Vec<u8>>>>) {
match self.kind {
DiffBaseLoadKind::Committed(result_tx) => {
let result = texts.and_then(|texts| {
let mut texts = texts.into_iter();
let head = texts.next().flatten().map(decode_git_text).transpose()?;
let index = texts.next().flatten().map(decode_git_text).transpose()?;
Ok(if head == index {
DiffBasesChange::SetBoth(head)
} else {
DiffBasesChange::SetEach { index, head }
})
});
result_tx.send(result).ok();
}
// Like `GitRepository::load_index_text`, treats an unreadable index entry as a
// missing one, so the unstaged diff still opens.
DiffBaseLoadKind::Staged(result_tx) => {
let index = texts
.context("failed to load index text")
.warn_on_err()
.and_then(|texts| texts.into_iter().next().flatten());
result_tx.send(index.map(decode_git_text).transpose()).ok();
}
}
}
}
/// Loads all groups' revisions with one `git cat-file` process. One unreadable revision fails the
/// whole batch, so after a failure each group is retried in its own process.
async fn load_revision_groups(
backend: &dyn GitRepository,
mut groups: Vec<Vec<String>>,
) -> Vec<Result<Vec<Option<Vec<u8>>>>> {
// `git cat-file --batch` reads one revision per line, so `load_revisions` fails any batch with
// a newline in a revision. Loading those groups on their own keeps them from sending every
// other group to the retries below.
let separate_groups = groups
.iter_mut()
.enumerate()
.filter(|(_, group)| group.iter().any(|revision| revision.contains('\n')))
.map(|(index, group)| (index, mem::take(group)))
.collect::<Vec<_>>();
let mut results = match backend.load_revisions(groups.concat()).await {
Ok(texts) => {
let mut texts = texts.into_iter();
groups
.iter()
.map(|group| Ok(texts.by_ref().take(group.len()).collect()))
.collect()
}
Err(error) if groups.len() == 1 => vec![Err(error)],
Err(error) => {
log::warn!(
"retrying {} revision groups separately: {error:#}",
groups.len()
);
let mut results = Vec::with_capacity(groups.len());
for group in groups {
results.push(backend.load_revisions(group).await);
}
results
}
};
for (index, group) in separate_groups {
if let Some(result) = results.get_mut(index) {
*result = backend.load_revisions(group).await;
}
}
results
}
async fn load_diff_bases(state: RepositoryState, loads: Vec<DiffBaseLoad>, cx: AsyncApp) {
match state {
RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
// A batch can hold many texts, so decode them off the main thread.
cx.background_spawn(async move {
let groups = loads.iter().map(DiffBaseLoad::revisions).collect();
let results = load_revision_groups(backend.as_ref(), groups).await;
for (load, texts) in loads.into_iter().zip(results) {
load.finish(texts);
}
})
.await;
}
RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
use proto::open_uncommitted_diff_response::Mode;
let project_id = project_id.to_proto();
for load in loads {
let buffer_id = load.buffer_id.to_proto();
match load.kind {
DiffBaseLoadKind::Committed(result_tx) => {
let result = async {
let response = client
.request(proto::OpenUncommittedDiff {
project_id,
buffer_id,
})
.await?;
let mode =
Mode::try_from(response.mode).ok().context("Invalid mode")?;
Ok(match mode {
Mode::IndexMatchesHead => {
DiffBasesChange::SetBoth(response.committed_text)
}
Mode::IndexAndHead => DiffBasesChange::SetEach {
head: response.committed_text,
index: response.staged_text,
},
})
}
.await;
result_tx.send(result).ok();
}
DiffBaseLoadKind::Staged(result_tx) => {
let result = client
.request(proto::OpenUnstagedDiff {
project_id,
buffer_id,
})
.await
.map(|response| response.staged_text);
result_tx.send(result).ok();
}
}
}
}
}
}
fn decode_git_text(bytes: Vec<u8>) -> Result<String> {
let text = decode_text(bytes)?.text;
anyhow::ensure!(
@@ -702,6 +858,8 @@ pub struct Repository {
job_debug_queue: job_debug_queue::GitJobDebugQueue,
pending_ops: SumTree<PendingOps>,
job_id: JobId,
// The most recent diff base load job's loads, and `job_id` just after that job was queued.
pending_diff_base_loads: (JobId, Weak<RefCell<Vec<DiffBaseLoad>>>),
askpass_delegates: RemoteAskPassDelegates,
latest_askpass_id: u64,
repository_state: Shared<Task<Result<RepositoryState, String>>>,
@@ -6513,6 +6671,8 @@ impl Repository {
.shared();
self.job_sender.close_channel();
self._worker_task = Task::ready(());
// Loads queued on the old worker are canceled with it, so later loads need a new job.
self.pending_diff_base_loads = Default::default();
self.active_jobs.clear();
self.job_debug_queue
.mark_unfinished_complete(job_debug_queue::CompletedJobStatus::Skipped);
@@ -6597,6 +6757,7 @@ impl Repository {
latest_askpass_id: 0,
job_sender: mpsc::unbounded().0,
job_id: 0,
pending_diff_base_loads: Default::default(),
active_jobs: Default::default(),
job_debug_queue: job_debug_queue::GitJobDebugQueue::new(),
initial_graph_data: Default::default(),
@@ -6652,6 +6813,7 @@ impl Repository {
active_jobs: Default::default(),
job_debug_queue: job_debug_queue::GitJobDebugQueue::new(),
job_id: 0,
pending_diff_base_loads: Default::default(),
initial_graph_data: Default::default(),
commit_data: Default::default(),
commit_data_handler: CommitDataHandlerState::Closed,
@@ -6750,29 +6912,34 @@ impl Repository {
let buffer_diff_base_changes = cx
.background_spawn(async move {
let mut revisions = Vec::new();
let mut revision_groups = Vec::new();
for (_, repo_path, is_symlink, current_index_text, current_head_text) in
&repo_diff_state_updates
{
let mut revisions = Vec::new();
if current_index_text.is_some() && !*is_symlink {
revisions.push(format!(":{}", repo_path.as_unix_str()));
}
if current_head_text.is_some() && !*is_symlink {
revisions.push(format!("HEAD:{}", repo_path.as_unix_str()));
}
revision_groups.push(revisions);
}
let mut loaded_revisions = backend
.load_revisions(revisions)
.await
.log_err()
.into_iter()
.flatten();
let loaded_groups =
load_revision_groups(backend.as_ref(), revision_groups).await;
let mut changes = Vec::new();
for (buffer, _, is_symlink, current_index_text, current_head_text) in
&repo_diff_state_updates
for (
(buffer, _, is_symlink, current_index_text, current_head_text),
loaded_revisions,
) in repo_diff_state_updates.iter().zip(loaded_groups)
{
// A buffer whose texts fail to load keeps its current diff bases.
let Some(loaded_revisions) = loaded_revisions.log_err() else {
changes.push((buffer.clone(), None));
continue;
};
let mut loaded_revisions = loaded_revisions.into_iter();
let index_text = (current_index_text.is_some() && !*is_symlink)
.then(|| loaded_revisions.next().flatten())
.flatten()
@@ -10309,25 +10476,13 @@ impl Repository {
repo_path: RepoPath,
cx: &App,
) -> Task<Result<Option<String>>> {
let rx = self.send_job("load_staged_text", None, move |state, _| async move {
match state {
RepositoryState::Local(LocalRepositoryState { backend, .. }) => backend
.load_index_text(repo_path)
.await
.map(decode_git_text)
.transpose(),
RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
let response = client
.request(proto::OpenUnstagedDiff {
project_id: project_id.to_proto(),
buffer_id: buffer_id.to_proto(),
})
.await?;
Ok(response.staged_text)
}
}
let (result_tx, result_rx) = oneshot::channel();
self.queue_diff_base_load(DiffBaseLoad {
buffer_id,
repo_path,
kind: DiffBaseLoadKind::Staged(result_tx),
});
cx.spawn(|_: &mut AsyncApp| async move { rx.await? })
cx.spawn(|_: &mut AsyncApp| async move { result_rx.await? })
}
fn load_committed_text(
@@ -10336,57 +10491,35 @@ impl Repository {
repo_path: RepoPath,
cx: &App,
) -> Task<Result<DiffBasesChange>> {
let rx = self.send_job("load_committed_text", None, move |state, _| async move {
match state {
RepositoryState::Local(LocalRepositoryState { backend, .. }) => {
let revisions = vec![
format!("HEAD:{}", repo_path.as_unix_str()),
format!(":{}", repo_path.as_unix_str()),
];
let mut loaded_revisions = backend.load_revisions(revisions).await?.into_iter();
let committed_text = loaded_revisions
.next()
.flatten()
.map(decode_git_text)
.transpose()?;
let staged_text = loaded_revisions
.next()
.flatten()
.map(decode_git_text)
.transpose()?;
let diff_bases_change = if committed_text == staged_text {
DiffBasesChange::SetBoth(committed_text)
} else {
DiffBasesChange::SetEach {
index: staged_text,
head: committed_text,
}
};
anyhow::Ok(diff_bases_change)
}
RepositoryState::Remote(RemoteRepositoryState { project_id, client }) => {
use proto::open_uncommitted_diff_response::Mode;
let response = client
.request(proto::OpenUncommittedDiff {
project_id: project_id.to_proto(),
buffer_id: buffer_id.to_proto(),
})
.await?;
let mode = Mode::try_from(response.mode).ok().context("Invalid mode")?;
let bases = match mode {
Mode::IndexMatchesHead => DiffBasesChange::SetBoth(response.committed_text),
Mode::IndexAndHead => DiffBasesChange::SetEach {
head: response.committed_text,
index: response.staged_text,
},
};
Ok(bases)
}
}
let (result_tx, result_rx) = oneshot::channel();
self.queue_diff_base_load(DiffBaseLoad {
buffer_id,
repo_path,
kind: DiffBaseLoadKind::Committed(result_tx),
});
cx.spawn(|_: &mut AsyncApp| async move { result_rx.await? })
}
cx.spawn(|_: &mut AsyncApp| async move { rx.await? })
fn queue_diff_base_load(&mut self, load: DiffBaseLoad) {
// Joining only the most recently queued job keeps loads ordered with other git jobs.
let (next_job_id, loads) = &self.pending_diff_base_loads;
if *next_job_id == self.job_id
&& let Some(loads) = loads.upgrade()
{
loads.borrow_mut().push(load);
return;
}
// The job owns the only `Rc`, so its loads are canceled if it's dropped without running,
// and no load can join once it starts.
let loads = Rc::new(RefCell::new(vec![load]));
let pending_loads = Rc::downgrade(&loads);
let _ = self.send_job("load_diff_bases", None, move |state, cx| async move {
let loads = Rc::into_inner(loads)
.map(RefCell::into_inner)
.unwrap_or_default();
load_diff_bases(state, loads, cx).await;
});
self.pending_diff_base_loads = (self.job_id, pending_loads);
}
pub fn load_commit_template_text(
@@ -12070,6 +12203,235 @@ mod tests {
});
}
async fn setup_repository(
cx: &mut TestAppContext,
contents_by_path: &[(&str, String)],
) -> (Arc<FakeFs>, Entity<Project>, Entity<Repository>) {
use util::path;
let project_root = Path::new(path!("/project"));
let fs = FakeFs::new(cx.executor());
fs.insert_tree(project_root, json!({ ".git": {} })).await;
fs.set_head_and_index_for_repo(&project_root.join(".git"), contents_by_path);
let project = Project::test(fs.clone(), [project_root], cx).await;
project
.update(cx, |project, cx| project.git_scans_complete(cx))
.await;
let repository =
project.read_with(cx, |project, cx| project.active_repository(cx).unwrap());
(fs, project, repository)
}
#[gpui::test]
async fn test_diff_base_loads_fail_when_their_job_is_dropped(cx: &mut TestAppContext) {
init_test(cx);
let (_, _, repository) = setup_repository(cx, &[]).await;
let load_both = |repository: &mut Repository, cx: &mut Context<Repository>| {
let buffer_id = BufferId::new(1).unwrap();
(
repository.load_committed_text(buffer_id, repo_path("a.txt"), cx),
repository.load_staged_text(buffer_id, repo_path("a.txt"), cx),
)
};
let queued_loads = repository.update(cx, |repository, cx| {
let loads = load_both(repository, cx);
// Cancels the worker. Running the executor then drops its queued job and its end of
// the job channel.
repository._worker_task = Task::ready(());
loads
});
cx.run_until_parked();
let unsent_loads = repository.update(cx, load_both);
cx.run_until_parked();
for (committed, staged) in [queued_loads, unsent_loads] {
assert!(committed.now_or_never().expect("load hung").is_err());
assert!(staged.now_or_never().expect("load hung").is_err());
}
}
#[gpui::test]
async fn test_diff_base_loads_join_only_the_newest_queued_job(cx: &mut TestAppContext) {
init_test(cx);
let (_, _, repository) = setup_repository(cx, &[("a.txt", "old\n".into())]).await;
let ((committed, staged), index_write, staged_after_write) =
repository.update(cx, |repository, cx| {
let buffer_id = BufferId::new(1).unwrap();
let first_job_id = repository.job_id;
let batched_loads = (
repository.load_committed_text(buffer_id, repo_path("a.txt"), cx),
repository.load_staged_text(buffer_id, repo_path("a.txt"), cx),
);
assert_eq!(repository.job_id, first_job_id + 1);
let index_write = repository.spawn_set_index_text_job(
repo_path("a.txt"),
Some("new\n".into()),
encoding_rs::UTF_8,
false,
None,
cx,
);
let staged_after_write =
repository.load_staged_text(buffer_id, repo_path("a.txt"), cx);
assert_eq!(repository.job_id, first_job_id + 3);
(batched_loads, index_write, staged_after_write)
});
assert!(matches!(
committed.await.unwrap(),
DiffBasesChange::SetBoth(Some(text)) if text == "old\n"
));
assert_eq!(staged.await.unwrap().as_deref(), Some("old\n"));
index_write.await.unwrap().unwrap();
assert_eq!(staged_after_write.await.unwrap().as_deref(), Some("new\n"));
}
#[gpui::test]
async fn test_diff_base_load_with_unreadable_revision_fails_alone(cx: &mut TestAppContext) {
init_test(cx);
let paths = ["a.txt", "b.txt", "c.txt"];
let (fs, _, repository) =
setup_repository(cx, &paths.map(|path| (path, format!("{path}\n")))).await;
fs.with_git_state(Path::new(util::path!("/project/.git")), false, |state| {
state
.simulated_unreadable_revisions
.insert("HEAD:b.txt".into());
})
.unwrap();
let ([committed_a, committed_b, committed_c], staged_b) =
repository.update(cx, |repository, cx| {
let buffer_id = BufferId::new(1).unwrap();
(
paths
.map(|path| repository.load_committed_text(buffer_id, repo_path(path), cx)),
repository.load_staged_text(buffer_id, repo_path("b.txt"), cx),
)
});
assert!(matches!(
committed_a.await.unwrap(),
DiffBasesChange::SetBoth(Some(text)) if text == "a.txt\n"
));
assert!(committed_b.await.is_err());
assert!(matches!(
committed_c.await.unwrap(),
DiffBasesChange::SetBoth(Some(text)) if text == "c.txt\n"
));
assert_eq!(staged_b.await.unwrap().as_deref(), Some("b.txt\n"));
}
#[gpui::test]
async fn test_diff_base_load_with_newline_in_path_loads_separately(cx: &mut TestAppContext) {
init_test(cx);
let paths = ["a.txt", "new\nline.txt", "c.txt"];
let (fs, _, repository) =
setup_repository(cx, &paths.map(|path| (path, format!("{path}\n")))).await;
let dot_git = Path::new(util::path!("/project/.git"));
cx.run_until_parked();
fs.with_git_state(dot_git, false, |state| state.load_revisions_calls.clear())
.unwrap();
let [committed_a, committed_newline, committed_c] =
repository.update(cx, |repository, cx| {
let buffer_id = BufferId::new(1).unwrap();
paths.map(|path| repository.load_committed_text(buffer_id, repo_path(path), cx))
});
assert!(matches!(
committed_a.await.unwrap(),
DiffBasesChange::SetBoth(Some(text)) if text == "a.txt\n"
));
assert!(committed_newline.await.is_err());
assert!(matches!(
committed_c.await.unwrap(),
DiffBasesChange::SetBoth(Some(text)) if text == "c.txt\n"
));
let calls = fs
.with_git_state(dot_git, false, |state| {
mem::take(&mut state.load_revisions_calls)
})
.unwrap();
assert_eq!(
calls,
[
vec!["HEAD:a.txt", ":a.txt", "HEAD:c.txt", ":c.txt"],
vec!["HEAD:new\nline.txt", ":new\nline.txt"],
]
);
}
#[gpui::test]
async fn test_diff_base_load_after_worker_respawn_starts_a_new_job(cx: &mut TestAppContext) {
init_test(cx);
let (fs, project, repository) = setup_repository(cx, &[("a.txt", "a\n".into())]).await;
let environment = project.read_with(cx, |project, _| project.environment().downgrade());
let (canceled_load, load) = repository.update(cx, |repository, cx| {
let buffer_id = BufferId::new(1).unwrap();
let canceled_load = repository.load_staged_text(buffer_id, repo_path("a.txt"), cx);
repository.respawn_local_worker(environment, fs, true, cx);
let load = repository.load_staged_text(buffer_id, repo_path("a.txt"), cx);
(canceled_load, load)
});
assert!(canceled_load.await.is_err());
assert_eq!(load.await.unwrap().as_deref(), Some("a\n"));
}
#[gpui::test]
async fn test_diff_base_reload_with_unreadable_revision_fails_alone(cx: &mut TestAppContext) {
use util::path;
init_test(cx);
let project_root = Path::new(path!("/project"));
let dot_git = project_root.join(".git");
let names = ["a.txt", "b.txt", "c.txt"];
let fs = FakeFs::new(cx.executor());
fs.insert_tree(
project_root,
json!({ ".git": {}, "a.txt": "a\n", "b.txt": "b\n", "c.txt": "c\n" }),
)
.await;
fs.set_head_and_index_for_repo(&dot_git, &names.map(|name| (name, format!("old {name}"))));
let project = Project::test(fs.clone(), [project_root], cx).await;
let mut buffers_and_diffs = Vec::new();
for name in names {
let buffer = project
.update(cx, |project, cx| {
project.open_local_buffer(project_root.join(name), cx)
})
.await
.unwrap();
let diff = project
.update(cx, |project, cx| {
project.open_uncommitted_diff(buffer.clone(), cx)
})
.await
.unwrap();
buffers_and_diffs.push((buffer, diff));
}
fs.with_git_state(&dot_git, false, |state| {
state
.simulated_unreadable_revisions
.insert("HEAD:b.txt".into());
})
.unwrap();
fs.set_head_and_index_for_repo(&dot_git, &names.map(|name| (name, format!("new {name}"))));
cx.run_until_parked();
let base_texts = buffers_and_diffs
.iter()
.map(|(_, diff)| diff.read_with(cx, |diff, cx| diff.base_text_string(cx)))
.collect::<Vec<_>>();
assert_eq!(
base_texts,
[
Some("new a.txt".into()),
Some("old b.txt".into()),
Some("new c.txt".into()),
]
);
}
#[gpui::test]
async fn test_merge_base_list_announces_a_repeated_reload_failure_once(
cx: &mut TestAppContext,
@@ -15973,6 +15973,93 @@ async fn test_staged_diff_without_unstaged_diff(cx: &mut gpui::TestAppContext) {
});
}
#[gpui::test]
async fn test_diffs_opened_together(cx: &mut gpui::TestAppContext) {
init_test(cx);
let fs = FakeFs::new(cx.background_executor.clone());
fs.insert_tree(
path!("/dir"),
json!({
".git": {},
"same.txt": "same working\n",
"staged.txt": "staged working\n",
"untracked.txt": "untracked working\n",
"unstaged.txt": "unstaged working\n",
}),
)
.await;
fs.set_head_for_repo(
path!("/dir/.git").as_ref(),
&[
("same.txt", "same head\n".into()),
("staged.txt", "staged head\n".into()),
("unstaged.txt", "unstaged head\n".into()),
],
"deadbeef",
);
fs.set_index_for_repo(
path!("/dir/.git").as_ref(),
&[
("same.txt", "same head\n".into()),
("staged.txt", "staged index\n".into()),
("unstaged.txt", "unstaged index\n".into()),
],
);
let project = Project::test(fs, [path!("/dir").as_ref()], cx).await;
let mut buffers = Vec::new();
for name in ["same.txt", "staged.txt", "untracked.txt", "unstaged.txt"] {
let buffer = project
.update(cx, |project, cx| {
project.open_local_buffer(Path::new(path!("/dir")).join(name), cx)
})
.await
.unwrap();
buffers.push(buffer);
}
// Requested in one update, so all four loads share one job.
let (uncommitted_diffs, unstaged_diff) = project.update(cx, |project, cx| {
(
buffers[..3]
.iter()
.map(|buffer| project.open_uncommitted_diff(buffer.clone(), cx))
.collect::<Vec<_>>(),
project.open_unstaged_diff(buffers[3].clone(), cx),
)
});
let uncommitted_diffs = future::try_join_all(uncommitted_diffs).await.unwrap();
let unstaged_diff = unstaged_diff.await.unwrap();
cx.run_until_parked();
let base_texts = uncommitted_diffs
.iter()
.map(|diff| {
diff.read_with(cx, |diff, cx| {
let index_text = diff
.secondary_diff()
.and_then(|unstaged_diff| unstaged_diff.read(cx).base_text_string(cx));
(diff.base_text_string(cx), index_text)
})
})
.collect::<Vec<_>>();
assert_eq!(
base_texts,
[
(Some("same head\n".into()), Some("same head\n".into())),
(Some("staged head\n".into()), Some("staged index\n".into())),
(None, None),
]
);
unstaged_diff.read_with(cx, |diff, cx| {
assert_eq!(
diff.base_text_string(cx).as_deref(),
Some("unstaged index\n")
);
});
}
#[gpui::test]
async fn test_uncommitted_diff_for_buffer(cx: &mut gpui::TestAppContext) {
init_test(cx);