summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2022-01-18 09:02:21 -0500
committerGitHub <noreply@github.com>2022-01-18 09:02:21 -0500
commit7d8533c9845275ecb5aee6524316be6b81dea76b (patch)
treeb8e1ccb9133bb77206061ac6c5cc6b4033f9da05
parenta3c76df0b9add53344117f3b16b35c3c02505cda (diff)
Cwd fixups (#918)
Do cwd computations once only; rename cwd variable
-rw-r--r--src/config.rs16
-rw-r--r--src/features/hyperlinks.rs11
-rw-r--r--src/handlers/hunk_header.rs25
-rw-r--r--src/utils/cwd.rs17
4 files changed, 39 insertions, 30 deletions
diff --git a/src/config.rs b/src/config.rs
index 72fd4710..77483502 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -24,6 +24,7 @@ use crate::style;
use crate::style::Style;
use crate::tests::TESTING;
use crate::utils::bat::output::PagingMode;
+use crate::utils::cwd::cwd_of_user_shell_process;
use crate::utils::regex_replacement::RegexReplacement;
use crate::utils::syntect::FromDeltaStyle;
use crate::wrapping::WrapConfig;
@@ -70,7 +71,8 @@ pub struct Config {
pub color_only: bool,
pub commit_regex: Regex,
pub commit_style: Style,
- pub cwd: Option<PathBuf>,
+ pub cwd_of_delta_process: Option<PathBuf>,
+ pub cwd_of_user_shell_process: Option<PathBuf>,
pub cwd_relative_to_repo_root: Option<String>,
pub decorations_width: cli::Width,
pub default_language: Option<String>,
@@ -241,6 +243,13 @@ impl From<cli::Opt> for Config {
let wrap_max_lines_plus1 = adapt_wrap_max_lines_argument(opt.wrap_max_lines);
+ let cwd_of_delta_process = std::env::current_dir().ok();
+ let cwd_relative_to_repo_root = std::env::var("GIT_PREFIX").ok();
+ let cwd_of_user_shell_process = cwd_of_user_shell_process(
+ cwd_of_delta_process.as_ref(),
+ cwd_relative_to_repo_root.as_deref(),
+ );
+
Self {
available_terminal_width: opt.computed.available_terminal_width,
background_color_extends_to_terminal_width: opt
@@ -255,8 +264,9 @@ impl From<cli::Opt> for Config {
commit_style: styles["commit-style"],
color_only: opt.color_only,
commit_regex,
- cwd: std::env::current_dir().ok(),
- cwd_relative_to_repo_root: std::env::var("GIT_PREFIX").ok(),
+ cwd_of_delta_process,
+ cwd_of_user_shell_process,
+ cwd_relative_to_repo_root,
decorations_width: opt.computed.decorations_width,
default_language: opt.default_language,
diff_stat_align_width: opt.diff_stat_align_width,
diff --git a/src/features/hyperlinks.rs b/src/features/hyperlinks.rs
index 4a2fb8fc..587faf9b 100644
--- a/src/features/hyperlinks.rs
+++ b/src/features/hyperlinks.rs
@@ -7,8 +7,6 @@ use regex::{Captures, Regex};
use crate::config::Config;
use crate::features::OptionValueFunction;
use crate::git_config::{GitConfig, GitConfigEntry, GitRemoteRepo};
-use crate::utils::cwd::cwd_of_user_shell_process;
-
pub fn make_feature() -> Vec<(String, OptionValueFunction)> {
builtin_feature!([
(
@@ -61,7 +59,7 @@ pub fn format_osc8_file_hyperlink<'a>(
text: &str,
config: &Config,
) -> Cow<'a, str> {
- if let Some(cwd) = cwd_of_user_shell_process(config) {
+ if let Some(cwd) = &config.cwd_of_user_shell_process {
let absolute_path = cwd.join(relative_path);
let mut url = config
.hyperlinks_file_link_format
@@ -144,8 +142,7 @@ pub mod tests {
"--hyperlinks-file-link-format",
"{path}",
]);
- config.cwd_relative_to_repo_root = None; // Not set because we were not invoked by git
- config.cwd = Some(PathBuf::from("/some/cwd"));
+ config.cwd_of_user_shell_process = Some(PathBuf::from("/some/cwd"));
assert_file_hyperlink_matches("a", "/some/cwd/a", &config)
}
@@ -161,9 +158,7 @@ pub mod tests {
"--hyperlinks-file-link-format",
"{path}",
]);
- config.cwd_relative_to_repo_root = None; // Not set because we were not invoked by git
- config.cwd = Some("/some/repo-root".into()); // Git invokes delta from the repo root
- config.cwd_relative_to_repo_root = Some("b".into()); // Git preserves the user's directory in the GIT_PREFIX env var
+ config.cwd_of_user_shell_process = Some(PathBuf::from("/some/repo-root/b"));
assert_file_hyperlink_matches("a", "/some/repo-root/b/a", &config)
}
}
diff --git a/src/handlers/hunk_header.rs b/src/handlers/hunk_header.rs
index 32d0bea5..53cfbacb 100644
--- a/src/handlers/hunk_header.rs
+++ b/src/handlers/hunk_header.rs
@@ -314,6 +314,8 @@ fn write_to_output_buffer(
#[cfg(test)]
pub mod tests {
+ use std::path::PathBuf;
+
use super::*;
use crate::ansi::strip_ansi_codes;
use crate::tests::integration_test_utils;
@@ -391,9 +393,9 @@ pub mod tests {
fn test_paint_file_path_with_line_number_default() {
// hunk-header-style (by default) includes 'line-number' but not 'file'.
// This test confirms that `paint_file_path_with_line_number` returns a painted line number.
- let cfg = integration_test_utils::make_config_from_args(&[]);
+ let config = integration_test_utils::make_config_from_args(&[]);
- let result = paint_file_path_with_line_number(Some(3), "some-file", &cfg);
+ let result = paint_file_path_with_line_number(Some(3), "some-file", &config);
assert_eq!(result, "\u{1b}[34m3\u{1b}[0m");
}
@@ -408,10 +410,11 @@ pub mod tests {
// This test confirms that, under those circumstances, `paint_file_path_with_line_number`
// returns a hyperlinked file path with line number.
- let mut cfg = integration_test_utils::make_config_from_args(&["--features", "hyperlinks"]);
- cfg.cwd = Some("/some/current/directory".into());
+ let mut config =
+ integration_test_utils::make_config_from_args(&["--features", "hyperlinks"]);
+ config.cwd_of_user_shell_process = Some(PathBuf::from("/some/current/directory"));
- let result = paint_file_path_with_line_number(Some(3), "some-file", &cfg);
+ let result = paint_file_path_with_line_number(Some(3), "some-file", &config);
assert_eq!(result, "\u{1b}]8;;file:///some/current/directory/some-file\u{1b}\\\u{1b}[34m3\u{1b}[0m\u{1b}]8;;\u{1b}\\");
}
@@ -420,14 +423,14 @@ pub mod tests {
fn test_paint_file_path_with_line_number_empty() {
// hunk-header-style includes neither 'file' nor 'line-number'.
// This causes `paint_file_path_with_line_number` to return empty string.
- let cfg = integration_test_utils::make_config_from_args(&[
+ let config = integration_test_utils::make_config_from_args(&[
"--hunk-header-style",
"syntax bold",
"--hunk-header-decoration-style",
"omit",
]);
- let result = paint_file_path_with_line_number(Some(3), "some-file", &cfg);
+ let result = paint_file_path_with_line_number(Some(3), "some-file", &config);
assert_eq!(result, "");
}
@@ -438,7 +441,7 @@ pub mod tests {
// This causes `paint_file_path_with_line_number` to return empty string.
// This test confirms that this remains true even when we are requesting hyperlinks.
- let cfg = integration_test_utils::make_config_from_args(&[
+ let config = integration_test_utils::make_config_from_args(&[
"--hunk-header-style",
"syntax bold",
"--hunk-header-decoration-style",
@@ -447,14 +450,14 @@ pub mod tests {
"hyperlinks",
]);
- let result = paint_file_path_with_line_number(Some(3), "some-file", &cfg);
+ let result = paint_file_path_with_line_number(Some(3), "some-file", &config);
assert_eq!(result, "");
}
#[test]
fn test_paint_file_path_with_line_number_empty_navigate() {
- let cfg = integration_test_utils::make_config_from_args(&[
+ let config = integration_test_utils::make_config_from_args(&[
"--hunk-header-style",
"syntax bold",
"--hunk-header-decoration-style",
@@ -462,7 +465,7 @@ pub mod tests {
"--navigate",
]);
- let result = paint_file_path_with_line_number(Some(3), "δ some-file", &cfg);
+ let result = paint_file_path_with_line_number(Some(3), "δ some-file", &config);
assert_eq!(result, "");
}
diff --git a/src/utils/cwd.rs b/src/utils/cwd.rs
index 951c2ead..78b47719 100644
--- a/src/utils/cwd.rs
+++ b/src/utils/cwd.rs
@@ -1,13 +1,14 @@
use std::path::PathBuf;
-use crate::config::Config;
-
-/// Return current working directory of the user's shell process. I.e. the directory in which they
-/// are in when delta exits. This is the directory relative to which the file paths in delta output
-/// are constructed if they are using either (a) delta's relative-paths option or (b) git's
-/// --relative flag.
-pub fn cwd_of_user_shell_process(config: &Config) -> Option<PathBuf> {
- match (&config.cwd, &config.cwd_relative_to_repo_root) {
+/// Return current working directory of the user's shell process. I.e. the directory which they are
+/// in when delta exits. This is the directory relative to which the file paths in delta output are
+/// constructed if they are using either (a) delta's relative-paths option or (b) git's --relative
+/// flag.
+pub fn cwd_of_user_shell_process(
+ cwd_of_delta_process: Option<&PathBuf>,
+ cwd_relative_to_repo_root: Option<&str>,
+) -> Option<PathBuf> {
+ match (cwd_of_delta_process, cwd_relative_to_repo_root) {
(Some(cwd), None) => {
// We are not a child process of git
Some(PathBuf::from(cwd))