Skip to content

Commit 0103501

Browse files
committed
feat: add DOT_GIT_DIR constant, containing the name ".git". (#331)
1 parent e263e13 commit 0103501

File tree

5 files changed

+28
-19
lines changed

5 files changed

+28
-19
lines changed

git-discover/src/is.rs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1+
use crate::DOT_GIT_DIR;
12
use std::{borrow::Cow, ffi::OsStr, path::Path};
23

34
/// Returns true if the given `git_dir` seems to be a bare repository.
45
///
56
/// Please note that repositories without an index generally _look_ bare, even though they might also be uninitialized.
67
pub fn bare(git_dir_candidate: impl AsRef<Path>) -> bool {
78
let git_dir = git_dir_candidate.as_ref();
8-
!(git_dir.join("index").exists() || (git_dir.file_name() == Some(OsStr::new(".git")) && git_dir.is_file()))
9+
!(git_dir.join("index").exists() || (git_dir.file_name() == Some(OsStr::new(DOT_GIT_DIR)) && git_dir.is_file()))
910
}
1011

1112
/// What constitutes a valid git repository, returning the guessed repository kind
1213
/// purely based on the presence of files. Note that the git-config ultimately decides what's bare.
1314
///
14-
/// Returns the Kind of git directory that was passed, possibly alongside the supporting private worktree git dir
15+
/// Returns the `Kind` of git directory that was passed, possibly alongside the supporting private worktree git dir.
1516
///
16-
/// Note that `.git` files are followed to a valid git directory, which then requires
17+
/// Note that `.git` files are followed to a valid git directory, which then requires…
18+
///
19+
/// * …a valid head
20+
/// * …an objects directory
21+
/// * …a refs directory
1722
///
18-
/// * a valid head
19-
/// * an objects directory
20-
/// * a refs directory
2123
pub fn git(git_dir: impl AsRef<Path>) -> Result<crate::repository::Kind, crate::is_git::Error> {
2224
#[derive(Eq, PartialEq)]
2325
enum Kind {
@@ -52,13 +54,15 @@ pub fn git(git_dir: impl AsRef<Path>) -> Result<crate::repository::Kind, crate::
5254
}
5355
Err(crate::path::from_gitdir_file::Error::Io(err)) if is_directory(&err) => {
5456
let common_dir = git_dir.join("commondir");
55-
match crate::path::from_plain_file(common_dir)
56-
.and_then(Result::ok)
57-
.and_then(|cd| {
58-
crate::path::from_plain_file(git_dir.join("gitdir"))
59-
.and_then(Result::ok)
60-
.map(|worktree_gitfile| (crate::path::without_dot_git_dir(worktree_gitfile), cd))
61-
}) {
57+
let worktree_and_common_dir =
58+
crate::path::from_plain_file(common_dir)
59+
.and_then(Result::ok)
60+
.and_then(|cd| {
61+
crate::path::from_plain_file(git_dir.join("gitdir"))
62+
.and_then(Result::ok)
63+
.map(|worktree_gitfile| (crate::path::without_dot_git_dir(worktree_gitfile), cd))
64+
});
65+
match worktree_and_common_dir {
6266
Some((work_dir, common_dir)) => {
6367
let common_dir = git_dir.join(common_dir);
6468
(

git-discover/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
#![forbid(unsafe_code, rust_2018_idioms)]
55
#![deny(missing_docs)]
66

7+
/// The name of the `.git` directory.
8+
pub const DOT_GIT_DIR: &str = ".git";
9+
710
///
811
pub mod repository;
912

git-discover/src/path.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::DOT_GIT_DIR;
12
use std::{io::Read, path::PathBuf};
23

34
///
@@ -60,7 +61,7 @@ pub fn from_gitdir_file(path: impl AsRef<std::path::Path>) -> Result<PathBuf, fr
6061

6162
/// Conditionally pop a trailing `.git` dir if present.
6263
pub fn without_dot_git_dir(mut path: PathBuf) -> PathBuf {
63-
if path.file_name().and_then(|n| n.to_str()) == Some(".git") {
64+
if path.file_name().and_then(|n| n.to_str()) == Some(DOT_GIT_DIR) {
6465
path.pop();
6566
}
6667
path

git-discover/src/repository.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod path {
2424
use crate::{
2525
path::without_dot_git_dir,
2626
repository::{Kind, Path},
27+
DOT_GIT_DIR,
2728
};
2829

2930
impl AsRef<std::path::Path> for Path {
@@ -84,7 +85,7 @@ mod path {
8485
pub fn into_repository_and_work_tree_directories(self) -> (PathBuf, Option<PathBuf>) {
8586
match self {
8687
Path::LinkedWorkTree { work_dir, git_dir } => (git_dir, Some(work_dir)),
87-
Path::WorkTree(working_tree) => (working_tree.join(".git"), Some(working_tree)),
88+
Path::WorkTree(working_tree) => (working_tree.join(DOT_GIT_DIR), Some(working_tree)),
8889
Path::Repository(repository) => (repository, None),
8990
}
9091
}

git-discover/src/upwards.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ pub(crate) mod function {
155155
use git_sec::Trust;
156156

157157
use super::{Error, Options};
158-
use crate::is_git;
158+
use crate::{is_git, DOT_GIT_DIR};
159159

160160
/// Find the location of the git repository directly in `directory` or in any of its parent directories and provide
161161
/// an associated Trust level by looking at the git directory's ownership, and control discovery using `options`.
@@ -241,7 +241,7 @@ pub(crate) mod function {
241241

242242
for append_dot_git in &[false, true] {
243243
if *append_dot_git {
244-
cursor.push(".git");
244+
cursor.push(DOT_GIT_DIR);
245245
}
246246
if let Ok(kind) = is_git(&cursor) {
247247
match filter_by_trust(&cursor)? {
@@ -302,7 +302,7 @@ pub(crate) mod function {
302302
}
303303

304304
if let Some(cwd) = cwd {
305-
debug_assert_eq!(cursor.file_name().and_then(|f| f.to_str()), Some(".git"));
305+
debug_assert_eq!(cursor.file_name().and_then(|f| f.to_str()), Some(DOT_GIT_DIR));
306306
let parent = cursor.parent().expect(".git appended");
307307
cwd.strip_prefix(parent)
308308
.ok()
@@ -312,7 +312,7 @@ pub(crate) mod function {
312312
(relative_path_components * "..".len() < current_component_len).then(|| {
313313
std::iter::repeat("..")
314314
.take(relative_path_components)
315-
.chain(Some(".git"))
315+
.chain(Some(DOT_GIT_DIR))
316316
.collect()
317317
})
318318
})

0 commit comments

Comments
 (0)