Repository navigation
Expand file tree
/
Copy pathgit.rs
More file actions
46 lines (40 loc) · 1.39 KB
/
Copy pathgit.rs
File metadata and controls
46 lines (40 loc) · 1.39 KB
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
use std::ffi;
use std::path;
use std::process;
pub(crate) fn repo<P: AsRef<path::Path>>(p: P) -> anyhow::Result<gix::Repository> {
// TODO: handle Trust?
let repo = gix::ThreadSafeRepository::discover_with_environment_overrides(p)?.to_thread_local();
if repo.is_bare() {
anyhow::bail!("pre-commit needs a worktree, not a bare repo");
}
Ok(repo)
}
pub(crate) fn has_unmerged_paths(repo: &gix::Repository) -> anyhow::Result<bool> {
for entry in repo.index()?.entries() {
if entry.flags.stage() != gix::index::entry::Stage::Unconflicted {
return Ok(true);
}
}
Ok(false)
}
trait CmdKv {
fn arg_kv<K: AsRef<ffi::OsStr>, V: AsRef<ffi::OsStr>>(&mut self, k: K, v: V) -> &mut Self;
}
impl CmdKv for process::Command {
fn arg_kv<K: AsRef<ffi::OsStr>, V: AsRef<ffi::OsStr>>(&mut self, k: K, v: V) -> &mut Self {
self.arg(k).arg(v)
}
}
pub(crate) fn has_unstaged_config(repo: &gix::Repository, config: &str) -> anyhow::Result<bool> {
// TODO: need `write-tree` / `diff`: Byron/gitoxide#301
let retc = process::Command::new("git")
.arg_kv("-C", repo.workdir().unwrap())
.args(["diff", "--quiet", "--no-ext-diff", config])
.stdin(process::Stdio::null())
.stdout(process::Stdio::null())
.stderr(process::Stdio::null())
.status()?
.code()
.unwrap_or(255);
Ok(retc == 1)
}