Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions crates/fs/src/fake_git_repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub struct FakeGitRepositoryState {
pub unmerged_paths: HashMap<RepoPath, UnmergedStatus>,
pub head_contents: HashMap<RepoPath, String>,
pub index_contents: HashMap<RepoPath, String>,
pub head_symlinks: HashSet<RepoPath>,
pub index_symlinks: HashSet<RepoPath>,
// everything in commit contents is in oids
pub merge_base_contents: HashMap<RepoPath, Oid>,
pub oids: HashMap<Oid, String>,
Expand All @@ -86,6 +88,8 @@ impl FakeGitRepositoryState {
event_emitter,
head_contents: Default::default(),
index_contents: Default::default(),
head_symlinks: Default::default(),
index_symlinks: Default::default(),
unmerged_paths: Default::default(),
blames: Default::default(),
current_branch_name: Default::default(),
Expand Down Expand Up @@ -163,24 +167,38 @@ impl FakeGitRepository {
impl GitRepository for FakeGitRepository {
fn load_index_text(&self, path: RepoPath) -> BoxFuture<'_, Option<String>> {
let fut = self.with_state_async(false, move |state| {
// Use git's recorded mode (like RealGitRepository's `git ls-files`), not the on-disk symlink.
if state.index_symlinks.contains(&path) {
return Ok(None);
}
state
.index_contents
.get(&path)
.context("not present in index")
.cloned()
.map(Some)
});
self.executor.spawn(async move { fut.await.ok() }).boxed()
self.executor
.spawn(async move { fut.await.ok().flatten() })
.boxed()
}
Comment thread
smitbarmase marked this conversation as resolved.
Resolved

fn load_committed_text(&self, path: RepoPath) -> BoxFuture<'_, Option<String>> {
let fut = self.with_state_async(false, move |state| {
// Use git's recorded mode (like RealGitRepository's `git ls-tree`), not the on-disk symlink.
if state.head_symlinks.contains(&path) {
return Ok(None);
}
state
.head_contents
.get(&path)
.context("not present in HEAD")
.cloned()
.map(Some)
});
self.executor.spawn(async move { fut.await.ok() }).boxed()
self.executor
.spawn(async move { fut.await.ok().flatten() })
.boxed()
}

fn load_commit_template(&self) -> BoxFuture<'_, Result<Option<GitCommitTemplate>>> {
Expand Down
12 changes: 12 additions & 0 deletions crates/fs/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2188,6 +2188,18 @@ impl FakeFs {
.unwrap();
}

pub fn set_symlinks_for_repo(&self, dot_git: &Path, symlink_paths: &[&str]) {
self.with_git_state(dot_git, true, |state| {
state.head_symlinks.clear();
state.index_symlinks.clear();
for path in symlink_paths {
state.head_symlinks.insert(repo_path(path));
state.index_symlinks.insert(repo_path(path));
}
})
.unwrap();
}

pub fn set_merge_base_content_for_repo(
&self,
dot_git: &Path,
Expand Down
206 changes: 204 additions & 2 deletions crates/git/src/repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,10 +1538,14 @@ impl GitRepository for RealGitRepository {

fn load_index_text(&self, path: RepoPath) -> BoxFuture<'_, Option<String>> {
let git_binary = self.git_binary();
let path_str = format!(":{}", path.as_unix_str());
let unix_path = path.as_unix_str().to_owned();
let path_str = format!(":{unix_path}");
self.executor
.spawn(async move {
let git = git_binary;
if git_entry_is_symlink(&git, &["ls-files", "--stage", "--", &unix_path]).await {
return None;
}
Comment thread
smitbarmase marked this conversation as resolved.
Resolved
let output = git
.build_command(&["show", &path_str])
.stdout(Stdio::piped())
Expand All @@ -1559,9 +1563,13 @@ impl GitRepository for RealGitRepository {

fn load_committed_text(&self, path: RepoPath) -> BoxFuture<'_, Option<String>> {
let git = self.git_binary();
let path_str = format!("HEAD:{}", path.as_unix_str());
let unix_path = path.as_unix_str().to_owned();
let path_str = format!("HEAD:{unix_path}");
self.executor
.spawn(async move {
if git_entry_is_symlink(&git, &["ls-tree", "HEAD", "--", &unix_path]).await {
return None;
}
let output = git
.build_command(&["show", &path_str])
.stdout(Stdio::piped())
Expand Down Expand Up @@ -3398,6 +3406,31 @@ async fn exclude_files(git: &GitBinary) -> Result<GitExcludeOverride> {
Ok(excludes)
}

async fn git_entry_is_symlink(git: &GitBinary, args: &[&str]) -> bool {
const GIT_MODE_SYMLINK: &str = "120000";

let Some(output) = git
.build_command(args)
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.output()
.await
.log_err()
else {
return false;
};

if !output.status.success() {
return false;
}

String::from_utf8(output.stdout)
.ok()
.and_then(|stdout| stdout.split_whitespace().next().map(str::to_owned))
.as_deref()
== Some(GIT_MODE_SYMLINK)
}

pub(crate) struct GitBinary {
git_binary_path: PathBuf,
working_directory: PathBuf,
Expand Down Expand Up @@ -4340,6 +4373,175 @@ mod tests {
);
}

#[cfg(unix)]
#[gpui::test]
async fn test_load_text_skips_symlinks(cx: &mut TestAppContext) {
disable_git_global_config();
cx.executor().allow_parking();

let repo_dir = tempfile::tempdir().unwrap();
git_init_repo(repo_dir.path());

smol::fs::write(repo_dir.path().join("target.txt"), "real contents\n")
.await
.unwrap();
std::os::unix::fs::symlink("target.txt", repo_dir.path().join("link.txt")).unwrap();

let repo = RealGitRepository::new(
&repo_dir.path().join(".git"),
None,
Some("git".into()),
cx.executor(),
)
.unwrap();

repo.stage_paths(
vec![repo_path("target.txt"), repo_path("link.txt")],
Arc::new(HashMap::default()),
)
.await
.unwrap();
repo.commit(
"Initial commit".into(),
None,
CommitOptions::default(),
AskPassDelegate::new(&mut cx.to_async(), |_, _, _| {}),
Arc::new(test_commit_envs()),
)
.await
.unwrap();

// regular file returns its contents
assert_eq!(
repo.load_index_text(repo_path("target.txt"))
.await
.as_deref(),
Some("real contents\n")
);
assert_eq!(
repo.load_committed_text(repo_path("target.txt"))
.await
.as_deref(),
Some("real contents\n")
);

// symlink returns no base text (its blob is just the target path)
assert_eq!(repo.load_index_text(repo_path("link.txt")).await, None);
assert_eq!(repo.load_committed_text(repo_path("link.txt")).await, None);
}

#[cfg(unix)]
#[gpui::test]
async fn test_load_text_uses_git_mode_for_worktree_only_symlink(cx: &mut TestAppContext) {
disable_git_global_config();
cx.executor().allow_parking();

let repo_dir = tempfile::tempdir().unwrap();
git_init_repo(repo_dir.path());

smol::fs::write(repo_dir.path().join("agents.md"), "regular contents\n")
.await
.unwrap();
smol::fs::write(repo_dir.path().join("target.txt"), "target\n")
.await
.unwrap();

let repo = RealGitRepository::new(
&repo_dir.path().join(".git"),
None,
Some("git".into()),
cx.executor(),
)
.unwrap();

repo.stage_paths(
vec![repo_path("agents.md"), repo_path("target.txt")],
Arc::new(HashMap::default()),
)
.await
.unwrap();
repo.commit(
"Initial commit".into(),
None,
CommitOptions::default(),
AskPassDelegate::new(&mut cx.to_async(), |_, _, _| {}),
Arc::new(test_commit_envs()),
)
.await
.unwrap();

// Replace the committed regular file with a symlink on disk, leaving it unstaged.
smol::fs::remove_file(repo_dir.path().join("agents.md"))
.await
.unwrap();
std::os::unix::fs::symlink("target.txt", repo_dir.path().join("agents.md")).unwrap();

// git still records a regular file, so both return its contents despite the on-disk symlink
assert_eq!(
repo.load_index_text(repo_path("agents.md"))
.await
.as_deref(),
Some("regular contents\n")
);
assert_eq!(
repo.load_committed_text(repo_path("agents.md"))
.await
.as_deref(),
Some("regular contents\n")
);
}

#[cfg(unix)]
#[gpui::test]
async fn test_load_text_uses_git_mode_for_git_only_symlink(cx: &mut TestAppContext) {
disable_git_global_config();
cx.executor().allow_parking();

let repo_dir = tempfile::tempdir().unwrap();
git_init_repo(repo_dir.path());

smol::fs::write(repo_dir.path().join("target.txt"), "target\n")
.await
.unwrap();
std::os::unix::fs::symlink("target.txt", repo_dir.path().join("agents.md")).unwrap();

let repo = RealGitRepository::new(
&repo_dir.path().join(".git"),
None,
Some("git".into()),
cx.executor(),
)
.unwrap();

repo.stage_paths(
vec![repo_path("agents.md"), repo_path("target.txt")],
Arc::new(HashMap::default()),
)
.await
.unwrap();
repo.commit(
"Initial commit".into(),
None,
CommitOptions::default(),
AskPassDelegate::new(&mut cx.to_async(), |_, _, _| {}),
Arc::new(test_commit_envs()),
)
.await
.unwrap();

// Replace the committed symlink with a regular file on disk, leaving it unstaged.
smol::fs::remove_file(repo_dir.path().join("agents.md"))
.await
.unwrap();
smol::fs::write(repo_dir.path().join("agents.md"), "now regular\n")
.await
.unwrap();

// git still records a symlink, so both are skipped despite the on-disk file
assert_eq!(repo.load_index_text(repo_path("agents.md")).await, None);
assert_eq!(repo.load_committed_text(repo_path("agents.md")).await, None);
}

#[gpui::test]
async fn test_checkpoint_basic(cx: &mut TestAppContext) {
disable_git_global_config();
Expand Down
Loading
Loading