| 1 | use std::ops::{Index, IndexMut}; |
| 2 | |
| 3 | use itertools::Itertools; |
| 4 | use unicode_segmentation::UnicodeSegmentation; |
| 5 | |
| 6 | use super::draw; |
| 7 | use crate::cli; |
| 8 | use crate::config::{self, delta_unreachable}; |
| 9 | use crate::delta::{DiffType, InMergeConflict, MergeParents, State, StateMachine}; |
| 10 | use crate::minusplus::MinusPlus; |
| 11 | use crate::paint::{self, prepare}; |
| 12 | use crate::style::Style; |
| 13 | |
| 14 | #[derive(Clone, Debug, PartialEq, Eq)] |
| 15 | pub enum MergeConflictCommit { |
| 16 | Ours, |
| 17 | Ancestral, |
| 18 | Theirs, |
| 19 | } |
| 20 | |
| 21 | pub struct MergeConflictCommits<T> { |
| 22 | ours: T, |
| 23 | ancestral: T, |
| 24 | theirs: T, |
| 25 | } |
| 26 | |
| 27 | pub type MergeConflictLines = MergeConflictCommits<Vec<(String, State)>>; |
| 28 | |
| 29 | pub type MergeConflictCommitNames = MergeConflictCommits<Option<String>>; |
| 30 | |
| 31 | impl StateMachine<'_> { |
| 32 | pub fn handle_merge_conflict_line(&mut self) -> std::io::Result<bool> { |
| 33 | use DiffType::*; |
| 34 | use MergeConflictCommit::*; |
| 35 | use State::*; |
| 36 | |
| 37 | let mut handled_line = false; |
| 38 | if self.config.color_only || !self.config.handle_merge_conflicts { |
| 39 | return Ok(handled_line); |
| 40 | } |
| 41 | |
| 42 | match self.state.clone() { |
| 43 | HunkHeader(Combined(merge_parents, InMergeConflict::No), _, _, _) |
| 44 | | HunkMinus(Combined(merge_parents, InMergeConflict::No), _) |
| 45 | | HunkZero(Combined(merge_parents, InMergeConflict::No), _) |
| 46 | | HunkPlus(Combined(merge_parents, InMergeConflict::No), _) => { |
| 47 | handled_line = self.enter_merge_conflict(&merge_parents) |
| 48 | } |
| 49 | MergeConflict(merge_parents, Ours) => { |
| 50 | handled_line = self.enter_ancestral(&merge_parents) |
| 51 | || self.enter_theirs(&merge_parents) |
| 52 | || self.exit_merge_conflict(&merge_parents)? |
| 53 | || self.store_line( |
| 54 | Ours, |
| 55 | HunkPlus(Combined(merge_parents, InMergeConflict::Yes), None), |
| 56 | ); |
| 57 | } |
| 58 | MergeConflict(merge_parents, Ancestral) => { |
| 59 | handled_line = self.enter_theirs(&merge_parents) |
| 60 | || self.exit_merge_conflict(&merge_parents)? |
| 61 | || self.store_line( |
| 62 | Ancestral, |
| 63 | HunkMinus(Combined(merge_parents, InMergeConflict::Yes), None), |
| 64 | ); |
| 65 | } |
| 66 | MergeConflict(merge_parents, Theirs) => { |
| 67 | handled_line = self.exit_merge_conflict(&merge_parents)? |
| 68 | || self.store_line( |
| 69 | Theirs, |
| 70 | HunkPlus(Combined(merge_parents, InMergeConflict::Yes), None), |
| 71 | ); |
| 72 | } |
| 73 | _ => {} |
| 74 | } |
| 75 | |
| 76 | Ok(handled_line) |
| 77 | } |
| 78 | |
| 79 | fn enter_merge_conflict(&mut self, merge_parents: &MergeParents) -> bool { |
| 80 | use State::*; |
| 81 | if let Some(commit) = parse_merge_marker(&self.line, "++<<<<<<<") { |
| 82 | self.state = MergeConflict(merge_parents.clone(), Ours); |
| 83 | self.painter.merge_conflict_commit_names[Ours] = Some(commit.to_string()); |
| 84 | true |
| 85 | } else { |
| 86 | false |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | fn enter_ancestral(&mut self, merge_parents: &MergeParents) -> bool { |
| 91 | use State::*; |
| 92 | if let Some(commit) = parse_merge_marker(&self.line, "++|||||||") { |
| 93 | self.state = MergeConflict(merge_parents.clone(), Ancestral); |
| 94 | self.painter.merge_conflict_commit_names[Ancestral] = Some(commit.to_string()); |
| 95 | true |
| 96 | } else { |
| 97 | false |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | fn enter_theirs(&mut self, merge_parents: &MergeParents) -> bool { |
| 102 | use State::*; |
| 103 | if self.line.starts_with("++=======") { |
| 104 | self.state = MergeConflict(merge_parents.clone(), Theirs); |
| 105 | true |
| 106 | } else { |
| 107 | false |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | fn exit_merge_conflict(&mut self, merge_parents: &MergeParents) -> std::io::Result<bool> { |
| 112 | if let Some(commit) = parse_merge_marker(&self.line, "++>>>>>>>") { |
| 113 | self.painter.merge_conflict_commit_names[Theirs] = Some(commit.to_string()); |
| 114 | self.paint_buffered_merge_conflict_lines(merge_parents)?; |
| 115 | Ok(true) |
| 116 | } else { |
| 117 | Ok(false) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | fn store_line(&mut self, commit: MergeConflictCommit, state: State) -> bool { |
| 122 | use State::*; |
| 123 | if let HunkMinus(diff_type, _) | HunkZero(diff_type, _) | HunkPlus(diff_type, _) = &state { |
| 124 | let line = prepare(&self.line, diff_type.n_parents(), self.config); |
| 125 | self.painter.merge_conflict_lines[commit].push((line, state)); |
| 126 | true |
| 127 | } else { |
| 128 | delta_unreachable(&format!("Invalid state: {state:?}")) |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | fn paint_buffered_merge_conflict_lines( |
| 133 | &mut self, |
| 134 | merge_parents: &MergeParents, |
| 135 | ) -> std::io::Result<()> { |
| 136 | use DiffType::*; |
| 137 | use State::*; |
| 138 | self.painter.emit()?; |
| 139 | |
| 140 | write_merge_conflict_bar( |
| 141 | &self.config.merge_conflict_begin_symbol, |
| 142 | &mut self.painter, |
| 143 | self.config, |
| 144 | )?; |
| 145 | for (derived_commit_type, header_style) in &[ |
| 146 | (Ours, self.config.merge_conflict_ours_diff_header_style), |
| 147 | (Theirs, self.config.merge_conflict_theirs_diff_header_style), |
| 148 | ] { |
| 149 | write_diff_header( |
| 150 | derived_commit_type, |
| 151 | *header_style, |
| 152 | &mut self.painter, |
| 153 | self.config, |
| 154 | )?; |
| 155 | self.painter.emit()?; |
| 156 | paint::paint_minus_and_plus_lines( |
| 157 | MinusPlus::new( |
| 158 | &self.painter.merge_conflict_lines[Ancestral], |
| 159 | &self.painter.merge_conflict_lines[derived_commit_type], |
| 160 | ), |
| 161 | &mut self.painter.line_numbers_data, |
| 162 | &mut self.painter.highlighter, |
| 163 | &mut self.painter.output_buffer, |
| 164 | self.config, |
| 165 | ); |
| 166 | self.painter.emit()?; |
| 167 | } |
| 168 | // write_merge_conflict_decoration("bold ol", &mut self.painter, self.config)?; |
| 169 | write_merge_conflict_bar( |
| 170 | &self.config.merge_conflict_end_symbol, |
| 171 | &mut self.painter, |
| 172 | self.config, |
| 173 | )?; |
| 174 | self.painter.merge_conflict_lines.clear(); |
| 175 | self.state = HunkZero(Combined(merge_parents.clone(), InMergeConflict::No), None); |
| 176 | Ok(()) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | fn write_diff_header( |
| 181 | derived_commit_type: &MergeConflictCommit, |
| 182 | style: Style, |
| 183 | painter: &mut paint::Painter, |
| 184 | config: &config::Config, |
| 185 | ) -> std::io::Result<()> { |
| 186 | let (mut draw_fn, pad, decoration_ansi_term_style) = |
| 187 | draw::get_draw_function(style.decoration_style); |
| 188 | let derived_commit_name = &painter.merge_conflict_commit_names[derived_commit_type]; |
| 189 | let text = if let Some(_ancestral_commit) = &painter.merge_conflict_commit_names[Ancestral] { |
| 190 | format!( |
| 191 | "ancestor {} {}{}", |
| 192 | config.right_arrow, |
| 193 | derived_commit_name.as_deref().unwrap_or("?"), |
| 194 | if pad { " " } else { "" } |
| 195 | ) |
| 196 | } else { |
| 197 | derived_commit_name.as_deref().unwrap_or("?").to_string() |
| 198 | }; |
| 199 | draw_fn( |
| 200 | painter.writer, |
| 201 | &text, |
| 202 | &text, |
| 203 | "", |
| 204 | &config.decorations_width, |
| 205 | style, |
| 206 | decoration_ansi_term_style, |
| 207 | )?; |
| 208 | Ok(()) |
| 209 | } |
| 210 | |
| 211 | fn write_merge_conflict_bar( |
| 212 | s: &str, |
| 213 | painter: &mut paint::Painter, |
| 214 | config: &config::Config, |
| 215 | ) -> std::io::Result<()> { |
| 216 | let width = match config.decorations_width { |
| 217 | cli::Width::Fixed(width) => width, |
| 218 | cli::Width::Variable => config.available_terminal_width, |
| 219 | }; |
| 220 | writeln!( |
| 221 | painter.writer, |
| 222 | "{}", |
| 223 | &s.graphemes(true).cycle().take(width).join("") |
| 224 | )?; |
| 225 | Ok(()) |
| 226 | } |
| 227 | |
| 228 | fn parse_merge_marker<'a>(line: &'a str, marker: &str) -> Option<&'a str> { |
| 229 | match line.strip_prefix(marker) { |
| 230 | Some(suffix) => { |
| 231 | let suffix = suffix.trim(); |
| 232 | if !suffix.is_empty() { |
| 233 | Some(suffix) |
| 234 | } else { |
| 235 | None |
| 236 | } |
| 237 | } |
| 238 | None => None, |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | pub use MergeConflictCommit::*; |
| 243 | |
| 244 | impl<T> Index<MergeConflictCommit> for MergeConflictCommits<T> { |
| 245 | type Output = T; |
| 246 | fn index(&self, commit: MergeConflictCommit) -> &Self::Output { |
| 247 | match commit { |
| 248 | Ours => &self.ours, |
| 249 | Ancestral => &self.ancestral, |
| 250 | Theirs => &self.theirs, |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | impl<T> Index<&MergeConflictCommit> for MergeConflictCommits<T> { |
| 256 | type Output = T; |
| 257 | fn index(&self, commit: &MergeConflictCommit) -> &Self::Output { |
| 258 | match commit { |
| 259 | Ours => &self.ours, |
| 260 | Ancestral => &self.ancestral, |
| 261 | Theirs => &self.theirs, |
| 262 | } |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | impl<T> IndexMut<MergeConflictCommit> for MergeConflictCommits<T> { |
| 267 | fn index_mut(&mut self, commit: MergeConflictCommit) -> &mut Self::Output { |
| 268 | match commit { |
| 269 | Ours => &mut self.ours, |
| 270 | Ancestral => &mut self.ancestral, |
| 271 | Theirs => &mut self.theirs, |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | impl MergeConflictLines { |
| 277 | pub fn new() -> Self { |
| 278 | Self { |
| 279 | ours: Vec::new(), |
| 280 | ancestral: Vec::new(), |
| 281 | theirs: Vec::new(), |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | fn clear(&mut self) { |
| 286 | self[Ours].clear(); |
| 287 | self[Ancestral].clear(); |
| 288 | self[Theirs].clear(); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | impl MergeConflictCommitNames { |
| 293 | pub fn new() -> Self { |
| 294 | Self { |
| 295 | ours: None, |
| 296 | ancestral: None, |
| 297 | theirs: None, |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | #[cfg(test)] |
| 303 | mod tests { |
| 304 | use crate::ansi::strip_ansi_codes; |
| 305 | use crate::tests::integration_test_utils; |
| 306 | |
| 307 | #[test] |
| 308 | fn test_toy_merge_conflict_no_context() { |
| 309 | let config = integration_test_utils::make_config_from_args(&[]); |
| 310 | let output = integration_test_utils::run_delta(GIT_TOY_MERGE_CONFLICT_NO_CONTEXT, &config); |
| 311 | let output = strip_ansi_codes(&output); |
| 312 | assert!(output.contains("\n▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼")); |
| 313 | assert!(output.contains( |
| 314 | "\ |
| 315 | ──────────────────┐ |
| 316 | ancestor ⟶ HEAD │ |
| 317 | ──────────────────┘ |
| 318 | " |
| 319 | )); |
| 320 | assert!(output.contains("\n▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲")); |
| 321 | } |
| 322 | |
| 323 | #[test] |
| 324 | fn test_real_merge_conflict() { |
| 325 | let config = integration_test_utils::make_config_from_args(&[]); |
| 326 | let output = integration_test_utils::run_delta(GIT_MERGE_CONFLICT, &config); |
| 327 | let output = strip_ansi_codes(&output); |
| 328 | assert!(output.contains("\n▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼")); |
| 329 | assert!(output.contains( |
| 330 | "\ |
| 331 | ──────────────────┐ |
| 332 | ancestor ⟶ HEAD │ |
| 333 | ──────────────────┘ |
| 334 | " |
| 335 | )); |
| 336 | assert!(output.contains("\n▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲")); |
| 337 | } |
| 338 | |
| 339 | #[test] |
| 340 | #[allow(non_snake_case)] |
| 341 | fn test_real_merge_conflict_U0() { |
| 342 | let config = integration_test_utils::make_config_from_args(&[]); |
| 343 | let output = integration_test_utils::run_delta(GIT_MERGE_CONFLICT_U0, &config); |
| 344 | let output = strip_ansi_codes(&output); |
| 345 | assert!(output.contains("\n▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼▼")); |
| 346 | assert!(output.contains( |
| 347 | "\ |
| 348 | ──────────────────┐ |
| 349 | ancestor ⟶ HEAD │ |
| 350 | ──────────────────┘ |
| 351 | " |
| 352 | )); |
| 353 | assert!(output.contains("\n▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲▲")); |
| 354 | } |
| 355 | |
| 356 | const GIT_TOY_MERGE_CONFLICT_NO_CONTEXT: &str = "\ |
| 357 | diff --cc file |
| 358 | index 6178079,7898192..0000000 |
| 359 | --- a/file |
| 360 | +++ b/file |
| 361 | @@@ -1,1 -1,1 +1,6 @@@ |
| 362 | ++<<<<<<< HEAD |
| 363 | +a |
| 364 | ++||||||| parent of 0c20c9d... wip |
| 365 | ++======= |
| 366 | + b |
| 367 | ++>>>>>>> 0c20c9d... wip |
| 368 | "; |
| 369 | |
| 370 | const GIT_MERGE_CONFLICT: &str = r#"\ |
| 371 | diff --cc src/handlers/merge_conflict.rs |
| 372 | index 27d47c0,3a7e7b9..0000000 |
| 373 | --- a/src/handlers/merge_conflict.rs |
| 374 | +++ b/src/handlers/merge_conflict.rs |
| 375 | @@@ -1,14 -1,13 +1,24 @@@ |
| 376 | -use std::cmp::min; |
| 377 | use std::ops::{Index, IndexMut}; |
| 378 | |
| 379 | ++<<<<<<< HEAD |
| 380 | +use itertools::Itertools; |
| 381 | +use unicode_segmentation::UnicodeSegmentation; |
| 382 | + |
| 383 | +use super::draw; |
| 384 | +use crate::cli; |
| 385 | +use crate::config::{self, delta_unreachable}; |
| 386 | +use crate::delta::{DiffType, InMergeConflict, MergeParents, State, StateMachine}; |
| 387 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 388 | ++use crate::delta::{DiffType, MergeParents, State, StateMachine}; |
| 389 | ++======= |
| 390 | + use super::draw; |
| 391 | + use crate::cli; |
| 392 | + use crate::config::{self, delta_unreachable}; |
| 393 | + use crate::delta::{DiffType, MergeParents, State, StateMachine}; |
| 394 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 395 | use crate::minusplus::MinusPlus; |
| 396 | use crate::paint; |
| 397 | + use crate::style::DecorationStyle; |
| 398 | |
| 399 | #[derive(Clone, Debug, PartialEq)] |
| 400 | pub enum MergeConflictCommit { |
| 401 | @@@ -30,7 -29,8 +40,15 @@@ pub type MergeConflictCommitNames = Mer |
| 402 | impl<'a> StateMachine<'a> { |
| 403 | pub fn handle_merge_conflict_line(&mut self) -> std::io::Result<bool> { |
| 404 | use DiffType::*; |
| 405 | ++<<<<<<< HEAD |
| 406 | use MergeConflictCommit::*; |
| 407 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 408 | + use MergeParents::*; |
| 409 | ++ use Source::*; |
| 410 | ++======= |
| 411 | ++ use MergeConflictCommit::*; |
| 412 | ++ use MergeParents::*; |
| 413 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 414 | use State::*; |
| 415 | |
| 416 | let mut handled_line = false; |
| 417 | @@@ -38,36 -38,28 +56,113 @@@ |
| 418 | return Ok(handled_line); |
| 419 | } |
| 420 | |
| 421 | ++<<<<<<< HEAD |
| 422 | + match self.state.clone() { |
| 423 | + HunkHeader(Combined(merge_parents, InMergeConflict::No), _, _) |
| 424 | + | HunkMinus(Combined(merge_parents, InMergeConflict::No), _) |
| 425 | + | HunkZero(Combined(merge_parents, InMergeConflict::No)) |
| 426 | + | HunkPlus(Combined(merge_parents, InMergeConflict::No), _) => { |
| 427 | + handled_line = self.enter_merge_conflict(&merge_parents) |
| 428 | + } |
| 429 | + MergeConflict(merge_parents, Ours) => { |
| 430 | + handled_line = self.enter_ancestral(&merge_parents) |
| 431 | + || self.enter_theirs(&merge_parents) |
| 432 | + || self.exit_merge_conflict(&merge_parents)? |
| 433 | + || self.store_line( |
| 434 | + Ours, |
| 435 | + HunkPlus(Combined(merge_parents, InMergeConflict::Yes), None), |
| 436 | + ); |
| 437 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 438 | ++ // TODO: don't allocate on heap at this point |
| 439 | ++ let prefix = self.line[..min(self.line.len(), 2)].to_string(); |
| 440 | ++ let diff_type = Combined(Prefix(prefix)); |
| 441 | ++ |
| 442 | ++ match self.state { |
| 443 | ++ // The only transition into a merge conflict is HunkZero => MergeConflict(Ours) |
| 444 | ++ // TODO: shouldn't this be HunkZero(Some(_))? |
| 445 | ++ HunkZero(_) => { |
| 446 | ++ if self.line.starts_with("++<<<<<<<") { |
| 447 | ++ self.state = MergeConflict(Ours); |
| 448 | ++ handled_line = true |
| 449 | ++ } |
| 450 | + } |
| 451 | ++ MergeConflict(Ours) => { |
| 452 | ++ if self.line.starts_with("++|||||||") { |
| 453 | ++ self.state = MergeConflict(Ancestral); |
| 454 | ++ } else if self.line.starts_with("++=======") { |
| 455 | ++ self.state = MergeConflict(Theirs); |
| 456 | ++ } else if self.line.starts_with("++>>>>>>>") { |
| 457 | ++ self.paint_buffered_merge_conflict_lines(diff_type)?; |
| 458 | ++ } else { |
| 459 | ++ let line = self.painter.prepare(&self.line, diff_type.n_parents()); |
| 460 | ++ self.painter.merge_conflict_lines[Ours].push((line, HunkPlus(diff_type, None))); |
| 461 | ++ } |
| 462 | ++ handled_line = true |
| 463 | ++======= |
| 464 | + // TODO: don't allocate on heap at this point |
| 465 | + let prefix = self.line[..min(self.line.len(), 2)].to_string(); |
| 466 | + let diff_type = Combined(Prefix(prefix)); |
| 467 | + |
| 468 | + match self.state { |
| 469 | + // The only transition into a merge conflict is HunkZero => MergeConflict(Ours) |
| 470 | + // TODO: shouldn't this be HunkZero(Some(_))? |
| 471 | + HunkZero(_) => handled_line = self.enter_merge_conflict(), |
| 472 | + MergeConflict(Ours) => { |
| 473 | + handled_line = self.enter_ancestral() |
| 474 | + || self.enter_theirs() |
| 475 | + || self.exit_merge_conflict(diff_type.clone())? |
| 476 | + || self.store_line(Ours, HunkPlus(diff_type, None)); |
| 477 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 478 | + } |
| 479 | ++<<<<<<< HEAD |
| 480 | + MergeConflict(merge_parents, Ancestral) => { |
| 481 | + handled_line = self.enter_theirs(&merge_parents) |
| 482 | + || self.exit_merge_conflict(&merge_parents)? |
| 483 | + || self.store_line( |
| 484 | + Ancestral, |
| 485 | + HunkMinus(Combined(merge_parents, InMergeConflict::Yes), None), |
| 486 | + ); |
| 487 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 488 | ++ MergeConflict(Ancestral) => { |
| 489 | ++ if self.line.starts_with("++=======") { |
| 490 | ++ self.state = MergeConflict(Theirs); |
| 491 | ++ } else if self.line.starts_with("++>>>>>>>") { |
| 492 | ++ self.paint_buffered_merge_conflict_lines(diff_type)?; |
| 493 | ++ } else { |
| 494 | ++ let line = self.painter.prepare(&self.line, diff_type.n_parents()); |
| 495 | ++ self.painter.merge_conflict_lines[Ancestral] |
| 496 | ++ .push((line, HunkMinus(diff_type, None))); |
| 497 | ++ } |
| 498 | ++ handled_line = true |
| 499 | ++======= |
| 500 | + MergeConflict(Ancestral) => { |
| 501 | + handled_line = self.enter_theirs() |
| 502 | + || self.exit_merge_conflict(diff_type.clone())? |
| 503 | + || self.store_line(Ancestral, HunkMinus(diff_type, None)); |
| 504 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 505 | } |
| 506 | ++<<<<<<< HEAD |
| 507 | + MergeConflict(merge_parents, Theirs) => { |
| 508 | + handled_line = self.exit_merge_conflict(&merge_parents)? |
| 509 | + || self.store_line( |
| 510 | + Theirs, |
| 511 | + HunkPlus(Combined(merge_parents, InMergeConflict::Yes), None), |
| 512 | + ); |
| 513 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 514 | ++ MergeConflict(Theirs) => { |
| 515 | ++ if self.line.starts_with("++>>>>>>>") { |
| 516 | ++ self.paint_buffered_merge_conflict_lines(diff_type)?; |
| 517 | ++ } else { |
| 518 | ++ let line = self.painter.prepare(&self.line, diff_type.n_parents()); |
| 519 | ++ self.painter.merge_conflict_lines[Theirs] |
| 520 | ++ .push((line, HunkPlus(diff_type, None))); |
| 521 | ++ } |
| 522 | ++ handled_line = true |
| 523 | ++======= |
| 524 | + MergeConflict(Theirs) => { |
| 525 | + handled_line = self.exit_merge_conflict(diff_type.clone())? |
| 526 | + || self.store_line(Theirs, HunkPlus(diff_type, None)); |
| 527 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 528 | } |
| 529 | _ => {} |
| 530 | } |
| 531 | @@@ -75,75 -67,71 +170,150 @@@ |
| 532 | Ok(handled_line) |
| 533 | } |
| 534 | |
| 535 | ++<<<<<<< HEAD |
| 536 | + fn enter_merge_conflict(&mut self, merge_parents: &MergeParents) -> bool { |
| 537 | + use State::*; |
| 538 | + if let Some(commit) = parse_merge_marker(&self.line, "++<<<<<<<") { |
| 539 | + self.state = MergeConflict(merge_parents.clone(), Ours); |
| 540 | + self.painter.merge_conflict_commit_names[Ours] = Some(commit.to_string()); |
| 541 | + true |
| 542 | + } else { |
| 543 | + false |
| 544 | + } |
| 545 | + } |
| 546 | + |
| 547 | + fn enter_ancestral(&mut self, merge_parents: &MergeParents) -> bool { |
| 548 | + use State::*; |
| 549 | + if let Some(commit) = parse_merge_marker(&self.line, "++|||||||") { |
| 550 | + self.state = MergeConflict(merge_parents.clone(), Ancestral); |
| 551 | + self.painter.merge_conflict_commit_names[Ancestral] = Some(commit.to_string()); |
| 552 | + true |
| 553 | + } else { |
| 554 | + false |
| 555 | + } |
| 556 | + } |
| 557 | + |
| 558 | + fn enter_theirs(&mut self, merge_parents: &MergeParents) -> bool { |
| 559 | + use State::*; |
| 560 | + if self.line.starts_with("++=======") { |
| 561 | + self.state = MergeConflict(merge_parents.clone(), Theirs); |
| 562 | + true |
| 563 | + } else { |
| 564 | + false |
| 565 | + } |
| 566 | + } |
| 567 | + |
| 568 | + fn exit_merge_conflict(&mut self, merge_parents: &MergeParents) -> std::io::Result<bool> { |
| 569 | + if let Some(commit) = parse_merge_marker(&self.line, "++>>>>>>>") { |
| 570 | + self.painter.merge_conflict_commit_names[Theirs] = Some(commit.to_string()); |
| 571 | + self.paint_buffered_merge_conflict_lines(merge_parents)?; |
| 572 | + Ok(true) |
| 573 | + } else { |
| 574 | + Ok(false) |
| 575 | + } |
| 576 | + } |
| 577 | + |
| 578 | + fn store_line(&mut self, commit: MergeConflictCommit, state: State) -> bool { |
| 579 | + use State::*; |
| 580 | + if let HunkMinus(diff_type, _) | HunkZero(diff_type) | HunkPlus(diff_type, _) = &state { |
| 581 | + let line = self.painter.prepare(&self.line, diff_type.n_parents()); |
| 582 | + self.painter.merge_conflict_lines[commit].push((line, state)); |
| 583 | + true |
| 584 | + } else { |
| 585 | + delta_unreachable(&format!("Invalid state: {:?}", state)) |
| 586 | + } |
| 587 | + } |
| 588 | + |
| 589 | + fn paint_buffered_merge_conflict_lines( |
| 590 | + &mut self, |
| 591 | + merge_parents: &MergeParents, |
| 592 | + ) -> std::io::Result<()> { |
| 593 | + use DiffType::*; |
| 594 | + use State::*; |
| 595 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 596 | ++ fn paint_buffered_merge_conflict_lines(&mut self, diff_type: DiffType) -> std::io::Result<()> { |
| 597 | ++======= |
| 598 | + fn enter_merge_conflict(&mut self) -> bool { |
| 599 | + use State::*; |
| 600 | + if let Some(commit) = parse_merge_marker(&self.line, "++<<<<<<<") { |
| 601 | + self.state = MergeConflict(Ours); |
| 602 | + self.painter.merge_conflict_commit_names[Ours] = Some(commit.to_string()); |
| 603 | + true |
| 604 | + } else { |
| 605 | + false |
| 606 | + } |
| 607 | + } |
| 608 | + |
| 609 | + fn enter_ancestral(&mut self) -> bool { |
| 610 | + use State::*; |
| 611 | + if let Some(commit) = parse_merge_marker(&self.line, "++|||||||") { |
| 612 | + self.state = MergeConflict(Ancestral); |
| 613 | + self.painter.merge_conflict_commit_names[Ancestral] = Some(commit.to_string()); |
| 614 | + true |
| 615 | + } else { |
| 616 | + false |
| 617 | + } |
| 618 | + } |
| 619 | + |
| 620 | + fn enter_theirs(&mut self) -> bool { |
| 621 | + use State::*; |
| 622 | + if self.line.starts_with("++=======") { |
| 623 | + self.state = MergeConflict(Theirs); |
| 624 | + true |
| 625 | + } else { |
| 626 | + false |
| 627 | + } |
| 628 | + } |
| 629 | + |
| 630 | + fn exit_merge_conflict(&mut self, diff_type: DiffType) -> std::io::Result<bool> { |
| 631 | + if let Some(commit) = parse_merge_marker(&self.line, "++>>>>>>>") { |
| 632 | + self.painter.merge_conflict_commit_names[Theirs] = Some(commit.to_string()); |
| 633 | + self.paint_buffered_merge_conflict_lines(diff_type)?; |
| 634 | + Ok(true) |
| 635 | + } else { |
| 636 | + Ok(false) |
| 637 | + } |
| 638 | + } |
| 639 | + |
| 640 | + fn store_line(&mut self, commit: MergeConflictCommit, state: State) -> bool { |
| 641 | + use State::*; |
| 642 | + if let HunkMinus(diff_type, _) | HunkZero(diff_type) | HunkPlus(diff_type, _) = &state { |
| 643 | + let line = self.painter.prepare(&self.line, diff_type.n_parents()); |
| 644 | + self.painter.merge_conflict_lines[commit].push((line, state)); |
| 645 | + true |
| 646 | + } else { |
| 647 | + delta_unreachable(&format!("Invalid state: {:?}", state)) |
| 648 | + } |
| 649 | + } |
| 650 | + |
| 651 | + fn paint_buffered_merge_conflict_lines(&mut self, diff_type: DiffType) -> std::io::Result<()> { |
| 652 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 653 | self.painter.emit()?; |
| 654 | ++<<<<<<< HEAD |
| 655 | + |
| 656 | + write_merge_conflict_bar( |
| 657 | + &self.config.merge_conflict_begin_symbol, |
| 658 | + &mut self.painter, |
| 659 | + self.config, |
| 660 | + )?; |
| 661 | + for derived_commit_type in &[Ours, Theirs] { |
| 662 | + write_diff_header(derived_commit_type, &mut self.painter, self.config)?; |
| 663 | + self.painter.emit()?; |
| 664 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 665 | ++ let lines = &self.painter.merge_conflict_lines; |
| 666 | ++ for derived_lines in &[&lines[Ours], &lines[Theirs]] { |
| 667 | ++======= |
| 668 | + |
| 669 | + write_merge_conflict_bar("▼", &mut self.painter, self.config)?; |
| 670 | + for (derived_commit_type, decoration_style) in &[(Ours, "box"), (Theirs, "box")] { |
| 671 | + write_subhunk_header( |
| 672 | + derived_commit_type, |
| 673 | + decoration_style, |
| 674 | + &mut self.painter, |
| 675 | + self.config, |
| 676 | + )?; |
| 677 | + self.painter.emit()?; |
| 678 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 679 | paint::paint_minus_and_plus_lines( |
| 680 | MinusPlus::new( |
| 681 | &self.painter.merge_conflict_lines[Ancestral], |
| 682 | @@@ -156,78 -144,94 +326,190 @@@ |
| 683 | ); |
| 684 | self.painter.emit()?; |
| 685 | } |
| 686 | ++<<<<<<< HEAD |
| 687 | + // write_merge_conflict_decoration("bold ol", &mut self.painter, self.config)?; |
| 688 | + write_merge_conflict_bar( |
| 689 | + &self.config.merge_conflict_end_symbol, |
| 690 | + &mut self.painter, |
| 691 | + self.config, |
| 692 | + )?; |
| 693 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 694 | ++======= |
| 695 | + // write_merge_conflict_decoration("bold ol", &mut self.painter, self.config)?; |
| 696 | + write_merge_conflict_bar("▲", &mut self.painter, self.config)?; |
| 697 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 698 | self.painter.merge_conflict_lines.clear(); |
| 699 | - self.state = State::HunkZero(diff_type); |
| 700 | + self.state = HunkZero(Combined(merge_parents.clone(), InMergeConflict::No)); |
| 701 | Ok(()) |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | ++<<<<<<< HEAD |
| 706 | +fn write_diff_header( |
| 707 | + derived_commit_type: &MergeConflictCommit, |
| 708 | + painter: &mut paint::Painter, |
| 709 | + config: &config::Config, |
| 710 | +) -> std::io::Result<()> { |
| 711 | + let (mut draw_fn, pad, decoration_ansi_term_style) = |
| 712 | + draw::get_draw_function(config.merge_conflict_diff_header_style.decoration_style); |
| 713 | + let derived_commit_name = &painter.merge_conflict_commit_names[derived_commit_type]; |
| 714 | + let text = if let Some(_ancestral_commit) = &painter.merge_conflict_commit_names[Ancestral] { |
| 715 | + format!( |
| 716 | + "ancestor {} {}{}", |
| 717 | + config.right_arrow, |
| 718 | + derived_commit_name.as_deref().unwrap_or("?"), |
| 719 | + if pad { " " } else { "" } |
| 720 | + ) |
| 721 | + } else { |
| 722 | + derived_commit_name.as_deref().unwrap_or("?").to_string() |
| 723 | + }; |
| 724 | + draw_fn( |
| 725 | + painter.writer, |
| 726 | + &text, |
| 727 | + &text, |
| 728 | + &config.decorations_width, |
| 729 | + config.merge_conflict_diff_header_style, |
| 730 | + decoration_ansi_term_style, |
| 731 | + )?; |
| 732 | + Ok(()) |
| 733 | +} |
| 734 | + |
| 735 | +fn write_merge_conflict_bar( |
| 736 | + s: &str, |
| 737 | + painter: &mut paint::Painter, |
| 738 | + config: &config::Config, |
| 739 | +) -> std::io::Result<()> { |
| 740 | + if let cli::Width::Fixed(width) = config.decorations_width { |
| 741 | + writeln!( |
| 742 | + painter.writer, |
| 743 | + "{}", |
| 744 | + &s.graphemes(true).cycle().take(width).join("") |
| 745 | + )?; |
| 746 | + } |
| 747 | + Ok(()) |
| 748 | +} |
| 749 | + |
| 750 | +fn parse_merge_marker<'a>(line: &'a str, marker: &str) -> Option<&'a str> { |
| 751 | + match line.strip_prefix(marker) { |
| 752 | + Some(suffix) => { |
| 753 | + let suffix = suffix.trim(); |
| 754 | + if !suffix.is_empty() { |
| 755 | + Some(suffix) |
| 756 | + } else { |
| 757 | + None |
| 758 | + } |
| 759 | + } |
| 760 | + None => None, |
| 761 | + } |
| 762 | +} |
| 763 | + |
| 764 | +pub use MergeConflictCommit::*; |
| 765 | + |
| 766 | ++impl<T> Index<MergeConflictCommit> for MergeConflictCommits<T> { |
| 767 | ++ type Output = T; |
| 768 | ++ fn index(&self, commit: MergeConflictCommit) -> &Self::Output { |
| 769 | ++ match commit { |
| 770 | ++ Ours => &self.ours, |
| 771 | ++ Ancestral => &self.ancestral, |
| 772 | ++ Theirs => &self.theirs, |
| 773 | ++ } |
| 774 | ++ } |
| 775 | ++} |
| 776 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 777 | ++pub use Source::*; |
| 778 | ++======= |
| 779 | + fn write_subhunk_header( |
| 780 | + derived_commit_type: &MergeConflictCommit, |
| 781 | + decoration_style: &str, |
| 782 | + painter: &mut paint::Painter, |
| 783 | + config: &config::Config, |
| 784 | + ) -> std::io::Result<()> { |
| 785 | + let (mut draw_fn, pad, decoration_ansi_term_style) = |
| 786 | + draw::get_draw_function(DecorationStyle::from_str( |
| 787 | + decoration_style, |
| 788 | + config.true_color, |
| 789 | + config.git_config.as_ref(), |
| 790 | + )); |
| 791 | + let derived_commit_name = &painter.merge_conflict_commit_names[derived_commit_type]; |
| 792 | + let text = if let Some(_ancestral_commit) = &painter.merge_conflict_commit_names[Ancestral] { |
| 793 | + format!( |
| 794 | + "ancestor {} {}{}", |
| 795 | + config.right_arrow, |
| 796 | + derived_commit_name.as_deref().unwrap_or("?"), |
| 797 | + if pad { " " } else { "" } |
| 798 | + ) |
| 799 | + } else { |
| 800 | + derived_commit_name.as_deref().unwrap_or("?").to_string() |
| 801 | + }; |
| 802 | + draw_fn( |
| 803 | + painter.writer, |
| 804 | + &text, |
| 805 | + &text, |
| 806 | + &config.decorations_width, |
| 807 | + config.hunk_header_style, |
| 808 | + decoration_ansi_term_style, |
| 809 | + )?; |
| 810 | + Ok(()) |
| 811 | + } |
| 812 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 813 | + |
| 814 | ++<<<<<<< HEAD |
| 815 | ++impl<T> Index<&MergeConflictCommit> for MergeConflictCommits<T> { |
| 816 | ++ type Output = T; |
| 817 | ++ fn index(&self, commit: &MergeConflictCommit) -> &Self::Output { |
| 818 | ++ match commit { |
| 819 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 820 | ++impl Index<Source> for MergeConflictLines { |
| 821 | ++ type Output = Vec<(String, State)>; |
| 822 | ++ fn index(&self, source: Source) -> &Self::Output { |
| 823 | ++ match source { |
| 824 | ++======= |
| 825 | + #[allow(unused)] |
| 826 | + fn write_merge_conflict_line( |
| 827 | + painter: &mut paint::Painter, |
| 828 | + config: &config::Config, |
| 829 | + ) -> std::io::Result<()> { |
| 830 | + let (mut draw_fn, _pad, decoration_ansi_term_style) = draw::get_draw_function( |
| 831 | + DecorationStyle::from_str("bold ol", config.true_color, config.git_config.as_ref()), |
| 832 | + ); |
| 833 | + draw_fn( |
| 834 | + painter.writer, |
| 835 | + "", |
| 836 | + "", |
| 837 | + &config.decorations_width, |
| 838 | + config.hunk_header_style, |
| 839 | + decoration_ansi_term_style, |
| 840 | + )?; |
| 841 | + Ok(()) |
| 842 | + } |
| 843 | + |
| 844 | + fn write_merge_conflict_bar( |
| 845 | + s: &str, |
| 846 | + painter: &mut paint::Painter, |
| 847 | + config: &config::Config, |
| 848 | + ) -> std::io::Result<()> { |
| 849 | + if let cli::Width::Fixed(width) = config.decorations_width { |
| 850 | + writeln!(painter.writer, "{}", s.repeat(width))?; |
| 851 | + } |
| 852 | + Ok(()) |
| 853 | + } |
| 854 | + |
| 855 | + fn parse_merge_marker<'a>(line: &'a str, marker: &str) -> Option<&'a str> { |
| 856 | + match line.strip_prefix(marker) { |
| 857 | + Some(suffix) => { |
| 858 | + let suffix = suffix.trim(); |
| 859 | + if !suffix.is_empty() { |
| 860 | + Some(suffix) |
| 861 | + } else { |
| 862 | + None |
| 863 | + } |
| 864 | + } |
| 865 | + None => None, |
| 866 | + } |
| 867 | + } |
| 868 | + |
| 869 | + pub use MergeConflictCommit::*; |
| 870 | + |
| 871 | impl<T> Index<MergeConflictCommit> for MergeConflictCommits<T> { |
| 872 | type Output = T; |
| 873 | fn index(&self, commit: MergeConflictCommit) -> &Self::Output { |
| 874 | @@@ -243,6 -247,6 +525,7 @@@ impl<T> Index<&MergeConflictCommit> fo |
| 875 | type Output = T; |
| 876 | fn index(&self, commit: &MergeConflictCommit) -> &Self::Output { |
| 877 | match commit { |
| 878 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 879 | Ours => &self.ours, |
| 880 | Ancestral => &self.ancestral, |
| 881 | Theirs => &self.theirs, |
| 882 | "#; |
| 883 | |
| 884 | const GIT_MERGE_CONFLICT_U0: &str = r#"\ |
| 885 | diff --cc src/handlers/merge_conflict.rs |
| 886 | index 27d47c0,3a7e7b9..0000000 |
| 887 | --- a/src/handlers/merge_conflict.rs |
| 888 | +++ b/src/handlers/merge_conflict.rs |
| 889 | @@@ -3,7 -4,4 +3,16 @@@ use std::ops::{Index, IndexMut} |
| 890 | ++<<<<<<< HEAD |
| 891 | +use itertools::Itertools; |
| 892 | +use unicode_segmentation::UnicodeSegmentation; |
| 893 | + |
| 894 | +use super::draw; |
| 895 | +use crate::cli; |
| 896 | +use crate::config::{self, delta_unreachable}; |
| 897 | +use crate::delta::{DiffType, InMergeConflict, MergeParents, State, StateMachine}; |
| 898 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 899 | ++use crate::delta::{DiffType, MergeParents, State, StateMachine}; |
| 900 | ++======= |
| 901 | + use super::draw; |
| 902 | + use crate::cli; |
| 903 | + use crate::config::{self, delta_unreachable}; |
| 904 | + use crate::delta::{DiffType, MergeParents, State, StateMachine}; |
| 905 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 906 | @@@ -33,0 -32,0 +43,1 @@@ impl<'a> StateMachine<'a> |
| 907 | ++<<<<<<< HEAD |
| 908 | @@@ -34,0 -33,1 +45,7 @@@ |
| 909 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 910 | + use MergeParents::*; |
| 911 | ++ use Source::*; |
| 912 | ++======= |
| 913 | ++ use MergeConflictCommit::*; |
| 914 | ++ use MergeParents::*; |
| 915 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 916 | @@@ -41,23 -41,18 +59,84 @@@ |
| 917 | ++<<<<<<< HEAD |
| 918 | + match self.state.clone() { |
| 919 | + HunkHeader(Combined(merge_parents, InMergeConflict::No), _, _) |
| 920 | + | HunkMinus(Combined(merge_parents, InMergeConflict::No), _) |
| 921 | + | HunkZero(Combined(merge_parents, InMergeConflict::No)) |
| 922 | + | HunkPlus(Combined(merge_parents, InMergeConflict::No), _) => { |
| 923 | + handled_line = self.enter_merge_conflict(&merge_parents) |
| 924 | + } |
| 925 | + MergeConflict(merge_parents, Ours) => { |
| 926 | + handled_line = self.enter_ancestral(&merge_parents) |
| 927 | + || self.enter_theirs(&merge_parents) |
| 928 | + || self.exit_merge_conflict(&merge_parents)? |
| 929 | + || self.store_line( |
| 930 | + Ours, |
| 931 | + HunkPlus(Combined(merge_parents, InMergeConflict::Yes), None), |
| 932 | + ); |
| 933 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 934 | ++ // TODO: don't allocate on heap at this point |
| 935 | ++ let prefix = self.line[..min(self.line.len(), 2)].to_string(); |
| 936 | ++ let diff_type = Combined(Prefix(prefix)); |
| 937 | ++ |
| 938 | ++ match self.state { |
| 939 | ++ // The only transition into a merge conflict is HunkZero => MergeConflict(Ours) |
| 940 | ++ // TODO: shouldn't this be HunkZero(Some(_))? |
| 941 | ++ HunkZero(_) => { |
| 942 | ++ if self.line.starts_with("++<<<<<<<") { |
| 943 | ++ self.state = MergeConflict(Ours); |
| 944 | ++ handled_line = true |
| 945 | ++ } |
| 946 | + } |
| 947 | ++ MergeConflict(Ours) => { |
| 948 | ++ if self.line.starts_with("++|||||||") { |
| 949 | ++ self.state = MergeConflict(Ancestral); |
| 950 | ++ } else if self.line.starts_with("++=======") { |
| 951 | ++ self.state = MergeConflict(Theirs); |
| 952 | ++ } else if self.line.starts_with("++>>>>>>>") { |
| 953 | ++ self.paint_buffered_merge_conflict_lines(diff_type)?; |
| 954 | ++ } else { |
| 955 | ++ let line = self.painter.prepare(&self.line, diff_type.n_parents()); |
| 956 | ++ self.painter.merge_conflict_lines[Ours].push((line, HunkPlus(diff_type, None))); |
| 957 | ++ } |
| 958 | ++ handled_line = true |
| 959 | ++======= |
| 960 | + // TODO: don't allocate on heap at this point |
| 961 | + let prefix = self.line[..min(self.line.len(), 2)].to_string(); |
| 962 | + let diff_type = Combined(Prefix(prefix)); |
| 963 | + |
| 964 | + match self.state { |
| 965 | + // The only transition into a merge conflict is HunkZero => MergeConflict(Ours) |
| 966 | + // TODO: shouldn't this be HunkZero(Some(_))? |
| 967 | + HunkZero(_) => handled_line = self.enter_merge_conflict(), |
| 968 | + MergeConflict(Ours) => { |
| 969 | + handled_line = self.enter_ancestral() |
| 970 | + || self.enter_theirs() |
| 971 | + || self.exit_merge_conflict(diff_type.clone())? |
| 972 | + || self.store_line(Ours, HunkPlus(diff_type, None)); |
| 973 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 974 | + } |
| 975 | ++<<<<<<< HEAD |
| 976 | + MergeConflict(merge_parents, Ancestral) => { |
| 977 | + handled_line = self.enter_theirs(&merge_parents) |
| 978 | + || self.exit_merge_conflict(&merge_parents)? |
| 979 | + || self.store_line( |
| 980 | + Ancestral, |
| 981 | + HunkMinus(Combined(merge_parents, InMergeConflict::Yes), None), |
| 982 | + ); |
| 983 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 984 | ++ MergeConflict(Ancestral) => { |
| 985 | ++ if self.line.starts_with("++=======") { |
| 986 | ++ self.state = MergeConflict(Theirs); |
| 987 | ++ } else if self.line.starts_with("++>>>>>>>") { |
| 988 | ++ self.paint_buffered_merge_conflict_lines(diff_type)?; |
| 989 | ++ } else { |
| 990 | ++ let line = self.painter.prepare(&self.line, diff_type.n_parents()); |
| 991 | ++ self.painter.merge_conflict_lines[Ancestral] |
| 992 | ++ .push((line, HunkMinus(diff_type, None))); |
| 993 | ++ } |
| 994 | ++ handled_line = true |
| 995 | ++======= |
| 996 | + MergeConflict(Ancestral) => { |
| 997 | + handled_line = self.enter_theirs() |
| 998 | + || self.exit_merge_conflict(diff_type.clone())? |
| 999 | + || self.store_line(Ancestral, HunkMinus(diff_type, None)); |
| 1000 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 1001 | @@@ -65,6 -60,3 +144,22 @@@ |
| 1002 | ++<<<<<<< HEAD |
| 1003 | + MergeConflict(merge_parents, Theirs) => { |
| 1004 | + handled_line = self.exit_merge_conflict(&merge_parents)? |
| 1005 | + || self.store_line( |
| 1006 | + Theirs, |
| 1007 | + HunkPlus(Combined(merge_parents, InMergeConflict::Yes), None), |
| 1008 | + ); |
| 1009 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 1010 | ++ MergeConflict(Theirs) => { |
| 1011 | ++ if self.line.starts_with("++>>>>>>>") { |
| 1012 | ++ self.paint_buffered_merge_conflict_lines(diff_type)?; |
| 1013 | ++ } else { |
| 1014 | ++ let line = self.painter.prepare(&self.line, diff_type.n_parents()); |
| 1015 | ++ self.painter.merge_conflict_lines[Theirs] |
| 1016 | ++ .push((line, HunkPlus(diff_type, None))); |
| 1017 | ++ } |
| 1018 | ++ handled_line = true |
| 1019 | ++======= |
| 1020 | + MergeConflict(Theirs) => { |
| 1021 | + handled_line = self.exit_merge_conflict(diff_type.clone())? |
| 1022 | + || self.store_line(Theirs, HunkPlus(diff_type, None)); |
| 1023 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 1024 | @@@ -78,59 -70,54 +173,118 @@@ |
| 1025 | ++<<<<<<< HEAD |
| 1026 | + fn enter_merge_conflict(&mut self, merge_parents: &MergeParents) -> bool { |
| 1027 | + use State::*; |
| 1028 | + if let Some(commit) = parse_merge_marker(&self.line, "++<<<<<<<") { |
| 1029 | + self.state = MergeConflict(merge_parents.clone(), Ours); |
| 1030 | + self.painter.merge_conflict_commit_names[Ours] = Some(commit.to_string()); |
| 1031 | + true |
| 1032 | + } else { |
| 1033 | + false |
| 1034 | + } |
| 1035 | + } |
| 1036 | + |
| 1037 | + fn enter_ancestral(&mut self, merge_parents: &MergeParents) -> bool { |
| 1038 | + use State::*; |
| 1039 | + if let Some(commit) = parse_merge_marker(&self.line, "++|||||||") { |
| 1040 | + self.state = MergeConflict(merge_parents.clone(), Ancestral); |
| 1041 | + self.painter.merge_conflict_commit_names[Ancestral] = Some(commit.to_string()); |
| 1042 | + true |
| 1043 | + } else { |
| 1044 | + false |
| 1045 | + } |
| 1046 | + } |
| 1047 | + |
| 1048 | + fn enter_theirs(&mut self, merge_parents: &MergeParents) -> bool { |
| 1049 | + use State::*; |
| 1050 | + if self.line.starts_with("++=======") { |
| 1051 | + self.state = MergeConflict(merge_parents.clone(), Theirs); |
| 1052 | + true |
| 1053 | + } else { |
| 1054 | + false |
| 1055 | + } |
| 1056 | + } |
| 1057 | + |
| 1058 | + fn exit_merge_conflict(&mut self, merge_parents: &MergeParents) -> std::io::Result<bool> { |
| 1059 | + if let Some(commit) = parse_merge_marker(&self.line, "++>>>>>>>") { |
| 1060 | + self.painter.merge_conflict_commit_names[Theirs] = Some(commit.to_string()); |
| 1061 | + self.paint_buffered_merge_conflict_lines(merge_parents)?; |
| 1062 | + Ok(true) |
| 1063 | + } else { |
| 1064 | + Ok(false) |
| 1065 | + } |
| 1066 | + } |
| 1067 | + |
| 1068 | + fn store_line(&mut self, commit: MergeConflictCommit, state: State) -> bool { |
| 1069 | + use State::*; |
| 1070 | + if let HunkMinus(diff_type, _) | HunkZero(diff_type) | HunkPlus(diff_type, _) = &state { |
| 1071 | + let line = self.painter.prepare(&self.line, diff_type.n_parents()); |
| 1072 | + self.painter.merge_conflict_lines[commit].push((line, state)); |
| 1073 | + true |
| 1074 | + } else { |
| 1075 | + delta_unreachable(&format!("Invalid state: {:?}", state)) |
| 1076 | + } |
| 1077 | + } |
| 1078 | + |
| 1079 | + fn paint_buffered_merge_conflict_lines( |
| 1080 | + &mut self, |
| 1081 | + merge_parents: &MergeParents, |
| 1082 | + ) -> std::io::Result<()> { |
| 1083 | + use DiffType::*; |
| 1084 | + use State::*; |
| 1085 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 1086 | ++ fn paint_buffered_merge_conflict_lines(&mut self, diff_type: DiffType) -> std::io::Result<()> { |
| 1087 | ++======= |
| 1088 | + fn enter_merge_conflict(&mut self) -> bool { |
| 1089 | + use State::*; |
| 1090 | + if let Some(commit) = parse_merge_marker(&self.line, "++<<<<<<<") { |
| 1091 | + self.state = MergeConflict(Ours); |
| 1092 | + self.painter.merge_conflict_commit_names[Ours] = Some(commit.to_string()); |
| 1093 | + true |
| 1094 | + } else { |
| 1095 | + false |
| 1096 | + } |
| 1097 | + } |
| 1098 | + |
| 1099 | + fn enter_ancestral(&mut self) -> bool { |
| 1100 | + use State::*; |
| 1101 | + if let Some(commit) = parse_merge_marker(&self.line, "++|||||||") { |
| 1102 | + self.state = MergeConflict(Ancestral); |
| 1103 | + self.painter.merge_conflict_commit_names[Ancestral] = Some(commit.to_string()); |
| 1104 | + true |
| 1105 | + } else { |
| 1106 | + false |
| 1107 | + } |
| 1108 | + } |
| 1109 | + |
| 1110 | + fn enter_theirs(&mut self) -> bool { |
| 1111 | + use State::*; |
| 1112 | + if self.line.starts_with("++=======") { |
| 1113 | + self.state = MergeConflict(Theirs); |
| 1114 | + true |
| 1115 | + } else { |
| 1116 | + false |
| 1117 | + } |
| 1118 | + } |
| 1119 | + |
| 1120 | + fn exit_merge_conflict(&mut self, diff_type: DiffType) -> std::io::Result<bool> { |
| 1121 | + if let Some(commit) = parse_merge_marker(&self.line, "++>>>>>>>") { |
| 1122 | + self.painter.merge_conflict_commit_names[Theirs] = Some(commit.to_string()); |
| 1123 | + self.paint_buffered_merge_conflict_lines(diff_type)?; |
| 1124 | + Ok(true) |
| 1125 | + } else { |
| 1126 | + Ok(false) |
| 1127 | + } |
| 1128 | + } |
| 1129 | + |
| 1130 | + fn store_line(&mut self, commit: MergeConflictCommit, state: State) -> bool { |
| 1131 | + use State::*; |
| 1132 | + if let HunkMinus(diff_type, _) | HunkZero(diff_type) | HunkPlus(diff_type, _) = &state { |
| 1133 | + let line = self.painter.prepare(&self.line, diff_type.n_parents()); |
| 1134 | + self.painter.merge_conflict_lines[commit].push((line, state)); |
| 1135 | + true |
| 1136 | + } else { |
| 1137 | + delta_unreachable(&format!("Invalid state: {:?}", state)) |
| 1138 | + } |
| 1139 | + } |
| 1140 | + |
| 1141 | + fn paint_buffered_merge_conflict_lines(&mut self, diff_type: DiffType) -> std::io::Result<()> { |
| 1142 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 1143 | @@@ -138,9 -125,10 +292,25 @@@ |
| 1144 | ++<<<<<<< HEAD |
| 1145 | + |
| 1146 | + write_merge_conflict_bar( |
| 1147 | + &self.config.merge_conflict_begin_symbol, |
| 1148 | + &mut self.painter, |
| 1149 | + self.config, |
| 1150 | + )?; |
| 1151 | + for derived_commit_type in &[Ours, Theirs] { |
| 1152 | + write_diff_header(derived_commit_type, &mut self.painter, self.config)?; |
| 1153 | + self.painter.emit()?; |
| 1154 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 1155 | ++ let lines = &self.painter.merge_conflict_lines; |
| 1156 | ++ for derived_lines in &[&lines[Ours], &lines[Theirs]] { |
| 1157 | ++======= |
| 1158 | + |
| 1159 | + write_merge_conflict_bar("▼", &mut self.painter, self.config)?; |
| 1160 | + for (derived_commit_type, decoration_style) in &[(Ours, "box"), (Theirs, "box")] { |
| 1161 | + write_subhunk_header( |
| 1162 | + derived_commit_type, |
| 1163 | + decoration_style, |
| 1164 | + &mut self.painter, |
| 1165 | + self.config, |
| 1166 | + )?; |
| 1167 | + self.painter.emit()?; |
| 1168 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 1169 | @@@ -159,6 -147,2 +329,12 @@@ |
| 1170 | ++<<<<<<< HEAD |
| 1171 | + // write_merge_conflict_decoration("bold ol", &mut self.painter, self.config)?; |
| 1172 | + write_merge_conflict_bar( |
| 1173 | + &self.config.merge_conflict_end_symbol, |
| 1174 | + &mut self.painter, |
| 1175 | + self.config, |
| 1176 | + )?; |
| 1177 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 1178 | ++======= |
| 1179 | + // write_merge_conflict_decoration("bold ol", &mut self.painter, self.config)?; |
| 1180 | + write_merge_conflict_bar("▲", &mut self.painter, self.config)?; |
| 1181 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 1182 | @@@ -171,60 -155,80 +347,166 @@@ |
| 1183 | ++<<<<<<< HEAD |
| 1184 | +fn write_diff_header( |
| 1185 | + derived_commit_type: &MergeConflictCommit, |
| 1186 | + painter: &mut paint::Painter, |
| 1187 | + config: &config::Config, |
| 1188 | +) -> std::io::Result<()> { |
| 1189 | + let (mut draw_fn, pad, decoration_ansi_term_style) = |
| 1190 | + draw::get_draw_function(config.merge_conflict_diff_header_style.decoration_style); |
| 1191 | + let derived_commit_name = &painter.merge_conflict_commit_names[derived_commit_type]; |
| 1192 | + let text = if let Some(_ancestral_commit) = &painter.merge_conflict_commit_names[Ancestral] { |
| 1193 | + format!( |
| 1194 | + "ancestor {} {}{}", |
| 1195 | + config.right_arrow, |
| 1196 | + derived_commit_name.as_deref().unwrap_or("?"), |
| 1197 | + if pad { " " } else { "" } |
| 1198 | + ) |
| 1199 | + } else { |
| 1200 | + derived_commit_name.as_deref().unwrap_or("?").to_string() |
| 1201 | + }; |
| 1202 | + draw_fn( |
| 1203 | + painter.writer, |
| 1204 | + &text, |
| 1205 | + &text, |
| 1206 | + &config.decorations_width, |
| 1207 | + config.merge_conflict_diff_header_style, |
| 1208 | + decoration_ansi_term_style, |
| 1209 | + )?; |
| 1210 | + Ok(()) |
| 1211 | +} |
| 1212 | + |
| 1213 | +fn write_merge_conflict_bar( |
| 1214 | + s: &str, |
| 1215 | + painter: &mut paint::Painter, |
| 1216 | + config: &config::Config, |
| 1217 | +) -> std::io::Result<()> { |
| 1218 | + if let cli::Width::Fixed(width) = config.decorations_width { |
| 1219 | + writeln!( |
| 1220 | + painter.writer, |
| 1221 | + "{}", |
| 1222 | + &s.graphemes(true).cycle().take(width).join("") |
| 1223 | + )?; |
| 1224 | + } |
| 1225 | + Ok(()) |
| 1226 | +} |
| 1227 | + |
| 1228 | +fn parse_merge_marker<'a>(line: &'a str, marker: &str) -> Option<&'a str> { |
| 1229 | + match line.strip_prefix(marker) { |
| 1230 | + Some(suffix) => { |
| 1231 | + let suffix = suffix.trim(); |
| 1232 | + if !suffix.is_empty() { |
| 1233 | + Some(suffix) |
| 1234 | + } else { |
| 1235 | + None |
| 1236 | + } |
| 1237 | + } |
| 1238 | + None => None, |
| 1239 | + } |
| 1240 | +} |
| 1241 | + |
| 1242 | +pub use MergeConflictCommit::*; |
| 1243 | + |
| 1244 | ++impl<T> Index<MergeConflictCommit> for MergeConflictCommits<T> { |
| 1245 | ++ type Output = T; |
| 1246 | ++ fn index(&self, commit: MergeConflictCommit) -> &Self::Output { |
| 1247 | ++ match commit { |
| 1248 | ++ Ours => &self.ours, |
| 1249 | ++ Ancestral => &self.ancestral, |
| 1250 | ++ Theirs => &self.theirs, |
| 1251 | ++ } |
| 1252 | ++ } |
| 1253 | ++} |
| 1254 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 1255 | ++pub use Source::*; |
| 1256 | ++======= |
| 1257 | + fn write_subhunk_header( |
| 1258 | + derived_commit_type: &MergeConflictCommit, |
| 1259 | + decoration_style: &str, |
| 1260 | + painter: &mut paint::Painter, |
| 1261 | + config: &config::Config, |
| 1262 | + ) -> std::io::Result<()> { |
| 1263 | + let (mut draw_fn, pad, decoration_ansi_term_style) = |
| 1264 | + draw::get_draw_function(DecorationStyle::from_str( |
| 1265 | + decoration_style, |
| 1266 | + config.true_color, |
| 1267 | + config.git_config.as_ref(), |
| 1268 | + )); |
| 1269 | + let derived_commit_name = &painter.merge_conflict_commit_names[derived_commit_type]; |
| 1270 | + let text = if let Some(_ancestral_commit) = &painter.merge_conflict_commit_names[Ancestral] { |
| 1271 | + format!( |
| 1272 | + "ancestor {} {}{}", |
| 1273 | + config.right_arrow, |
| 1274 | + derived_commit_name.as_deref().unwrap_or("?"), |
| 1275 | + if pad { " " } else { "" } |
| 1276 | + ) |
| 1277 | + } else { |
| 1278 | + derived_commit_name.as_deref().unwrap_or("?").to_string() |
| 1279 | + }; |
| 1280 | + draw_fn( |
| 1281 | + painter.writer, |
| 1282 | + &text, |
| 1283 | + &text, |
| 1284 | + &config.decorations_width, |
| 1285 | + config.hunk_header_style, |
| 1286 | + decoration_ansi_term_style, |
| 1287 | + )?; |
| 1288 | + Ok(()) |
| 1289 | + } |
| 1290 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 1291 | + |
| 1292 | ++<<<<<<< HEAD |
| 1293 | ++impl<T> Index<&MergeConflictCommit> for MergeConflictCommits<T> { |
| 1294 | ++ type Output = T; |
| 1295 | ++ fn index(&self, commit: &MergeConflictCommit) -> &Self::Output { |
| 1296 | ++ match commit { |
| 1297 | ++||||||| parent of b2b28c8... Display merge conflict branches |
| 1298 | ++impl Index<Source> for MergeConflictLines { |
| 1299 | ++ type Output = Vec<(String, State)>; |
| 1300 | ++ fn index(&self, source: Source) -> &Self::Output { |
| 1301 | ++ match source { |
| 1302 | ++======= |
| 1303 | + #[allow(unused)] |
| 1304 | + fn write_merge_conflict_line( |
| 1305 | + painter: &mut paint::Painter, |
| 1306 | + config: &config::Config, |
| 1307 | + ) -> std::io::Result<()> { |
| 1308 | + let (mut draw_fn, _pad, decoration_ansi_term_style) = draw::get_draw_function( |
| 1309 | + DecorationStyle::from_str("bold ol", config.true_color, config.git_config.as_ref()), |
| 1310 | + ); |
| 1311 | + draw_fn( |
| 1312 | + painter.writer, |
| 1313 | + "", |
| 1314 | + "", |
| 1315 | + &config.decorations_width, |
| 1316 | + config.hunk_header_style, |
| 1317 | + decoration_ansi_term_style, |
| 1318 | + )?; |
| 1319 | + Ok(()) |
| 1320 | + } |
| 1321 | + |
| 1322 | + fn write_merge_conflict_bar( |
| 1323 | + s: &str, |
| 1324 | + painter: &mut paint::Painter, |
| 1325 | + config: &config::Config, |
| 1326 | + ) -> std::io::Result<()> { |
| 1327 | + if let cli::Width::Fixed(width) = config.decorations_width { |
| 1328 | + writeln!(painter.writer, "{}", s.repeat(width))?; |
| 1329 | + } |
| 1330 | + Ok(()) |
| 1331 | + } |
| 1332 | + |
| 1333 | + fn parse_merge_marker<'a>(line: &'a str, marker: &str) -> Option<&'a str> { |
| 1334 | + match line.strip_prefix(marker) { |
| 1335 | + Some(suffix) => { |
| 1336 | + let suffix = suffix.trim(); |
| 1337 | + if !suffix.is_empty() { |
| 1338 | + Some(suffix) |
| 1339 | + } else { |
| 1340 | + None |
| 1341 | + } |
| 1342 | + } |
| 1343 | + None => None, |
| 1344 | + } |
| 1345 | + } |
| 1346 | + |
| 1347 | + pub use MergeConflictCommit::*; |
| 1348 | + |
| 1349 | @@@ -246,0 -250,0 +528,1 @@@ impl<T> Index<&MergeConflictCommit> fo |
| 1350 | ++>>>>>>> b2b28c8... Display merge conflict branches |
| 1351 | "#; |
| 1352 | } |