Skip to content

handle status when dir is replaced with file #1736

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 22, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 12 additions & 0 deletions gix-fs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ pub fn is_executable(metadata: &std::fs::Metadata) -> bool {
(metadata.mode() & 0o100) != 0
}

/// Classifiers for IO-errors.
pub mod io_err {
use std::io::ErrorKind;

/// Return `true` if `err` indicates that the entry doesn't exist on disk. `raw` is used as well
/// for additional checks while the variants are outside the MSRV.
pub fn is_not_found(err: ErrorKind, raw_err: Option<i32>) -> bool {
// TODO: use variant once MSRV is 1.83
err == ErrorKind::NotFound || raw_err == Some(20)
}
}

#[cfg(not(unix))]
/// Returns whether a a file has the executable permission set.
pub fn is_executable(_metadata: &std::fs::Metadata) -> bool {
Expand Down
12 changes: 8 additions & 4 deletions gix-status/src/index_as_worktree/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,10 @@ impl<'index> State<'_, 'index> {
{
let worktree_path = match self.path_stack.verified_path(gix_path::from_bstr(rela_path).as_ref()) {
Ok(path) => path,
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(Some(Change::Removed.into())),
Err(err) => return Err(Error::Io(err)),
Err(err) if gix_fs::io_err::is_not_found(err.kind(), err.raw_os_error()) => {
return Ok(Some(Change::Removed.into()))
}
Err(err) => return Err(err.into()),
};
self.symlink_metadata_calls.fetch_add(1, Ordering::Relaxed);
let metadata = match gix_index::fs::Metadata::from_path_no_follow(worktree_path) {
Expand All @@ -379,7 +381,9 @@ impl<'index> State<'_, 'index> {
}
}
Ok(metadata) => metadata,
Err(err) if err.kind() == io::ErrorKind::NotFound => return Ok(Some(Change::Removed.into())),
Err(err) if gix_fs::io_err::is_not_found(err.kind(), err.raw_os_error()) => {
return Ok(Some(Change::Removed.into()))
}
Err(err) => {
return Err(err.into());
}
Expand Down Expand Up @@ -539,7 +543,7 @@ where
// conversion to bstr can never fail because symlinks are only used
// on unix (by git) so no reason to use the try version here
let symlink_path =
gix_path::to_unix_separators_on_windows(gix_path::into_bstr(std::fs::read_link(self.path)?));
gix_path::to_unix_separators_on_windows(gix_path::into_bstr(std::fs::read_link(self.path).unwrap()));
self.buf.extend_from_slice(&symlink_path);
self.worktree_bytes.fetch_add(self.buf.len() as u64, Ordering::Relaxed);
Stream {
Expand Down
Binary file modified gix-status/tests/fixtures/generated-archives/status_many.tar
Binary file not shown.
13 changes: 13 additions & 0 deletions gix-status/tests/fixtures/status_many.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,16 @@ cp -R changed-and-untracked changed-and-untracked-and-renamed
echo change >> content-with-rewrite

)

cp -R changed-and-untracked replace-dir-with-file
(cd replace-dir-with-file
git checkout executable
rm untracked dir/untracked

mkdir dir/sub
touch dir/sub/nested
git add dir && git commit -m "add file in sub-directory"

rm -Rf dir/
touch dir
)
25 changes: 25 additions & 0 deletions gix-status/tests/status/index_as_worktree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,31 @@ fn removed() {
);
}

#[test]
fn replace_dir_with_file() {
let out = fixture_filtered_detailed(
"status_many",
"replace-dir-with-file",
&[],
&[
(BStr::new(b"dir/content"), 0, status_removed()),
(BStr::new(b"dir/content2"), 1, status_removed()),
(BStr::new(b"dir/sub/nested"), 2, status_removed()),
],
|_| {},
false,
);
assert_eq!(
out,
Outcome {
entries_to_process: 5,
entries_processed: 5,
symlink_metadata_calls: if cfg!(windows) { 5 } else { 4 },
..Default::default()
}
);
}

#[test]
fn subomdule_nochange() {
assert_eq!(
Expand Down
Loading