liboxen 0.49.1

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
use crate::api;
use crate::error::OxenError;
use crate::model::LocalRepository;
use crate::model::RemoteRepository;
use crate::model::StagedData;
use crate::model::StagedEntry;
use crate::model::StagedEntryStatus;
use crate::model::staged_data::StagedDataOpts;

use crate::core::v_latest::status::status_from_opts_and_staged_data;

use std::collections::HashMap;
use std::path::{Path, PathBuf};

pub async fn status(
    local_repository: &LocalRepository,
    remote_repo: &RemoteRepository,
    workspace_identifier: &str,
    directory: impl AsRef<Path>,
    opts: &StagedDataOpts,
) -> Result<StagedData, OxenError> {
    let page_size = opts.limit;
    let page_num = opts.skip / page_size;

    let remote_status = api::client::workspaces::changes::list(
        remote_repo,
        workspace_identifier,
        directory,
        page_num,
        page_size,
    )
    .await?;

    let mut status = StagedData::empty();
    status.staged_dirs = remote_status.added_dirs;

    let added_files: HashMap<PathBuf, StagedEntry> =
        HashMap::from_iter(remote_status.added_files.entries.into_iter().map(|e| {
            (
                PathBuf::from(e.filename()),
                StagedEntry::empty_status(StagedEntryStatus::Added),
            )
        }));
    let added_mods: HashMap<PathBuf, StagedEntry> =
        HashMap::from_iter(remote_status.modified_files.entries.into_iter().map(|e| {
            (
                PathBuf::from(e.filename()),
                StagedEntry::empty_status(StagedEntryStatus::Modified),
            )
        }));
    let staged_removals: HashMap<PathBuf, StagedEntry> =
        HashMap::from_iter(remote_status.removed_files.entries.into_iter().map(|e| {
            (
                PathBuf::from(e.filename()),
                StagedEntry::empty_status(StagedEntryStatus::Removed),
            )
        }));
    status.staged_files = added_files
        .into_iter()
        .chain(added_mods)
        .chain(staged_removals)
        .collect();

    // Get local status
    let is_remote = false;
    let local_opts = StagedDataOpts {
        paths: opts.paths.clone(),
        skip: opts.skip,
        limit: opts.limit,
        print_all: opts.print_all,
        is_remote,
        ignore: None,
    };

    status_from_opts_and_staged_data(local_repository, &local_opts, &mut status).await?;

    Ok(status)
}

#[cfg(test)]
mod tests {

    use std::path::PathBuf;

    use crate::error::OxenError;
    use crate::model::staged_data::StagedDataOpts;
    use crate::opts::clone_opts::CloneOpts;

    use crate::{api, repositories, test, util};

    // For reference, the fully synced repo structure is as follows:
    // nlp/
    //   classification/
    //     annotations/
    //       train.tsv
    //       test.tsv
    //
    // train/
    //   dog_1.jpg
    //   dog_2.jpg
    //   dog_3.jpg
    //   cat_1.jpg
    //   cat_2.jpg
    // test/
    //   1.jpg
    //   2.jpg
    // annotations/
    //   README.md
    //   train/
    //     bounding_box.csv
    //     one_shot.csv
    //     two_shot.csv
    //     annotations.txt
    //   test/
    //     annotations.csv
    // prompts.jsonl
    // labels.txt
    // LICENSE
    // README.md

    #[tokio::test]
    async fn test_repo_clean_with_all_files_unsynced_after_remote_mode_clone()
    -> Result<(), OxenError> {
        test::run_training_data_fully_sync_remote(|mut _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?;

                let workspace_identifier = cloned_repo.workspace_name.clone().unwrap();
                let directory = ".".to_string();
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(&[PathBuf::from(directory.clone())]);
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    &status_opts,
                )
                .await?;
                status.print();
                // Files/dirs in subdirs don't appear as separate items in unsynced_files/dirs
                assert_eq!(status.unsynced_dirs.len(), 4);
                assert_eq!(status.unsynced_files.len(), 4);

                // The repo is clean
                assert!(status.is_clean());

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    #[tokio::test]
    async fn test_remote_mode_subdirectory_status() -> 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 repo_path = cloned_repo.path.clone();

                let directory = ".".to_string();
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(std::slice::from_ref(&repo_path));
                let workspace_identifier = cloned_repo.workspace_name.clone().unwrap();
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    &status_opts,
                )
                .await?;
                status.print();

                // Files/dirs in subdirs don't appear as separate items in unsynced_files/dirs
                assert_eq!(status.unsynced_dirs.len(), 4);
                assert_eq!(status.unsynced_files.len(), 4);

                // Download specific files from the remote
                let subdir_path = PathBuf::from("annotations").join("train");
                let one_shot_path = subdir_path.join("one_shot.csv");
                let two_shot_path = subdir_path.join("two_shot.csv");
                let bounding_box_path = subdir_path.join("bounding_box.csv");

                let head_commit = repositories::commits::head_commit(&cloned_repo)?;
                repositories::remote_mode::restore(
                    &cloned_repo,
                    std::slice::from_ref(&one_shot_path),
                    &head_commit.id,
                )
                .await?;
                repositories::remote_mode::restore(
                    &cloned_repo,
                    std::slice::from_ref(&two_shot_path),
                    &head_commit.id,
                )
                .await?;
                repositories::remote_mode::restore(
                    &cloned_repo,
                    std::slice::from_ref(&bounding_box_path),
                    &head_commit.id,
                )
                .await?;

                // Modify one_shot.csv
                let new_content = "new content coming in hot";
                test::modify_txt_file(cloned_repo.path.join(&one_shot_path), new_content)?;

                // Modify and add two_shot.csv
                let new_content = "new content coming in even hotter!";
                test::modify_txt_file(cloned_repo.path.join(&two_shot_path), new_content)?;
                api::client::workspaces::files::add(
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    vec![two_shot_path.clone()],
                    &Some(cloned_repo.clone()),
                )
                .await?;

                // Remove bounding_box.csv
                api::client::workspaces::files::rm_files(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    vec![bounding_box_path.clone()],
                )
                .await?;

                // Check status for corresponding changes
                let directory = ".".to_string();
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(std::slice::from_ref(&repo_path));
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    &status_opts,
                )
                .await?;
                status.print();

                // 6 unsynced files, as creating the parent dirs for the restored files causes more subfiles to be registed as unsynced
                assert_eq!(status.unsynced_dirs.len(), 4);
                assert_eq!(status.unsynced_files.len(), 6);

                assert_eq!(status.modified_files.len(), 1);
                assert!(status.modified_files.contains(&one_shot_path));

                assert_eq!(status.staged_files.len(), 2);
                assert!(status.staged_files.contains_key(&two_shot_path));
                assert!(status.staged_files.contains_key(&bounding_box_path));

                // Stage the subdirectory itself
                api::client::workspaces::files::add(
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    vec![subdir_path.clone()],
                    &Some(cloned_repo.clone()),
                )
                .await?;

                // Re-check status
                let directory = ".".to_string();
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(std::slice::from_ref(&repo_path));
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    &status_opts,
                )
                .await?;
                status.print();

                assert_eq!(status.unsynced_dirs.len(), 4);
                assert_eq!(status.unsynced_files.len(), 6);
                assert_eq!(status.staged_files.len(), 3);
                assert_eq!(status.modified_files.len(), 0);

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    // Regression test for the missing-on-disk classification gate. A file that's tracked
    // in the merkle tree, server-staged with status Added/Modified, and then removed
    // from local disk should still surface as unsynced — only `Removed`-status entries
    // should suppress the unsynced classification. Earlier the gate used a plain
    // `contains_key` check on the staged-files map, which over-suppressed.
    #[tokio::test]
    async fn test_remote_mode_status_unsynced_when_modified_stage_missing_on_disk()
    -> 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?;

                let repo_path = cloned_repo.path.clone();
                let directory = ".".to_string();
                let workspace_identifier = cloned_repo.workspace_name.clone().unwrap();
                let status_opts =
                    StagedDataOpts::from_paths_remote_mode(std::slice::from_ref(&repo_path));

                // Restore a single file from HEAD so it lives on disk and in the tree.
                let target_path = PathBuf::from("annotations")
                    .join("train")
                    .join("two_shot.csv");
                let head_commit = repositories::commits::head_commit(&cloned_repo)?;
                repositories::remote_mode::restore(
                    &cloned_repo,
                    std::slice::from_ref(&target_path),
                    &head_commit.id,
                )
                .await?;

                // Modify locally and stage on the server (Modified).
                test::modify_txt_file(cloned_repo.path.join(&target_path), "new contents")?;
                api::client::workspaces::files::add(
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    vec![target_path.clone()],
                    &Some(cloned_repo.clone()),
                )
                .await?;

                // Sanity: file is server-staged and not unsynced while it's still on disk.
                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    &status_opts,
                )
                .await?;
                assert!(status.staged_files.contains_key(&target_path));
                assert!(!status.unsynced_files.contains(&target_path));

                // Delete the local copy without going through `oxen rm` — the staged
                // entry is still Modified on the server.
                util::fs::remove_file(cloned_repo.path.join(&target_path))?;

                let status = repositories::remote_mode::status(
                    &cloned_repo,
                    &remote_repo,
                    &workspace_identifier,
                    &directory,
                    &status_opts,
                )
                .await?;
                status.print();

                // The server-side stage is still present, AND the missing-on-disk file
                // now surfaces as unsynced because its staged status is Modified, not
                // Removed.
                assert!(status.staged_files.contains_key(&target_path));
                assert!(status.unsynced_files.contains(&target_path));

                Ok(())
            })
            .await?;

            Ok(remote_repo_copy)
        })
        .await
    }

    // NOTE: With the current workspace::changes::status command used in remote_mode::status,
    //       We cannot detect moved files accurately, as it does not return the hashes of the staged entries
    //       TODO: Consider fixing this

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

                let repo_path = cloned_repo.path.clone();
                log::debug!("Cloned repo path: {:?}", util::fs::canonicalize(&cloned_repo.path));
                let workspace_identifier = cloned_repo.workspace_name.clone().unwrap();
                let directory = ".".to_string();

                let head_commit = repositories::commits::head_commit(&cloned_repo)?;

                let og_basename = PathBuf::from("README.md");
                repositories::remote_mode::restore(&cloned_repo, &vec![og_basename.clone()], &head_commit.id).await?;

                let og_file = cloned_repo.path.join(&og_basename);
                let new_basename = PathBuf::from("README2.md");
                let new_file = cloned_repo.path.join(&new_basename);

                util::fs::rename(&og_file, &new_file)?;

                // Status before adding should show 4 unsynced files (README.md,  LICENSE, prompts.jsonl, labels.txt) and an untracked file
                let status_opts = StagedDataOpts::from_paths_remote_mode(std::slice::from_ref(&repo_path));
                let status = repositories::remote_mode::status(&cloned_repo, &remote_repo, &workspace_identifier, &directory, &status_opts).await?;
                status.print();
                assert_eq!(status.moved_files.len(), 0);
                assert_eq!(status.unsynced_files.len(), 4);
                assert_eq!(status.untracked_files.len(), 1);

                // Remove the previous file
                api::client::workspaces::files::rm_files(&cloned_repo, &remote_repo, &workspace_identifier, vec![og_basename.clone()]).await?;
                let status_opts = StagedDataOpts::from_paths_remote_mode(std::slice::from_ref(&repo_path));
                let status = repositories::remote_mode::status(&cloned_repo, &remote_repo, &workspace_identifier, &directory, &status_opts).await?;
                status.print();
                assert_eq!(status.moved_files.len(), 0);
                assert_eq!(status.staged_files.len(), 1);
                assert_eq!(status.untracked_files.len(), 1);

                // Add the new file to complete the pair
                api::client::workspaces::files::add(&cloned_repo, &remote_repo, &workspace_identifier, &directory, vec![new_basename.clone()]).await?;
                let status_opts = StagedDataOpts::from_paths_remote_mode(&[repo_path]);
                let status = repositories::remote_mode::status(&cloned_repo, &remote_repo, &workspace_identifier, &directory, &status_opts).await?;
                status.print();
                assert_eq!(status.moved_files.len(), 1);
                assert_eq!(status.staged_files.len(), 2);

                Ok(())
            }).await?;

            Ok(remote_repo_copy)
        }).await
    }
    */
}