git: Compute and synchronize diffs from HEAD (#23626)

This PR builds on #21258 to make it possible to use HEAD as a diff base.
The buffer store is extended to support holding multiple change sets,
and collab gains support for synchronizing the committed text of files
when any collaborator requires it.

Not implemented in this PR:

- Exposing the diff from HEAD to the user
- Decorating the diff from HEAD with information about which hunks are
staged

`test_random_multibuffer` now fails first at `SEED=13277`, similar to
the previous high-water mark, but with various bugs in the multibuffer
logic now shaken out.

Release Notes:

- N/A

---------

Co-authored-by: Max <max@zed.dev>
Co-authored-by: Ben <ben@zed.dev>
Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
Co-authored-by: Conrad <conrad@zed.dev>
This commit is contained in:
Cole Miller 2025-02-04 15:29:10 -05:00 committed by GitHub
parent 871f98bc4d
commit 5704b50fb1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
29 changed files with 2799 additions and 603 deletions

View file

@ -29,9 +29,15 @@ pub struct Branch {
pub trait GitRepository: Send + Sync {
fn reload_index(&self);
/// Loads a git repository entry's contents.
/// Returns the contents of an entry in the repository's index, or None if there is no entry for the given path.
///
/// Note that for symlink entries, this will return the contents of the symlink, not the target.
fn load_index_text(&self, relative_file_path: &Path) -> Option<String>;
fn load_index_text(&self, path: &RepoPath) -> Option<String>;
/// Returns the contents of an entry in the repository's HEAD, or None if HEAD does not exist or has no entry for the given path.
///
/// Note that for symlink entries, this will return the contents of the symlink, not the target.
fn load_committed_text(&self, path: &RepoPath) -> Option<String>;
/// Returns the URL of the remote with the given name.
fn remote_url(&self, name: &str) -> Option<String>;
@ -106,15 +112,15 @@ impl GitRepository for RealGitRepository {
repo.path().into()
}
fn load_index_text(&self, relative_file_path: &Path) -> Option<String> {
fn logic(repo: &git2::Repository, relative_file_path: &Path) -> Result<Option<String>> {
fn load_index_text(&self, path: &RepoPath) -> Option<String> {
fn logic(repo: &git2::Repository, path: &RepoPath) -> Result<Option<String>> {
const STAGE_NORMAL: i32 = 0;
let index = repo.index()?;
// This check is required because index.get_path() unwraps internally :(
check_path_to_repo_path_errors(relative_file_path)?;
check_path_to_repo_path_errors(path)?;
let oid = match index.get_path(relative_file_path, STAGE_NORMAL) {
let oid = match index.get_path(path, STAGE_NORMAL) {
Some(entry) if entry.mode != GIT_MODE_SYMLINK => entry.id,
_ => return Ok(None),
};
@ -123,13 +129,22 @@ impl GitRepository for RealGitRepository {
Ok(Some(String::from_utf8(content)?))
}
match logic(&self.repository.lock(), relative_file_path) {
match logic(&self.repository.lock(), path) {
Ok(value) => return value,
Err(err) => log::error!("Error loading head text: {:?}", err),
Err(err) => log::error!("Error loading index text: {:?}", err),
}
None
}
fn load_committed_text(&self, path: &RepoPath) -> Option<String> {
let repo = self.repository.lock();
let head = repo.head().ok()?.peel_to_tree().log_err()?;
let oid = head.get_path(path).ok()?.id();
let content = repo.find_blob(oid).log_err()?.content().to_owned();
let content = String::from_utf8(content).log_err()?;
Some(content)
}
fn remote_url(&self, name: &str) -> Option<String> {
let repo = self.repository.lock();
let remote = repo.find_remote(name).ok()?;
@ -325,8 +340,9 @@ pub struct FakeGitRepository {
pub struct FakeGitRepositoryState {
pub dot_git_dir: PathBuf,
pub event_emitter: smol::channel::Sender<PathBuf>,
pub index_contents: HashMap<PathBuf, String>,
pub blames: HashMap<PathBuf, Blame>,
pub head_contents: HashMap<RepoPath, String>,
pub index_contents: HashMap<RepoPath, String>,
pub blames: HashMap<RepoPath, Blame>,
pub statuses: HashMap<RepoPath, FileStatus>,
pub current_branch_name: Option<String>,
pub branches: HashSet<String>,
@ -343,6 +359,7 @@ impl FakeGitRepositoryState {
FakeGitRepositoryState {
dot_git_dir,
event_emitter,
head_contents: Default::default(),
index_contents: Default::default(),
blames: Default::default(),
statuses: Default::default(),
@ -355,9 +372,14 @@ impl FakeGitRepositoryState {
impl GitRepository for FakeGitRepository {
fn reload_index(&self) {}
fn load_index_text(&self, path: &Path) -> Option<String> {
fn load_index_text(&self, path: &RepoPath) -> Option<String> {
let state = self.state.lock();
state.index_contents.get(path).cloned()
state.index_contents.get(path.as_ref()).cloned()
}
fn load_committed_text(&self, path: &RepoPath) -> Option<String> {
let state = self.state.lock();
state.head_contents.get(path.as_ref()).cloned()
}
fn remote_url(&self, _name: &str) -> Option<String> {
@ -529,6 +551,12 @@ impl From<&Path> for RepoPath {
}
}
impl From<Arc<Path>> for RepoPath {
fn from(value: Arc<Path>) -> Self {
RepoPath(value)
}
}
impl From<PathBuf> for RepoPath {
fn from(value: PathBuf) -> Self {
RepoPath::new(value)