git_revise/
git.rs

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
use cmit::GitCommit;
use diff::GitDiff;
use repo::GitRepository;

use crate::error::ReviseResult;

pub mod cmit;
pub mod diff;
pub mod repo;

pub struct GitUtils {
    repo: git2::Repository,
}

impl Default for GitUtils {
    fn default() -> Self {
        Self::new()
    }
}

impl GitUtils {
    pub fn new() -> Self {
        Self {
            repo: Self::git_repo().expect("Failed to get repository"),
        }
    }
    pub fn diff(&self) -> ReviseResult<String> {
        Self::git_diff(&self.repo)
    }
    pub fn commit(&self, message: &str) -> ReviseResult<()> {
        Ok(Self::git_cmit(&self.repo, message)?)
    }
}

impl GitDiff for GitUtils {}
impl GitCommit for GitUtils {}
impl GitRepository for GitUtils {}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_git_repository() {
        GitUtils::git_repo().unwrap();
    }
}