Skip to content
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

Bug/oxen add #499

Closed
wants to merge 3 commits into from
Closed
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
2 changes: 1 addition & 1 deletion src/cli/src/cmd/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl RunCmd for AddCmd {
OxenError::basic_str(format!("Failed to get current directory: {}", e))
})?;
let joined_path = current_dir.join(p);
joined_path.canonicalize().or_else(|_| Ok(joined_path))
dunce::canonicalize(&joined_path).or_else(|_| Ok(joined_path))
})
.collect::<Result<Vec<PathBuf>, OxenError>>()?;

Expand Down
2 changes: 1 addition & 1 deletion src/cli/src/cmd/rm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl RunCmd for RmCmd {
OxenError::basic_str(format!("Failed to get current directory: {}", e))
})?;
let joined_path = current_dir.join(p);
joined_path.canonicalize().or_else(|_| Ok(joined_path))
dunce::canonicalize(&joined_path).or_else(|_| Ok(joined_path))
})
.collect::<Result<Vec<PathBuf>, OxenError>>()?;

Expand Down
45 changes: 30 additions & 15 deletions src/lib/src/core/v0_19_0/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ pub fn process_add_dir(
let path = path.clone();
let repo = repo.clone();
let maybe_head_commit = maybe_head_commit.clone();
let repo_path = repo.path.clone();
let repo_path = &repo.path.clone();

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Arc;
Expand Down Expand Up @@ -219,7 +219,7 @@ pub fn process_add_dir(
let added_file_counter_clone = Arc::clone(&added_file_counter);
let unchanged_file_counter_clone = Arc::clone(&unchanged_file_counter);

let dir_path = util::fs::path_relative_to_dir(dir, &repo_path).unwrap();
let dir_path = util::fs::path_relative_to_dir(dir, repo_path).unwrap();
let dir_node = maybe_load_directory(&repo, &maybe_head_commit, &dir_path).unwrap();
let seen_dirs = Arc::new(Mutex::new(HashSet::new()));

Expand All @@ -246,7 +246,7 @@ pub fn process_add_dir(
let seen_dirs_clone = Arc::clone(&seen_dirs);
match process_add_file(
&repo,
&repo_path,
repo_path,
versions_path,
staged_db,
&dir_node,
Expand Down Expand Up @@ -314,21 +314,21 @@ fn add_file_inner(
path: &Path,
staged_db: &DBWithThreadMode<MultiThreaded>,
) -> Result<Option<StagedMerkleTreeNode>, OxenError> {
let repo_path = repo.path.clone();
let repo_path = &repo.path.clone();
let versions_path = util::fs::oxen_hidden_dir(&repo.path)
.join(VERSIONS_DIR)
.join(FILES_DIR);
let mut maybe_dir_node = None;
if let Some(head_commit) = maybe_head_commit {
let path = util::fs::path_relative_to_dir(path, &repo_path)?;
let path = util::fs::path_relative_to_dir(path, repo_path)?;
let parent_path = path.parent().unwrap_or(Path::new(""));
maybe_dir_node = CommitMerkleTree::dir_with_children(repo, head_commit, parent_path)?;
}

let seen_dirs = Arc::new(Mutex::new(HashSet::new()));
process_add_file(
repo,
&repo_path,
repo_path,
&versions_path,
staged_db,
&maybe_dir_node,
Expand Down Expand Up @@ -360,17 +360,32 @@ pub fn process_add_file(
seen_dirs: &Arc<Mutex<HashSet<PathBuf>>>,
) -> Result<Option<StagedMerkleTreeNode>, OxenError> {
log::debug!("process_add_file {:?}", path);
let relative_path = util::fs::path_relative_to_dir(path, repo_path)?;
let full_path = repo_path.join(&relative_path);
let mut relative_path = util::fs::path_relative_to_dir(path, repo_path)?;
let mut full_path = repo_path.join(&relative_path);

if !full_path.is_file() {
// If it's not a file - no need to add it
// We handle directories by traversing the parents of files below
log::debug!("file is not a file - skipping add on {:?}", full_path);
return Ok(Some(StagedMerkleTreeNode {
status: StagedEntryStatus::Added,
node: MerkleTreeNode::default_dir(),
}));
// Fix for Windows CLI
// util::fs::path_relative_to_dir can fail if the capitalization of the input path differs from what it is in the working directory
// TODO: is there ever a situation where process_add_file will be called on a path that doesn't exist? That will be propogated as an error here
log::debug!(
"file {:?} was not found. Checking for Windows CLI path",
full_path
);
let canon_repo_path = dunce::canonicalize(repo_path)?;
let cli_path = util::fs::path_relative_to_dir(path, &canon_repo_path)?;

if cli_path.is_file() {
relative_path = cli_path;
full_path = canon_repo_path.join(&relative_path);
} else {
// If it's not a file - no need to add it
// We handle directories by traversing the parents of files below
log::debug!("file is not a file - skipping add on {:?}", full_path);
return Ok(Some(StagedMerkleTreeNode {
status: StagedEntryStatus::Added,
node: MerkleTreeNode::default_dir(),
}));
}
}

// Check if the file is already in the head commit
Expand Down
7 changes: 3 additions & 4 deletions src/lib/src/repositories/diffs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,12 @@ pub fn diff(
return Err(OxenError::basic_str(error));
};

let file_node = Some(
repositories::entries::get_file(&repository, &commit, &path_1)?.ok_or_else(|| {
let file_node = repositories::entries::get_file(&repository, &commit, &path_1)?
.ok_or_else(|| {
OxenError::ResourceNotFound(
format!("Error: file {} not committed", path_1.as_ref().display()).into(),
)
})?,
);
})?;

let hash = file_node.hash.to_string();

Expand Down
43 changes: 42 additions & 1 deletion src/lib/src/util/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,7 +1351,48 @@ pub fn path_relative_to_dir(
let mut mut_path = path.to_path_buf();
let mut components: Vec<PathBuf> = vec![];
while mut_path.parent().is_some() {
// println!("Comparing {:?} => {:?} => {:?}", path, mut_path.parent(), dir);
log::debug!(
"Comparing {:?} => {:?} => {:?}",
path,
mut_path.parent(),
dir
);
if let Some(filename) = mut_path.file_name() {
if mut_path != dir {
components.push(PathBuf::from(filename));
} else {
break;
}
}

mut_path.pop();
}
components.reverse();

let mut result = PathBuf::new();
for component in components.iter() {
result = result.join(component);
}

Ok(result)
}

pub fn path_relative_to_canon_dir(
path: impl AsRef<Path>,
dir: impl AsRef<Path>,
) -> Result<PathBuf, OxenError> {
let path = path.as_ref();
let dir = dunce::canonicalize(dir.as_ref())?;

let mut mut_path = path.to_path_buf();
let mut components: Vec<PathBuf> = vec![];
while mut_path.parent().is_some() {
log::debug!(
"Comparing {:?} => {:?} => {:?}",
path,
mut_path.parent(),
dir
);
if let Some(filename) = mut_path.file_name() {
if mut_path != dir {
components.push(PathBuf::from(filename));
Expand Down
Loading