| 1 | use std::borrow::Cow; |
| 2 | use std::path::Path; |
| 3 | |
| 4 | use lazy_static::lazy_static; |
| 5 | use regex::{Match, Matches, Regex}; |
| 6 | |
| 7 | use crate::config::Config; |
| 8 | use crate::features::OptionValueFunction; |
| 9 | |
| 10 | #[cfg(test)] |
| 11 | use crate::git_config::GitConfig; |
| 12 | |
| 13 | pub fn make_feature() -> Vec<(String, OptionValueFunction)> { |
| 14 | builtin_feature!([ |
| 15 | ( |
| 16 | "hyperlinks", |
| 17 | bool, |
| 18 | None, |
| 19 | _opt => true |
| 20 | ) |
| 21 | ]) |
| 22 | } |
| 23 | |
| 24 | lazy_static! { |
| 25 | // Commit hashes can be abbreviated to 7 characters, these necessarily become longer |
| 26 | // when more objects are in a repository. |
| 27 | // Note: pure numbers are filtered out later again. |
| 28 | static ref COMMIT_HASH_REGEX: Regex = Regex::new(r"\b[0-9a-f]{7,40}\b").unwrap(); |
| 29 | } |
| 30 | |
| 31 | pub fn format_commit_line_with_osc8_commit_hyperlink<'a>( |
| 32 | line: &'a str, |
| 33 | config: &Config, |
| 34 | ) -> Cow<'a, str> { |
| 35 | // Given matches in a line, m = matches[0] and pos = 0: store line[pos..m.start()] first, then |
| 36 | // store the T(line[m.start()..m.end()]) match transformation, then set pos = m.end(). |
| 37 | // Repeat for matches[1..]. Finally, store line[pos..]. |
| 38 | struct HyperlinkCommits<T>(T) |
| 39 | where |
| 40 | T: Fn(&str) -> String; |
| 41 | impl<T: for<'b> Fn(&'b str) -> String> HyperlinkCommits<T> { |
| 42 | fn _m(&self, result: &mut String, line: &str, m: &Match, prev_pos: usize) -> usize { |
| 43 | result.push_str(&line[prev_pos..m.start()]); |
| 44 | let commit = &line[m.start()..m.end()]; |
| 45 | // Do not link numbers, require at least one non-decimal: |
| 46 | if commit.contains(|c| matches!(c, 'a'..='f')) { |
| 47 | result.push_str(&format_osc8_hyperlink(&self.0(commit), commit)); |
| 48 | } else { |
| 49 | result.push_str(commit); |
| 50 | } |
| 51 | m.end() |
| 52 | } |
| 53 | fn with_input(&self, line: &str, m0: &Match, matches123: &mut Matches) -> String { |
| 54 | let mut result = String::new(); |
| 55 | let mut pos = self._m(&mut result, line, m0, 0); |
| 56 | // limit number of matches per line, an exhaustive `find_iter` is O(len(line) * len(regex)^2) |
| 57 | for m in matches123.take(12) { |
| 58 | pos = self._m(&mut result, line, &m, pos); |
| 59 | } |
| 60 | result.push_str(&line[pos..]); |
| 61 | result |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if let Some(commit_link_format) = &config.hyperlinks_commit_link_format { |
| 66 | let mut matches = COMMIT_HASH_REGEX.find_iter(line); |
| 67 | if let Some(first_match) = matches.next() { |
| 68 | let result = |
| 69 | HyperlinkCommits(|commit_hash| commit_link_format.replace("{commit}", commit_hash)) |
| 70 | .with_input(line, &first_match, &mut matches); |
| 71 | return Cow::from(result); |
| 72 | } |
| 73 | } else if let Some(config) = config.git_config() { |
| 74 | if let Some(repo) = config.get_remote_url() { |
| 75 | let mut matches = COMMIT_HASH_REGEX.find_iter(line); |
| 76 | if let Some(first_match) = matches.next() { |
| 77 | let result = HyperlinkCommits(|commit_hash| repo.format_commit_url(commit_hash)) |
| 78 | .with_input(line, &first_match, &mut matches); |
| 79 | return Cow::from(result); |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | Cow::from(line) |
| 84 | } |
| 85 | |
| 86 | /// Create a file hyperlink, displaying `text`. |
| 87 | pub fn format_osc8_file_hyperlink<'a, P>( |
| 88 | absolute_path: P, |
| 89 | line_number: Option<usize>, |
| 90 | text: &str, |
| 91 | config: &Config, |
| 92 | ) -> Cow<'a, str> |
| 93 | where |
| 94 | P: AsRef<Path>, |
| 95 | P: std::fmt::Debug, |
| 96 | { |
| 97 | debug_assert!(absolute_path.as_ref().is_absolute()); |
| 98 | let mut url = config |
| 99 | .hyperlinks_file_link_format |
| 100 | .replace("{path}", &absolute_path.as_ref().to_string_lossy()); |
| 101 | if let Some(host) = &config.hostname { |
| 102 | url = url.replace("{host}", host) |
| 103 | } |
| 104 | let n = line_number.unwrap_or(1); |
| 105 | url = url.replace("{line}", &format!("{n}")); |
| 106 | Cow::from(format_osc8_hyperlink(&url, text)) |
| 107 | } |
| 108 | |
| 109 | fn format_osc8_hyperlink(url: &str, text: &str) -> String { |
| 110 | format!( |
| 111 | "{osc}8;;{url}{st}{text}{osc}8;;{st}", |
| 112 | url = url, |
| 113 | text = text, |
| 114 | osc = "\x1b]", |
| 115 | st = "\x1b\\" |
| 116 | ) |
| 117 | } |
| 118 | |
| 119 | #[cfg(not(target_os = "windows"))] |
| 120 | #[cfg(test)] |
| 121 | pub mod tests { |
| 122 | use std::iter::FromIterator; |
| 123 | use std::path::PathBuf; |
| 124 | |
| 125 | use pretty_assertions::assert_eq; |
| 126 | |
| 127 | use super::*; |
| 128 | |
| 129 | use crate::{ |
| 130 | tests::integration_test_utils::{self, make_config_from_args, DeltaTest}, |
| 131 | utils, |
| 132 | }; |
| 133 | |
| 134 | #[test] |
| 135 | fn test_file_hyperlink_line_number_defaults_to_one() { |
| 136 | let config = |
| 137 | make_config_from_args(&["--hyperlinks-file-link-format", "file://{path}:{line}"]); |
| 138 | |
| 139 | let result = |
| 140 | format_osc8_file_hyperlink("/absolute/path/to/file.rs", Some(42), "file.rs", &config); |
| 141 | assert_eq!( |
| 142 | result, |
| 143 | "\u{1b}]8;;file:///absolute/path/to/file.rs:42\u{1b}\\file.rs\u{1b}]8;;\u{1b}\\", |
| 144 | ); |
| 145 | |
| 146 | let result = |
| 147 | format_osc8_file_hyperlink("/absolute/path/to/file.rs", None, "file.rs", &config); |
| 148 | assert_eq!( |
| 149 | result, |
| 150 | "\u{1b}]8;;file:///absolute/path/to/file.rs:1\u{1b}\\file.rs\u{1b}]8;;\u{1b}\\", |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | #[test] |
| 155 | fn test_formatted_hyperlinks() { |
| 156 | let config = make_config_from_args(&["--hyperlinks-commit-link-format", "HERE:{commit}"]); |
| 157 | |
| 158 | let line = "001234abcdf"; |
| 159 | let result = format_commit_line_with_osc8_commit_hyperlink(line, &config); |
| 160 | assert_eq!( |
| 161 | result, |
| 162 | "\u{1b}]8;;HERE:001234abcdf\u{1b}\\001234abcdf\u{1b}]8;;\u{1b}\\", |
| 163 | ); |
| 164 | |
| 165 | let line = "a2272718f0b398e48652ace17fca85c1962b3fc22"; // length: 41 > 40 |
| 166 | let result = format_commit_line_with_osc8_commit_hyperlink(line, &config); |
| 167 | assert_eq!(result, "a2272718f0b398e48652ace17fca85c1962b3fc22",); |
| 168 | |
| 169 | let line = "a2272718f0+b398e48652ace17f,ca85c1962b3fc2"; |
| 170 | let result = format_commit_line_with_osc8_commit_hyperlink(line, &config); |
| 171 | assert_eq!(result, "\u{1b}]8;;HERE:a2272718f0\u{1b}\\a2272718f0\u{1b}]8;;\u{1b}\\+\u{1b}]8;;\ |
| 172 | HERE:b398e48652ace17f\u{1b}\\b398e48652ace17f\u{1b}]8;;\u{1b}\\,\u{1b}]8;;HERE:ca85c1962b3fc2\ |
| 173 | \u{1b}\\ca85c1962b3fc2\u{1b}]8;;\u{1b}\\"); |
| 174 | |
| 175 | let line = "This 01234abcdf Hash"; |
| 176 | let result = format_commit_line_with_osc8_commit_hyperlink(line, &config); |
| 177 | assert_eq!( |
| 178 | result, |
| 179 | "This \u{1b}]8;;HERE:01234abcdf\u{1b}\\01234abcdf\u{1b}]8;;\u{1b}\\ Hash", |
| 180 | ); |
| 181 | |
| 182 | let line = |
| 183 | "Another 01234abcdf hash but also this one: dc623b084ad2dd14fe5d90189cacad5d49bfbfd3!"; |
| 184 | let result = format_commit_line_with_osc8_commit_hyperlink(line, &config); |
| 185 | assert_eq!( |
| 186 | result, |
| 187 | "Another \u{1b}]8;;HERE:01234abcdf\u{1b}\\01234abcdf\u{1b}]8;;\u{1b}\\ hash but \ |
| 188 | also this one: \u{1b}]8;;HERE:dc623b084ad2dd14fe5d90189cacad5d49bfbfd3\u{1b}\ |
| 189 | \\dc623b084ad2dd14fe5d90189cacad5d49bfbfd3\u{1b}]8;;\u{1b}\\!" |
| 190 | ); |
| 191 | |
| 192 | let line = "01234abcdf 03043baf30 12abcdef0 12345678"; |
| 193 | let result = format_commit_line_with_osc8_commit_hyperlink(line, &config); |
| 194 | assert_eq!( |
| 195 | result, |
| 196 | "\u{1b}]8;;HERE:01234abcdf\u{1b}\\01234abcdf\u{1b}]8;;\u{1b}\\ \u{1b}]8;;\ |
| 197 | HERE:03043baf30\u{1b}\\03043baf30\u{1b}]8;;\u{1b}\\ \u{1b}]8;;HERE:12abcdef0\u{1b}\\\ |
| 198 | 12abcdef0\u{1b}]8;;\u{1b}\\ 12345678" |
| 199 | ); |
| 200 | } |
| 201 | |
| 202 | #[test] |
| 203 | fn test_hyperlinks_to_repo() { |
| 204 | let mut config = make_config_from_args(&["--hyperlinks"]); |
| 205 | config.git_config = GitConfig::for_testing(); |
| 206 | |
| 207 | let line = "This a589ff9debaefdd delta commit"; |
| 208 | let result = format_commit_line_with_osc8_commit_hyperlink(line, &config); |
| 209 | assert_eq!( |
| 210 | result, |
| 211 | "This \u{1b}]8;;https://github.com/dandavison/delta/commit/a589ff9debaefdd\u{1b}\ |
| 212 | \\a589ff9debaefdd\u{1b}]8;;\u{1b}\\ delta commit", |
| 213 | ); |
| 214 | |
| 215 | let line = |
| 216 | "Another a589ff9debaefdd hash but also this one: c5696757c0827349a87daa95415656!"; |
| 217 | let result = format_commit_line_with_osc8_commit_hyperlink(line, &config); |
| 218 | assert_eq!( |
| 219 | result, |
| 220 | "Another \u{1b}]8;;https://github.com/dandavison/delta/commit/a589ff9debaefdd\ |
| 221 | \u{1b}\\a589ff9debaefdd\u{1b}]8;;\u{1b}\\ hash but also this one: \u{1b}]8;;\ |
| 222 | https://github.com/dandavison/delta/commit/c5696757c0827349a87daa95415656\u{1b}\ |
| 223 | \\c5696757c0827349a87daa95415656\u{1b}]8;;\ |
| 224 | \u{1b}\\!" |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | #[test] |
| 229 | fn test_paths_and_hyperlinks_user_in_repo_root_dir() { |
| 230 | // Expectations are uninfluenced by git's --relative and delta's relative_paths options. |
| 231 | let input_type = InputType::GitDiff; |
| 232 | let true_location_of_file_relative_to_repo_root = PathBuf::from("a"); |
| 233 | let git_prefix_env_var = Some(""); |
| 234 | |
| 235 | for (delta_relative_paths_option, calling_cmd) in [ |
| 236 | (false, Some("git diff")), |
| 237 | (false, Some("git diff --relative")), |
| 238 | (true, Some("git diff")), |
| 239 | (true, Some("git diff --relative")), |
| 240 | ] { |
| 241 | run_test(FilePathsTestCase { |
| 242 | name: &format!( |
| 243 | "delta relative_paths={delta_relative_paths_option} calling_cmd={calling_cmd:?}", |
| 244 | ), |
| 245 | true_location_of_file_relative_to_repo_root: |
| 246 | true_location_of_file_relative_to_repo_root.as_path(), |
| 247 | git_prefix_env_var, |
| 248 | delta_relative_paths_option, |
| 249 | input_type, |
| 250 | calling_cmd, |
| 251 | path_in_delta_input: "a", |
| 252 | expected_displayed_path: "a", |
| 253 | }) |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | #[test] |
| 258 | fn test_paths_and_hyperlinks_user_in_subdir_file_in_same_subdir() { |
| 259 | let input_type = InputType::GitDiff; |
| 260 | let true_location_of_file_relative_to_repo_root = PathBuf::from_iter(&["b", "a"]); |
| 261 | let git_prefix_env_var = Some("b"); |
| 262 | |
| 263 | run_test(FilePathsTestCase { |
| 264 | name: "b/a from b", |
| 265 | input_type, |
| 266 | calling_cmd: Some("git diff"), |
| 267 | true_location_of_file_relative_to_repo_root: |
| 268 | true_location_of_file_relative_to_repo_root.as_path(), |
| 269 | git_prefix_env_var, |
| 270 | delta_relative_paths_option: false, |
| 271 | path_in_delta_input: "b/a", |
| 272 | expected_displayed_path: "b/a", |
| 273 | }); |
| 274 | run_test(FilePathsTestCase { |
| 275 | name: "b/a from b", |
| 276 | input_type, |
| 277 | calling_cmd: Some("git diff --relative"), |
| 278 | true_location_of_file_relative_to_repo_root: |
| 279 | true_location_of_file_relative_to_repo_root.as_path(), |
| 280 | git_prefix_env_var, |
| 281 | delta_relative_paths_option: false, |
| 282 | path_in_delta_input: "a", |
| 283 | // delta saw a and wasn't configured to make any changes |
| 284 | expected_displayed_path: "a", |
| 285 | }); |
| 286 | run_test(FilePathsTestCase { |
| 287 | name: "b/a from b", |
| 288 | input_type, |
| 289 | calling_cmd: Some("git diff"), |
| 290 | true_location_of_file_relative_to_repo_root: |
| 291 | true_location_of_file_relative_to_repo_root.as_path(), |
| 292 | git_prefix_env_var, |
| 293 | delta_relative_paths_option: true, |
| 294 | path_in_delta_input: "b/a", |
| 295 | // delta saw b/a and changed it to a |
| 296 | expected_displayed_path: "a", |
| 297 | }); |
| 298 | run_test(FilePathsTestCase { |
| 299 | name: "b/a from b", |
| 300 | input_type, |
| 301 | calling_cmd: Some("git diff --relative"), |
| 302 | true_location_of_file_relative_to_repo_root: |
| 303 | true_location_of_file_relative_to_repo_root.as_path(), |
| 304 | git_prefix_env_var, |
| 305 | delta_relative_paths_option: true, |
| 306 | path_in_delta_input: "a", |
| 307 | // delta saw a and didn't change it |
| 308 | expected_displayed_path: "a", |
| 309 | }); |
| 310 | } |
| 311 | |
| 312 | #[test] |
| 313 | fn test_paths_and_hyperlinks_user_in_subdir_file_in_different_subdir() { |
| 314 | let input_type = InputType::GitDiff; |
| 315 | let true_location_of_file_relative_to_repo_root = PathBuf::from_iter(&["b", "a"]); |
| 316 | let git_prefix_env_var = Some("c"); |
| 317 | |
| 318 | run_test(FilePathsTestCase { |
| 319 | name: "b/a from c", |
| 320 | input_type, |
| 321 | calling_cmd: Some("git diff"), |
| 322 | delta_relative_paths_option: false, |
| 323 | true_location_of_file_relative_to_repo_root: |
| 324 | true_location_of_file_relative_to_repo_root.as_path(), |
| 325 | git_prefix_env_var, |
| 326 | path_in_delta_input: "b/a", |
| 327 | expected_displayed_path: "b/a", |
| 328 | }); |
| 329 | run_test(FilePathsTestCase { |
| 330 | name: "b/a from c", |
| 331 | input_type, |
| 332 | calling_cmd: Some("git diff --relative"), |
| 333 | delta_relative_paths_option: false, |
| 334 | true_location_of_file_relative_to_repo_root: |
| 335 | true_location_of_file_relative_to_repo_root.as_path(), |
| 336 | git_prefix_env_var, |
| 337 | path_in_delta_input: "../b/a", |
| 338 | expected_displayed_path: "../b/a", |
| 339 | }); |
| 340 | run_test(FilePathsTestCase { |
| 341 | name: "b/a from c", |
| 342 | input_type, |
| 343 | calling_cmd: Some("git diff"), |
| 344 | delta_relative_paths_option: true, |
| 345 | true_location_of_file_relative_to_repo_root: |
| 346 | true_location_of_file_relative_to_repo_root.as_path(), |
| 347 | git_prefix_env_var, |
| 348 | path_in_delta_input: "b/a", |
| 349 | expected_displayed_path: "../b/a", |
| 350 | }); |
| 351 | } |
| 352 | |
| 353 | #[test] |
| 354 | fn test_paths_and_hyperlinks_git_grep_user_in_root() { |
| 355 | let input_type = InputType::Grep; |
| 356 | let true_location_of_file_relative_to_repo_root = PathBuf::from_iter(&["b", "a.txt"]); |
| 357 | |
| 358 | run_test(FilePathsTestCase { |
| 359 | name: "git grep: b/a.txt from root dir", |
| 360 | input_type, |
| 361 | calling_cmd: Some("git grep foo"), |
| 362 | delta_relative_paths_option: false, |
| 363 | true_location_of_file_relative_to_repo_root: |
| 364 | true_location_of_file_relative_to_repo_root.as_path(), |
| 365 | git_prefix_env_var: Some(""), |
| 366 | path_in_delta_input: "b/a.txt", |
| 367 | expected_displayed_path: "b/a.txt:", |
| 368 | }); |
| 369 | } |
| 370 | |
| 371 | #[test] |
| 372 | fn test_paths_and_hyperlinks_grep_user_in_subdir_file_in_same_subdir() { |
| 373 | _run_test_grep_user_in_subdir_file_in_same_subdir(Some("git grep foo")); |
| 374 | _run_test_grep_user_in_subdir_file_in_same_subdir(Some("rg foo")); |
| 375 | } |
| 376 | |
| 377 | fn _run_test_grep_user_in_subdir_file_in_same_subdir(calling_cmd: Option<&str>) { |
| 378 | let input_type = InputType::Grep; |
| 379 | let true_location_of_file_relative_to_repo_root = PathBuf::from_iter(&["b", "a.txt"]); |
| 380 | run_test(FilePathsTestCase { |
| 381 | name: "git grep: b/a.txt from b/ dir", |
| 382 | input_type, |
| 383 | calling_cmd, |
| 384 | delta_relative_paths_option: false, |
| 385 | true_location_of_file_relative_to_repo_root: |
| 386 | true_location_of_file_relative_to_repo_root.as_path(), |
| 387 | git_prefix_env_var: Some("b/"), |
| 388 | path_in_delta_input: "a.txt", |
| 389 | expected_displayed_path: "a.txt:", |
| 390 | }); |
| 391 | } |
| 392 | |
| 393 | const GIT_DIFF_OUTPUT: &str = r#" |
| 394 | diff --git a/__path__ b/__path__ |
| 395 | index 587be6b..975fbec 100644 |
| 396 | --- a/__path__ |
| 397 | +++ b/__path__ |
| 398 | @@ -1 +1 @@ |
| 399 | -x |
| 400 | +y |
| 401 | "#; |
| 402 | |
| 403 | const GIT_GREP_OUTPUT: &str = "\ |
| 404 | __path__: some matching line |
| 405 | "; |
| 406 | |
| 407 | struct FilePathsTestCase<'a> { |
| 408 | // True location of file in repo |
| 409 | true_location_of_file_relative_to_repo_root: &'a Path, |
| 410 | |
| 411 | // Git spawns delta from repo root, and stores in this env var the cwd in which the user invoked delta. |
| 412 | git_prefix_env_var: Option<&'a str>, |
| 413 | |
| 414 | delta_relative_paths_option: bool, |
| 415 | input_type: InputType, |
| 416 | calling_cmd: Option<&'a str>, |
| 417 | path_in_delta_input: &'a str, |
| 418 | expected_displayed_path: &'a str, |
| 419 | #[allow(dead_code)] |
| 420 | name: &'a str, |
| 421 | } |
| 422 | |
| 423 | #[derive(Debug)] |
| 424 | enum GitDiffRelative { |
| 425 | Yes, |
| 426 | No, |
| 427 | } |
| 428 | |
| 429 | #[derive(Debug)] |
| 430 | enum CallingProcess { |
| 431 | GitDiff(GitDiffRelative), |
| 432 | GitGrep, |
| 433 | OtherGrep, |
| 434 | } |
| 435 | |
| 436 | #[derive(Clone, Copy, Debug)] |
| 437 | enum InputType { |
| 438 | GitDiff, |
| 439 | Grep, |
| 440 | } |
| 441 | |
| 442 | impl<'a> FilePathsTestCase<'a> { |
| 443 | pub fn get_args(&self) -> Vec<String> { |
| 444 | let mut args = vec![ |
| 445 | "--navigate", // helps locate the file path in the output |
| 446 | "--line-numbers", |
| 447 | "--hyperlinks", |
| 448 | "--hyperlinks-file-link-format", |
| 449 | "{path}", |
| 450 | "--grep-file-style", |
| 451 | "raw", |
| 452 | "--grep-line-number-style", |
| 453 | "raw", |
| 454 | "--grep-output-type", |
| 455 | "classic", |
| 456 | "--hunk-header-file-style", |
| 457 | "raw", |
| 458 | "--hunk-header-line-number-style", |
| 459 | "raw", |
| 460 | "--line-numbers-plus-style", |
| 461 | "raw", |
| 462 | "--line-numbers-left-style", |
| 463 | "raw", |
| 464 | "--line-numbers-right-style", |
| 465 | "raw", |
| 466 | "--line-numbers-left-format", |
| 467 | "{nm}અ", |
| 468 | "--line-numbers-right-format", |
| 469 | "{np}જ", |
| 470 | ]; |
| 471 | if self.delta_relative_paths_option { |
| 472 | args.push("--relative-paths"); |
| 473 | } |
| 474 | args.iter().map(|s| s.to_string()).collect() |
| 475 | } |
| 476 | |
| 477 | pub fn calling_process(&self) -> CallingProcess { |
| 478 | match (&self.input_type, self.calling_cmd) { |
| 479 | (InputType::GitDiff, Some(s)) if s.starts_with("git diff --relative") => { |
| 480 | CallingProcess::GitDiff(GitDiffRelative::Yes) |
| 481 | } |
| 482 | (InputType::GitDiff, Some(s)) if s.starts_with("git diff") => { |
| 483 | CallingProcess::GitDiff(GitDiffRelative::No) |
| 484 | } |
| 485 | (InputType::Grep, Some(s)) if s.starts_with("git grep") => CallingProcess::GitGrep, |
| 486 | (InputType::Grep, Some(s)) if s.starts_with("rg") => CallingProcess::OtherGrep, |
| 487 | (InputType::Grep, None) => CallingProcess::GitGrep, |
| 488 | _ => panic!( |
| 489 | "Unexpected calling spec: {:?} {:?}", |
| 490 | self.input_type, self.calling_cmd |
| 491 | ), |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | pub fn path_in_git_output(&self) -> String { |
| 496 | match self.calling_process() { |
| 497 | CallingProcess::GitDiff(GitDiffRelative::No) => self |
| 498 | .true_location_of_file_relative_to_repo_root |
| 499 | .to_string_lossy() |
| 500 | .to_string(), |
| 501 | CallingProcess::GitDiff(GitDiffRelative::Yes) => pathdiff::diff_paths( |
| 502 | self.true_location_of_file_relative_to_repo_root, |
| 503 | self.git_prefix_env_var.unwrap(), |
| 504 | ) |
| 505 | .unwrap() |
| 506 | .to_string_lossy() |
| 507 | .into(), |
| 508 | _ => panic!("Unexpected calling process: {:?}", self.calling_process()), |
| 509 | } |
| 510 | } |
| 511 | |
| 512 | /// Return the relative path as it would appear in grep output, i.e. accounting for facts |
| 513 | /// such as that the user may have invoked the grep command from a non-root directory |
| 514 | /// in the repo. |
| 515 | pub fn path_in_grep_output(&self) -> String { |
| 516 | use CallingProcess::*; |
| 517 | match (self.calling_process(), self.git_prefix_env_var) { |
| 518 | (GitGrep, None) => self |
| 519 | .true_location_of_file_relative_to_repo_root |
| 520 | .to_string_lossy() |
| 521 | .into(), |
| 522 | (GitGrep, Some(dir)) => { |
| 523 | // Delta must have been invoked as core.pager since GIT_PREFIX env var is set. |
| 524 | // Note that it is possible that `true_location_of_file_relative_to_repo_root` |
| 525 | // is not under `git_prefix_env_var` since one can do things like `git grep foo |
| 526 | // ..` |
| 527 | pathdiff::diff_paths(self.true_location_of_file_relative_to_repo_root, dir) |
| 528 | .unwrap() |
| 529 | .to_string_lossy() |
| 530 | .into() |
| 531 | } |
| 532 | (OtherGrep, None) => { |
| 533 | // Output from e.g. rg has been piped to delta. |
| 534 | // Therefore |
| 535 | // (a) the cwd that the delta process reports is the user's shell process cwd |
| 536 | // (b) the file in question must be under this cwd |
| 537 | // (c) grep output will contain the path relative to this cwd |
| 538 | |
| 539 | // So to compute the path as it would appear in grep output, we could form the |
| 540 | // absolute path to the file and strip off the config.cwd_of_delta_process |
| 541 | // prefix. The absolute path to the file could be constructed as (absolute path |
| 542 | // to repo root) + true_location_of_file_relative_to_repo_root). But I don't |
| 543 | // think we know the absolute path to repo root. |
| 544 | panic!("Not implemented") |
| 545 | } |
| 546 | _ => panic!("Not implemented"), |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | pub fn expected_hyperlink_path(&self) -> PathBuf { |
| 551 | utils::path::fake_delta_cwd_for_tests() |
| 552 | .join(self.true_location_of_file_relative_to_repo_root) |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | fn run_test(test_case: FilePathsTestCase) { |
| 557 | let mut config = integration_test_utils::make_config_from_args( |
| 558 | test_case |
| 559 | .get_args() |
| 560 | .iter() |
| 561 | .map(|s| s.as_str()) |
| 562 | .collect::<Vec<&str>>() |
| 563 | .as_slice(), |
| 564 | ); |
| 565 | // The test is simulating delta invoked by git hence these are the same |
| 566 | config.cwd_relative_to_repo_root = test_case.git_prefix_env_var.map(|s| s.to_string()); |
| 567 | config.cwd_of_user_shell_process = utils::path::cwd_of_user_shell_process( |
| 568 | config.cwd_of_delta_process.as_ref(), |
| 569 | config.cwd_relative_to_repo_root.as_deref(), |
| 570 | ); |
| 571 | let mut delta_test = DeltaTest::with_config(&config); |
| 572 | if let Some(cmd) = test_case.calling_cmd { |
| 573 | delta_test = delta_test.with_calling_process(cmd) |
| 574 | } |
| 575 | let delta_test = match test_case.calling_process() { |
| 576 | CallingProcess::GitDiff(_) => { |
| 577 | assert_eq!( |
| 578 | test_case.path_in_delta_input, |
| 579 | test_case.path_in_git_output() |
| 580 | ); |
| 581 | delta_test |
| 582 | .with_input(&GIT_DIFF_OUTPUT.replace("__path__", test_case.path_in_delta_input)) |
| 583 | } |
| 584 | CallingProcess::GitGrep => { |
| 585 | assert_eq!( |
| 586 | test_case.path_in_delta_input, |
| 587 | test_case.path_in_grep_output() |
| 588 | ); |
| 589 | delta_test |
| 590 | .with_input(&GIT_GREP_OUTPUT.replace("__path__", test_case.path_in_delta_input)) |
| 591 | } |
| 592 | CallingProcess::OtherGrep => delta_test |
| 593 | .with_input(&GIT_GREP_OUTPUT.replace("__path__", test_case.path_in_delta_input)), |
| 594 | }; |
| 595 | let make_expected_hyperlink = |text| { |
| 596 | format_osc8_hyperlink(&test_case.expected_hyperlink_path().to_string_lossy(), text) |
| 597 | }; |
| 598 | match test_case.calling_process() { |
| 599 | CallingProcess::GitDiff(_) => { |
| 600 | let line_number = "1"; |
| 601 | delta_test |
| 602 | .inspect_raw() |
| 603 | // file hyperlink |
| 604 | .expect_raw_contains(&format!( |
| 605 | "Δ {}", |
| 606 | make_expected_hyperlink(test_case.expected_displayed_path) |
| 607 | )) |
| 608 | // hunk header hyperlink |
| 609 | .expect_raw_contains(&format!("• {}", make_expected_hyperlink(line_number))) |
| 610 | // line number hyperlink |
| 611 | .expect_raw_contains(&format!("અ{}જ", make_expected_hyperlink(line_number))); |
| 612 | } |
| 613 | CallingProcess::GitGrep | CallingProcess::OtherGrep => { |
| 614 | delta_test |
| 615 | .inspect_raw() |
| 616 | .expect_raw_contains(&make_expected_hyperlink( |
| 617 | test_case.expected_displayed_path, |
| 618 | )); |
| 619 | } |
| 620 | } |
| 621 | } |
| 622 | } |