git: Handle buffer file path changes (#41944)

Update `GitStore.on_buffer_store_event` so that, when a
`BufferStoreEvent::BufferChangedFilePath` event is received, we check if
there's any diff state for the buffer and, if so, update it according to
the new file path, in case the file exists in the repository.

Closes #40499

Release Notes:

- Fixed issue with git diff tracking when updating a buffer's file from
an untracked to a tracked file
This commit is contained in:
Dino
2025-11-10 18:19:08 +00:00
committed by GitHub
parent c748b177c4
commit d896af2f15
2 changed files with 151 additions and 0 deletions
+37
View File
@@ -1399,7 +1399,44 @@ impl GitStore {
diffs.remove(buffer_id);
}
}
BufferStoreEvent::BufferChangedFilePath { buffer, .. } => {
// Whenever a buffer's file path changes, it's possible that the
// new path is actually a path that is being tracked by a git
// repository. In that case, we'll want to update the buffer's
// `BufferDiffState`, in case it already has one.
let buffer_id = buffer.read(cx).remote_id();
let diff_state = self.diffs.get(&buffer_id);
let repo = self.repository_and_path_for_buffer_id(buffer_id, cx);
if let Some(diff_state) = diff_state
&& let Some((repo, repo_path)) = repo
{
let buffer = buffer.clone();
let diff_state = diff_state.clone();
cx.spawn(async move |_git_store, cx| {
async {
let diff_bases_change = repo
.update(cx, |repo, cx| {
repo.load_committed_text(buffer_id, repo_path, cx)
})?
.await?;
diff_state.update(cx, |diff_state, cx| {
let buffer_snapshot = buffer.read(cx).text_snapshot();
diff_state.diff_bases_changed(
buffer_snapshot,
Some(diff_bases_change),
cx,
);
})
}
.await
.log_err();
})
.detach();
}
}
_ => {}
}
}