Skip to main content
← dvs documentation Rust API reference

dvs/
lib.rs

1pub mod audit;
2pub mod backends;
3mod cache;
4pub mod config;
5mod files;
6mod gitignore;
7pub mod globbing;
8mod hashes;
9pub mod init;
10pub mod paths;
11pub mod progress;
12pub(crate) mod utils;
13
14pub use backends::Backend;
15pub use config::Compression;
16pub use files::add::{AddDetail, AddResult, add_files};
17pub use files::get::{GetDetail, GetResult, get_files};
18pub use files::metadata::FileMetadata;
19pub use files::status::{FileStatus, StatusDetail, get_status};
20pub use files::types::{Outcome, Status};
21pub use hashes::{HashAlg, Hashes};
22pub use paths::{AddPathStatus, DvsPaths, PathFilter, find_repo_root};
23pub use progress::FileProgress;
24pub use utils::{format_size, set_num_threads};
25
26pub const VERSION: &str = env!("CARGO_PKG_VERSION");
27
28#[cfg(test)]
29pub mod testutil {
30    use crate::config::Config;
31    use crate::init::init;
32    use fs_err as fs;
33    use std::path::{Path, PathBuf};
34    use tempfile::TempDir;
35
36    /// Creates a temporary directory with a .git folder (simulating a git repo).
37    /// Returns the TempDir (owns the directory) and the path to the repo root.
38    ///
39    /// IMPORTANT: Keep the TempDir alive for the duration of the test,
40    /// otherwise the directory gets deleted.
41    pub fn create_temp_git_repo() -> (TempDir, PathBuf) {
42        let tmp = tempfile::tempdir().unwrap();
43        // The repo lives in a subdirectory of the tempdir so that storage can
44        // be placed as a sibling (outside the repo), matching real usage and
45        // the storage-inside-repo guard in `init`.
46        let repo_root = fs::canonicalize(tmp.path()).unwrap().join("repo");
47        fs::create_dir(&repo_root).unwrap();
48        fs::create_dir(repo_root.join(".git")).unwrap();
49        (tmp, repo_root)
50    }
51
52    /// Creates a file with the given content at the specified path.
53    /// Creates parent directories if needed.
54    /// Returns the full path to the created file.
55    pub fn create_file(dir: &Path, relative_path: &str, content: &[u8]) -> PathBuf {
56        let path = dir.join(relative_path);
57        if let Some(parent) = path.parent() {
58            fs::create_dir_all(parent).ok();
59        }
60        fs::write(&path, content).unwrap();
61        path
62    }
63
64    /// Initializes a DVS repository in the given directory.
65    /// Creates storage at `{repo_root}/../.storage` (a sibling, outside the
66    /// repo) and metadata at `{repo_root}/.dvs`.
67    /// Returns (config, dvs_metadata_dir).
68    pub fn init_dvs_repo(repo_root: &Path) -> (Config, PathBuf) {
69        let storage_dir = repo_root.parent().unwrap().join(".storage");
70        let config = Config::new_local(&storage_dir, None).unwrap();
71        init(repo_root, config.clone()).unwrap();
72        let dvs_dir = repo_root.join(".dvs");
73        (config, dvs_dir)
74    }
75}