summaryrefslogtreecommitdiffstats
path: root/src/util/git.rs
blob: 3926c14831641d2d936531806cde29856ff6c0bd (plain)
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
use std::path::Path;

use anyhow::anyhow;
use anyhow::Result;
use anyhow::Context;
use anyhow::Error;
use git2::Repository;

pub fn repo_is_clean(p: &Path) -> Result<bool> {
    Repository::open(p)
        .map_err(Error::from)
        .map(|r| r.state() == git2::RepositoryState::Clean)
}

pub fn get_repo_head_commit_hash(p: &Path) -> Result<String> {
    let r = Repository::open(p)
        .with_context(|| anyhow!("Opening repository at {}", p.display()))?;

    let s = r.head()
        .with_context(|| anyhow!("Getting HEAD from repository at {}", p.display()))?
        .shorthand()
        .ok_or_else(|| {
            anyhow!("Failed to get commit hash: Not valid UTF8")
        })?
        .to_owned();

    trace!("Found git commit hash = {}", s);
    Ok(s)
}