liboxen 0.46.10

Oxen is a fast, unstructured data version control, to help version large machine learning datasets written in Rust.
Documentation
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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
// Placeholder file, for unit test organization only
// `oxen rm` currently does not have any unique logic in remote-mode

#[cfg(test)]
mod tests {

    use std::path::PathBuf;

    use crate::api;
    use crate::error::OxenError;
    use crate::model::staged_data::StagedDataOpts;
    use crate::opts::clone_opts::CloneOpts;
    use crate::repositories;
    use crate::test;
    use crate::util;

    use crate::config::UserConfig;
    use crate::model::NewCommitBody;
    use crate::model::StagedEntryStatus;

    #[tokio::test]
    async fn test_remote_mode_rm_synced_file() -> Result<(), OxenError> {
        test::run_readme_remote_repo_test(|_local_repo, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();

            test::run_empty_dir_test_async(|dir| async move {
                // Clone a repo in remote mode
                let mut opts = CloneOpts::new(&remote_repo.remote.url, dir.join("new_repo"));
                opts.is_remote = true;
                let cloned_repo = repositories::clone(&opts).await?;
                assert!(cloned_repo.is_remote_mode());

                // Restore the README file
                let file_path = PathBuf::from("README.md");
                let head_commit = repositories::commits::head_commit(&cloned_repo)?;
                repositories::remote_mode::restore(
                    &cloned_repo,
                    &[file_path.clone()],
                    &head_commit.id,
                )
                .await?;

                // Remove the README file
                let workspace_identifier = cloned_repo.workspace_name.clone().unwrap();
                let directory = ".".to_string();
                api::client::workspaces::files::rm_files(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    vec![file_path],
                )
                .await?;

                // Get status, should show staged file
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(&[cloned_repo.path.clone()]);
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    &status_opts,
                )
                .await?;
                status.print();

                assert_eq!(status.untracked_files.len(), 0);
                assert_eq!(status.staged_files.len(), 1);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_remote_mode_rm_unsynced_file() -> Result<(), OxenError> {
        test::run_readme_remote_repo_test(|_local_repo, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();

            test::run_empty_dir_test_async(|dir| async move {
                // Clone a repo in remote mode
                let mut opts = CloneOpts::new(&remote_repo.remote.url, dir.join("new_repo"));
                opts.is_remote = true;
                let cloned_repo = repositories::clone(&opts).await?;
                assert!(cloned_repo.is_remote_mode());

                // Get path to unsynced README file
                let file_path = PathBuf::from("README.md");

                // Add file with full path
                let workspace_identifier = cloned_repo.workspace_name.clone().unwrap();
                let directory = ".".to_string();
                api::client::workspaces::files::rm_files(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    vec![file_path],
                )
                .await?;

                // Get status, should show staged file
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(&[cloned_repo.path.clone()]);
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    &status_opts,
                )
                .await?;
                status.print();

                assert_eq!(status.untracked_files.len(), 0);
                assert_eq!(status.staged_files.len(), 1);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_remote_mode_rm_unsynced_subdir_file() -> Result<(), OxenError> {
        test::run_remote_repo_test_bounding_box_csv_pushed(|_local_repo, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();

            test::run_empty_dir_test_async(|dir| async move {
                // Clone a repo in remote mode
                let mut opts = CloneOpts::new(&remote_repo.remote.url, dir.join("new_repo"));
                opts.is_remote = true;
                let cloned_repo = repositories::clone(&opts).await?;
                assert!(cloned_repo.is_remote_mode());

                // Get path to unsynced README file
                let file_path = PathBuf::from("annotations")
                    .join("train")
                    .join("bounding_box.csv");

                // Remove the file
                let workspace_identifier = cloned_repo.workspace_name.clone().unwrap();
                let directory = ".".to_string();
                api::client::workspaces::files::rm_files(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    vec![file_path],
                )
                .await?;

                // Get status, should show staged file
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(&[cloned_repo.path.clone()]);
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    &status_opts,
                )
                .await?;
                status.print();

                assert_eq!(status.untracked_files.len(), 0);
                assert_eq!(status.staged_files.len(), 1);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_remote_mode_rm_unsynced_file_with_full_path() -> Result<(), OxenError> {
        test::run_readme_remote_repo_test(|_local_repo, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();

            test::run_empty_dir_test_async(|dir| async move {
                // Clone a repo in remote mode
                let mut opts = CloneOpts::new(&remote_repo.remote.url, dir.join("new_repo"));
                opts.is_remote = true;
                let cloned_repo = repositories::clone(&opts).await?;
                assert!(cloned_repo.is_remote_mode());

                // Get path to unsynced README file
                let file_path = PathBuf::from("README.md");
                let full_path = cloned_repo.path.join(&file_path);

                // Add file with full path
                let workspace_identifier = cloned_repo.workspace_name.clone().unwrap();
                let directory = ".".to_string();
                api::client::workspaces::files::rm_files(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    vec![full_path],
                )
                .await?;

                // Get status, should show staged file
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(&[cloned_repo.path.clone()]);
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    &status_opts,
                )
                .await?;
                status.print();

                assert_eq!(status.untracked_files.len(), 0);
                assert_eq!(status.staged_files.len(), 1);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_remote_mode_rm_dir_that_is_not_committed_should_throw_error()
    -> Result<(), OxenError> {
        test::run_remote_repo_test_bounding_box_csv_pushed(|_local_repo, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();

            test::run_empty_dir_test_async(|dir| async move {
                let mut opts = CloneOpts::new(&remote_repo.remote.url, dir.join("new_repo"));
                opts.is_remote = true;
                let cloned_repo = repositories::clone(&opts).await?;
                assert!(cloned_repo.is_remote_mode());

                let workspace_id = cloned_repo.workspace_name.clone().unwrap();
                let untracked_dir = PathBuf::from("new_dir");
                let full_path = cloned_repo.path.join(&untracked_dir);
                util::fs::create_dir_all(&full_path)?;
                let _ = test::add_txt_file_to_dir(&full_path, "new_file.txt")?;

                // Try to remove it. This should fail because the directory is not a committed part of the remote repo.
                let paths_to_remove = vec![untracked_dir.clone()];
                let result = api::client::workspaces::files::rm_files(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_id,
                    paths_to_remove,
                )
                .await;
                assert!(result.is_err());

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_remote_mode_rm_staged_file() -> Result<(), OxenError> {
        test::run_remote_repo_test_bounding_box_csv_pushed(|_local_repo, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();

            test::run_empty_dir_test_async(|dir| async move {
                let mut opts = CloneOpts::new(&remote_repo.remote.url, dir.join("new_repo"));
                opts.is_remote = true;
                let cloned_repo = repositories::clone(&opts).await?;
                assert!(cloned_repo.is_remote_mode());

                let workspace_id = cloned_repo.workspace_name.clone().unwrap();
                let directory = ".".to_string();
                let file_path = PathBuf::from("README.md");
                let full_path = cloned_repo.path.join(&file_path);

                // Create and stage a file
                test::write_txt_file_to_path(&full_path, "Test content")?;
                api::client::workspaces::files::add(
                    &remote_repo,
                    &workspace_id,
                    &directory,
                    vec![file_path.clone()],
                    &Some(cloned_repo.clone()),
                )
                .await?;

                // Check status to confirm it's staged
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(&[cloned_repo.path.clone()]);
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_id,
                    &directory,
                    &status_opts,
                )
                .await?;
                assert_eq!(status.staged_files.len(), 1);

                // Remove it from staged area
                let paths_to_remove = vec![file_path.clone()];
                api::client::workspaces::files::rm_files_from_staged(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_id,
                    paths_to_remove,
                )
                .await?;

                // Check status again; it should be unstaged
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_id,
                    &directory,
                    &status_opts,
                )
                .await?;
                assert_eq!(status.staged_files.len(), 0);
                assert_eq!(status.untracked_files.len(), 1);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_remote_mode_rm_one_file_in_dir() -> Result<(), OxenError> {
        test::run_remote_repo_test_bounding_box_csv_pushed(|_local_repo, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();

            test::run_empty_dir_test_async(|dir| async move {
                let mut opts = CloneOpts::new(&remote_repo.remote.url, dir.join("new_repo"));
                opts.is_remote = true;
                let cloned_repo = repositories::clone(&opts).await?;
                assert!(cloned_repo.is_remote_mode());

                let workspace_id = cloned_repo.workspace_name.clone().unwrap();
                let directory = ".".to_string();

                // Create a directory with multiple files and commit it
                let images_dir = PathBuf::from("images");
                let full_images_dir = cloned_repo.path.join(&images_dir);
                util::fs::create_dir_all(&full_images_dir)?;
                let file1 = images_dir.join("dog_1.txt");
                let file2 = images_dir.join("dog_2.txt");
                let file3 = images_dir.join("cat_1.txt");

                test::write_txt_file_to_path(cloned_repo.path.join(&file1), "dog")?;
                test::write_txt_file_to_path(cloned_repo.path.join(&file2), "dog")?;
                test::write_txt_file_to_path(cloned_repo.path.join(&file3), "cat")?;

                println!(
                    "File 1: {file1:?}; exists? {:?}; clone repo path: {:?}; exist? {:?}",
                    file1.exists(),
                    cloned_repo.path.join(&file1),
                    cloned_repo.path.join(&file1).exists(),
                );

                let files_to_add = vec![file1.clone(), file2.clone(), file3.clone()];

                api::client::workspaces::files::add(
                    &remote_repo,
                    &workspace_id,
                    &directory,
                    files_to_add,
                    &Some(cloned_repo.clone()),
                )
                .await?;
                let commit_body = NewCommitBody::from_config(&UserConfig::get()?, "Adding images");
                repositories::remote_mode::commit(&cloned_repo, &commit_body).await?;

                let head = repositories::commits::head_commit(&cloned_repo)?;
                let tree =
                    repositories::tree::get_root_with_children(&cloned_repo, &head)?.unwrap();
                let (files, _) = repositories::tree::list_files_and_dirs(&tree)?;

                // The three created files and bounding_box.csv
                assert_eq!(files.len(), 4);

                // Remove just one file
                let paths_to_remove = vec![file1.clone()];
                api::client::workspaces::files::rm_files(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_id,
                    paths_to_remove,
                )
                .await?;

                // Check status: only one file should be staged for removal
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(&[cloned_repo.path.clone()]);
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_id,
                    &directory,
                    &status_opts,
                )
                .await?;
                println!("status: {status:?}");
                assert_eq!(status.staged_files.len(), 1);
                assert_eq!(
                    status.staged_files.get(&file1).unwrap().status,
                    StagedEntryStatus::Removed
                );

                // Commit removal
                let commit_body = NewCommitBody::from_config(&UserConfig::get()?, "Removing file");
                repositories::remote_mode::commit(&cloned_repo, &commit_body).await?;

                // Confirm file was removed locally and from the tree
                assert!(!cloned_repo.path.join(file1.clone()).exists());
                let head = repositories::commits::head_commit(&cloned_repo)?;
                let tree =
                    repositories::tree::get_root_with_children(&cloned_repo, &head)?.unwrap();
                let (files, _) = repositories::tree::list_files_and_dirs(&tree)?;
                println!("files: {files:?}");
                assert_eq!(files.len(), 3);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    // TODO: 'status.print()' has weird behavior when staging subdirs for removal
    //       Resolve this at some point
    #[tokio::test]
    async fn test_remote_mode_rm_dir_with_asterisk() -> Result<(), OxenError> {
        test::run_training_data_fully_sync_remote(|_local_repo, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();

            test::run_empty_dir_test_async(|dir| async move {
                let mut opts = CloneOpts::new(&remote_repo.remote.url, dir.join("new_repo"));
                opts.is_remote = true;
                let cloned_repo = repositories::clone(&opts).await?;
                assert!(cloned_repo.is_remote_mode());

                // Count files and dirs for comparison
                let head_commit = repositories::commits::head_commit(&cloned_repo)?;
                let commit_root =
                    repositories::tree::get_root_with_children(&cloned_repo, &head_commit)?
                        .unwrap();
                let files_in_tree =
                    repositories::tree::list_all_files(&commit_root, &PathBuf::from("."))?;
                let prev_files = files_in_tree.len();
                let dirs_in_tree = repositories::tree::list_all_dirs(&commit_root)?;
                let prev_dirs = dirs_in_tree.len();

                let workspace_id = cloned_repo.workspace_name.clone().unwrap();
                let directory = ".".to_string();
                let path_with_asterisk = PathBuf::from("annotations").join("*");

                // Remove the directory using a path with a trailing slash
                let paths_to_remove = vec![path_with_asterisk.clone()];
                api::client::workspaces::files::rm_files(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_id,
                    paths_to_remove,
                )
                .await?;

                // Check status to verify files are staged for removal
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(&[cloned_repo.path.clone()]);
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_id,
                    &directory,
                    &status_opts,
                )
                .await?;
                status.print();

                // assert_eq!(status.staged_dirs.paths.len(), 2);
                assert_eq!(status.staged_files.len(), 6);

                // Commit the removals
                let commit_message = "Removing subdirectories".to_string();
                let commit_body = NewCommitBody::from_config(&UserConfig::get()?, &commit_message);
                let new_commit =
                    repositories::remote_mode::commit(&cloned_repo, &commit_body).await?;

                // Check that the files and dirs were removed
                let commit_root =
                    repositories::tree::get_root_with_children(&cloned_repo, &new_commit)?.unwrap();

                let files_in_tree =
                    repositories::tree::list_all_files(&commit_root, &PathBuf::from("."))?;
                let dirs_in_tree = repositories::tree::list_all_dirs(&commit_root)?;

                // 6 files were removed
                assert_eq!(files_in_tree.len(), prev_files - 6);

                // 2 dirs were removed
                assert_eq!(dirs_in_tree.len(), prev_dirs - 2);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_remote_mode_rm_dir_with_glob_path() -> Result<(), OxenError> {
        test::run_training_data_fully_sync_remote(|_local_repo, remote_repo| async move {
            let remote_repo_copy = remote_repo.clone();

            test::run_empty_dir_test_async(|dir| async move {
                let mut opts = CloneOpts::new(&remote_repo.remote.url, dir.join("new_repo"));
                opts.is_remote = true;
                let cloned_repo = repositories::clone(&opts).await?;
                assert!(cloned_repo.is_remote_mode());

                let workspace_id = cloned_repo.workspace_name.clone().unwrap();
                let directory = ".".to_string();
                let path_with_slash = PathBuf::from("annotations").join("t*");

                // Remove the directory using a path with a trailing slash
                let paths_to_remove = vec![path_with_slash.clone()];
                api::client::workspaces::files::rm_files(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_id,
                    paths_to_remove,
                )
                .await?;

                // Check status to verify files are staged for removal
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(&[cloned_repo.path.clone()]);
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_id,
                    &directory,
                    &status_opts,
                )
                .await?;
                status.print();

                // Does not remove the README file
                assert_eq!(status.staged_files.len(), 5);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }
}