Skip to content

Commit d0c8407

Browse files
committed
Turn attribute files into a Cow to support other usecases… (#301)
…but it feels a little like a hack. Maybe this should tell us that we need to reorganize code elsewhere?
1 parent a86ed7b commit d0c8407

File tree

3 files changed

+7
-4
lines changed

3 files changed

+7
-4
lines changed

git-worktree/src/fs/cache/state.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::fs::PathOidMapping;
33
use bstr::{BStr, BString, ByteSlice};
44
use git_glob::pattern::Case;
55
use git_hash::oid;
6+
use std::borrow::Cow;
67
use std::path::Path;
78

89
type AttributeMatchGroup = git_attributes::MatchGroup<git_attributes::Attributes>;
@@ -150,7 +151,7 @@ impl Ignore {
150151
let ignore_path_relative = rela_dir.join(".gitignore");
151152
let ignore_path_relative = git_path::to_unix_separators_on_windows(git_path::into_bstr(ignore_path_relative));
152153
let ignore_file_in_index =
153-
attribute_files_in_index.binary_search_by(|t| t.0.cmp(ignore_path_relative.as_ref()));
154+
attribute_files_in_index.binary_search_by(|t| t.0.as_ref().cmp(ignore_path_relative.as_ref()));
154155
let follow_symlinks = ignore_file_in_index.is_err();
155156
if !self
156157
.stack
@@ -267,7 +268,7 @@ impl State {
267268
if is_ignore && !entry.flags.contains(git_index::entry::Flags::SKIP_WORKTREE) {
268269
return None;
269270
}
270-
Some((path, entry.id))
271+
Some((Cow::Borrowed(path), entry.id))
271272
} else {
272273
None
273274
}

git-worktree/src/fs/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use bstr::BStr;
2+
use std::borrow::Cow;
23
use std::path::PathBuf;
34

45
/// Common knowledge about the worktree that is needed across most interactions with the work tree
@@ -69,7 +70,7 @@ pub struct Cache<'paths> {
6970
attribute_files_in_index: Vec<PathOidMapping<'paths>>,
7071
}
7172

72-
pub(crate) type PathOidMapping<'paths> = (&'paths BStr, git_hash::ObjectId);
73+
pub(crate) type PathOidMapping<'paths> = (Cow<'paths, BStr>, git_hash::ObjectId);
7374

7475
///
7576
pub mod cache;

git-worktree/tests/worktree/fs/cache.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ mod create_directory {
118118
#[allow(unused)]
119119
mod ignore_and_attributes {
120120
use bstr::{BStr, ByteSlice};
121+
use std::borrow::Cow;
121122
use std::path::Path;
122123

123124
use git_glob::pattern::Case;
@@ -180,7 +181,7 @@ mod ignore_and_attributes {
180181
assert_eq!(
181182
attribute_files_in_index,
182183
vec![(
183-
"other-dir-with-ignore/.gitignore".as_bytes().as_bstr(),
184+
Cow::Borrowed("other-dir-with-ignore/.gitignore".as_bytes().as_bstr()),
184185
hex_to_id("5c7e0ed672d3d31d83a3df61f13cc8f7b22d5bfd")
185186
)]
186187
);

0 commit comments

Comments
 (0)