Skip to content

Commit b27a8c2

Browse files
committed
refactor
- move environment variable up to git-repository as plumbing crate can't deal with git-config style values and it's uncommon enough to not make that functionality available more easily in git-config.
1 parent 06e96a4 commit b27a8c2

File tree

4 files changed

+29
-21
lines changed

4 files changed

+29
-21
lines changed

Cargo.lock

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

git-discover/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ doctest = false
1414
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1515

1616
[dependencies]
17-
git-config = { version = "^0.4.0", path = "../git-config" }
1817
git-sec = { version = "^0.1.2", path = "../git-sec", features = ["thiserror"] }
1918
git-path = { version = "^0.1.3", path = "../git-path" }
2019
git-ref = { version = "^0.13.0", path = "../git-ref" }

git-discover/src/upwards.rs

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use bstr::{ByteSlice, ByteVec};
2-
use git_config::values::Boolean as GitBoolean;
32
use std::borrow::Cow;
4-
use std::convert::TryFrom;
53
use std::env;
64
use std::path::PathBuf;
75

@@ -68,23 +66,16 @@ impl Options {
6866
///
6967
/// The environment variables are:
7068
/// - `GIT_CEILING_DIRECTORIES` for `ceiling_dirs`
71-
/// - `GIT_DISCOVERY_ACROSS_FILESYSTEM` for `cross_fs`
69+
///
70+
/// Note that `GIT_DISCOVERY_ACROSS_FILESYSTEM` for `cross_fs` is **not** read,
71+
/// as it requires parsing of `git-config` style boolean values.
7272
// TODO: test
73-
pub fn apply_environment(&mut self) {
74-
/// Gets the values of an environment variable as bytes.
75-
fn get_env_bytes(name: &str) -> Option<Vec<u8>> {
76-
env::var_os(name).and_then(|c| Vec::from_os_string(c).ok())
77-
}
78-
79-
if let Some(ceiling_dirs) = get_env_bytes("GIT_CEILING_DIRECTORIES") {
73+
pub fn apply_environment(mut self) -> Self {
74+
let name = "GIT_CEILING_DIRECTORIES";
75+
if let Some(ceiling_dirs) = env::var_os(name).and_then(|c| Vec::from_os_string(c).ok()) {
8076
self.ceiling_dirs = parse_ceiling_dirs(&ceiling_dirs);
8177
}
82-
83-
if let Some(cross_fs) = get_env_bytes("GIT_DISCOVERY_ACROSS_FILESYSTEM") {
84-
if let Ok(b) = GitBoolean::try_from(cross_fs) {
85-
self.cross_fs = b.to_bool();
86-
}
87-
}
78+
self
8879
}
8980
}
9081

git-repository/src/lib.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,18 +423,37 @@ pub mod discover {
423423

424424
/// Try to open a git repository directly from the environment, which reads `GIT_DIR`
425425
/// if it is set. If unset, discover upwards from `directory` until one is found,
426-
/// while applying `options` with overrides from the environment.
426+
/// while applying `options` with overrides from the environment which includes:
427427
///
428-
/// Then use the `trust_map` to determine which of our own repository options to use.
428+
/// - `GIT_DISCOVERY_ACROSS_FILESYSTEM`
429+
/// - `GIT_CEILING_DIRECTORIES`
430+
///
431+
/// Finally, use the `trust_map` to determine which of our own repository options to use
432+
/// based on the trust level of the effective repository directory.
429433
pub fn discover_with_environment_overrides_opts(
430434
directory: impl AsRef<Path>,
431435
mut options: upwards::Options,
432436
trust_map: git_sec::trust::Mapping<crate::open::Options>,
433437
) -> Result<Self, Error> {
438+
fn apply_additional_environment(mut opts: upwards::Options) -> upwards::Options {
439+
use crate::bstr::ByteVec;
440+
use std::convert::TryFrom;
441+
442+
if let Some(cross_fs) =
443+
std::env::var_os("GIT_DISCOVERY_ACROSS_FILESYSTEM").and_then(|v| Vec::from_os_string(v).ok())
444+
{
445+
if let Ok(b) = git_config::values::Boolean::try_from(cross_fs) {
446+
opts.cross_fs = b.to_bool();
447+
}
448+
}
449+
opts
450+
}
451+
434452
if std::env::var_os("GIT_DIR").is_some() {
435453
return Self::open_with_environment_overrides(directory.as_ref(), trust_map).map_err(Error::Open);
436454
}
437-
options.apply_environment();
455+
456+
options = apply_additional_environment(options.apply_environment());
438457
Self::discover_opts(directory, options, trust_map)
439458
}
440459
}

0 commit comments

Comments
 (0)