Skip to content

Commit

Permalink
Allow tests to work with symlinks on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 5, 2024
1 parent 03a4545 commit 3d2e015
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 49 deletions.
14 changes: 11 additions & 3 deletions gix-status/src/index_as_worktree/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -424,12 +424,19 @@ impl<'index> State<'_, 'index> {

self.buf.clear();
self.buf2.clear();
let file_size_bytes = if cfg!(windows) && metadata.is_symlink() {
// symlinks on Windows seem to have a length of zero, so just pretend
// they have the correct length to avoid short-cutting, and enforce a full buffer check.
entry.stat.size as u64
} else {
metadata.len()
};
let fetch_data = ReadDataImpl {
buf: &mut self.buf,
path: worktree_path,
rela_path,
entry,
file_len: metadata.len(),
file_len: file_size_bytes,
filter: &mut self.filter,
attr_stack: &mut self.attr_stack,
options: self.options,
Expand All @@ -440,7 +447,7 @@ impl<'index> State<'_, 'index> {
odb_reads: self.odb_reads,
odb_bytes: self.odb_bytes,
};
let content_change = diff.compare_blobs(entry, metadata.len(), fetch_data, &mut self.buf2)?;
let content_change = diff.compare_blobs(entry, file_size_bytes, fetch_data, &mut self.buf2)?;
// This file is racy clean! Set the size to 0 so we keep detecting this as the file is updated.
if content_change.is_some() || executable_bit_changed {
let set_entry_stat_size_zero = content_change.is_some() && racy_clean;
Expand Down Expand Up @@ -531,7 +538,8 @@ where
let out = if is_symlink && self.options.fs.symlink {
// 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::into_bstr(std::fs::read_link(self.path)?);
let symlink_path =
gix_path::to_unix_separators_on_windows(gix_path::into_bstr(std::fs::read_link(self.path)?));
self.buf.extend_from_slice(&symlink_path);
self.worktree_bytes.fetch_add(self.buf.len() as u64, Ordering::Relaxed);
Stream {
Expand Down
14 changes: 2 additions & 12 deletions gix-status/tests/stack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,21 +83,11 @@ fn paths_leading_through_symlinks_are_rejected() {
}

fn is_symlink(m: &std::fs::Metadata) -> bool {
if cfg!(windows) {
// On windows, symlinks can't be seen, at least not through std
m.is_file()
} else {
m.is_symlink()
}
m.is_symlink()
}

fn is_symlinked_dir(m: &std::fs::Metadata) -> bool {
if cfg!(windows) {
// On windows, symlinks can't be seen, at least not through std
m.is_dir()
} else {
m.is_symlink()
}
m.is_symlink()
}
fn is_file(m: &std::fs::Metadata) -> bool {
m.is_file()
Expand Down
22 changes: 2 additions & 20 deletions gix-status/tests/status/index_as_worktree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,16 +469,7 @@ fn refresh() {
}
.into(),
),
(
BStr::new("empty"),
3,
Change::Modification {
executable_bit_changed: false,
content_change: Some(()),
set_entry_stat_size_zero: false
}
.into(),
),
(BStr::new("empty"), 3, Change::Type.into()),
(
BStr::new("executable"),
4,
Expand Down Expand Up @@ -551,16 +542,7 @@ fn modified() {
}
.into(),
),
(
BStr::new("empty"),
3,
Change::Modification {
executable_bit_changed: false,
content_change: Some(()),
set_entry_stat_size_zero: false,
}
.into(),
),
(BStr::new("empty"), 3, Change::Type.into()),
(
BStr::new("executable"),
4,
Expand Down
16 changes: 12 additions & 4 deletions gix-worktree-state/src/checkout/entry.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::{
fs::OpenOptions,
io::Write,
Expand Down Expand Up @@ -145,12 +146,19 @@ where
err,
path: dest.to_path_buf(),
})?;
let symlink_destination = gix_path::try_from_byte_slice(obj.data)
.map_err(|_| crate::checkout::Error::IllformedUtf8 { path: obj.data.into() })?;

if symlink {
#[cfg_attr(not(windows), allow(unused_mut))]
let mut symlink_destination = Cow::Borrowed(
gix_path::try_from_byte_slice(obj.data)
.map_err(|_| crate::checkout::Error::IllformedUtf8 { path: obj.data.into() })?,
);
#[cfg(windows)]
{
symlink_destination = gix_path::to_native_path_on_windows(gix_path::into_bstr(symlink_destination))
}

try_op_or_unlink(dest, overwrite_existing, |p| {
gix_fs::symlink::create(symlink_destination, p)
gix_fs::symlink::create(symlink_destination.as_ref(), p)
})?;
} else {
let mut file = try_op_or_unlink(dest, overwrite_existing, |p| {
Expand Down
12 changes: 2 additions & 10 deletions gix-worktree-stream/tests/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,7 @@ mod from_tree {
} else {
EntryKind::BlobExecutable
};
let expected_link_mode = if cfg!(windows) {
EntryKind::Blob
} else {
EntryKind::Link
};
let expected_link_mode = EntryKind::Link;
assert_eq!(
paths_and_modes,
&[
Expand All @@ -141,11 +137,7 @@ mod from_tree {
(
"symlink-to-a".into(),
expected_link_mode,
hex_to_id(if cfg!(windows) {
"45b983be36b73c0788dc9cbcb76cbb80fc7bb057"
} else {
"2e65efe2a145dda7ee51d1741299f848e5bf752e"
})
hex_to_id("2e65efe2a145dda7ee51d1741299f848e5bf752e")
),
(
"dir/.gitattributes".into(),
Expand Down

0 comments on commit 3d2e015

Please sign in to comment.