Skip to main content

stratum/
lib.rs

1use std::path::Path;
2
3// For now include everything as pub mod to get errors in IDE
4use git_url_parse::GitUrlParseError;
5use thiserror::Error;
6
7mod domain;
8mod repository;
9mod url;
10
11pub use domain::{actor::Actor, commit::Commit, mfile::ModifiedFile};
12pub use repository::{Local, Remote, Repository};
13pub use url::GitUrl;
14
15/// Helper function for opening a local repository given a path P
16pub fn open_repository<P: AsRef<Path>>(p: P) -> Result<Repository<Local>, Error> {
17    Repository::<Local>::new(p)
18}
19
20/// Helper function for cloning a remote repository given a url
21pub fn clone_repository<P: AsRef<Path>>(
22    url: &str,
23    dest: Option<P>,
24) -> Result<Repository<Local>, Error> {
25    Repository::<Remote>::new(url, dest)
26}
27
28#[derive(Debug, Error)]
29pub enum Error {
30    /// An abstraction of git2::Error to raise the error effectively
31    #[error(transparent)]
32    Git(#[from] git2::Error),
33
34    /// An abstraction of git-url-parse::GitUrlParseError
35    #[error(transparent)]
36    GitUrlError(#[from] GitUrlParseError),
37
38    /// If a URL can be parsed but is not a valid GitUrl schem
39    #[error("URL scheme was {0}, cannot clone URL.")]
40    UrlScheme(String),
41
42    /// An error associated with a bad path
43    #[error("{0}")]
44    PathError(String),
45}
46
47/// Common functionality that can be imported into any and all unit tests
48/// throughout the library
49#[cfg(test)]
50mod common {
51    use once_cell::sync::Lazy;
52    use std::{fs, path::Path};
53    use tempfile::TempDir;
54
55    pub const EXPECTED_MSG: &str = "commit msg";
56    pub const EXPECTED_ACTOR_NAME: &str = "test";
57    pub const EXPECTED_ACTOR_EMAIL: &str = "test@example.com";
58
59    /// Write to a file that exists within a temp directory root
60    fn write_fp(root: &TempDir, path: &str, content: &str) {
61        let fp = root.path().join(path);
62        fs::write(&fp, content).expect("Failed to write to file");
63    }
64
65    /// Write a file to a git index
66    fn write_to_index(index: &mut git2::Index, file: &str) {
67        index
68            .add_path(Path::new(file))
69            .expect("Failed to add file to index");
70        index.write().expect("Failed to write index");
71    }
72
73    /// Write an index to a repository tree
74    fn write_tree<'a>(
75        repo: &'a git2::Repository,
76        index: &mut git2::Index,
77        file: &str,
78    ) -> git2::Tree<'a> {
79        write_to_index(index, file);
80
81        let tree_id = index.write_tree().expect("Failed to write tree");
82        repo.find_tree(tree_id).expect("Failed to find tree")
83    }
84
85    /// Commit a file that has been modified
86    fn commit_file(
87        repo: &git2::Repository,
88        sig: &git2::Signature,
89        file: &str,
90        parent: Option<&git2::Commit<'_>>,
91    ) -> git2::Oid {
92        // Stage file for commit
93        let mut index = repo.index().expect("Failed to get index");
94        let tree = write_tree(repo, &mut index, file);
95
96        let parents = match parent {
97            Some(v) => vec![v],
98            None => vec![],
99        };
100
101        repo.commit(Some("HEAD"), sig, sig, EXPECTED_MSG, &tree, &parents)
102            .expect("Failed to create commit")
103    }
104
105    /// Make a repository with a very basic histroy.
106    fn make_repo(tmpdir: &TempDir) {
107        let repo = git2::Repository::init(tmpdir.path()).expect("Failed to init repo");
108        let sig = git2::Signature::now(EXPECTED_ACTOR_NAME, EXPECTED_ACTOR_EMAIL)
109            .expect("Failed to create actor signature");
110
111        let file = "file.txt";
112        write_fp(tmpdir, file, "Hello World\n");
113        let first_commit_id = commit_file(&repo, &sig, file, None);
114
115        write_fp(tmpdir, file, "Hello World\nFile Update\n");
116        let parent = repo
117            .find_commit(first_commit_id)
118            .expect("Failed to find first commit");
119        commit_file(&repo, &sig, file, Some(&parent));
120    }
121
122    /// Lazily construct the test data into a temp dir that will last the length of
123    /// a single modules test span
124    static TEST_DATA_DIR: Lazy<TempDir> = Lazy::new(|| {
125        let dir = TempDir::new().expect("Create temp dir");
126        make_repo(&dir);
127        dir
128    });
129
130    /// The path to the test data directory
131    fn test_data_dir() -> &'static Path {
132        TEST_DATA_DIR.path()
133    }
134
135    /// Init a repository object using the lazily constructed git2 repo
136    ///
137    /// The repository exists in a temporary diectory and is made with some
138    /// expected values that are public constants.
139    ///
140    /// The commit history is two commits long, two commits so that 2/3 of the
141    /// diff creation methods can be tested, this removes the need to mock
142    /// git2::Diff, which may not even be possible.
143    ///
144    /// ## Commit 1
145    ///
146    /// - One file was added, `file.txt`
147    ///     - The string "Hello World\n" was written to this file
148    /// - The commit is authored and committed by: test <test@example.com>
149    /// - The commit message is: "commit msg"
150    ///
151    /// ## Commit 2
152    ///
153    /// - The same file, file.txt is updated
154    ///     - The file now contains the string "Hello World\nFile Update\n"
155    /// - The commit is authored and committed by: test <test@example.com>
156    /// - The commit message is: "commit msg"
157    pub fn init_repo() -> git2::Repository {
158        git2::Repository::open(test_data_dir()).expect("Failed to open temp repo")
159    }
160}