blob: 628e51100d6ace4947871afe84369b40e041d1ce (
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
//
// Copyright (c) 2020-2021 science+computing ag and other contributors
//
// This program and the accompanying materials are made
// available under the terms of the Eclipse Public License 2.0
// which is available at https://www.eclipse.org/legal/epl-2.0/
//
// SPDX-License-Identifier: EPL-2.0
//
use std::path::Path;
use anyhow::anyhow;
use anyhow::Context;
use anyhow::Error;
use anyhow::Result;
use git2::Repository;
use log::trace;
pub fn repo_is_clean(p: &Path) -> Result<bool> {
let r = Repository::open(p)?;
r.diff_index_to_workdir(None, None)
.and_then(|d| d.stats())
.map_err(Error::from)
.map(|st| {
trace!("Repo stats: {:?}", st);
trace!("Repo state: {:?}", r.state());
st.files_changed() == 0 && 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()))?
.peel_to_commit()
.with_context(|| anyhow!("Failed to get commit hash: Not valid UTF8"))?
.id()
.to_string();
trace!("Found git commit hash = {}", s);
Ok(s)
}
|