liboxen 0.65.0

Oxen is a fast data version control system, built with machine learning training data in mind. Designed to handle terabytes of data with ease, using a workflow similar to git. Version both structured and unstructured data of any modality: text, images, video, audio, CSV, Parquet, JSONL, model checkpoints, and more. liboxen is the embeddable core library behind the oxen CLI and server, which power fine tuning and inference pipelines for multimodal LLMs, image models, and video models on Oxen.ai.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
use crate::core;
use crate::error::OxenError;
use crate::model::Workspace;

use std::path::PathBuf;

pub use crate::core::v_latest::workspaces::files::exists;

pub use crate::core::v_latest::workspaces::files::is_dir;

pub use crate::core::v_latest::workspaces::files::add;

pub use crate::core::v_latest::workspaces::files::rm;

pub use crate::core::v_latest::workspaces::files::unstage;

pub async fn import(
    url: &str,
    auth: &str,
    directory: PathBuf,
    filename: Option<String>,
    workspace: &Workspace,
    allow_loopback: bool,
) -> Result<(), OxenError> {
    core::v_latest::workspaces::files::import(
        url,
        auth,
        directory,
        filename,
        workspace,
        allow_loopback,
    )
    .await?;
    Ok(())
}

pub use crate::core::v_latest::workspaces::files::upload_zip;

pub use crate::core::v_latest::workspaces::files::mv;

#[cfg(test)]
mod tests {
    use std::path::{Path, PathBuf};

    use crate::config::UserConfig;
    use crate::core::staged::staged_db_manager::get_staged_db_manager;
    use crate::error::OxenError;
    use crate::model::{Commit, LocalRepository, NewCommitBody, StagedEntryStatus, Workspace};
    use crate::repositories::size;
    use crate::repositories::{self, workspaces};
    use crate::test;
    use crate::util::fs;

    #[tokio::test]
    async fn test_mv_file_in_workspace() -> Result<(), OxenError> {
        // Skip workspace ops on windows
        if std::env::consts::OS == "windows" {
            return Ok(());
        }

        test::run_bounding_box_csv_repo_test_fully_committed_async(|repo| async move {
            let branch_name = "test-mv";
            let branch = repositories::branches::create_checkout(&repo, branch_name)?;
            let commit = repositories::commits::get_by_id(&repo, &branch.commit_id)?.unwrap();
            let workspace_id = UserConfig::identifier()?;
            let workspace = repositories::workspaces::create(&repo, &commit, workspace_id, true)?;

            // Original file path that exists in the repo
            let original_path = Path::new("annotations")
                .join("train")
                .join("bounding_box.csv");
            let new_path = Path::new("renamed").join("data").join("bbox_renamed.csv");

            // Move the file
            workspaces::files::mv(&workspace, &original_path, &new_path).await?;

            // Check status - should show the original as removed and new as added
            let status = workspaces::status::status(&workspace)?;
            println!("Status after mv: {status:?}");

            // The original path should be staged as removed in staged_files
            let removed_entry = status.staged_files.get(&original_path);
            assert!(
                removed_entry.is_some(),
                "Original path should be in staged_files"
            );
            assert_eq!(
                removed_entry.unwrap().status,
                crate::model::StagedEntryStatus::Removed,
                "Original path should have Removed status"
            );

            // The new path should be in staged_files with Added status
            let added_entry = status.staged_files.get(&new_path);
            assert!(added_entry.is_some(), "New path should be staged");
            assert_eq!(
                added_entry.unwrap().status,
                crate::model::StagedEntryStatus::Added,
                "New path should have Added status"
            );

            // The move should be detected
            assert!(
                !status.moved_files.is_empty(),
                "Move should be detected in moved_files"
            );

            assert!(
                !size::repo_size_path(&repo).exists(),
                "committing locally records no size, so there is none to start from"
            );

            // Commit the workspace and verify the file is at the new location
            let user = UserConfig::get()?.to_user();
            let new_commit = NewCommitBody {
                author: user.name.clone(),
                email: user.email.clone(),
                message: "Moved file to new location".to_string(),
            };
            let commit =
                workspaces::commit(&workspace, &new_commit, branch_name.to_string()).await?;

            // Verify the file exists at the new path in the commit
            let new_file = repositories::tree::get_file_by_path(&repo, &commit, &new_path)?;
            assert!(
                new_file.is_some(),
                "File should exist at new path after commit"
            );

            // Verify the file no longer exists at the original path
            let old_file = repositories::tree::get_file_by_path(&repo, &commit, &original_path)?;
            assert!(
                old_file.is_none(),
                "File should not exist at original path after commit"
            );

            assert_eq!(
                size::wait_for_recorded_size(&repo)?,
                repo.version_bytes()?,
                "the figure recorded is the bytes the tree references"
            );
            assert_eq!(
                fs::read_from_path(size::repo_size_path(&repo))?.trim(),
                repo.version_bytes()?.to_string(),
                "the figure is recorded on its own, with no status beside it"
            );

            // A record claiming a calculation is under way, with nothing running to finish it.
            fs::write_to_path(
                size::repo_size_path(&repo),
                r#"{"status":"pending","size":7}"#,
            )?;
            assert_eq!(
                size::wait_for_recorded_size(&repo)?,
                repo.version_bytes()?,
                "a record no pass stands behind is recalculated rather than believed"
            );

            Ok(())
        })
        .await
    }

    /// An editable workspace called `name` on `commit`, and a branch called `name` at `commit` for
    /// it to commit to.
    fn committing_workspace(
        repo: &LocalRepository,
        commit: &Commit,
        name: &str,
    ) -> Result<Workspace, OxenError> {
        repositories::branches::create(repo, name, &commit.id)?;
        repositories::workspaces::create(repo, commit, name, true)
    }

    async fn stage_new_file(workspace: &Workspace, path: &str) -> Result<(), OxenError> {
        let path = workspace.workspace_repo.path.join(path);
        fs::write_to_path(&path, "new")?;
        workspaces::files::add(workspace, path).await?;
        Ok(())
    }

    fn staged_status(workspace: &Workspace, path: &str) -> Option<StagedEntryStatus> {
        workspaces::status::status(workspace)
            .unwrap()
            .staged_files
            .get(Path::new(path))
            .map(|entry| entry.status.clone())
    }

    fn assert_files(repo: &LocalRepository, commit: &Commit, present: &[&str], absent: &[&str]) {
        for path in present {
            assert!(
                repositories::tree::get_file_by_path(repo, commit, path)
                    .unwrap()
                    .is_some(),
                "{path} should be in the commit"
            );
        }
        for path in absent {
            assert!(
                repositories::tree::get_node_by_path(repo, commit, path)
                    .unwrap()
                    .is_none(),
                "{path} should not be in the commit"
            );
            assert!(
                !repositories::tree::has_dir(repo, commit, path).unwrap(),
                "{path} should not be a directory in the commit"
            );
        }
    }

    /// Each case works in its own workspace on one commit of data/a.txt, data/sub/b.txt, other.txt
    /// and table.csv. A case that commits does so to its own branch, with its own message, since
    /// two commits with the same parent, message, and author in the same second get the same id.
    #[tokio::test]
    async fn test_move_and_remove_workspace_files() -> Result<(), OxenError> {
        test::run_empty_local_repo_test_async(|repo| async move {
            for (path, content) in [
                ("data/a.txt", "A"),
                ("data/sub/b.txt", "B"),
                ("other.txt", "Other"),
                ("table.csv", "x,y\n1,2\n"),
            ] {
                fs::write_to_path(repo.path.join(path), content)?;
            }
            repositories::add(&repo, &repo.path).await?;
            let commit = repositories::commit(&repo, "Add data")?;
            let user = UserConfig::get()?;
            let other = Path::new("other.txt");

            // exists() is false for a path the workspace has not staged, and true once it has.
            let workspace = repositories::workspaces::create(&repo, &commit, "exists", false)?;
            let nonexistent = Path::new("does_not_exist.txt");
            assert!(
                !workspaces::files::exists(&workspace, other)?,
                "a committed path the workspace has not staged"
            );
            let workspace_file = workspace.workspace_repo.path.join(other);
            fs::write_to_path(&workspace_file, "Other, changed")?;
            assert!(
                !workspaces::files::exists(&workspace, other)?,
                "a changed working copy is not staged until it is added"
            );
            assert!(!workspaces::files::exists(&workspace, nonexistent)?);
            workspaces::files::add(&workspace, &workspace_file).await?;
            assert!(workspaces::files::exists(&workspace, other)?);
            assert!(!workspaces::files::exists(&workspace, nonexistent)?);

            // Import refuses every URL that does not reach a public HTTP host.
            let workspace = repositories::workspaces::create_temporary(&repo, &commit).await?;
            let cases = [
                ("http://127.0.0.1/secret", "loopback address"),
                ("http://10.0.0.1/internal", "private address"),
                (
                    "http://169.254.169.254/latest/meta-data/",
                    "link-local/metadata address",
                ),
                ("http://[::1]/secret", "IPv6 loopback"),
                ("file:///etc/passwd", "non-HTTP scheme"),
            ];
            for (url, label) in cases {
                // allow_loopback is false, so loopback targets are rejected like other private IPs.
                let result = workspaces::files::import(
                    url,
                    "",
                    PathBuf::from("data"),
                    None,
                    &workspace,
                    false,
                )
                .await;
                assert!(result.is_err(), "should reject {label}");
            }
            drop(workspace);

            // Skip workspace moves and removals on windows
            if std::env::consts::OS == "windows" {
                return Ok(());
            }

            // A committed directory moves with its nested subdirectory, and retrying the move
            // finds its source staged for removal.
            let workspace = committing_workspace(&repo, &commit, "nested")?;
            workspaces::files::mv(&workspace, Path::new("data"), Path::new("moved")).await?;
            for (path, status) in [
                ("data/a.txt", StagedEntryStatus::Removed),
                ("data/sub/b.txt", StagedEntryStatus::Removed),
                ("moved/a.txt", StagedEntryStatus::Added),
                ("moved/sub/b.txt", StagedEntryStatus::Added),
            ] {
                assert_eq!(staged_status(&workspace, path), Some(status), "{path}");
            }
            let retry =
                workspaces::files::mv(&workspace, Path::new("data"), Path::new("moved")).await;
            assert!(
                matches!(retry, Err(OxenError::PathStagedForRemoval(_))),
                "retrying the move finds its source staged for removal, got {retry:?}"
            );
            let committed = workspaces::commit(
                &workspace,
                &NewCommitBody::from_config(&user, "Move data"),
                "nested",
            )
            .await?;
            assert_files(
                &repo,
                &committed,
                &["moved/a.txt", "moved/sub/b.txt", "other.txt"],
                &["data", "data/sub", "data/a.txt", "data/sub/b.txt"],
            );
            assert!(repositories::tree::has_dir(&repo, &committed, "moved/sub")?);

            // Files added in the workspace move with the directory, and a file staged for removal
            // stays removed rather than reappearing under the new path.
            let workspace = committing_workspace(&repo, &commit, "mixed")?;
            stage_new_file(&workspace, "data/new.txt").await?;
            stage_new_file(&workspace, "data/fresh/deep.txt").await?;
            workspaces::files::rm(&workspace, &[PathBuf::from("data/a.txt")]).await?;
            workspaces::files::mv(&workspace, Path::new("data"), Path::new("moved")).await?;
            assert_eq!(staged_status(&workspace, "data/new.txt"), None);
            assert_eq!(staged_status(&workspace, "data/fresh/deep.txt"), None);
            assert_eq!(staged_status(&workspace, "moved/a.txt"), None);
            assert_eq!(
                staged_status(&workspace, "moved/new.txt"),
                Some(StagedEntryStatus::Added)
            );
            assert_eq!(
                staged_status(&workspace, "moved/fresh/deep.txt"),
                Some(StagedEntryStatus::Added)
            );
            let committed = workspaces::commit(
                &workspace,
                &NewCommitBody::from_config(&user, "Move data with added files"),
                "mixed",
            )
            .await?;
            assert_files(
                &repo,
                &committed,
                &["moved/new.txt", "moved/fresh/deep.txt", "moved/sub/b.txt"],
                &["data", "data/fresh", "moved/a.txt"],
            );

            // A directory that exists only in the workspace moves, and its emptied parent goes.
            let workspace = committing_workspace(&repo, &commit, "staged-only")?;
            stage_new_file(&workspace, "outer/fresh/x.txt").await?;
            stage_new_file(&workspace, "outer/fresh/deep/y.txt").await?;
            workspaces::files::mv(&workspace, Path::new("outer/fresh"), Path::new("renamed"))
                .await?;
            let committed = workspaces::commit(
                &workspace,
                &NewCommitBody::from_config(&user, "Move fresh"),
                "staged-only",
            )
            .await?;
            assert_files(
                &repo,
                &committed,
                &["renamed/x.txt", "renamed/deep/y.txt", "data/a.txt"],
                &["outer", "outer/fresh"],
            );

            // Removing a directory also unstages the files added under it in the workspace, so
            // the commit does not bring the directory back.
            let workspace = committing_workspace(&repo, &commit, "rm-dir")?;
            stage_new_file(&workspace, "data/new.txt").await?;
            stage_new_file(&workspace, "data/fresh/deep.txt").await?;
            let err_files = workspaces::files::rm(&workspace, &[PathBuf::from("data")]).await?;
            assert!(err_files.is_empty(), "{err_files:?}");
            assert_eq!(staged_status(&workspace, "data/new.txt"), None);
            assert_eq!(staged_status(&workspace, "data/fresh/deep.txt"), None);
            let committed = workspaces::commit(
                &workspace,
                &NewCommitBody::from_config(&user, "Remove data"),
                "rm-dir",
            )
            .await?;
            assert_files(
                &repo,
                &committed,
                &["other.txt"],
                &["data", "data/new.txt", "data/fresh"],
            );

            // A directory move onto an existing or staged path, into itself, or from a missing
            // source is refused and stages nothing.
            let workspace = repositories::workspaces::create(&repo, &commit, "refused", true)?;
            stage_new_file(&workspace, "staged/z.txt").await?;
            for dest in ["other.txt", "staged"] {
                let result =
                    workspaces::files::mv(&workspace, Path::new("data"), Path::new(dest)).await;
                assert!(
                    matches!(result, Err(OxenError::DestinationAlreadyStaged(_))),
                    "moving onto {dest} should be refused, got {result:?}"
                );
            }
            for dest in ["data", "data/sub/inner"] {
                let result =
                    workspaces::files::mv(&workspace, Path::new("data"), Path::new(dest)).await;
                assert!(
                    matches!(result, Err(OxenError::MoveIntoItself(_))),
                    "moving into {dest} should be refused, got {result:?}"
                );
            }
            let result =
                workspaces::files::mv(&workspace, Path::new("nope"), Path::new("elsewhere")).await;
            assert!(
                matches!(result, Err(OxenError::PathDoesNotExist(_))),
                "a missing source should be refused, got {result:?}"
            );
            // Beneath a committed or a staged file, a move would turn that file into a directory.
            for (source, dest) in [
                ("data", "other.txt/inner"),
                ("data/a.txt", "other.txt/a.txt"),
                ("data", "staged/z.txt/deep/inner"),
            ] {
                let result =
                    workspaces::files::mv(&workspace, Path::new(source), Path::new(dest)).await;
                assert!(
                    matches!(result, Err(OxenError::NotADirectory(_))),
                    "moving {source} to {dest} should be refused, got {result:?}"
                );
            }
            let result = repositories::workspaces::data_frames::rename(
                &workspace,
                "table.csv",
                "other.txt/table.csv",
            )
            .await;
            assert!(
                matches!(result, Err(OxenError::NotADirectory(_))),
                "renaming a data frame beneath a file should be refused, got {result:?}"
            );
            assert_eq!(staged_status(&workspace, "data/a.txt"), None);
            assert_eq!(staged_status(&workspace, "other.txt"), None);
            assert_eq!(
                staged_status(&workspace, "staged/z.txt"),
                Some(StagedEntryStatus::Added)
            );
            // A data frame the workspace never indexed renames too.
            repositories::workspaces::data_frames::rename(&workspace, "table.csv", "renamed.csv")
                .await?;
            assert_eq!(
                staged_status(&workspace, "renamed.csv"),
                Some(StagedEntryStatus::Added)
            );

            // Moving or renaming a file the workspace removed would restore its content at the new
            // path. Tabular files move through the data frame rename, which refuses it too.
            let workspace = repositories::workspaces::create(&repo, &commit, "removed", true)?;
            workspaces::files::rm(&workspace, &[PathBuf::from("other.txt")]).await?;
            let result =
                workspaces::files::mv(&workspace, Path::new("other.txt"), Path::new("moved.txt"))
                    .await;
            assert!(
                matches!(result, Err(OxenError::PathStagedForRemoval(_))),
                "{result:?}"
            );
            assert_eq!(staged_status(&workspace, "moved.txt"), None);
            assert_eq!(
                staged_status(&workspace, "other.txt"),
                Some(StagedEntryStatus::Removed)
            );
            workspaces::files::rm(&workspace, &[PathBuf::from("table.csv")]).await?;
            let result =
                repositories::workspaces::data_frames::rename(&workspace, "table.csv", "moved.csv")
                    .await;
            assert!(
                matches!(result, Err(OxenError::PathStagedForRemoval(_))),
                "{result:?}"
            );
            assert_eq!(staged_status(&workspace, "moved.csv"), None);
            assert_eq!(
                staged_status(&workspace, "table.csv"),
                Some(StagedEntryStatus::Removed)
            );

            // A chained rename: the second move's source is staged but absent from the base
            // commit, so it is unstaged outright rather than staged for removal, and only the
            // original committed path carries the `Removed` marker.
            let workspace = repositories::workspaces::create(&repo, &commit, "mv-twice", true)?;
            let second = Path::new("second.txt");
            let third = Path::new("third.txt");
            super::mv(&workspace, other, second).await?;
            workspaces::files::mv(&workspace, second, third).await?;
            let status = workspaces::status::status(&workspace)?;
            assert_eq!(
                status.staged_files.get(other).map(|entry| &entry.status),
                Some(&StagedEntryStatus::Removed),
                "the committed source keeps its Removed marker across both moves"
            );
            assert_eq!(
                status.staged_files.get(third).map(|entry| &entry.status),
                Some(&StagedEntryStatus::Added),
                "the final destination is staged as Added"
            );
            assert!(
                !status.staged_files.contains_key(second),
                "the intermediate path was never committed, so it is unstaged rather than \
                 staged for removal"
            );

            // Renaming a data frame onto one staged in the workspace is refused, so the staged one
            // keeps its rows.
            let workspace = repositories::workspaces::create(&repo, &commit, "df-onto", true)?;
            let staged_csv = workspace.workspace_repo.path.join("staged.csv");
            fs::write_to_path(&staged_csv, "x,y\n3,4\n")?;
            workspaces::files::add(&workspace, &staged_csv).await?;
            repositories::workspaces::data_frames::index(&repo, &workspace, "table.csv").await?;
            let result = repositories::workspaces::data_frames::rename(
                &workspace,
                "table.csv",
                "staged.csv",
            )
            .await;
            assert!(
                matches!(result, Err(OxenError::DestinationAlreadyStaged(_))),
                "{result:?}"
            );
            assert_eq!(
                staged_status(&workspace, "staged.csv"),
                Some(StagedEntryStatus::Added)
            );
            assert_eq!(staged_status(&workspace, "table.csv"), None);

            // A path that exists only in the workspace is unstaged, and only a path with nothing
            // at it fails.
            let workspace = repositories::workspaces::create(&repo, &commit, "rm-staged", true)?;
            stage_new_file(&workspace, "outer/fresh/x.txt").await?;
            stage_new_file(&workspace, "outer/fresh/deep/y.txt").await?;
            stage_new_file(&workspace, "lone.txt").await?;
            stage_new_file(&workspace, "data/new.txt").await?;
            let err_files = workspaces::files::rm(
                &workspace,
                &[
                    PathBuf::from("outer/fresh"),
                    PathBuf::from("lone.txt"),
                    PathBuf::from("data/new.txt"),
                ],
            )
            .await?;
            assert!(err_files.is_empty(), "{err_files:?}");
            // Markers on committed directories may stay; nothing else should.
            let left: Vec<PathBuf> = get_staged_db_manager(&workspace.workspace_repo)?
                .read_entries_under(Path::new(""))?
                .into_iter()
                .map(|(path, _)| path)
                .filter(|path| path != Path::new("") && path != Path::new("data"))
                .collect();
            assert!(left.is_empty(), "nothing should be left staged: {left:?}");
            let err_files = workspaces::files::rm(&workspace, &[PathBuf::from("nope")]).await?;
            assert_eq!(err_files.len(), 1);

            Ok(())
        })
        .await
    }
}