Skip to content

Commit 0149de4

Browse files
authored
git: Fix panic in git2 due to empty repo paths (#42304)
Fixes ZED-1VR Release Notes: - Fixed sporadic panic in git features
1 parent 359160c commit 0149de4

File tree

13 files changed

+88
-82
lines changed

13 files changed

+88
-82
lines changed

crates/agent_servers/src/acp.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl AcpConnection {
136136
while let Ok(n) = stderr.read_line(&mut line).await
137137
&& n > 0
138138
{
139-
log::warn!("agent stderr: {}", &line);
139+
log::warn!("agent stderr: {}", line.trim());
140140
line.clear();
141141
}
142142
Ok(())

crates/editor/src/test/editor_test_context.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use buffer_diff::DiffHunkStatusKind;
66
use collections::BTreeMap;
77
use futures::Future;
88

9+
use git::repository::RepoPath;
910
use gpui::{
1011
AnyWindowHandle, App, Context, Entity, Focusable as _, Keystroke, Pixels, Point,
1112
VisualTestContext, Window, WindowHandle, prelude::*,
@@ -334,7 +335,10 @@ impl EditorTestContext {
334335
let path = self.update_buffer(|buffer, _| buffer.file().unwrap().path().clone());
335336
let mut found = None;
336337
fs.with_git_state(&Self::root_path().join(".git"), false, |git_state| {
337-
found = git_state.index_contents.get(&path.into()).cloned();
338+
found = git_state
339+
.index_contents
340+
.get(&RepoPath::from_rel_path(&path))
341+
.cloned();
338342
})
339343
.unwrap();
340344
assert_eq!(expected, found.as_deref());

crates/fs/src/fake_git_repo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ impl GitRepository for FakeGitRepository {
272272
.ok()
273273
.map(|content| String::from_utf8(content).unwrap())?;
274274
let repo_path = RelPath::new(repo_path, PathStyle::local()).ok()?;
275-
Some((repo_path.into(), (content, is_ignored)))
275+
Some((RepoPath::from_rel_path(&repo_path), (content, is_ignored)))
276276
})
277277
.collect();
278278

@@ -436,7 +436,7 @@ impl GitRepository for FakeGitRepository {
436436
state
437437
.blames
438438
.get(&path)
439-
.with_context(|| format!("failed to get blame for {:?}", path.0))
439+
.with_context(|| format!("failed to get blame for {:?}", path))
440440
.cloned()
441441
})
442442
}

crates/fs/src/fs.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1792,7 +1792,8 @@ impl FakeFs {
17921792
for (path, content) in workdir_contents {
17931793
use util::{paths::PathStyle, rel_path::RelPath};
17941794

1795-
let repo_path: RepoPath = RelPath::new(path.strip_prefix(&workdir_path).unwrap(), PathStyle::local()).unwrap().into();
1795+
let repo_path = RelPath::new(path.strip_prefix(&workdir_path).unwrap(), PathStyle::local()).unwrap();
1796+
let repo_path = RepoPath::from_rel_path(&repo_path);
17961797
let status = statuses
17971798
.iter()
17981799
.find_map(|(p, status)| (*p == repo_path.as_unix_str()).then_some(status));

crates/git/src/repository.rs

Lines changed: 36 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use rope::Rope;
1414
use schemars::JsonSchema;
1515
use serde::Deserialize;
1616
use smol::io::{AsyncBufReadExt, AsyncReadExt, BufReader};
17-
use std::borrow::Cow;
1817
use std::ffi::{OsStr, OsString};
1918
use std::process::{ExitStatus, Stdio};
2019
use std::{
@@ -848,7 +847,7 @@ impl GitRepository for RealGitRepository {
848847
}
849848

850849
files.push(CommitFile {
851-
path: rel_path.into(),
850+
path: RepoPath(Arc::from(rel_path)),
852851
old_text,
853852
new_text,
854853
})
@@ -2049,6 +2048,11 @@ fn git_status_args(path_prefixes: &[RepoPath]) -> Vec<OsString> {
20492048
OsString::from("--no-renames"),
20502049
OsString::from("-z"),
20512050
];
2051+
args.extend(
2052+
path_prefixes
2053+
.iter()
2054+
.map(|path_prefix| path_prefix.as_std_path().into()),
2055+
);
20522056
args.extend(path_prefixes.iter().map(|path_prefix| {
20532057
if path_prefix.is_empty() {
20542058
Path::new(".").into()
@@ -2304,52 +2308,54 @@ async fn run_askpass_command(
23042308
}
23052309
}
23062310

2307-
#[derive(Clone, Debug, Ord, Hash, PartialOrd, Eq, PartialEq)]
2308-
pub struct RepoPath(pub Arc<RelPath>);
2311+
#[derive(Clone, Ord, Hash, PartialOrd, Eq, PartialEq)]
2312+
pub struct RepoPath(Arc<RelPath>);
2313+
2314+
impl std::fmt::Debug for RepoPath {
2315+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
2316+
self.0.fmt(f)
2317+
}
2318+
}
23092319

23102320
impl RepoPath {
23112321
pub fn new<S: AsRef<str> + ?Sized>(s: &S) -> Result<Self> {
23122322
let rel_path = RelPath::unix(s.as_ref())?;
2313-
Ok(rel_path.into())
2314-
}
2315-
2316-
pub fn from_proto(proto: &str) -> Result<Self> {
2317-
let rel_path = RelPath::from_proto(proto)?;
2318-
Ok(rel_path.into())
2323+
Ok(Self::from_rel_path(rel_path))
23192324
}
23202325

23212326
pub fn from_std_path(path: &Path, path_style: PathStyle) -> Result<Self> {
23222327
let rel_path = RelPath::new(path, path_style)?;
2323-
Ok(Self(rel_path.as_ref().into()))
2328+
Ok(Self::from_rel_path(&rel_path))
23242329
}
2325-
}
23262330

2327-
#[cfg(any(test, feature = "test-support"))]
2328-
pub fn repo_path<S: AsRef<str> + ?Sized>(s: &S) -> RepoPath {
2329-
RepoPath(RelPath::unix(s.as_ref()).unwrap().into())
2330-
}
2331+
pub fn from_proto(proto: &str) -> Result<Self> {
2332+
let rel_path = RelPath::from_proto(proto)?;
2333+
Ok(Self(rel_path))
2334+
}
23312335

2332-
impl From<&RelPath> for RepoPath {
2333-
fn from(value: &RelPath) -> Self {
2334-
RepoPath(value.into())
2336+
pub fn from_rel_path(path: &RelPath) -> RepoPath {
2337+
Self(Arc::from(path))
23352338
}
2336-
}
23372339

2338-
impl<'a> From<Cow<'a, RelPath>> for RepoPath {
2339-
fn from(value: Cow<'a, RelPath>) -> Self {
2340-
value.as_ref().into()
2340+
pub fn as_std_path(&self) -> &Path {
2341+
// git2 does not like empty paths and our RelPath infra turns `.` into ``
2342+
// so undo that here
2343+
if self.is_empty() {
2344+
Path::new(".")
2345+
} else {
2346+
self.0.as_std_path()
2347+
}
23412348
}
23422349
}
23432350

2344-
impl From<Arc<RelPath>> for RepoPath {
2345-
fn from(value: Arc<RelPath>) -> Self {
2346-
RepoPath(value)
2347-
}
2351+
#[cfg(any(test, feature = "test-support"))]
2352+
pub fn repo_path<S: AsRef<str> + ?Sized>(s: &S) -> RepoPath {
2353+
RepoPath(RelPath::unix(s.as_ref()).unwrap().into())
23482354
}
23492355

2350-
impl Default for RepoPath {
2351-
fn default() -> Self {
2352-
RepoPath(RelPath::empty().into())
2356+
impl AsRef<Arc<RelPath>> for RepoPath {
2357+
fn as_ref(&self) -> &Arc<RelPath> {
2358+
&self.0
23532359
}
23542360
}
23552361

@@ -2361,12 +2367,6 @@ impl std::ops::Deref for RepoPath {
23612367
}
23622368
}
23632369

2364-
// impl AsRef<Path> for RepoPath {
2365-
// fn as_ref(&self) -> &Path {
2366-
// RelPath::as_ref(&self.0)
2367-
// }
2368-
// }
2369-
23702370
#[derive(Debug)]
23712371
pub struct RepoPathDescendants<'a>(pub &'a RepoPath);
23722372

crates/git/src/status.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ impl FromStr for GitStatus {
454454
let status = entry.as_bytes()[0..2].try_into().unwrap();
455455
let status = FileStatus::from_bytes(status).log_err()?;
456456
// git-status outputs `/`-delimited repo paths, even on Windows.
457-
let path = RepoPath(RelPath::unix(path).log_err()?.into());
457+
let path = RepoPath::from_rel_path(RelPath::unix(path).log_err()?);
458458
Some((path, status))
459459
})
460460
.collect::<Vec<_>>();
@@ -539,7 +539,7 @@ impl FromStr for TreeDiff {
539539
let mut fields = s.split('\0');
540540
let mut parsed = HashMap::default();
541541
while let Some((status, path)) = fields.next().zip(fields.next()) {
542-
let path = RepoPath(RelPath::unix(path)?.into());
542+
let path = RepoPath::from_rel_path(RelPath::unix(path)?);
543543

544544
let mut fields = status.split(" ").skip(2);
545545
let old_sha = fields

crates/git_ui/src/commit_view.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ impl language::File for GitBlob {
266266
}
267267

268268
fn path(&self) -> &Arc<RelPath> {
269-
&self.path.0
269+
self.path.as_ref()
270270
}
271271

272272
fn full_path(&self, _: &App) -> PathBuf {

crates/git_ui/src/git_panel.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ impl GitPanel {
879879
let active_repository = self.active_repository.as_ref()?.downgrade();
880880

881881
cx.spawn(async move |_, cx| {
882-
let file_path_str = repo_path.0.display(PathStyle::Posix);
882+
let file_path_str = repo_path.as_ref().display(PathStyle::Posix);
883883

884884
let repo_root = active_repository.read_with(cx, |repository, _| {
885885
repository.snapshot().work_directory_abs_path
@@ -1074,7 +1074,7 @@ impl GitPanel {
10741074
}
10751075
let mut details = entries
10761076
.iter()
1077-
.filter_map(|entry| entry.repo_path.0.file_name())
1077+
.filter_map(|entry| entry.repo_path.as_ref().file_name())
10781078
.map(|filename| filename.to_string())
10791079
.take(5)
10801080
.join("\n");
@@ -1129,7 +1129,7 @@ impl GitPanel {
11291129
.map(|entry| {
11301130
entry
11311131
.repo_path
1132-
.0
1132+
.as_ref()
11331133
.file_name()
11341134
.map(|f| f.to_string())
11351135
.unwrap_or_default()
@@ -5647,7 +5647,7 @@ mod tests {
56475647
assert_eq!(
56485648
entry.status_entry().map(|status| status
56495649
.repo_path
5650-
.0
5650+
.as_ref()
56515651
.as_std_path()
56525652
.to_string_lossy()
56535653
.to_string()),

crates/git_ui/src/project_diff.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl ProjectDiff {
336336
};
337337
let repo = git_repo.read(cx);
338338
let sort_prefix = sort_prefix(repo, &entry.repo_path, entry.status, cx);
339-
let path_key = PathKey::with_sort_prefix(sort_prefix, entry.repo_path.0);
339+
let path_key = PathKey::with_sort_prefix(sort_prefix, entry.repo_path.as_ref().clone());
340340

341341
self.move_to_path(path_key, window, cx)
342342
}
@@ -566,7 +566,7 @@ impl ProjectDiff {
566566
for entry in buffers_to_load.iter() {
567567
let sort_prefix = sort_prefix(&repo, &entry.repo_path, entry.file_status, cx);
568568
let path_key =
569-
PathKey::with_sort_prefix(sort_prefix, entry.repo_path.0.clone());
569+
PathKey::with_sort_prefix(sort_prefix, entry.repo_path.as_ref().clone());
570570
previous_paths.remove(&path_key);
571571
path_keys.push(path_key)
572572
}

crates/project/src/git_store.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ impl sum_tree::Item for StatusEntry {
227227

228228
fn summary(&self, _: <Self::Summary as sum_tree::Summary>::Context<'_>) -> Self::Summary {
229229
PathSummary {
230-
max_path: self.repo_path.0.clone(),
230+
max_path: self.repo_path.as_ref().clone(),
231231
item_summary: self.status.summary(),
232232
}
233233
}
@@ -237,7 +237,7 @@ impl sum_tree::KeyedItem for StatusEntry {
237237
type Key = PathKey;
238238

239239
fn key(&self) -> Self::Key {
240-
PathKey(self.repo_path.0.clone())
240+
PathKey(self.repo_path.as_ref().clone())
241241
}
242242
}
243243

@@ -990,7 +990,7 @@ impl GitStore {
990990
RepositoryState::Local { backend, .. } => backend
991991
.blame(repo_path.clone(), content)
992992
.await
993-
.with_context(|| format!("Failed to blame {:?}", repo_path.0))
993+
.with_context(|| format!("Failed to blame {:?}", repo_path.as_ref()))
994994
.map(Some),
995995
RepositoryState::Remote { project_id, client } => {
996996
let response = client
@@ -2376,7 +2376,7 @@ impl GitStore {
23762376
.entries
23772377
.into_iter()
23782378
.map(|(path, status)| proto::TreeDiffStatus {
2379-
path: path.0.to_proto(),
2379+
path: path.as_ref().to_proto(),
23802380
status: match status {
23812381
TreeDiffStatus::Added {} => proto::tree_diff_status::Status::Added.into(),
23822382
TreeDiffStatus::Modified { .. } => {
@@ -3152,13 +3152,13 @@ impl RepositorySnapshot {
31523152

31533153
pub fn status_for_path(&self, path: &RepoPath) -> Option<StatusEntry> {
31543154
self.statuses_by_path
3155-
.get(&PathKey(path.0.clone()), ())
3155+
.get(&PathKey(path.as_ref().clone()), ())
31563156
.cloned()
31573157
}
31583158

31593159
pub fn pending_ops_for_path(&self, path: &RepoPath) -> Option<PendingOps> {
31603160
self.pending_ops_by_path
3161-
.get(&PathKey(path.0.clone()), ())
3161+
.get(&PathKey(path.as_ref().clone()), ())
31623162
.cloned()
31633163
}
31643164

@@ -4727,7 +4727,9 @@ impl Repository {
47274727
}
47284728
};
47294729
Some((
4730-
RepoPath(RelPath::from_proto(&entry.path).log_err()?),
4730+
RepoPath::from_rel_path(
4731+
&RelPath::from_proto(&entry.path).log_err()?,
4732+
),
47314733
status,
47324734
))
47334735
})
@@ -5289,7 +5291,8 @@ impl Repository {
52895291
let mut cursor = prev_statuses.cursor::<PathProgress>(());
52905292
for path in changed_paths.into_iter() {
52915293
if cursor.seek_forward(&PathTarget::Path(&path), Bias::Left) {
5292-
changed_path_statuses.push(Edit::Remove(PathKey(path.0)));
5294+
changed_path_statuses
5295+
.push(Edit::Remove(PathKey(path.as_ref().clone())));
52935296
}
52945297
}
52955298
changed_path_statuses
@@ -5435,10 +5438,8 @@ fn get_permalink_in_rust_registry_src(
54355438
remote,
54365439
BuildPermalinkParams::new(
54375440
&cargo_vcs_info.git.sha1,
5438-
&RepoPath(
5439-
RelPath::new(&path, PathStyle::local())
5440-
.context("invalid path")?
5441-
.into_arc(),
5441+
&RepoPath::from_rel_path(
5442+
&RelPath::new(&path, PathStyle::local()).context("invalid path")?,
54425443
),
54435444
Some(selection),
54445445
),
@@ -5640,7 +5641,11 @@ async fn compute_snapshot(
56405641
let mut events = Vec::new();
56415642
let branches = backend.branches().await?;
56425643
let branch = branches.into_iter().find(|branch| branch.is_head);
5643-
let statuses = backend.status(&[RelPath::empty().into()]).await?;
5644+
let statuses = backend
5645+
.status(&[RepoPath::from_rel_path(
5646+
&RelPath::new(".".as_ref(), PathStyle::local()).unwrap(),
5647+
)])
5648+
.await?;
56445649
let stash_entries = backend.stash_entries().await?;
56455650
let statuses_by_path = SumTree::from_iter(
56465651
statuses

0 commit comments

Comments
 (0)