liboxen 0.48.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
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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
use crate::config::RepositoryConfig;
use crate::constants::SHALLOW_FLAG;
use crate::constants::{self, DEFAULT_VNODE_SIZE, MIN_OXEN_VERSION};
use crate::core::versions::MinOxenVersion;
use crate::error::OxenError;
use crate::model::{MetadataEntry, Remote, RemoteRepository};
use crate::opts::StorageOpts;
use crate::storage::{StorageConfig, VersionStore, create_version_store};
use crate::util;
use crate::view::RepositoryView;

use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::fmt::Debug;
use std::path::{Path, PathBuf};
use std::sync::{Arc, LazyLock, Mutex};
use std::time::{Duration, SystemTime};
use utoipa::ToSchema;

/// Per-process cache of mtime round-trip tolerance, keyed by repo path. Probed at most
/// once per repo per process via `probe_mtime_drift`.
static MTIME_TOLERANCE_CACHE: LazyLock<Mutex<HashMap<PathBuf, Duration>>> =
    LazyLock::new(|| Mutex::new(HashMap::new()));

#[derive(Serialize, Deserialize, Debug, Clone, ToSchema)]
pub struct LocalRepository {
    #[schema(value_type = String)]
    pub path: PathBuf,
    // Optional remotes to sync the data to
    remote_name: Option<String>, // name of the current remote ("origin" by default)
    min_version: Option<String>, // write the version if it is past v0.18.4
    remotes: Vec<Remote>,        // List of possible remotes
    vnode_size: Option<u64>,     // Size of the vnodes
    #[schema(value_type = Option<Vec<String>>)]
    subtree_paths: Option<Vec<PathBuf>>, // If the user clones a subtree, we store the paths here so that we know we don't have the full tree
    pub depth: Option<i32>, // If the user clones with a depth, we store the depth here so that we know we don't have the full tree
    pub vfs: Option<bool>,  // Flag for repositories stored on virtual file systems
    pub remote_mode: Option<bool>, // Flag for remote repositories
    pub workspace_name: Option<String>, // ID of the associated workspace for remote mode
    workspaces: Option<Vec<String>>, // List of workspaces for remote mode

    // Skip this field during serialization/deserialization
    #[serde(skip)]
    #[schema(ignore)]
    version_store: Option<Arc<dyn VersionStore>>,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct LocalRepositoryWithEntries {
    pub local_repo: LocalRepository,
    pub entries: Option<Vec<MetadataEntry>>,
}

impl LocalRepository {
    /// Create a LocalRepository from a directory
    pub fn from_dir(path: impl AsRef<Path>) -> Result<Self, OxenError> {
        let path = path.as_ref().to_path_buf();
        let config_path = util::fs::config_filepath(&path);
        let config = RepositoryConfig::from_file(&config_path)?;

        let mut repo = LocalRepository {
            path,
            remote_name: config.remote_name,
            min_version: config.min_version,
            remotes: config.remotes,
            vnode_size: config.vnode_size,
            subtree_paths: config.subtree_paths.clone(),
            depth: config.depth,
            version_store: None,
            vfs: config.vfs,
            remote_mode: config.remote_mode,
            workspace_name: config.workspace_name,
            workspaces: config.workspaces,
        };

        // Initialize the version store based on config
        let storage_opts = if let Some(storage_config) = config.storage {
            StorageOpts::from_repo_config(&repo, &storage_config)?
        } else {
            StorageOpts::from_path(&repo.path, true)
        };
        let store = create_version_store(&repo.path, &storage_opts)?;
        repo.version_store = Some(store);
        Ok(repo)
    }

    /// Get a reference to the version store
    pub fn version_store(&self) -> Result<Arc<dyn VersionStore>, OxenError> {
        match &self.version_store {
            Some(store) => Ok(Arc::clone(store)),
            None => Err(OxenError::basic_str("Version store not initialized")),
        }
    }

    pub fn init_version_store(&mut self, storage_opts: &StorageOpts) -> Result<(), OxenError> {
        let store = create_version_store(&self.path, storage_opts)?;
        self.version_store = Some(store);
        Ok(())
    }

    /// Initialize the default version store
    /// this will be a local storage backend
    pub fn init_default_version_store(&mut self) -> Result<(), OxenError> {
        let storage_opts = StorageOpts::from_path(&self.path, true);

        // Create and initialize the store
        let store = create_version_store(&self.path, &storage_opts)?;
        self.version_store = Some(store);
        Ok(())
    }

    /// Initialize local version store at a new location
    pub async fn set_version_store(&mut self, storage_opts: &StorageOpts) -> Result<(), OxenError> {
        let version_store = create_version_store(&self.path, storage_opts)?;
        version_store.init().await?;
        self.version_store = Some(version_store);

        Ok(())
    }

    /// Load a repository from the current directory
    /// this traverses up the directory tree until it finds a .oxen/ directory
    pub fn from_current_dir() -> Result<LocalRepository, OxenError> {
        let current_dir = std::env::current_dir().map_err(OxenError::from)?;
        let repo_dir = util::fs::get_repo_root_from_current_dir()
            .ok_or_else(|| OxenError::local_repo_not_found(&current_dir))?;

        LocalRepository::from_dir(&repo_dir)
    }

    /// Instantiate a new repository at a given path
    /// Note: Does not create the repository on disk, or read the config file, just instantiates the struct
    /// To load the repository, use `LocalRepository::from_dir` or `LocalRepository::from_current_dir`
    pub fn new(
        path: impl AsRef<Path>,
        storage_opts: Option<StorageOpts>,
    ) -> Result<LocalRepository, OxenError> {
        let mut repo = LocalRepository {
            path: path.as_ref().to_path_buf(),
            // No remotes are set yet
            remotes: vec![],
            remote_name: None,
            // New with a path should default to our current MIN_OXEN_VERSION
            min_version: Some(MIN_OXEN_VERSION.to_string()),
            vnode_size: None,
            subtree_paths: None,
            depth: None,
            version_store: None,
            vfs: None,
            remote_mode: None,
            workspace_name: None,
            workspaces: None,
        };

        if let Some(storage_opts) = storage_opts {
            repo.init_version_store(&storage_opts)?;
        } else {
            repo.init_default_version_store()?;
        }
        Ok(repo)
    }

    /// Load an older version of a repository with older oxen core logic
    pub fn new_from_version(
        path: impl AsRef<Path>,
        min_version: impl AsRef<str>,
        storage_opts: Option<StorageOpts>,
    ) -> Result<LocalRepository, OxenError> {
        let mut repo = LocalRepository {
            path: path.as_ref().to_path_buf(),
            remotes: vec![],
            remote_name: None,
            min_version: Some(min_version.as_ref().to_string()),
            vnode_size: None,
            subtree_paths: None,
            depth: None,
            version_store: None,
            vfs: None,
            remote_mode: None,
            workspace_name: None,
            workspaces: None,
        };

        if let Some(storage_opts) = storage_opts {
            repo.init_version_store(&storage_opts)?;
        } else {
            repo.init_default_version_store()?;
        }
        Ok(repo)
    }

    pub fn from_view(view: RepositoryView) -> Result<LocalRepository, OxenError> {
        let mut repo = LocalRepository {
            path: std::env::current_dir()?.join(view.name),
            remotes: vec![],
            remote_name: None,
            min_version: None,
            vnode_size: None,
            subtree_paths: None,
            depth: None,
            version_store: None,
            vfs: None,
            remote_mode: None,
            workspace_name: None,
            workspaces: None,
        };

        repo.init_default_version_store()?;
        Ok(repo)
    }

    pub fn from_remote(repo: RemoteRepository, path: &Path) -> Result<LocalRepository, OxenError> {
        let mut local_repo = LocalRepository {
            path: path.to_owned(),
            remotes: vec![repo.remote],
            remote_name: Some(String::from(constants::DEFAULT_REMOTE_NAME)),
            min_version: None,
            vnode_size: None,
            subtree_paths: None,
            depth: None,
            version_store: None,
            vfs: None,
            remote_mode: None,
            workspace_name: None,
            workspaces: None,
        };

        local_repo.init_default_version_store()?;
        Ok(local_repo)
    }

    pub fn min_version(&self) -> MinOxenVersion {
        match MinOxenVersion::or_earliest(self.min_version.clone()) {
            Ok(version) => version,
            Err(err) => {
                panic!("Invalid repo version\n{err}")
            }
        }
    }

    pub fn set_remote_name(&mut self, name: impl AsRef<str>) {
        self.remote_name = Some(name.as_ref().to_string());
    }

    pub fn set_min_version(&mut self, version: MinOxenVersion) {
        self.min_version = Some(version.to_string());
    }

    pub fn remotes(&self) -> &Vec<Remote> {
        &self.remotes
    }

    pub fn dirname(&self) -> String {
        String::from(self.path.file_name().unwrap().to_str().unwrap())
    }

    pub fn vnode_size(&self) -> u64 {
        self.vnode_size.unwrap_or(DEFAULT_VNODE_SIZE)
    }

    pub fn set_vnode_size(&mut self, size: u64) {
        self.vnode_size = Some(size);
    }

    pub fn subtree_paths(&self) -> Option<Vec<PathBuf>> {
        self.subtree_paths.as_ref().map(|paths| {
            paths
                .iter()
                .map(|p| {
                    if p == &PathBuf::from(".") {
                        PathBuf::from("")
                    } else {
                        p.clone()
                    }
                })
                .collect()
        })
    }

    pub fn set_subtree_paths(&mut self, paths: Option<Vec<PathBuf>>) {
        self.subtree_paths = paths;
    }

    pub fn depth(&self) -> Option<i32> {
        self.depth
    }

    pub fn set_depth(&mut self, depth: Option<i32>) {
        self.depth = depth;
    }

    pub fn set_remote_mode(&mut self, is_remote: Option<bool>) {
        self.remote_mode = is_remote;
    }

    pub fn is_remote_mode(&self) -> bool {
        self.remote_mode.unwrap_or(false)
    }

    pub fn is_vfs(&self) -> bool {
        self.vfs.unwrap_or(false)
    }

    pub fn set_vfs(&mut self, is_vfs: Option<bool>) {
        self.vfs = is_vfs;
    }

    /// Save the repository configuration to disk
    pub fn save(&self) -> Result<(), OxenError> {
        let config_path = util::fs::config_filepath(&self.path);

        // Determine the current storage type and settings using the trait methods
        let storage = self
            .version_store
            .as_ref()
            .map(|store| -> Result<StorageConfig, OxenError> {
                let settings = store.storage_settings();
                match store.storage_type() {
                    "local" => {
                        let path = settings.get("path").ok_or_else(|| {
                            OxenError::basic_str("Storage settings missing 'path' key")
                        })?;
                        let storage_path = if util::fs::is_relative_to_dir(
                            path,
                            util::fs::oxen_hidden_dir(&self.path),
                        ) {
                            // If path is within .oxen (default location), use the relative path in case the repo was moved
                            util::fs::path_relative_to_dir(path, &self.path)
                                .unwrap()
                                .to_string_lossy()
                                .into_owned()
                        } else {
                            // Otherwise, use the absolute path
                            path.clone()
                        };

                        Ok(StorageConfig {
                            type_: store.storage_type().to_string(),
                            settings: HashMap::from([("path".to_string(), storage_path)]),
                        })
                    }
                    _ => Ok(StorageConfig {
                        type_: store.storage_type().to_string(),
                        settings,
                    }),
                }
            })
            .transpose()?;

        let config = RepositoryConfig {
            remote_name: self.remote_name.clone(),
            remotes: self.remotes.clone(),
            subtree_paths: self.subtree_paths.clone(),
            depth: self.depth,
            min_version: self.min_version.clone(),
            vnode_size: self.vnode_size,
            storage,
            vfs: self.vfs,
            remote_mode: self.remote_mode,
            workspace_name: self.workspace_name.clone(),
            workspaces: self.workspaces.clone(),
        };

        config.save(&config_path)
    }

    pub fn set_remote(&mut self, name: impl AsRef<str>, url: impl AsRef<str>) -> Remote {
        self.remote_name = Some(name.as_ref().to_owned());
        let name = name.as_ref();
        let url = url.as_ref();
        let remote = Remote {
            name: name.to_owned(),
            url: url.to_owned(),
        };
        if self.has_remote(name) {
            // find remote by name and set
            for i in 0..self.remotes.len() {
                if self.remotes[i].name == name {
                    self.remotes[i] = remote.clone()
                }
            }
        } else {
            // we don't have the key, just push
            self.remotes.push(remote.clone());
        }
        remote
    }

    pub fn delete_remote(&mut self, name: impl AsRef<str>) {
        let name = name.as_ref();
        let mut new_remotes: Vec<Remote> = vec![];
        for i in 0..self.remotes.len() {
            if self.remotes[i].name != name {
                new_remotes.push(self.remotes[i].clone());
            }
        }
        self.remotes = new_remotes;
    }

    pub fn has_remote(&self, name: impl AsRef<str>) -> bool {
        let name = name.as_ref();
        for remote in self.remotes.iter() {
            if remote.name == name {
                return true;
            }
        }
        false
    }

    pub fn get_remote(&self, name: impl AsRef<str>) -> Option<Remote> {
        let name = name.as_ref();
        log::trace!("Checking for remote {name} have {}", self.remotes.len());
        for remote in self.remotes.iter() {
            log::trace!("comparing: {name} -> {}", remote.name);
            if remote.name == name {
                return Some(remote.clone());
            }
        }
        None
    }

    pub fn remote(&self) -> Option<Remote> {
        if let Some(name) = &self.remote_name {
            self.get_remote(name)
        } else {
            None
        }
    }

    pub fn add_workspace(&mut self, name: impl AsRef<str>) {
        let workspace_name = name.as_ref();
        let workspaces = self.workspaces.clone().unwrap_or_default();

        let mut new_workspaces = HashSet::new();
        for workspace in workspaces {
            new_workspaces.insert(workspace.clone());
        }

        new_workspaces.insert(workspace_name.to_string());
        self.workspaces = Some(new_workspaces.iter().cloned().collect());
    }

    pub fn delete_workspace(&mut self, name: impl AsRef<str>) -> Result<(), OxenError> {
        let name = name.as_ref();

        if self.workspaces.is_none() {
            return Err(OxenError::basic_str(format!(
                "Error: Cannot delete workspace {name:?} as it does not exist"
            )));
        }

        // TODO: Allow deletions when workspace_name isn't set?
        // This seems like an impossible scenario...
        if self.workspace_name.is_some() && name == self.workspace_name.as_ref().unwrap() {
            return Err(OxenError::basic_str(
                "Error: Cannot delete current workspace",
            ));
        }

        let mut new_workspaces: Vec<String> = vec![];
        let prev_workspaces = self.workspaces.clone().unwrap();
        for workspace in prev_workspaces {
            if workspace != name {
                new_workspaces.push(workspace.clone());
            }
        }
        self.workspaces = Some(new_workspaces);
        Ok(())
    }

    pub fn has_workspace(&self, name: impl AsRef<str>) -> bool {
        let workspace_name = name.as_ref();
        self.workspaces.is_some()
            && self
                .workspaces
                .clone()
                .unwrap()
                .contains(&workspace_name.to_string())
    }

    // TODO: Should we define setting a workspace that's not in the workspaces vec to be an error?
    pub fn set_workspace(&mut self, name: impl AsRef<str>) -> Result<(), OxenError> {
        let workspace_name = name.as_ref();

        if let Some(ws_name) = self
            .workspaces
            .clone()
            .unwrap()
            .iter()
            .find(|ws| ws.starts_with(&format!("{workspace_name}: ")))
        {
            self.workspace_name = Some(ws_name.to_string());
        } else {
            self.add_workspace(workspace_name);
            self.workspace_name = Some(workspace_name.to_string());
        }
        Ok(())
    }

    pub fn num_workspaces(&self) -> usize {
        if let Some(workspaces) = &self.workspaces {
            workspaces.len()
        } else {
            0
        }
    }

    pub fn write_is_shallow(&self, shallow: bool) -> Result<(), OxenError> {
        let shallow_flag_path = util::fs::oxen_hidden_dir(&self.path).join(SHALLOW_FLAG);
        log::debug!("Write is shallow [{shallow}] to path: {shallow_flag_path:?}");
        if shallow {
            util::fs::write_to_path(&shallow_flag_path, "true")?;
        } else if shallow_flag_path.exists() {
            util::fs::remove_file(&shallow_flag_path)?;
        }
        Ok(())
    }

    /// Tolerance to allow when comparing an on-disk file's mtime against a value recorded
    /// on a merkle-tree node (used by `restore`'s fast-path skip check, and available for
    /// any other caller that needs to do mtime equality against a recorded value). The
    /// working tree is always on the same filesystem as `.oxen/`, so we probe inside
    /// `.oxen/` — same filesystem, guaranteed writable for a local repo, and already
    /// hidden from `oxen status`.
    ///
    /// Returns `Duration::ZERO` if the filesystem round-trips nanosecond-precision
    /// mtimes exactly (ext4 / APFS / NTFS), `Duration::from_secs(2)` if any drift is
    /// detected (conservative upper bound covering FAT/exFAT's 2s rounding, HFS+'s 1s,
    /// coarse NFS mounts, etc. — one probe isn't authoritative about the true max drift,
    /// so we pick a safe ceiling). I/O errors also return `ZERO`.
    ///
    /// Probed at most once per repo per process; the result is memoized in a module-level
    /// `HashMap` keyed by `self.path`.
    pub async fn mtime_tolerance(&self) -> Duration {
        if let Some(&t) = MTIME_TOLERANCE_CACHE
            .lock()
            .expect("mtime tolerance cache poisoned")
            .get(&self.path)
        {
            return t;
        }
        let t = probe_mtime_drift(&self.path.join(constants::OXEN_HIDDEN_DIR)).await;
        MTIME_TOLERANCE_CACHE
            .lock()
            .expect("mtime tolerance cache poisoned")
            .insert(self.path.clone(), t);
        t
    }
}

/// Write a probe file inside `probe_dir`, set its mtime to a non-zero-nanosecond value,
/// read the mtime back, and return a conservative tolerance. See `LocalRepository::mtime_tolerance`
/// for the policy and rationale.
async fn probe_mtime_drift(probe_dir: &Path) -> Duration {
    let probe_path = probe_dir.join(".oxen-mtime-probe");
    if tokio::fs::write(&probe_path, b"").await.is_err() {
        return Duration::ZERO;
    }
    // 1970-01-01 00:00:01.123456789 — non-zero nanoseconds so any rounding is measurable.
    let target = SystemTime::UNIX_EPOCH + Duration::new(1, 123_456_789);
    let set_ok =
        filetime::set_file_mtime(&probe_path, filetime::FileTime::from_system_time(target)).is_ok();
    let drift_detected = if set_ok {
        match tokio::fs::metadata(&probe_path).await {
            Ok(meta) => meta
                .modified()
                .map(|actual| actual != target)
                .unwrap_or(false),
            Err(_) => false,
        }
    } else {
        false
    };
    let _ = tokio::fs::remove_file(&probe_path).await;
    if drift_detected {
        Duration::from_secs(2)
    } else {
        Duration::ZERO
    }
}

#[cfg(test)]
mod tests {
    use crate::error::OxenError;
    use crate::model::{LocalRepository, RepoNew};
    use crate::test;
    use tempfile::TempDir;

    #[test]
    fn test_get_dirname_from_url() -> Result<(), OxenError> {
        let url = "http://0.0.0.0:3000/repositories/OxenData";
        let repo = RepoNew::from_url(url)?;
        assert_eq!(repo.name, "OxenData");
        assert_eq!(repo.namespace, "repositories");
        Ok(())
    }

    #[test]
    fn test_get_set_has_remote() -> Result<(), OxenError> {
        test::run_empty_local_repo_test(|mut local_repo| {
            let url = "http://0.0.0.0:3000/repositories/OxenData";
            let remote_name = "origin";
            local_repo.set_remote(remote_name, url);
            let remote = local_repo.get_remote(remote_name).unwrap();
            assert_eq!(remote.name, remote_name);
            assert_eq!(remote.url, url);

            Ok(())
        })
    }

    #[test]
    fn test_delete_remote() -> Result<(), OxenError> {
        test::run_empty_local_repo_test(|mut local_repo| {
            let origin_url = "http://0.0.0.0:3000/repositories/OxenData";
            let origin_name = "origin";

            let other_url = "http://0.0.0.0:4000/repositories/OxenData";
            let other_name = "other";
            local_repo.set_remote(origin_name, origin_url);
            local_repo.set_remote(other_name, other_url);

            // Remove and make sure we cannot get again
            local_repo.delete_remote(origin_name);
            let remote = local_repo.get_remote(origin_name);
            assert!(remote.is_none());

            Ok(())
        })
    }

    // Note: Adding/Setting/Deleting workspaces does not currently require the repo to be in remote mode
    // Do we want to require that?
    #[test]
    fn test_add_workspace() -> Result<(), OxenError> {
        let temp_dir = TempDir::new()?;
        let repo_path = temp_dir.path().to_path_buf();
        let mut repo = LocalRepository::new(repo_path, None)?;

        let sample_name = "sample";
        repo.add_workspace(sample_name);

        let result = repo.has_workspace(sample_name);
        assert!(result);

        repo.set_workspace(sample_name)?;
        assert_eq!(repo.workspace_name, Some(sample_name.to_string()));

        Ok(())
    }

    #[test]
    fn test_cannot_add_repeat_workspace() -> Result<(), OxenError> {
        let temp_dir = TempDir::new()?;
        let repo_path = temp_dir.path().to_path_buf();
        let mut repo = LocalRepository::new(repo_path, None)?;

        let sample_name = "sample";
        repo.add_workspace(sample_name);
        assert_eq!(repo.num_workspaces(), 1);

        Ok(())
    }

    #[test]
    fn test_delete_workspace() -> Result<(), OxenError> {
        let temp_dir = TempDir::new()?;
        let repo_path = temp_dir.path().to_path_buf();
        let mut repo = LocalRepository::new(repo_path, None)?;

        let sample_name = "sample";
        repo.add_workspace(sample_name);
        repo.set_workspace(sample_name)?;

        // Cannot delete current workspace_name
        let result = repo.delete_workspace(sample_name);
        assert!(result.is_err());

        let sample_2 = "second";
        repo.add_workspace(sample_2);
        repo.set_workspace(sample_2)?;

        // Can delete previous workspace_name
        repo.delete_workspace(sample_name)?;

        Ok(())
    }
}