| 1 | use std::cmp::min; |
| 2 | |
| 3 | use lazy_static::lazy_static; |
| 4 | |
| 5 | use crate::cli; |
| 6 | use crate::config::{delta_unreachable, Config}; |
| 7 | use crate::delta::{DiffType, InMergeConflict, MergeParents, State, StateMachine}; |
| 8 | use crate::paint::{prepare, prepare_raw_line}; |
| 9 | use crate::style; |
| 10 | use crate::utils::process::{self, CallingProcess}; |
| 11 | use crate::utils::tabs; |
| 12 | |
| 13 | // HACK: WordDiff should probably be a distinct top-level line state |
| 14 | pub fn is_word_diff() -> bool { |
| 15 | #[cfg(not(test))] |
| 16 | { |
| 17 | *CACHED_IS_WORD_DIFF |
| 18 | } |
| 19 | #[cfg(test)] |
| 20 | { |
| 21 | compute_is_word_diff() |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | lazy_static! { |
| 26 | static ref CACHED_IS_WORD_DIFF: bool = compute_is_word_diff(); |
| 27 | } |
| 28 | |
| 29 | fn compute_is_word_diff() -> bool { |
| 30 | match &*process::calling_process() { |
| 31 | CallingProcess::GitDiff(cmd_line) |
| 32 | | CallingProcess::GitShow(cmd_line, _) |
| 33 | | CallingProcess::GitLog(cmd_line) |
| 34 | | CallingProcess::GitReflog(cmd_line) => { |
| 35 | cmd_line.long_options.contains("--word-diff") |
| 36 | || cmd_line.long_options.contains("--word-diff-regex") |
| 37 | || cmd_line.long_options.contains("--color-words") |
| 38 | } |
| 39 | _ => false, |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | impl StateMachine<'_> { |
| 44 | #[inline] |
| 45 | fn test_hunk_line(&self) -> bool { |
| 46 | matches!( |
| 47 | self.state, |
| 48 | State::HunkHeader(_, _, _, _) |
| 49 | | State::HunkZero(_, _) |
| 50 | | State::HunkMinus(_, _) |
| 51 | | State::HunkPlus(_, _) |
| 52 | ) |
| 53 | } |
| 54 | |
| 55 | /// Handle a hunk line, i.e. a minus line, a plus line, or an unchanged line. |
| 56 | // In the case of a minus or plus line, we store the line in a |
| 57 | // buffer. When we exit the changed region we process the collected |
| 58 | // minus and plus lines jointly, in order to paint detailed |
| 59 | // highlighting according to inferred edit operations. In the case of |
| 60 | // an unchanged line, we paint it immediately. |
| 61 | pub fn handle_hunk_line(&mut self) -> std::io::Result<bool> { |
| 62 | use DiffType::*; |
| 63 | use State::*; |
| 64 | |
| 65 | // A true hunk line should start with one of: '+', '-', ' '. However, handle_hunk_line |
| 66 | // handles all lines until the state transitions away from the hunk states. |
| 67 | if !self.test_hunk_line() { |
| 68 | return Ok(false); |
| 69 | } |
| 70 | // Don't let the line buffers become arbitrarily large -- if we |
| 71 | // were to allow that, then for a large deleted/added file we |
| 72 | // would process the entire file before painting anything. |
| 73 | if self.painter.minus_lines.len() > self.config.line_buffer_size |
| 74 | || self.painter.plus_lines.len() > self.config.line_buffer_size |
| 75 | { |
| 76 | self.painter.paint_buffered_minus_and_plus_lines(); |
| 77 | } |
| 78 | if let State::HunkHeader(_, parsed_hunk_header, line, raw_line) = &self.state.clone() { |
| 79 | self.emit_hunk_header_line(parsed_hunk_header, line, raw_line)?; |
| 80 | } |
| 81 | self.state = match new_line_state(&self.line, &self.raw_line, &self.state, self.config) { |
| 82 | Some(HunkMinus(diff_type, raw_line)) => { |
| 83 | if let HunkPlus(_, _) = self.state { |
| 84 | // We have just entered a new subhunk; process the previous one |
| 85 | // and flush the line buffers. |
| 86 | self.painter.paint_buffered_minus_and_plus_lines(); |
| 87 | } |
| 88 | let n_parents = diff_type.n_parents(); |
| 89 | let line = prepare(&self.line, n_parents, self.config); |
| 90 | let state = HunkMinus(diff_type, raw_line); |
| 91 | self.painter.minus_lines.push((line, state.clone())); |
| 92 | self.minus_line_counter.count_line(); |
| 93 | state |
| 94 | } |
| 95 | Some(HunkPlus(diff_type, raw_line)) => { |
| 96 | let n_parents = diff_type.n_parents(); |
| 97 | let line = prepare(&self.line, n_parents, self.config); |
| 98 | let state = HunkPlus(diff_type, raw_line); |
| 99 | self.painter.plus_lines.push((line, state.clone())); |
| 100 | state |
| 101 | } |
| 102 | Some(HunkZero(diff_type, raw_line)) => { |
| 103 | // We are in a zero (unchanged) line, therefore we have just exited a subhunk (a |
| 104 | // sequence of consecutive minus (removed) and/or plus (added) lines). Process that |
| 105 | // subhunk and flush the line buffers. |
| 106 | self.painter.paint_buffered_minus_and_plus_lines(); |
| 107 | let n_parents = if is_word_diff() { |
| 108 | 0 |
| 109 | } else { |
| 110 | diff_type.n_parents() |
| 111 | }; |
| 112 | let line = prepare(&self.line, n_parents, self.config); |
| 113 | let state = State::HunkZero(diff_type, raw_line); |
| 114 | self.painter.paint_zero_line(&line, state.clone()); |
| 115 | self.minus_line_counter.count_line(); |
| 116 | state |
| 117 | } |
| 118 | _ => { |
| 119 | // The first character here could be e.g. '\' from '\ No newline at end of file'. This |
| 120 | // is not a hunk line, but the parser does not have a more accurate state corresponding |
| 121 | // to this. |
| 122 | self.painter.paint_buffered_minus_and_plus_lines(); |
| 123 | self.painter |
| 124 | .output_buffer |
| 125 | .push_str(&tabs::expand(&self.raw_line, &self.config.tab_cfg)); |
| 126 | self.painter.output_buffer.push('\n'); |
| 127 | State::HunkZero(Unified, None) |
| 128 | } |
| 129 | }; |
| 130 | self.painter.emit()?; |
| 131 | Ok(true) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | // Return Some(prepared_raw_line) if delta should emit this line raw. |
| 136 | fn maybe_raw_line( |
| 137 | raw_line: &str, |
| 138 | state_style_is_raw: bool, |
| 139 | n_parents: usize, |
| 140 | non_raw_styles: &[style::Style], |
| 141 | config: &Config, |
| 142 | ) -> Option<String> { |
| 143 | let emit_raw_line = is_word_diff() |
| 144 | || config.inspect_raw_lines == cli::InspectRawLines::True |
| 145 | && style::line_has_style_other_than(raw_line, non_raw_styles) |
| 146 | || state_style_is_raw; |
| 147 | if emit_raw_line { |
| 148 | Some(prepare_raw_line(raw_line, n_parents, config)) |
| 149 | } else { |
| 150 | None |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // Return the new state corresponding to `new_line`, given the previous state. A return value of |
| 155 | // None means that `new_line` is not recognized as a hunk line. |
| 156 | fn new_line_state( |
| 157 | new_line: &str, |
| 158 | new_raw_line: &str, |
| 159 | prev_state: &State, |
| 160 | config: &Config, |
| 161 | ) -> Option<State> { |
| 162 | use DiffType::*; |
| 163 | use MergeParents::*; |
| 164 | use State::*; |
| 165 | |
| 166 | if is_word_diff() { |
| 167 | return Some(HunkZero( |
| 168 | Unified, |
| 169 | maybe_raw_line(new_raw_line, config.zero_style.is_raw, 0, &[], config), |
| 170 | )); |
| 171 | } |
| 172 | |
| 173 | // 1. Given the previous line state, compute the new line diff type. These are basically the |
| 174 | // same, except that a string prefix is converted into an integer number of parents (prefix |
| 175 | // length). |
| 176 | let diff_type = match prev_state { |
| 177 | HunkMinus(Unified, _) |
| 178 | | HunkZero(Unified, _) |
| 179 | | HunkPlus(Unified, _) |
| 180 | | HunkHeader(Unified, _, _, _) => Unified, |
| 181 | HunkHeader(Combined(Number(n), InMergeConflict::No), _, _, _) => { |
| 182 | Combined(Number(*n), InMergeConflict::No) |
| 183 | } |
| 184 | // The prefixes are specific to the previous line, but the number of merge parents remains |
| 185 | // equal to the prefix length. |
| 186 | HunkHeader(Combined(Prefix(prefix), InMergeConflict::No), _, _, _) => { |
| 187 | Combined(Number(prefix.len()), InMergeConflict::No) |
| 188 | } |
| 189 | HunkMinus(Combined(Prefix(prefix), in_merge_conflict), _) |
| 190 | | HunkZero(Combined(Prefix(prefix), in_merge_conflict), _) |
| 191 | | HunkPlus(Combined(Prefix(prefix), in_merge_conflict), _) => { |
| 192 | Combined(Number(prefix.len()), in_merge_conflict.clone()) |
| 193 | } |
| 194 | HunkMinus(Combined(Number(n), in_merge_conflict), _) |
| 195 | | HunkZero(Combined(Number(n), in_merge_conflict), _) |
| 196 | | HunkPlus(Combined(Number(n), in_merge_conflict), _) => { |
| 197 | Combined(Number(*n), in_merge_conflict.clone()) |
| 198 | } |
| 199 | _ => delta_unreachable(&format!( |
| 200 | "Unexpected state in new_line_state: {prev_state:?}", |
| 201 | )), |
| 202 | }; |
| 203 | |
| 204 | // 2. Given the new diff state, and the new line, compute the new prefix. |
| 205 | let (prefix_char, prefix, in_merge_conflict) = match diff_type.clone() { |
| 206 | Unified => (new_line.chars().next(), None, None), |
| 207 | Combined(Number(n_parents), in_merge_conflict) => { |
| 208 | let prefix = &new_line[..min(n_parents, new_line.len())]; |
| 209 | let prefix_char = match prefix.chars().find(|c| c == &'-' || c == &'+') { |
| 210 | Some(c) => Some(c), |
| 211 | None => match prefix.chars().find(|c| c != &' ') { |
| 212 | None => Some(' '), |
| 213 | Some(_) => None, |
| 214 | }, |
| 215 | }; |
| 216 | ( |
| 217 | prefix_char, |
| 218 | Some(prefix.to_string()), |
| 219 | Some(in_merge_conflict), |
| 220 | ) |
| 221 | } |
| 222 | _ => delta_unreachable(""), |
| 223 | }; |
| 224 | |
| 225 | let maybe_minus_raw_line = || { |
| 226 | maybe_raw_line( |
| 227 | new_raw_line, |
| 228 | config.minus_style.is_raw, |
| 229 | diff_type.n_parents(), |
| 230 | &[*style::GIT_DEFAULT_MINUS_STYLE, config.git_minus_style], |
| 231 | config, |
| 232 | ) |
| 233 | }; |
| 234 | let maybe_zero_raw_line = || { |
| 235 | maybe_raw_line( |
| 236 | new_raw_line, |
| 237 | config.zero_style.is_raw, |
| 238 | diff_type.n_parents(), |
| 239 | &[], |
| 240 | config, |
| 241 | ) |
| 242 | }; |
| 243 | let maybe_plus_raw_line = || { |
| 244 | maybe_raw_line( |
| 245 | new_raw_line, |
| 246 | config.plus_style.is_raw, |
| 247 | diff_type.n_parents(), |
| 248 | &[*style::GIT_DEFAULT_PLUS_STYLE, config.git_plus_style], |
| 249 | config, |
| 250 | ) |
| 251 | }; |
| 252 | |
| 253 | // 3. Given the new prefix, compute the full new line state...except without its raw_line, which |
| 254 | // is added later. TODO: that is not a sensible design. |
| 255 | match (prefix_char, prefix, in_merge_conflict) { |
| 256 | (Some('-'), None, None) => Some(HunkMinus(Unified, maybe_minus_raw_line())), |
| 257 | (Some(' '), None, None) => Some(HunkZero(Unified, maybe_zero_raw_line())), |
| 258 | (Some('+'), None, None) => Some(HunkPlus(Unified, maybe_plus_raw_line())), |
| 259 | (Some('-'), Some(prefix), Some(in_merge_conflict)) => Some(HunkMinus( |
| 260 | Combined(Prefix(prefix), in_merge_conflict), |
| 261 | maybe_minus_raw_line(), |
| 262 | )), |
| 263 | (Some(' '), Some(prefix), Some(in_merge_conflict)) => Some(HunkZero( |
| 264 | Combined(Prefix(prefix), in_merge_conflict), |
| 265 | maybe_zero_raw_line(), |
| 266 | )), |
| 267 | (Some('+'), Some(prefix), Some(in_merge_conflict)) => Some(HunkPlus( |
| 268 | Combined(Prefix(prefix), in_merge_conflict), |
| 269 | maybe_plus_raw_line(), |
| 270 | )), |
| 271 | _ => None, |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | #[cfg(test)] |
| 276 | mod tests { |
| 277 | use crate::tests::integration_test_utils::DeltaTest; |
| 278 | |
| 279 | mod word_diff { |
| 280 | use super::*; |
| 281 | |
| 282 | #[test] |
| 283 | fn test_word_diff() { |
| 284 | DeltaTest::with_args(&[]) |
| 285 | .with_calling_process("git diff --word-diff") |
| 286 | .explain_ansi() |
| 287 | .with_input(GIT_DIFF_WORD_DIFF) |
| 288 | .expect_after_skip( |
| 289 | 11, |
| 290 | " |
| 291 | #indent_mark |
| 292 | (blue)───(blue)┐(normal) |
| 293 | (blue)1(normal): (blue)│(normal) |
| 294 | (blue)───(blue)┘(normal) |
| 295 | (red)[-aaa-](green){+bbb+}(normal) |
| 296 | ", |
| 297 | ); |
| 298 | } |
| 299 | |
| 300 | #[test] |
| 301 | fn test_color_words() { |
| 302 | DeltaTest::with_args(&[]) |
| 303 | .with_calling_process("git diff --color-words") |
| 304 | .explain_ansi() |
| 305 | .with_input(GIT_DIFF_COLOR_WORDS) |
| 306 | .expect_after_skip( |
| 307 | 11, |
| 308 | " |
| 309 | #indent_mark |
| 310 | (blue)───(blue)┐(normal) |
| 311 | (blue)1(normal): (blue)│(normal) |
| 312 | (blue)───(blue)┘(normal) |
| 313 | (red)aaa(green)bbb(normal) |
| 314 | ", |
| 315 | ); |
| 316 | } |
| 317 | |
| 318 | #[test] |
| 319 | #[ignore] // FIXME |
| 320 | fn test_color_words_map_styles() { |
| 321 | DeltaTest::with_args(&[ |
| 322 | "--map-styles", |
| 323 | "red => bold yellow #dddddd, green => bold blue #dddddd", |
| 324 | ]) |
| 325 | .with_calling_process("git diff --color-words") |
| 326 | .explain_ansi() |
| 327 | .with_input(GIT_DIFF_COLOR_WORDS) |
| 328 | .inspect() |
| 329 | .expect_after_skip( |
| 330 | 11, |
| 331 | r##" |
| 332 | #indent_mark |
| 333 | (blue)───(blue)┐(normal) |
| 334 | (blue)1(normal): (blue)│(normal) |
| 335 | (blue)───(blue)┘(normal) |
| 336 | (bold yellow "#dddddd")aaa(bold blue "#dddddd")bbb(normal) |
| 337 | "##, |
| 338 | ); |
| 339 | } |
| 340 | |
| 341 | #[test] |
| 342 | fn test_hunk_line_style_raw() { |
| 343 | DeltaTest::with_args(&["--minus-style", "raw", "--plus-style", "raw"]) |
| 344 | .explain_ansi() |
| 345 | .with_input(GIT_DIFF_WITH_COLOR) |
| 346 | .expect_after_skip( |
| 347 | 14, |
| 348 | " |
| 349 | (red)aaa(normal) |
| 350 | (green)bbb(normal) |
| 351 | ", |
| 352 | ); |
| 353 | } |
| 354 | |
| 355 | #[test] |
| 356 | fn test_hunk_line_style_raw_map_styles() { |
| 357 | DeltaTest::with_args(&[ |
| 358 | "--minus-style", |
| 359 | "raw", |
| 360 | "--plus-style", |
| 361 | "raw", |
| 362 | "--map-styles", |
| 363 | "red => bold blue, green => dim yellow", |
| 364 | ]) |
| 365 | .explain_ansi() |
| 366 | .with_input(GIT_DIFF_WITH_COLOR) |
| 367 | .expect_after_skip( |
| 368 | 14, |
| 369 | " |
| 370 | (bold blue)aaa(normal) |
| 371 | (dim yellow)bbb(normal) |
| 372 | ", |
| 373 | ); |
| 374 | } |
| 375 | |
| 376 | const GIT_DIFF_WITH_COLOR: &str = r#"\ |
| 377 | [33mcommit 3ef7fba7258fe473f1d8befff367bb793c786107[m |
| 378 | Author: Dan Davison <[email protected]> |
| 379 | Date: Mon Dec 13 22:54:43 2021 -0500 |
| 380 | |
| 381 | 753 Test file |
| 382 | |
| 383 | [1mdiff --git a/file b/file[m |
| 384 | [1mindex 72943a1..f761ec1 100644[m |
| 385 | [1m--- a/file[m |
| 386 | [1m+++ b/file[m |
| 387 | [31m@@ -1 +1 @@[m |
| 388 | [31m-aaa[m |
| 389 | [32m+[m[32mbbb[m |
| 390 | "#; |
| 391 | |
| 392 | const GIT_DIFF_COLOR_WORDS: &str = r#"\ |
| 393 | [33mcommit 6feea4949c20583aaf16eee84f38d34d6a7f1741[m |
| 394 | Author: Dan Davison <[email protected]> |
| 395 | Date: Sat Dec 11 17:08:56 2021 -0500 |
| 396 | |
| 397 | file v2 |
| 398 | |
| 399 | [1mdiff --git a/file b/file[m |
| 400 | [1mindex c005da6..962086f 100644[m |
| 401 | [1m--- a/file[m |
| 402 | [1m+++ b/file[m |
| 403 | [31m@@ -1 +1 @@[m |
| 404 | [31maaa[m[32mbbb[m |
| 405 | "#; |
| 406 | |
| 407 | const GIT_DIFF_WORD_DIFF: &str = r#"\ |
| 408 | [33mcommit 6feea4949c20583aaf16eee84f38d34d6a7f1741[m |
| 409 | Author: Dan Davison <[email protected]> |
| 410 | Date: Sat Dec 11 17:08:56 2021 -0500 |
| 411 | |
| 412 | file v2 |
| 413 | |
| 414 | [1mdiff --git a/file b/file[m |
| 415 | [1mindex c005da6..962086f 100644[m |
| 416 | [1m--- a/file[m |
| 417 | [1m+++ b/file[m |
| 418 | [31m@@ -1 +1 @@[m |
| 419 | [31m[-aaa-][m[32m{+bbb+}[m |
| 420 | "#; |
| 421 | } |
| 422 | } |