Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/params.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ pub struct Params {
pub expand_tabs: bool,
pub ignore_tab_expansion: bool,
pub suppress_blank_empty: bool,
pub initial_tab: bool,
pub tabsize: usize,
pub width: usize,
pub missing_file: MissingFile,
Expand Down Expand Up @@ -103,6 +104,7 @@ impl Default for Params {
expand_tabs: false,
ignore_tab_expansion: false,
suppress_blank_empty: false,
initial_tab: false,
tabsize: 8,
width: 130,
missing_file: MissingFile::default(),
Expand Down Expand Up @@ -281,6 +283,10 @@ pub fn parse_params<I: Iterator<Item = OsString>>(
params.ignore_tab_expansion = true;
continue;
}
if param == "-T" || param == "--initial-tab" {
params.initial_tab = true;
continue;
}
if param == "--suppress-blank-empty" {
params.suppress_blank_empty = true;
continue;
Expand Down
23 changes: 22 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,15 +167,36 @@ impl<'a> PartialEq for ComparisonLine<'a> {

/// The marker to write before a line of the normal, context or unified
/// format: `prefix`, without its trailing blanks if `line` is empty and
/// `--suppress-blank-empty` is given.
/// `--suppress-blank-empty` is given, or ending in a tab instead of a space
/// with `-T` / `--initial-tab`.
pub fn line_prefix<'a>(prefix: &'a [u8], line: &[u8], params: &Params) -> &'a [u8] {
if params.suppress_blank_empty && line.is_empty() {
prefix.trim_ascii_end()
} else if params.initial_tab {
initial_tab_prefix(prefix)
} else {
prefix
}
}

/// `prefix` with its trailing space turned into a tab, or a tab added after
/// it, as GNU diff writes it with `-T`: `< ` becomes `<\t`, the unified
/// context marker ` ` becomes `\t` and `+` becomes `+\t`.
fn initial_tab_prefix(prefix: &[u8]) -> &[u8] {
match prefix {
b"< " => b"<\t",
b"> " => b">\t",
b" " => b" \t",
b"! " => b"!\t",
b"+ " => b"+\t",
b"- " => b"-\t",
b" " => b"\t",
b"+" => b"+\t",
b"-" => b"-\t",
_ => prefix,
}
}

/// Quotes a file name for the headers of the diff output: a name containing
/// spaces, double quotes, backslashes, control characters or non-ASCII bytes
/// is written between double quotes with C-style escapes, other names are
Expand Down
36 changes: 36 additions & 0 deletions tests/by-util/test_diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,42 @@ fn no_differences_side_by_side() {
.stdout_is("foo\t\t foo\nbar\t\t bar\n");
}

// -T puts a tab between the line marker and the line, as GNU diff does.
#[rstest]
#[case::normal(&["-T"], "2c2\n<\tb\n---\n>\tB\n4a5\n>\td\n")]
#[case::normal_long(&["--initial-tab"], "2c2\n<\tb\n---\n>\tB\n4a5\n>\td\n")]
#[case::unified(&["-T", "-u"], "@@ -1,4 +1,5 @@\n\ta\n-\tb\n+\tB\n\t\n\tc\tx\n+\td\n")]
#[case::unified_suppress_blank_empty(
&["-T", "-u", "--suppress-blank-empty"],
"@@ -1,4 +1,5 @@\n\ta\n-\tb\n+\tB\n\n\tc\tx\n+\td\n"
)]
#[case::unified_expand_tabs(&["-T", "-t", "-u"], "@@ -1,4 +1,5 @@\n\ta\n-\tb\n+\tB\n\t\n\tc x\n+\td\n")]
#[case::context(
&["-T", "-c"],
"***************\n*** 1,4 ****\n \ta\n!\tb\n \t\n \tc\tx\n--- 1,5 ----\n \ta\n!\tB\n \t\n \tc\tx\n+\td\n"
)]
#[case::ed(&["-T", "-e"], "4a\nd\n.\n2c\nB\n.\n")]
fn initial_tab(#[case] args: &[&str], #[case] expected: &str) {
let (at, ts) = at_and_ts!();
at.write("file1", "a\nb\n\nc\tx\n");
at.write("file2", "a\nB\n\nc\tx\nd\n");
let result = ts
.ucmd()
.args(args)
.args(&["file1", "file2"])
.fails_with_code(1);
// drop the file headers of the unified and context formats
let stdout = result.stdout_str();
let body = if args.contains(&"-u") {
stdout.splitn(3, '\n').nth(2).unwrap()
} else if args.contains(&"-c") {
stdout.splitn(3, '\n').nth(2).unwrap()
} else {
stdout
};
assert_eq!(body, expected);
}

#[rstest]
fn differences(#[values(&[][..], &["-u"], &["-c"], &["-e"])] format: &[&str]) {
let (at, ts) = at_and_ts!();
Expand Down