Skip to content

Commit

Permalink
Add --strip-cr
Browse files Browse the repository at this point in the history
  • Loading branch information
Wilfred committed Oct 10, 2023
1 parent b0dac91 commit 67dbf0e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 0.53 (unreleased)

### Command Line Interface

Added the option `--strip-cr`. This removes all carriage return
characters before diffing, which is helpful when dealing with a mix of
Windows and non-Windows flies.

## 0.52 (released 8th October 2023)

### Parsing
Expand Down
13 changes: 11 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ fn diff_file(
overrides: &[(LanguageOverride, Vec<glob::Pattern>)],
) -> DiffResult {
let (lhs_bytes, rhs_bytes) = read_files_or_die(lhs_path, rhs_path, missing_as_empty);
let (lhs_src, rhs_src) = match (guess_content(&lhs_bytes), guess_content(&rhs_bytes)) {
let (mut lhs_src, mut rhs_src) = match (guess_content(&lhs_bytes), guess_content(&rhs_bytes)) {
(ProbableFileKind::Binary, _) | (_, ProbableFileKind::Binary) => {
return DiffResult {
extra_info,
Expand All @@ -353,6 +353,11 @@ fn diff_file(
(ProbableFileKind::Text(lhs_src), ProbableFileKind::Text(rhs_src)) => (lhs_src, rhs_src),
};

if diff_options.strip_cr {
lhs_src.retain(|c| c != '\r');
rhs_src.retain(|c| c != '\r');
}

diff_file_content(
display_path,
extra_info,
Expand All @@ -374,14 +379,18 @@ fn diff_conflicts_file(
overrides: &[(LanguageOverride, Vec<glob::Pattern>)],
) -> DiffResult {
let bytes = read_file_or_die(path);
let src = match guess_content(&bytes) {
let mut src = match guess_content(&bytes) {
ProbableFileKind::Text(src) => src,
ProbableFileKind::Binary => {
eprintln!("error: Expected a text file with conflict markers, got a binary file.");
std::process::exit(EXIT_BAD_ARGUMENTS);
}
};

if diff_options.strip_cr {
src.retain(|c| c != '\r');
}

let conflict_files = match apply_conflict_markers(&src) {
Ok(cf) => cf,
Err(msg) => {
Expand Down
10 changes: 10 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ pub struct DiffOptions {
pub parse_error_limit: usize,
pub check_only: bool,
pub ignore_comments: bool,
pub strip_cr: bool,
}

impl Default for DiffOptions {
Expand All @@ -78,6 +79,7 @@ impl Default for DiffOptions {
parse_error_limit: DEFAULT_PARSE_ERROR_LIMIT,
check_only: false,
ignore_comments: false,
strip_cr: false,
}
}
}
Expand Down Expand Up @@ -199,6 +201,11 @@ json: Output the results as a machine-readable JSON array with an element per fi
.env("DFT_EXIT_CODE")
.help("Set the exit code to 1 if there are syntactic changes in any files. For files where there is no detected language (e.g. unsupported language or binary files), sets the exit code if there are any byte changes.")
)
.arg(
Arg::new("strip-cr").long("strip-cr")
.env("DFT_STRIP_CR")
.help("Remove any carriage return characters before diffing. This can be helpful when dealing with files on Windows that contain CRLF, i.e. `\\r\\n`.")
)
.arg(
Arg::new("check-only").long("check-only")
.env("DFT_CHECK_ONLY")
Expand Down Expand Up @@ -602,6 +609,8 @@ pub fn parse_args() -> Mode {

let set_exit_code = matches.is_present("exit-code");

let strip_cr = matches.is_present("strip-cr");

let check_only = matches.is_present("check-only");

let diff_options = DiffOptions {
Expand All @@ -610,6 +619,7 @@ pub fn parse_args() -> Mode {
parse_error_limit,
check_only,
ignore_comments,
strip_cr,
};

let args: Vec<_> = matches.values_of_os("paths").unwrap_or_default().collect();
Expand Down

0 comments on commit 67dbf0e

Please sign in to comment.