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 /src/patch.c | |
| 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.
Diffstat (limited to 'src/patch.c')
| -rw-r--r-- | src/patch.c | 34 |
1 files changed, 32 insertions, 2 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 |
