261119.1 d538fc8546242abbe897575c077b0f19dcacd2e6 Indicate submodule dirty state (#2349)
commit time warp
* Indicate submodule dirty state
* Add failing test: show a dirty submodule whose commit has not changed as one commit
* Show a dirty submodule whose commit has not changed as one commit, and leave (dirty) unstyled
* Add failing test: submodule commits are missing from delta --tui with diff.submodule=short
* Show submodule commits in delta --tui with diff.submodule=short
---------
Co-authored-by: Dan Davison <dandavison7@gmail.com>
Changes to src/. All files
src/handlers/diff_header.rs
@@ -1162,6 +1162,27 @@ Submodule sub/mod a32f27c..2d9f921 (rewind): "); } + #[test]+ fn test_hunk_header_record_on_submodule_short_row() {+ // With diff.submodule=short, delta draws the submodule's hunk as one+ // row of commits. That row stands in for the hunk header, so it must+ // carry the `h` record: without it a host cannot place the row.+ let input = "\+diff --git a/sub b/sub+index 64a392c..676de96 160000+--- a/sub++++ b/sub+@@ -1 +1 @@+-Subproject commit 64a392c2ccaeea0acaef893ddb0024f3c939ffd9++Subproject commit 676de96c0a8aefe32c4195376be47ebcce2b6703-dirty+";+ assert_snapshot!(visible_metadata_records(input, &[]), @r"+ ⟦1;f;;;sub⟧sub+ ⟦1;f;;;sub⟧───────────────────────────────────────────+ ⟦1;h;1;;sub⟧64a392c2ccae..676de96c0a8a (dirty)+ ");+ }+ #[test] fn test_no_file_header_record_on_an_unreadable_submodule_row() { // A row that opens with "Submodule " but states no submodule carries no
src/handlers/hunk_header.rs
@@ -34,7 +34,7 @@ use regex::Regex; #[derive(Clone, Default, Debug, PartialEq, Eq)] pub struct ParsedHunkHeader { code_fragment: String,- line_numbers_and_hunk_lengths: Vec<(usize, usize)>,+ pub line_numbers_and_hunk_lengths: Vec<(usize, usize)>, } impl ParsedHunkHeader {
src/handlers/submodule.rs
@@ -45,21 +45,49 @@ impl StateMachine<'_> { if !self.test_submodule_short_line() || self.config.color_only { return Ok(false); }- if let Some(commit) = get_submodule_short_commit(&self.line) {- if let State::HunkHeader(_, _, _, _) = self.state {+ if let Some((commit, dirty)) = get_submodule_short_commit(&self.line) {+ if let State::HunkHeader(_, parsed_hunk_header, _, _) = &self.state {+ // The row drawn below replaces the hunk, hunk header included,+ // so it carries the hunk header's record.+ if let Some(md) = self.painter.diff_line_metadata.as_mut() {+ let file = if self.plus_file == "/dev/null" {+ &self.minus_file+ } else {+ &self.plus_file+ };+ md.initialize_hunk(+ &parsed_hunk_header.line_numbers_and_hunk_lengths,+ file.clone(),+ );+ } self.state = State::SubmoduleShort(commit.to_owned()); } else if let State::SubmoduleShort(minus_commit) = &self.state { self.painter.emit()?;- writeln!(- self.painter.writer,- "{}..{}",- self.config- .minus_style- .paint(minus_commit.chars().take(12).collect::<String>()),- self.config- .plus_style- .paint(commit.chars().take(12).collect::<String>()),- )?;+ // git writes "-dirty" after the new commit when the submodule's+ // working tree has uncommitted changes or untracked files. When+ // that is the only change, the two commits are the same.+ let plus_commit = self+ .config+ .plus_style+ .paint(commit.chars().take(12).collect::<String>());+ let range = if minus_commit == commit {+ plus_commit.to_string()+ } else {+ format!(+ "{}..{}",+ self.config+ .minus_style+ .paint(minus_commit.chars().take(12).collect::<String>()),+ plus_commit,+ )+ };+ let dirty = if dirty { " (dirty)" } else { "" };+ let osc = self+ .painter+ .diff_line_metadata+ .as_ref()+ .map_or(String::new(), |md| md.osc_for_hunk_header());+ writeln!(self.painter.writer, "{osc}{range}{dirty}")?; } } Ok(true)@@ -86,11 +114,12 @@ lazy_static! { ).unwrap(); } -pub fn get_submodule_short_commit(line: &str) -> Option<&str> {- match SUBMODULE_SHORT_LINE_REGEX.captures(line) {- Some(caps) => Some(caps.get(1).unwrap().as_str()),- None => None,- }+/// The commit a "Subproject commit" row states, and whether the row marks the+/// submodule's working tree as dirty.+pub fn get_submodule_short_commit(line: &str) -> Option<(&str, bool)> {+ SUBMODULE_SHORT_LINE_REGEX+ .captures(line)+ .map(|caps| (caps.get(1).unwrap().as_str(), caps.get(2).is_some())) } /// The submodule a "Submodule …" row names, for the rows that are the header of@@ -147,4 +176,17 @@ mod tests { assert_eq!(get_submodule_log_path(line), expected, "{line}"); } }++ #[test]+ fn test_get_submodule_short_commit_reports_dirty_state() {+ const COMMIT: &str = "ca030fd1a02225a6fc1a834c480276d9c97a8c6f";++ let line = format!("-Subproject commit {COMMIT}");+ assert_eq!(get_submodule_short_commit(&line), Some((COMMIT, false)));++ let line = format!("+Subproject commit {COMMIT}-dirty");+ assert_eq!(get_submodule_short_commit(&line), Some((COMMIT, true)));++ assert_eq!(get_submodule_short_commit("Subproject commit nope"), None);+ } }
src/tests/test_example_diffs.rs
@@ -310,10 +310,38 @@ index 0123456..1234567 100644 r#" some_submodule ──────────────────────────────+ ca030fd1a022..803be42ca46a (dirty)"#,+ );+ }++ #[test]+ fn test_simple_clean_submodule_diff() {+ DeltaTest::with_args(&["--width", "30"])+ .with_input(SUBMODULE_CLEAN)+ .inspect()+ .expect_after_skip(+ 1,+ r#"+ some_submodule+ ────────────────────────────── ca030fd1a022..803be42ca46a"#, ); } + #[test]+ fn test_dirty_submodule_at_same_commit_diff() {+ DeltaTest::with_args(&["--width", "30"])+ .with_input(SUBMODULE_DIRTY_AT_SAME_COMMIT)+ .inspect()+ .expect_after_skip(+ 1,+ r#"+ some_submodule+ ──────────────────────────────+ ca030fd1a022 (dirty)"#,+ );+ }+ #[test] fn test_submodule_diff_log() { // See etc/examples/662-submodules@@ -2705,6 +2733,25 @@ index ca030fd1a0..803be42ca4 160000 @@ -1 +1 @@ -Subproject commit ca030fd1a02225a6fc1a834c480276d9c97a8c6f +Subproject commit 803be42ca46af0fbc65b54a9abfb499389516939-dirty+";++ const SUBMODULE_CLEAN: &str = "\+diff --git a/some_submodule b/some_submodule+index ca030fd1a0..803be42ca4 160000+--- a/some_submodule++++ b/some_submodule+@@ -1 +1 @@+-Subproject commit ca030fd1a02225a6fc1a834c480276d9c97a8c6f++Subproject commit 803be42ca46af0fbc65b54a9abfb499389516939+";++ const SUBMODULE_DIRTY_AT_SAME_COMMIT: &str = "\+diff --git a/some_submodule b/some_submodule+--- a/some_submodule++++ b/some_submodule+@@ -1 +1 @@+-Subproject commit ca030fd1a02225a6fc1a834c480276d9c97a8c6f++Subproject commit ca030fd1a02225a6fc1a834c480276d9c97a8c6f-dirty "; // See etc/examples/662-submodules