repo_path: rename to_internal_string() to separate names for dir vs file

This commit is contained in:
Martin von Zweigbergk 2021-05-16 23:18:33 -07:00
parent 257ea39e68
commit ef726be78b
2 changed files with 22 additions and 18 deletions

View file

@ -64,7 +64,7 @@ pub struct RepoPath {
impl Debug for RepoPath { impl Debug for RepoPath {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
f.write_fmt(format_args!("{:?}", &self.to_internal_string())) f.write_fmt(format_args!("{:?}", &self.to_internal_file_string()))
} }
} }
@ -84,8 +84,8 @@ impl RepoPath {
/// The full string form used internally, not for presenting to users (where /// The full string form used internally, not for presenting to users (where
/// we may want to use the platform's separator). /// we may want to use the platform's separator).
pub fn to_internal_string(&self) -> String { pub fn to_internal_file_string(&self) -> String {
self.dir.to_internal_string() + self.basename.value() self.dir.to_internal_dir_string() + self.basename.value()
} }
pub fn to_dir_repo_path(&self) -> DirRepoPath { pub fn to_dir_repo_path(&self) -> DirRepoPath {
@ -151,7 +151,7 @@ pub struct DirRepoPath {
impl Debug for DirRepoPath { impl Debug for DirRepoPath {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> {
f.write_fmt(format_args!("{:?}", &self.to_internal_string())) f.write_fmt(format_args!("{:?}", &self.to_internal_dir_string()))
} }
} }
@ -166,7 +166,7 @@ impl DirRepoPath {
/// The full string form used internally, not for presenting to users (where /// The full string form used internally, not for presenting to users (where
/// we may want to use the platform's separator). /// we may want to use the platform's separator).
pub fn to_internal_string(&self) -> String { pub fn to_internal_dir_string(&self) -> String {
let mut result = String::new(); let mut result = String::new();
for component in &self.value { for component in &self.value {
result.push_str(component.value()); result.push_str(component.value());
@ -272,14 +272,17 @@ mod tests {
#[test] #[test]
fn test_value() { fn test_value() {
assert_eq!(RepoPath::root().to_internal_string(), ""); assert_eq!(RepoPath::root().to_internal_file_string(), "");
assert_eq!(RepoPath::from("dir").to_internal_string(), "dir"); assert_eq!(RepoPath::from("dir").to_internal_file_string(), "dir");
assert_eq!(RepoPath::from("file").to_internal_string(), "file"); assert_eq!(RepoPath::from("file").to_internal_file_string(), "file");
assert_eq!(RepoPath::from("dir/file").to_internal_string(), "dir/file");
assert_eq!(DirRepoPath::root().to_internal_string(), "");
assert_eq!(DirRepoPath::from("dir/").to_internal_string(), "dir/");
assert_eq!( assert_eq!(
DirRepoPath::from("dir/subdir/").to_internal_string(), RepoPath::from("dir/file").to_internal_file_string(),
"dir/file"
);
assert_eq!(DirRepoPath::root().to_internal_dir_string(), "");
assert_eq!(DirRepoPath::from("dir/").to_internal_dir_string(), "dir/");
assert_eq!(
DirRepoPath::from("dir/subdir/").to_internal_dir_string(),
"dir/subdir/" "dir/subdir/"
); );
} }

View file

@ -229,9 +229,10 @@ impl TreeState {
let mut proto = crate::protos::working_copy::TreeState::new(); let mut proto = crate::protos::working_copy::TreeState::new();
proto.tree_id = self.tree_id.0.clone(); proto.tree_id = self.tree_id.0.clone();
for (file, file_state) in &self.file_states { for (file, file_state) in &self.file_states {
proto proto.file_states.insert(
.file_states file.to_internal_file_string(),
.insert(file.to_internal_string(), file_state_to_proto(file_state)); file_state_to_proto(file_state),
);
} }
let mut temp_file = NamedTempFile::new_in(&self.state_path).unwrap(); let mut temp_file = NamedTempFile::new_in(&self.state_path).unwrap();
@ -332,7 +333,7 @@ impl TreeState {
let (dir, disk_dir, git_ignore) = work.pop().unwrap(); let (dir, disk_dir, git_ignore) = work.pop().unwrap();
let git_ignore = TreeState::try_chain_gitignore( let git_ignore = TreeState::try_chain_gitignore(
&git_ignore, &git_ignore,
&dir.to_internal_string(), &dir.to_internal_dir_string(),
disk_dir.join(".gitignore"), disk_dir.join(".gitignore"),
); );
for maybe_entry in disk_dir.read_dir().unwrap() { for maybe_entry in disk_dir.read_dir().unwrap() {
@ -345,7 +346,7 @@ impl TreeState {
} }
if file_type.is_dir() { if file_type.is_dir() {
let subdir = dir.join(&DirRepoPathComponent::from(name)); let subdir = dir.join(&DirRepoPathComponent::from(name));
if git_ignore.matches_all_files_in(&subdir.to_internal_string()) { if git_ignore.matches_all_files_in(&subdir.to_internal_dir_string()) {
// If the whole directory is ignored, skip it unless we're already tracking // If the whole directory is ignored, skip it unless we're already tracking
// some file in it. TODO: This is pretty ugly... Also, we should // some file in it. TODO: This is pretty ugly... Also, we should
// optimize it to check exactly the already-tracked files (we know that // optimize it to check exactly the already-tracked files (we know that
@ -378,7 +379,7 @@ impl TreeState {
match self.file_states.get(&file) { match self.file_states.get(&file) {
None => { None => {
// untracked // untracked
if git_ignore.matches_file(&file.to_internal_string()) { if git_ignore.matches_file(&file.to_internal_file_string()) {
continue; continue;
} }
clean = false; clean = false;