liboxen 0.48.3

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
use crate::api;
use crate::error::OxenError;
use crate::model::{LocalRepository, NewCommitBody};
use crate::opts::FetchOpts;
use crate::repositories;

use crate::repositories::Commit;

pub async fn commit(
    local_repo: &LocalRepository,
    commit_body: &NewCommitBody,
) -> Result<Commit, OxenError> {
    let workspace_identifier = if local_repo.is_remote_mode() {
        &local_repo.workspace_name.clone().unwrap()
    } else {
        return Err(OxenError::basic_str(
            "Error: Cannot run remote mode commands outside remote mode repo",
        ));
    };

    println!("Committing to remote with message: {}", commit_body.message);
    let Some(branch) = repositories::branches::current_branch(local_repo)? else {
        log::error!("Remote-mode commit No current branch found");
        return Err(OxenError::must_be_on_valid_branch());
    };

    let remote_repo = api::client::repositories::get_default_remote(local_repo).await?;

    // TODO: Do we print successful commit already?
    let commit = api::client::workspaces::commit(
        &remote_repo,
        &branch.name,
        workspace_identifier,
        commit_body,
    )
    .await?;

    // Update local tree
    let fetch_opts = FetchOpts::from_branch(&branch.name);
    repositories::fetch::fetch_branch(local_repo, &fetch_opts).await?;

    Ok(commit)
}

// Actual bugs uncovered:
// 1: The same add one with the untracked files is happenieng here
#[cfg(test)]
mod tests {

    use crate::error::OxenError;
    use crate::model::NewCommitBody;
    use crate::opts::CloneOpts;
    use crate::{api, repositories, test, util};

    use crate::config::UserConfig;
    use crate::model::EntryDataType;
    use crate::model::staged_data::StagedDataOpts;
    use std::path::{Path, PathBuf};

    #[tokio::test]
    async fn test_remote_mode_commit_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 an empty 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());

                // Create new file in repo
                let file_path = test::add_txt_file_to_dir(&cloned_repo.path, "new file contents")?;

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

                // Commit
                let cfg = UserConfig::get()?;
                let body = NewCommitBody {
                    message: "Adding new file".to_string(),
                    author: cfg.name,
                    email: cfg.email,
                };

                let commit = repositories::remote_mode::commit(&cloned_repo, &body).await?;

                // Verify repo is clean
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(std::slice::from_ref(&cloned_repo.path));
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    &status_opts,
                )
                .await?;
                status.print();
                assert!(status.is_clean());

                // Verify head commit exists and is updated locally
                let head_commit = repositories::commits::head_commit(&cloned_repo)?;
                assert_eq!(head_commit.id, commit.id);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_remote_mode_commit_several_times() -> 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 head_commit = repositories::commits::head_commit(&cloned_repo)?;
                let commit_root =
                    repositories::tree::get_root_with_children(&cloned_repo, &head_commit)?
                        .unwrap();
                let mut files_in_tree =
                    repositories::tree::list_all_files(&commit_root, &PathBuf::from("."))?;
                let mut previous_head_commit = head_commit;

                assert_eq!(files_in_tree.len(), 1);

                // Perform several sequential commits and store the commit objects
                let mut commits = vec![];
                for i in 1..=4 {
                    let filename = format!("file_{i}.txt");
                    let file_path = PathBuf::from(&filename);
                    let full_path = cloned_repo.path.join(&file_path);
                    let file_content = format!("This is the content for file {i}");

                    test::write_txt_file_to_path(&full_path, &file_content)?;
                    api::client::workspaces::files::add(
                        &remote_repo,
                        &workspace_id,
                        &directory,
                        vec![file_path.clone()],
                        &Some(cloned_repo.clone()),
                    )
                    .await?;

                    let status_opts = StagedDataOpts::from_paths_remote_mode(std::slice::from_ref(
                        &cloned_repo.path,
                    ));
                    let status = repositories::remote_mode::status(
                        &cloned_repo,
                        &remote_repo,
                        &workspace_id,
                        &directory,
                        &status_opts,
                    )
                    .await?;
                    status.print();

                    let commit_message = format!("Adding {}", &filename);
                    let commit_body =
                        NewCommitBody::from_config(&UserConfig::get()?, &commit_message);

                    let new_commit =
                        repositories::remote_mode::commit(&cloned_repo, &commit_body).await?;
                    commits.push(new_commit.clone());

                    let new_head_commit = repositories::commits::head_commit(&cloned_repo)?;
                    assert_eq!(new_commit.id, new_head_commit.id);
                    assert_ne!(previous_head_commit.id, new_head_commit.id);

                    let commit_root =
                        repositories::tree::get_root_with_children(&cloned_repo, &new_commit)?
                            .unwrap();
                    let new_files_in_tree =
                        repositories::tree::list_all_files(&commit_root, &PathBuf::from("."))?;
                    assert_eq!(new_files_in_tree.len(), files_in_tree.len() + 1);
                    assert!(repositories::tree::has_path(
                        &cloned_repo,
                        &new_head_commit,
                        file_path
                    )?);

                    previous_head_commit = new_head_commit;
                    files_in_tree = new_files_in_tree;
                }

                // Test commit history list between two points
                let base_commit = commits[0].clone();
                let head_commit = commits[2].clone();

                let history =
                    repositories::commits::list_between(&cloned_repo, &base_commit, &head_commit)?;
                assert_eq!(history.len(), 3);

                assert_eq!(history.first().unwrap().message, head_commit.message);
                assert_eq!(history.last().unwrap().message, base_commit.message);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_remote_mode_add_and_commit_downloaded_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 {
                // Clone an empty 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());

                // Download file from remote:
                let head_commit = repositories::commits::head_commit(&cloned_repo)?;
                let annotations_dir = PathBuf::from("annotations");
                repositories::remote_mode::restore(
                    &cloned_repo,
                    std::slice::from_ref(&annotations_dir),
                    &head_commit.id,
                )
                .await?;

                // Verify bounding_box.csv and its parent dirs are no longer unsynced
                let workspace_identifier = cloned_repo.workspace_name.clone().unwrap();
                let directory = ".".to_string();
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(std::slice::from_ref(&cloned_repo.path));
                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.untracked_dirs.len(), 0);
                assert_eq!(status.unsynced_files.len(), 0);
                assert_eq!(status.unsynced_dirs.len(), 0);

                // Modify bounding box
                let subdir_path = PathBuf::from("annotations").join("train");
                let file_path = subdir_path.join("bounding_box.csv");
                let full_path = cloned_repo.path.join(&file_path);

                let new_contents = "file,label\ntrain/cat_1.jpg,1000";
                test::modify_txt_file(&full_path, new_contents)?;

                // Add and commit modified file
                api::client::workspaces::files::add(
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    vec![full_path],
                    &Some(cloned_repo.clone()),
                )
                .await?;
                let cfg = UserConfig::get()?;
                let body = NewCommitBody {
                    message: "Modifying bounding_box.csv".to_string(),
                    author: cfg.name,
                    email: cfg.email,
                };

                repositories::remote_mode::commit(&cloned_repo, &body).await?;

                // Verify the file is synced
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    &status_opts,
                )
                .await?;

                assert_eq!(status.untracked_files.len(), 0);
                assert_eq!(status.untracked_dirs.len(), 0);
                assert_eq!(status.unsynced_files.len(), 0);
                assert_eq!(status.unsynced_dirs.len(), 0);

                // TODO: Download file again to different name
                // TODO: Show that the file contents match new_contents

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_remote_mode_cannot_commit_without_staged_files() -> 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 {
                // Clone 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 the current number of commits
                let commits = repositories::commits::list(&cloned_repo)?;
                let initial_len = commits.len();

                // Modify a file, but do not add it
                let labels_path = cloned_repo.path.join(Path::new("labels.txt"));
                util::fs::write_to_path(&labels_path, "changing this guy, but not committing")?;

                // Try to commit, which should fail because nothing is staged
                let cfg = UserConfig::get()?;
                let body = NewCommitBody {
                    message: "Should not work".to_string(),
                    author: cfg.name,
                    email: cfg.email,
                };

                let result = repositories::remote_mode::commit(&cloned_repo, &body).await;
                assert!(result.is_err());

                // The number of commits should not have changed
                let commits = repositories::commits::list(&cloned_repo)?;
                assert_eq!(commits.len(), initial_len);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_remote_mode_commit_removed_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();

                // Create a file locally
                let hello_file_path = PathBuf::from("hello.txt");
                let full_path = cloned_repo.path.join(&hello_file_path);
                test::write_txt_file_to_path(&full_path, "Hello World")?;

                // Add the file, which uploads its content to the remote workspace
                api::client::workspaces::files::add(
                    &remote_repo,
                    &workspace_id,
                    &directory,
                    vec![hello_file_path.clone()],
                    &Some(cloned_repo.clone()),
                )
                .await?;

                // Remove the file locally from the working directory
                util::fs::remove_file(&full_path)?;

                // Commit the file, verifying the commit succeeds even though the file is no longer on disk
                let commit_body = NewCommitBody::from_config(&UserConfig::get()?, "My message");
                repositories::remote_mode::commit(&cloned_repo, &commit_body).await?;

                // Verify the head commit has two entries
                let head = repositories::commits::head_commit(&cloned_repo)?;
                let commit_list = repositories::entries::list_for_commit(&cloned_repo, &head)?;
                assert_eq!(commit_list.len(), 2);

                // Stage the deletion
                api::client::workspaces::files::rm_files(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_id,
                    vec![hello_file_path.clone()],
                )
                .await?;

                // Commit the deletion
                let commit_body = NewCommitBody::from_config(&UserConfig::get()?, "Second Message");
                repositories::remote_mode::commit(&cloned_repo, &commit_body).await?;

                // Verify only the orignal bounding box file remains
                let head = repositories::commits::head_commit(&cloned_repo)?;
                let commit_list = repositories::entries::list_for_commit(&cloned_repo, &head)?;
                assert_eq!(commit_list.len(), 1);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_remote_mode_commit_removed_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 files
                let dir_to_remove = PathBuf::from("train");
                let full_dir_path = cloned_repo.path.join(&dir_to_remove);
                util::fs::create_dir_all(&full_dir_path)?;
                let _ = test::add_txt_file_to_dir(&full_dir_path, "file1.txt")?;
                let _ = test::add_txt_file_to_dir(&full_dir_path, "file2.txt")?;
                let og_file_count = util::fs::rcount_files_in_dir(&full_dir_path) + 1; // +1 for the original bounding box

                // Add the directory, which stages its contents remotely
                api::client::workspaces::files::add(
                    &remote_repo,
                    &workspace_id,
                    &directory,
                    vec![dir_to_remove.clone()],
                    &Some(cloned_repo.clone()),
                )
                .await?;

                // Commit the new directory and its contents
                let commit_body =
                    NewCommitBody::from_config(&UserConfig::get()?, "Adding train directory");
                repositories::remote_mode::commit(&cloned_repo, &commit_body).await?;

                // Verify head has entries from the new directory
                let head = repositories::commits::head_commit(&cloned_repo)?;
                let commit_list = repositories::entries::list_for_commit(&cloned_repo, &head)?;
                assert_eq!(commit_list.len(), og_file_count);

                // Delete the directory locally
                util::fs::remove_dir_all(&full_dir_path)?;

                // Add the deletion to stage the removal
                api::client::workspaces::files::rm_files(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_id,
                    vec![dir_to_remove.clone()],
                )
                .await?;
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(&[PathBuf::from(directory.clone())]);
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_id,
                    &directory,
                    &status_opts,
                )
                .await?;
                status.print();
                // Commit the deletion
                let commit_body =
                    NewCommitBody::from_config(&UserConfig::get()?, "Removing train directory");
                repositories::remote_mode::commit(&cloned_repo, &commit_body).await?;

                // Verify no entries remain in the head commit except the original bounding box
                let head = repositories::commits::head_commit(&cloned_repo)?;
                let commit_list = repositories::entries::list_for_commit(&cloned_repo, &head)?;
                println!("Commit list: {commit_list:?}");
                assert_eq!(commit_list.len(), 1);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_remote_mode_commit_invalid_parquet_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();

                // Create an invalid parquet file locally
                let invalid_parquet_file = test::test_invalid_parquet_file();
                let full_path = cloned_repo.path.join("invalid.parquet");
                util::fs::copy(&invalid_parquet_file, &full_path)?;

                let file_path = PathBuf::from("invalid.parquet");
                api::client::workspaces::files::add(
                    &remote_repo,
                    &workspace_id,
                    &directory,
                    vec![file_path.clone()],
                    &Some(cloned_repo.clone()),
                )
                .await?;

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

                // Verify the file's data type in the commit tree
                let head = repositories::commits::head_commit(&cloned_repo)?;
                let tree =
                    repositories::tree::get_root_with_children(&cloned_repo, &head)?.unwrap();
                let file_node = tree.get_by_path(&file_path)?.unwrap();

                assert_eq!(*file_node.file()?.data_type(), EntryDataType::Binary);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }
}