Skip to content

Commit

Permalink
Put file-modified label in front of mode changes.
Browse files Browse the repository at this point in the history
When using `--navigate` with files that get their mode modified, the
file-modified label was not being prefixed, so these changes were not
being marked as skip destinations.  This is particularly bad if a file
has line changes along with the mode change AND the user has an empty
hunk label (since that would make the entire file's changes get skipped
with an "n").

I added a test of a mode-change with a line-change, and a test for a
mode change where the old & new mode values are not one of the two
expected value-pairs.  I also used format_file() on the mode filenames
since the other format() calls were using it.
  • Loading branch information
WayneD authored and dandavison committed Jan 1, 2022
1 parent 13f60da commit 50ece4b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
20 changes: 16 additions & 4 deletions src/handlers/diff_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,11 +334,23 @@ pub fn get_file_change_description_from_file_paths(
) if minus_file == plus_file => match (old_mode.as_str(), new_mode.as_str()) {
// 100755 for executable and 100644 for non-executable are the only file modes Git records.
// https://medium.com/@tahteche/how-git-treats-changes-in-file-permissions-f71874ca239d
("100644", "100755") => format!("{}: mode +x", plus_file),
("100755", "100644") => format!("{}: mode -x", plus_file),
("100644", "100755") => format!(
"{}{}: mode +x",
format_label(&config.file_modified_label),
format_file(plus_file)
),
("100755", "100644") => format!(
"{}{}: mode -x",
format_label(&config.file_modified_label),
format_file(plus_file)
),
_ => format!(
"{}: {} {} {}",
plus_file, old_mode, config.right_arrow, new_mode
"{}{}: {} {} {}",
format_label(&config.file_modified_label),
format_file(plus_file),
old_mode,
config.right_arrow,
new_mode
),
},
(minus_file, plus_file, _, _) if minus_file == plus_file => format!(
Expand Down
53 changes: 53 additions & 0 deletions src/tests/test_example_diffs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod tests {
use crate::tests::ansi_test_utils::ansi_test_utils;
use crate::tests::integration_test_utils;
use crate::tests::test_utils;
use regex::Regex;

#[test]
fn test_added_file() {
Expand Down Expand Up @@ -1606,6 +1607,40 @@ src/align.rs:71: impl<'a> Alignment<'a> { │
assert!(output.contains(r"src/delta.rs: mode -x"));
}

#[test]
fn test_file_mode_change_unexpected_bits() {
let config =
integration_test_utils::make_config_from_args(&["--navigate", "--right-arrow=->"]);
let output =
integration_test_utils::run_delta(GIT_DIFF_FILE_MODE_CHANGE_UNEXPECTED_BITS, &config);
let output = strip_ansi_codes(&output);
assert!(output.contains(r"Δ src/delta.rs: 100700 -> 100644"));
}

#[test]
fn test_file_mode_change_with_diff() {
let config = integration_test_utils::make_config_from_args(&[
"--navigate",
"--keep-plus-minus-markers",
]);
let output =
integration_test_utils::run_delta(GIT_DIFF_FILE_MODE_CHANGE_WITH_DIFF, &config);
let output = strip_ansi_codes(&output);
let re = Regex::new(r"\n─+\n").unwrap();
let output = re.replace(&output, "\n-----\n");
assert!(output.contains(
"Δ src/script: mode +x
-----
─────┐
• 1: │
─────┘
-#!/bin/sh
+#!/bin/bash
"
));
}

#[test]
fn test_hyperlinks_commit_link_format() {
let config = integration_test_utils::make_config_from_args(&[
Expand Down Expand Up @@ -2316,6 +2351,24 @@ new mode 100755
diff --git a/src/delta.rs b/src/delta.rs
old mode 100755
new mode 100644
";

const GIT_DIFF_FILE_MODE_CHANGE_UNEXPECTED_BITS: &str = "
diff --git a/src/delta.rs b/src/delta.rs
old mode 100700
new mode 100644
";

const GIT_DIFF_FILE_MODE_CHANGE_WITH_DIFF: &str = "
diff --git a/src/script b/src/script
old mode 100644
new mode 100755
index d00491f..0cfbf08 100644
--- a/src/script
+++ b/src/script
@@ -1 +1 @@
-#!/bin/sh
+#!/bin/bash
";

const GIT_DIFF_NO_INDEX_FILENAMES_WITH_SPACES: &str = "
Expand Down

0 comments on commit 50ece4b

Please sign in to comment.