History

fix: Use `std::env::var_os()` to avoid potential decode errors (#301)

Sebastian Thiel committed 4 years ago

Commit 36e2fc01 parent b04a3461 file · +9 −9Browse files

Unified · Split

git-config/src/file/git_config.rs+9−9
@@ -432,7 +432,7 @@ impl<'event> GitConfig<'event> { · 431 unmodified lines
432432 let mut paths = vec![];
433433
434434 if env::var("GIT_CONFIG_NO_SYSTEM").is_err() {
435− if let Ok(git_config_system) = env::var("GIT_CONFIG_SYSTEM") {
435+ if let Some(git_config_system) = env::var_os("GIT_CONFIG_SYSTEM") {
436436 paths.push(PathBuf::from(git_config_system))
437437 } else {
438438 // In git the fallback is set to a build time macro which defaults to /etc/gitconfig
@@ -440,24 +440,24 @@ impl<'event> GitConfig<'event> { · 1 unmodified line
440440 }
441441 }
442442
443− if let Ok(git_config_global) = env::var("GIT_CONFIG_GLOBAL") {
443+ if let Some(git_config_global) = env::var_os("GIT_CONFIG_GLOBAL") {
444444 paths.push(PathBuf::from(git_config_global));
445445 } else {
446446 // Divergence from git-config(1)
447447 // These two are supposed to share the same scope and override
448448 // rather than append according to git-config(1) documentation.
449− if let Ok(xdg_config_home) = env::var("XDG_CONFIG_HOME") {
449+ if let Some(xdg_config_home) = env::var_os("XDG_CONFIG_HOME") {
450450 paths.push(PathBuf::from(xdg_config_home).join("git/config"));
451− } else if let Ok(home) = env::var("HOME") {
451+ } else if let Some(home) = env::var_os("HOME") {
452452 paths.push(PathBuf::from(home).join(".config/git/config"));
453453 }
454454
455− if let Ok(home) = env::var("HOME") {
455+ if let Some(home) = env::var_os("HOME") {
456456 paths.push(PathBuf::from(home).join(".gitconfig"));
457457 }
458458 }
459459
460− if let Ok(git_dir) = env::var("GIT_DIR") {
460+ if let Some(git_dir) = env::var_os("GIT_DIR") {
461461 paths.push(PathBuf::from(git_dir).join("config"));
462462 }
463463
@@ -485,8 +485,8 @@ impl<'event> GitConfig<'event> { · 21 unmodified lines
485485 for i in 0..count {
486486 let key =
487487 env::var(format!("GIT_CONFIG_KEY_{}", i)).map_err(|_| from_env::Error::InvalidKeyId { key_id: i })?;
488− let value = env::var(format!("GIT_CONFIG_VALUE_{}", i))
489− .map_err(|_| from_env::Error::InvalidValueId { value_id: i })?;
488+ let value = env::var_os(format!("GIT_CONFIG_VALUE_{}", i))
489+ .ok_or_else(|| from_env::Error::InvalidValueId { value_id: i })?;
490490 if let Some((section_name, maybe_subsection)) = key.split_once('.') {
491491 let (subsection, key) = if let Some((subsection, key)) = maybe_subsection.rsplit_once('.') {
492492 (Some(subsection), key)
@@ -507,7 +507,7 @@ impl<'event> GitConfig<'event> { · 14 unmodified lines
507507
508508 section.push(
509509 Cow::<str>::Owned(key.to_string()).into(),
510− Cow::Owned(value.into_bytes()),
510+ Cow::Owned(git_path::into_bstr(PathBuf::from(value)).into_owned().into()),
511511 );
512512 } else {
513513 return Err(from_env::Error::InvalidKeyValue {