| 1 | use lazy_static::lazy_static; |
| 2 | use regex::Regex; |
| 3 | |
| 4 | use crate::delta::{State, StateMachine}; |
| 5 | |
| 6 | impl StateMachine<'_> { |
| 7 | #[inline] |
| 8 | fn test_submodule_log(&self) -> bool { |
| 9 | self.line.starts_with("Submodule ") |
| 10 | } |
| 11 | |
| 12 | pub fn handle_submodule_log_line(&mut self) -> std::io::Result<bool> { |
| 13 | if !self.test_submodule_log() { |
| 14 | return Ok(false); |
| 15 | } |
| 16 | self.handle_additional_cases(State::SubmoduleLog) |
| 17 | } |
| 18 | |
| 19 | #[inline] |
| 20 | fn test_submodule_short_line(&self) -> bool { |
| 21 | matches!(self.state, State::HunkHeader(_, _, _, _)) |
| 22 | && self.line.starts_with("-Subproject commit ") |
| 23 | || matches!(self.state, State::SubmoduleShort(_)) |
| 24 | && self.line.starts_with("+Subproject commit ") |
| 25 | } |
| 26 | |
| 27 | pub fn handle_submodule_short_line(&mut self) -> std::io::Result<bool> { |
| 28 | if !self.test_submodule_short_line() || self.config.color_only { |
| 29 | return Ok(false); |
| 30 | } |
| 31 | if let Some(commit) = get_submodule_short_commit(&self.line) { |
| 32 | if let State::HunkHeader(_, _, _, _) = self.state { |
| 33 | self.state = State::SubmoduleShort(commit.to_owned()); |
| 34 | } else if let State::SubmoduleShort(minus_commit) = &self.state { |
| 35 | self.painter.emit()?; |
| 36 | writeln!( |
| 37 | self.painter.writer, |
| 38 | "{}..{}", |
| 39 | self.config |
| 40 | .minus_style |
| 41 | .paint(minus_commit.chars().take(12).collect::<String>()), |
| 42 | self.config |
| 43 | .plus_style |
| 44 | .paint(commit.chars().take(12).collect::<String>()), |
| 45 | )?; |
| 46 | } |
| 47 | } |
| 48 | Ok(true) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | lazy_static! { |
| 53 | static ref SUBMODULE_SHORT_LINE_REGEX: Regex = |
| 54 | Regex::new("^[-+]Subproject commit ([0-9a-f]{40})(-dirty)?$").unwrap(); |
| 55 | } |
| 56 | |
| 57 | pub fn get_submodule_short_commit(line: &str) -> Option<&str> { |
| 58 | match SUBMODULE_SHORT_LINE_REGEX.captures(line) { |
| 59 | Some(caps) => Some(caps.get(1).unwrap().as_str()), |
| 60 | None => None, |
| 61 | } |
| 62 | } |