Skip to content

Commit 312bb4a

Browse files
authored
Merge pull request #502 from Oxen-AI/bug/oxen-add
Bug/oxen add
2 parents 3433a80 + 3551d01 commit 312bb4a

File tree

9 files changed

+71
-19
lines changed

9 files changed

+71
-19
lines changed

src/cli/src/cmd/add.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use async_trait::async_trait;
44
use clap::{Arg, Command};
55
use liboxen::error::OxenError;
66

7+
use crate::util;
78
use liboxen::model::LocalRepository;
89
use liboxen::opts::AddOpts;
910
use liboxen::repositories;
@@ -48,7 +49,7 @@ impl RunCmd for AddCmd {
4849
OxenError::basic_str(format!("Failed to get current directory: {}", e))
4950
})?;
5051
let joined_path = current_dir.join(p);
51-
joined_path.canonicalize().or_else(|_| Ok(joined_path))
52+
util::fs::canonicalize(&joined_path).or_else(|_| Ok(joined_path))
5253
})
5354
.collect::<Result<Vec<PathBuf>, OxenError>>()?;
5455

src/cli/src/cmd/init.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use liboxen::error::OxenError;
77

88
use crate::cmd::RunCmd;
99
use crate::helpers::{check_remote_version, get_host_or_default};
10+
use crate::util;
1011
use liboxen::repositories;
1112

1213
pub const INIT: &str = "init";
@@ -48,7 +49,7 @@ impl RunCmd for InitCmd {
4849
check_remote_version(host).await?;
4950

5051
// Initialize the repository
51-
let directory = dunce::canonicalize(PathBuf::from(&path))?;
52+
let directory = util::fs::canonicalize(PathBuf::from(&path))?;
5253
repositories::init::init_with_version(&directory, oxen_version)?;
5354
println!("🐂 repository initialized at: {directory:?}");
5455
Ok(())

src/cli/src/cmd/rm.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use clap::{Arg, ArgMatches, Command};
33

44
use crate::helpers::check_repo_migration_needed;
55

6+
use crate::util;
67
use liboxen::error::OxenError;
78
use liboxen::model::LocalRepository;
89
use liboxen::opts::RmOpts;
@@ -52,7 +53,7 @@ impl RunCmd for RmCmd {
5253
OxenError::basic_str(format!("Failed to get current directory: {}", e))
5354
})?;
5455
let joined_path = current_dir.join(p);
55-
joined_path.canonicalize().or_else(|_| Ok(joined_path))
56+
util::fs::canonicalize(&joined_path).or_else(|_| Ok(joined_path))
5657
})
5758
.collect::<Result<Vec<PathBuf>, OxenError>>()?;
5859

src/lib/src/api/client/workspaces/files.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ fn resolve_remote_add_file_path(
6262
opts: &AddOpts,
6363
) -> Result<(PathBuf, PathBuf), OxenError> {
6464
let path = path.as_ref();
65-
match dunce::canonicalize(path) {
65+
match util::fs::canonicalize(path) {
6666
Ok(path) => {
6767
if util::fs::file_exists_in_directory(&repo.path, &path) {
6868
// Path is in the repo, so we get the remote directory from the repo path

src/lib/src/core/v0_19_0/add.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ pub fn process_add_dir(
190190
let path = path.clone();
191191
let repo = repo.clone();
192192
let maybe_head_commit = maybe_head_commit.clone();
193-
let repo_path = repo.path.clone();
193+
let repo_path = &repo.path.clone();
194194

195195
use std::sync::atomic::{AtomicU64, Ordering};
196196
use std::sync::Arc;
@@ -219,7 +219,9 @@ pub fn process_add_dir(
219219
let added_file_counter_clone = Arc::clone(&added_file_counter);
220220
let unchanged_file_counter_clone = Arc::clone(&unchanged_file_counter);
221221

222-
let dir_path = util::fs::path_relative_to_dir(dir, &repo_path).unwrap();
222+
let dir_path = util::fs::path_relative_to_dir(dir, repo_path).unwrap();
223+
log::debug!("path now: {dir_path:?}");
224+
223225
let dir_node = maybe_load_directory(&repo, &maybe_head_commit, &dir_path).unwrap();
224226
let seen_dirs = Arc::new(Mutex::new(HashSet::new()));
225227

@@ -246,7 +248,7 @@ pub fn process_add_dir(
246248
let seen_dirs_clone = Arc::clone(&seen_dirs);
247249
match process_add_file(
248250
&repo,
249-
&repo_path,
251+
repo_path,
250252
versions_path,
251253
staged_db,
252254
&dir_node,
@@ -314,21 +316,21 @@ fn add_file_inner(
314316
path: &Path,
315317
staged_db: &DBWithThreadMode<MultiThreaded>,
316318
) -> Result<Option<StagedMerkleTreeNode>, OxenError> {
317-
let repo_path = repo.path.clone();
319+
let repo_path = &repo.path.clone();
318320
let versions_path = util::fs::oxen_hidden_dir(&repo.path)
319321
.join(VERSIONS_DIR)
320322
.join(FILES_DIR);
321323
let mut maybe_dir_node = None;
322324
if let Some(head_commit) = maybe_head_commit {
323-
let path = util::fs::path_relative_to_dir(path, &repo_path)?;
325+
let path = util::fs::path_relative_to_dir(path, repo_path)?;
324326
let parent_path = path.parent().unwrap_or(Path::new(""));
325327
maybe_dir_node = CommitMerkleTree::dir_with_children(repo, head_commit, parent_path)?;
326328
}
327329

328330
let seen_dirs = Arc::new(Mutex::new(HashSet::new()));
329331
process_add_file(
330332
repo,
331-
&repo_path,
333+
repo_path,
332334
&versions_path,
333335
staged_db,
334336
&maybe_dir_node,

src/lib/src/core/v0_19_0/rm.rs

+1
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,7 @@ fn remove_inner(
383383
for path in paths {
384384
// Get parent node
385385
let path = util::fs::path_relative_to_dir(path, &repo.path)?;
386+
386387
let parent_path = path.parent().unwrap_or(Path::new(""));
387388
let parent_node: MerkleTreeNode = if let Some(dir_node) =
388389
CommitMerkleTree::dir_with_children(repo, &head_commit, parent_path)?

src/lib/src/model/data_frame/schema.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ impl Schema {
284284
}
285285

286286
pub fn num_bytes(&self) -> u64 {
287-
let bytes = serde_json::to_string(&self).unwrap().as_bytes().len();
287+
let bytes = serde_json::to_string(&self).unwrap().len();
288288
bytes as u64
289289
}
290290

src/lib/src/repositories/diffs.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,12 @@ pub fn diff(
130130
return Err(OxenError::basic_str(error));
131131
};
132132

133-
let (file_node,) = (
134-
repositories::entries::get_file(&repository, &commit, &path_1)?.ok_or_else(|| {
133+
let file_node = repositories::entries::get_file(&repository, &commit, &path_1)?
134+
.ok_or_else(|| {
135135
OxenError::ResourceNotFound(
136136
format!("Error: file {} not committed", path_1.as_ref().display()).into(),
137137
)
138-
})?,
139-
);
138+
})?;
140139

141140
let hash = file_node.hash.to_string();
142141

src/lib/src/util/fs.rs

+51-4
Original file line numberDiff line numberDiff line change
@@ -1341,17 +1341,64 @@ pub fn is_in_oxen_hidden_dir(path: &Path) -> bool {
13411341
false
13421342
}
13431343

1344+
// Return canonicalized path if possible, otherwise return original
1345+
pub fn canonicalize(path: impl AsRef<Path>) -> Result<PathBuf, OxenError> {
1346+
let path = path.as_ref();
1347+
//log::debug!("Path to canonicalize: {path:?}");
1348+
match dunce::canonicalize(path) {
1349+
Ok(canon_path) => Ok(canon_path),
1350+
Err(err) => Err(OxenError::basic_str(format!(
1351+
"path {path:?} cannot be canonicalized due to err {err:?}"
1352+
))),
1353+
}
1354+
}
1355+
13441356
pub fn path_relative_to_dir(
13451357
path: impl AsRef<Path>,
13461358
dir: impl AsRef<Path>,
13471359
) -> Result<PathBuf, OxenError> {
1348-
let path = path.as_ref();
1349-
let dir = dir.as_ref();
1360+
// Embedded canonicalize calls to catch every case:
1361+
// -- if both paths can be canonicalized, they both will be
1362+
// -- if only the dir can be canonicalized, only the dir will be
1363+
// -- if the dir cannot be canonicalized, neither will be
1364+
let (path, dir) = match canonicalize(&dir) {
1365+
Ok(canon_dir) => {
1366+
/*log::debug!(
1367+
"dir {:?} canonicalized. Checking path {:?}",
1368+
dir.as_ref(),
1369+
path.as_ref()
1370+
);*/
1371+
match canonicalize(&path) {
1372+
Ok(canon_path) => (canon_path, canon_dir),
1373+
// '_' because the debug statement is commented out
1374+
Err(_err) => {
1375+
/*log::debug!(
1376+
"Err with canonicalization: {err:?}. Returning path {:?} immediately",
1377+
path.as_ref()
1378+
);*/
1379+
return Ok(path.as_ref().to_path_buf());
1380+
}
1381+
}
1382+
}
1383+
// '_' because the debug statement is commented out
1384+
Err(_err) => {
1385+
/*log::debug!(
1386+
"Err with canonicalization: {err:?}. Skipping canonicalization of path {:?}",
1387+
path.as_ref()
1388+
);*/
1389+
(path.as_ref().to_path_buf(), dir.as_ref().to_path_buf())
1390+
}
1391+
};
13501392

1351-
let mut mut_path = path.to_path_buf();
1393+
let mut mut_path = path.clone();
13521394
let mut components: Vec<PathBuf> = vec![];
13531395
while mut_path.parent().is_some() {
1354-
// println!("Comparing {:?} => {:?} => {:?}", path, mut_path.parent(), dir);
1396+
/*log::debug!(
1397+
"Comparing {:?} => {:?} => {:?}",
1398+
path,
1399+
mut_path.parent(),
1400+
dir
1401+
);*/
13551402
if let Some(filename) = mut_path.file_name() {
13561403
if mut_path != dir {
13571404
components.push(PathBuf::from(filename));

0 commit comments

Comments
 (0)