diff options
author | Andreas Gruenbacher <agruen@linbit.com> | 2012-09-22 19:55:42 +0200 |
---|---|---|
committer | Andreas Gruenbacher <agruen@linbit.com> | 2012-09-22 20:02:48 +0200 |
commit | db1bd7f6934cd8d04ec1fb6347a5571c2e7e1234 (patch) | |
tree | 6630fbbe77875d99a5a771974fe3a4475307c9b0 | |
parent | d24f630fbcaf27416fa81b3bcd686df4344df978 (diff) | |
download | patch-db1bd7f6934cd8d04ec1fb6347a5571c2e7e1234.tar.gz |
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.
-rw-r--r-- | src/patch.c | 34 | ||||
-rw-r--r-- | src/pch.c | 2 | ||||
-rw-r--r-- | 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 @@ -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' <<EOF -(Stripping trailing CRs from patch.) +(Stripping trailing CRs from patch; use --binary to disable.) patching file c EOF diff -c a b | lf2crlf > ab.diff echo 1 > c check 'patch c < ab.diff' <<EOF -(Stripping trailing CRs from patch.) +(Stripping trailing CRs from patch; use --binary to disable.) patching file c EOF diff -u a b | lf2crlf > ab.diff echo 1 > c check 'patch c < ab.diff' <<EOF -(Stripping trailing CRs from patch.) +(Stripping trailing CRs from patch; use --binary to disable.) patching file c EOF @@ -109,8 +109,16 @@ echo 1b > b diff a b | lf2crlf > ab.diff cp a c + +check 'patch --binary c < ab.diff || echo status: $?' <<EOF +patching file c +Hunk #1 FAILED at 1 (different line endings). +1 out of 1 hunk FAILED -- saving rejects to file c.rej +status: 1 +EOF + check 'patch c < ab.diff' <<EOF -(Stripping trailing CRs from patch.) +(Stripping trailing CRs from patch; use --binary to disable.) patching file c EOF @@ -132,7 +140,7 @@ EOF diff -u a b | lf2crlf > ab.diff cp a c check 'patch c < ab.diff' <<EOF -(Stripping trailing CRs from patch.) +(Stripping trailing CRs from patch; use --binary to disable.) patching file c EOF @@ -154,7 +162,7 @@ EOF diff -c a b | lf2crlf > ab.diff cp a c check 'patch c < ab.diff' <<EOF -(Stripping trailing CRs from patch.) +(Stripping trailing CRs from patch; use --binary to disable.) patching file c EOF |