git: Fix bug where opening a git blob from historic commit view could fail (#44226)

The failure would happen if the current version of the file was open as
an editor. This happened because the git blob and current version of the
buffer would have the same `ProjectPath`.

The fix was adding a new `DiskState::Historic` variant to represent
buffers that are past versions of a file (usually a snapshot from
version control). Historic buffers don't return a `ProjectPath` because
the file isn't real, thus there isn't and shouldn't be a `ProjectPath`
to it. (At least with the current way we represent a project path)

I also change the display name to use the local OS's path style instead
of being hardcoded to Posix, and cleaned up some code too.

Release Notes:

- N/A

---------

Co-authored-by: Cole Miller <cole@zed.dev>
Co-authored-by: cameron <cameron.studdstreet@gmail.com>
Co-authored-by: xipengjin <jinxp18@gmail.com>
This commit is contained in:
Anthony Eid
2025-12-19 18:55:17 -05:00
committed by GitHub
parent 6dfabddbb4
commit 12dbbdd1d3
11 changed files with 130 additions and 149 deletions
+6 -48
View File
@@ -69,7 +69,7 @@ struct GitBlob {
path: RepoPath,
worktree_id: WorktreeId,
is_deleted: bool,
display_name: Arc<str>,
display_name: String,
}
const COMMIT_MESSAGE_SORT_PREFIX: u64 = 0;
@@ -243,9 +243,8 @@ impl CommitView {
.path
.file_name()
.map(|name| name.to_string())
.unwrap_or_else(|| file.path.display(PathStyle::Posix).to_string());
let display_name: Arc<str> =
Arc::from(format!("{short_sha} - {file_name}").into_boxed_str());
.unwrap_or_else(|| file.path.display(PathStyle::local()).to_string());
let display_name = format!("{short_sha} - {file_name}");
let file = Arc::new(GitBlob {
path: file.path.clone(),
@@ -661,15 +660,13 @@ impl language::File for GitBlob {
}
fn disk_state(&self) -> DiskState {
if self.is_deleted {
DiskState::Deleted
} else {
DiskState::New
DiskState::Historic {
was_deleted: self.is_deleted,
}
}
fn path_style(&self, _: &App) -> PathStyle {
PathStyle::Posix
PathStyle::local()
}
fn path(&self) -> &Arc<RelPath> {
@@ -697,45 +694,6 @@ impl language::File for GitBlob {
}
}
// No longer needed since metadata buffer is not created
// impl language::File for CommitMetadataFile {
// fn as_local(&self) -> Option<&dyn language::LocalFile> {
// None
// }
//
// fn disk_state(&self) -> DiskState {
// DiskState::New
// }
//
// fn path_style(&self, _: &App) -> PathStyle {
// PathStyle::Posix
// }
//
// fn path(&self) -> &Arc<RelPath> {
// &self.title
// }
//
// fn full_path(&self, _: &App) -> PathBuf {
// self.title.as_std_path().to_path_buf()
// }
//
// fn file_name<'a>(&'a self, _: &'a App) -> &'a str {
// self.title.file_name().unwrap_or("commit")
// }
//
// fn worktree_id(&self, _: &App) -> WorktreeId {
// self.worktree_id
// }
//
// fn to_proto(&self, _cx: &App) -> language::proto::File {
// unimplemented!()
// }
//
// fn is_private(&self) -> bool {
// false
// }
// }
async fn build_buffer(
mut text: String,
blob: Arc<dyn File>,