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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use std::path::{Path, PathBuf};
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
#[error("Could not find a valid HEAD reference")]
FindHeadRef(#[from] git_ref::file::find::existing::Error),
#[error("Expected HEAD at '.git/HEAD', got '.git/{}'", .name)]
MisplacedHead { name: crate::bstr::BString },
#[error("Expected an objects directory at '{}'", .missing.display())]
MissingObjectsDirectory { missing: PathBuf },
#[error("Expected a refs directory at '{}'", .missing.display())]
MissingRefsDirectory { missing: PathBuf },
}
pub fn bare(git_dir: impl AsRef<Path>) -> bool {
!git_dir.as_ref().join("index").exists()
}
pub fn git(git_dir: impl AsRef<Path>) -> Result<crate::Kind, Error> {
let dot_git = git_dir.as_ref();
{
let refs = crate::RefStore::at(&dot_git, git_ref::store::WriteReflog::Normal);
let head = refs.find_loose("HEAD")?;
if head.name.as_bstr() != "HEAD" {
return Err(Error::MisplacedHead {
name: head.name.into_inner(),
});
}
}
{
let objects_path = std::env::var("GIT_OBJECT_DIRECTORY")
.map(PathBuf::from)
.unwrap_or_else(|_| dot_git.join("objects"));
if !objects_path.is_dir() {
return Err(Error::MissingObjectsDirectory { missing: objects_path });
}
}
{
let refs_path = dot_git.join("refs");
if !refs_path.is_dir() {
return Err(Error::MissingRefsDirectory { missing: refs_path });
}
}
Ok(if bare(git_dir) {
crate::Kind::Bare
} else {
crate::Kind::WorkTree
})
}