From f5b37173fe88a62e37208a9587a0ab4fec0ef107 Mon Sep 17 00:00:00 2001 From: Thomas Otto Date: Mon, 24 Jun 2024 19:26:28 +0200 Subject: [PATCH] Don't read git files when --no-gitconfig is given + unused variables (#1728) * Fix unused variables * Don't read git files when --no-gitconfig is given Despite starting delta with `--no-gitconfig`, strace still shows that `.git/config` etc. files are accessed. By no longer doing that it is very clear that no config options are read from there. --- src/cli.rs | 21 +++++++++++---------- src/config.rs | 2 -- src/env.rs | 2 ++ src/features/mod.rs | 2 +- src/main.rs | 6 +----- src/subcommands/show_colors.rs | 7 +------ src/subcommands/show_themes.rs | 9 +++------ src/tests/integration_test_utils.rs | 2 +- 8 files changed, 20 insertions(+), 31 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 723ea42c0..a18f11e46 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1159,18 +1159,19 @@ pub enum DetectDarkLight { } impl Opt { - pub fn from_args_and_git_config( - env: DeltaEnv, - git_config: Option, - assets: HighlightingAssets, - ) -> Self { - let mut final_config = git_config; + pub fn from_args_and_git_config(env: &DeltaEnv, assets: HighlightingAssets) -> Self { let matches = Self::command().get_matches(); + let mut final_config = if *matches.get_one::("no_gitconfig").unwrap_or(&false) { + None + } else { + GitConfig::try_create(env) + }; + if let Some(path) = matches.get_one::("config") { if !path.is_empty() { let path = Path::new(path); - final_config = Some(GitConfig::from_path(&env, path, true)); + final_config = Some(GitConfig::from_path(env, path, true)); } } @@ -1178,7 +1179,7 @@ impl Opt { } pub fn from_iter_and_git_config( - env: DeltaEnv, + env: &DeltaEnv, iter: I, git_config: Option, ) -> Self @@ -1196,14 +1197,14 @@ impl Opt { } fn from_clap_and_git_config( - env: DeltaEnv, + env: &DeltaEnv, arg_matches: clap::ArgMatches, mut git_config: Option, assets: HighlightingAssets, ) -> Self { let mut opt = Opt::from_arg_matches(&arg_matches) .unwrap_or_else(|_| delta_unreachable("Opt::from_arg_matches failed")); - opt.env = env; + opt.env = env.clone(); options::set::set_options(&mut opt, &mut git_config, &arg_matches, assets); opt.git_config = git_config; opt diff --git a/src/config.rs b/src/config.rs index 767892890..5247cdf85 100644 --- a/src/config.rs +++ b/src/config.rs @@ -125,7 +125,6 @@ pub struct Config { pub show_themes: bool, pub side_by_side_data: side_by_side::SideBySideData, pub side_by_side: bool, - pub syntax_dummy_theme: SyntaxTheme, pub syntax_set: SyntaxSet, pub syntax_theme: Option, pub tab_cfg: utils::tabs::TabCfg, @@ -417,7 +416,6 @@ impl From for Config { side_by_side: opt.side_by_side && !handlers::hunk::is_word_diff(), side_by_side_data, styles_map, - syntax_dummy_theme: SyntaxTheme::default(), syntax_set: opt.computed.syntax_set, syntax_theme: opt.computed.syntax_theme, tab_cfg: utils::tabs::TabCfg::new(opt.tab_width), diff --git a/src/env.rs b/src/env.rs index 8c169bccb..49c7b2123 100644 --- a/src/env.rs +++ b/src/env.rs @@ -77,6 +77,8 @@ pub mod tests { env::set_var("DELTA_FEATURES", feature); let env = DeltaEnv::init(); assert_eq!(env.features, Some(feature.into())); + // otherwise `current_dir` is not used in the test cfg: + assert_eq!(env.current_dir, env::current_dir().ok()); } #[test] diff --git a/src/features/mod.rs b/src/features/mod.rs index dfc3582de..133cea428 100644 --- a/src/features/mod.rs +++ b/src/features/mod.rs @@ -103,7 +103,7 @@ pub mod tests { let builtin_features = make_builtin_features(); let mut args = vec!["delta".to_string()]; args.extend(builtin_features.keys().map(|s| format!("--{s}"))); - let opt = cli::Opt::from_iter_and_git_config(DeltaEnv::default(), args, None); + let opt = cli::Opt::from_iter_and_git_config(&DeltaEnv::default(), args, None); let features: HashSet<&str> = opt .features .as_deref() diff --git a/src/main.rs b/src/main.rs index 67ea0ca30..925e7dc4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -76,11 +76,7 @@ fn main() -> std::io::Result<()> { fn run_app() -> std::io::Result { let assets = utils::bat::assets::load_highlighting_assets(); let env = env::DeltaEnv::init(); - let opt = cli::Opt::from_args_and_git_config( - env.clone(), - git_config::GitConfig::try_create(&env), - assets, - ); + let opt = cli::Opt::from_args_and_git_config(&env, assets); let subcommand_result = if let Some(shell) = opt.generate_completion { Some(subcommands::generate_completion::generate_completion_file( diff --git a/src/subcommands/show_colors.rs b/src/subcommands/show_colors.rs index 2898dd975..9d0428c75 100644 --- a/src/subcommands/show_colors.rs +++ b/src/subcommands/show_colors.rs @@ -4,7 +4,6 @@ use crate::colors; use crate::config; use crate::delta; use crate::env::DeltaEnv; -use crate::git_config; use crate::paint; use crate::paint::BgShouldFill; use crate::style; @@ -16,11 +15,7 @@ pub fn show_colors() -> std::io::Result<()> { let assets = utils::bat::assets::load_highlighting_assets(); let env = DeltaEnv::default(); - let opt = cli::Opt::from_args_and_git_config( - env.clone(), - git_config::GitConfig::try_create(&env), - assets, - ); + let opt = cli::Opt::from_args_and_git_config(&env, assets); let config = config::Config::from(opt); let mut output_type = diff --git a/src/subcommands/show_themes.rs b/src/subcommands/show_themes.rs index 52c923868..86cae34d9 100644 --- a/src/subcommands/show_themes.rs +++ b/src/subcommands/show_themes.rs @@ -36,7 +36,7 @@ pub fn show_themes(dark: bool, light: bool, computed_theme_is_light: bool) -> st let git_config = git_config::GitConfig::try_create(&env); let opt = cli::Opt::from_iter_and_git_config( - env.clone(), + &env, &["delta", "--navigate", "--show-themes"], git_config, ); @@ -47,11 +47,8 @@ pub fn show_themes(dark: bool, light: bool, computed_theme_is_light: bool) -> st for theme in &themes { let git_config = git_config::GitConfig::try_create(&env); - let opt = cli::Opt::from_iter_and_git_config( - env.clone(), - &["delta", "--features", theme], - git_config, - ); + let opt = + cli::Opt::from_iter_and_git_config(&env, &["delta", "--features", theme], git_config); let is_dark_theme = opt.dark; let is_light_theme = opt.light; let config = config::Config::from(opt); diff --git a/src/tests/integration_test_utils.rs b/src/tests/integration_test_utils.rs index 764be5d20..09e445e92 100644 --- a/src/tests/integration_test_utils.rs +++ b/src/tests/integration_test_utils.rs @@ -66,7 +66,7 @@ fn _make_options_from_args_and_git_config( None } }; - cli::Opt::from_iter_and_git_config(env, args, git_config) + cli::Opt::from_iter_and_git_config(&env, args, git_config) } pub fn make_options_from_args(args: &[&str]) -> cli::Opt {