Skip to content

Commit bc08511

Browse files
committed
Add required support for reading path files (#301)
Soon these will have to be read to find linked worktrees, maybe?
1 parent 4e914f8 commit bc08511

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

git-discover/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ doctest = false
1515

1616
[dependencies]
1717
git-sec = { version = "^0.1.0", path = "../git-sec", features = ["thiserror"] }
18+
git-path = { version = "^0.1.0", path = "../git-path" }
1819
git-ref = { version = "^0.12.1", path = "../git-ref" }
1920
git-hash = { version = "^0.9.3", path = "../git-hash" }
2021

21-
bstr = { version = "0.2.13", default-features = false, features = ["std"]}
22+
bstr = { version = "0.2.13", default-features = false, features = ["std", "unicode"] }
2223
thiserror = "1.0.26"
2324

2425
[dev-dependencies]

git-discover/src/lib.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
//! Find git repositories or search them upwards from a starting point.
1+
//! Find git repositories or search them upwards from a starting point, or determine if a directory looks like a git repository.
2+
//!
3+
//! Note that detection methods are educated guesses using the presence of files, without looking too much into the details.
24
#![forbid(unsafe_code, rust_2018_idioms)]
35
#![deny(missing_docs)]
46

@@ -100,3 +102,21 @@ pub use is::{bare as is_bare, git as is_git};
100102
///
101103
pub mod upwards;
102104
pub use upwards::function::{discover as upwards, discover_opts as upwards_opts};
105+
106+
///
107+
pub mod path {
108+
use std::path::PathBuf;
109+
110+
/// Reads a plain path from a file that contains it as its only content, with trailing newlines trimmed.
111+
pub fn from_plain_file(path: impl AsRef<std::path::Path>) -> Option<std::io::Result<PathBuf>> {
112+
use bstr::ByteSlice;
113+
let mut buf = match std::fs::read(path) {
114+
Ok(buf) => buf,
115+
Err(err) if err.kind() == std::io::ErrorKind::NotFound => return None,
116+
Err(err) => return Some(Err(err)),
117+
};
118+
let trimmed_len = buf.trim_end().len();
119+
buf.truncate(trimmed_len);
120+
Some(Ok(git_path::from_bstring(buf)))
121+
}
122+
}

0 commit comments

Comments
 (0)