From db1bd7f6934cd8d04ec1fb6347a5571c2e7e1234 Mon Sep 17 00:00:00 2001 From: Andreas Gruenbacher Date: Sat, 22 Sep 2012 19:55:42 +0200 Subject: Improve handling of LF vs. CRLF line endings * src/patch.c (check_line_endings): New function. (main): When a hunk fails, report when the line endings differ between the input file and the patch. * src/pch.c (there_is_another_patch): When saying that we strip trailing CRs, also say how to turn this off. * tests/crlf-handling: Update changed messages. Add test case that fails. --- src/patch.c | 34 ++++++++++++++++++++++++++++++++-- src/pch.c | 2 +- tests/crlf-handling | 20 ++++++++++++++------ 3 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/patch.c b/src/patch.c index 8b2f8c0..97eea5b 100644 --- a/src/patch.c +++ b/src/patch.c @@ -38,6 +38,7 @@ static FILE *create_output_file (char const *, int); static lin locate_hunk (lin); +static bool check_line_endings (lin); static bool apply_hunk (struct outstate *, lin); static bool patch_match (lin, lin, lin, lin); static bool spew_output (struct outstate *, struct stat *); @@ -445,9 +446,11 @@ main (int argc, char **argv) failed++; if (verbosity == VERBOSE || (! skip_rest_of_patch && verbosity != SILENT)) - say ("Hunk #%d %s at %s.\n", hunk, + say ("Hunk #%d %s at %s%s.\n", hunk, skip_rest_of_patch ? "ignored" : "FAILED", - format_linenum (numbuf, newwhere)); + format_linenum (numbuf, newwhere), + ! skip_rest_of_patch && check_line_endings (newwhere) + ? " (different line endings)" : ""); } else if (! merge && (verbosity == VERBOSE @@ -1659,6 +1662,33 @@ patch_match (lin base, lin offset, lin prefix_fuzz, lin suffix_fuzz) return true; } +/* Check if the line endings in the input file and in the patch differ. */ + +static bool +check_line_endings (lin where) +{ + char const *p; + size_t size; + bool input_crlf, patch_crlf; + + p = pfetch (1); + size = pch_line_len (1); + if (! size) + return false; + patch_crlf = size >= 2 && p[size - 2] == '\r' && p[size - 1] == '\n'; + + if (! input_lines) + return false; + if (where > input_lines) + where = input_lines; + p = ifetch (where, false, &size); + if (! size) + return false; + input_crlf = size >= 2 && p[size - 2] == '\r' && p[size - 1] == '\n'; + + return patch_crlf != input_crlf; +} + /* Do two lines match with canonicalized white space? */ bool diff --git a/src/pch.c b/src/pch.c index 3616bc8..75a642b 100644 --- a/src/pch.c +++ b/src/pch.c @@ -266,7 +266,7 @@ there_is_another_patch (bool need_header, mode_t *file_type) say ("(Patch is indented %lu space%s.)\n", (unsigned long int) p_indent, p_indent==1?"":"s"); if (p_strip_trailing_cr) - say ("(Stripping trailing CRs from patch.)\n"); + say ("(Stripping trailing CRs from patch; use --binary to disable.)\n"); if (! inname) { char numbuf[LINENUM_LENGTH_BOUND + 1]; diff --git a/tests/crlf-handling b/tests/crlf-handling index b86bf38..b704dc8 100644 --- a/tests/crlf-handling +++ b/tests/crlf-handling @@ -23,21 +23,21 @@ echo 1b > b diff a b | lf2crlf > ab.diff echo 1 > c check 'patch c < ab.diff' < ab.diff echo 1 > c check 'patch c < ab.diff' < ab.diff echo 1 > c check 'patch c < ab.diff' < b diff a b | lf2crlf > ab.diff cp a c + +check 'patch --binary c < ab.diff || echo status: $?' < ab.diff cp a c check 'patch c < ab.diff' < ab.diff cp a c check 'patch c < ab.diff' <