From b80bfda68fe5f01f5163fa4877f45dd4334c6abb Mon Sep 17 00:00:00 2001 From: Andreas Gruenbacher Date: Sat, 14 Mar 2009 16:24:09 +0100 Subject: [PATCH] Make "patch -r rejfile" work even when there are several rejects With a patch that includes rejects in more than one file and with the -r option, rejects would overwrite themselves and only the rejects from the last file would remain. Fix this. --- ChangeLog | 13 ++++++ Makefile.in | 2 +- patch.c | 18 ++++++-- patch.man | 2 +- tests/global-reject-files | 90 +++++++++++++++++++++++++++++++++++++++ util.c | 35 ++++++++++++--- util.h | 1 + 7 files changed, 149 insertions(+), 12 deletions(-) create mode 100755 tests/global-reject-files diff --git a/ChangeLog b/ChangeLog index dac3f20..b761b20 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,16 @@ +2009-03-21 Andreas Gruenbacher + + * patch.c (main): With -r, instead of moving the global reject + file into place, copy the first reject into the file, and + append all the rest. Discard rejects with -r -. + * patch.man: Document the latter. + * util.c (copy_to_fd): New function. + (copy_file): Use that. + (append_to_file): New function. + * util.h (append_to_file): Add function declaration. + * tests/global-reject-files: New test case. + * Makefile.in (TESTS): Add test case. + 2009-03-20 Andreas Gruenbacher * patch.c: New option --reject-format=FORMAT. diff --git a/Makefile.in b/Makefile.in index fec3514..a7d0d7e 100644 --- a/Makefile.in +++ b/Makefile.in @@ -88,7 +88,7 @@ HDRS = argmatch.h backupfile.h common.h dirname.h \ MISC = AUTHORS COPYING ChangeLog INSTALL Makefile.in NEWS README VERSION \ aclocal.m4 config.hin configure configure.ac install-sh \ mkinstalldirs patch.man stdbool_.h timespec.h -TESTS = tests/corrupt-reject-files tests/crlf-handling \ +TESTS = tests/corrupt-reject-files tests/crlf-handling tests/global-reject-files \ tests/no-newline-triggers-assert tests/preserve-c-function-names \ tests/preserve-mode-and-timestamp \ tests/reject-format tests/remember-backup-files diff --git a/patch.c b/patch.c index 7a89e71..3f66d32 100644 --- a/patch.c +++ b/patch.c @@ -124,6 +124,7 @@ main (int argc, char **argv) struct outstate outstate; struct stat outst; char numbuf[LINENUM_LENGTH_BOUND + 1]; + bool written_to_rejname = false; exit_failure = 2; program_name = argv[0]; @@ -427,7 +428,7 @@ main (int argc, char **argv) somefailed = true; say ("%d out of %d hunk%s %s", failed, hunk, "s" + (hunk == 1), skip_rest_of_patch ? "ignored" : "FAILED"); - if (outname) { + if (outname && (! rejname || strcmp (rejname, "-") != 0)) { char *rej = rejname; if (!rejname) { rej = xmalloc (strlen (outname) + 5); @@ -437,8 +438,19 @@ main (int argc, char **argv) say (" -- saving rejects to file %s", quotearg (rej)); if (! dry_run) { - move_file (TMPREJNAME, &TMPREJNAME_needs_removal, 0, - rej, 0666, false); + if (rejname) + { + if (! written_to_rejname) + { + copy_file (TMPREJNAME, rejname, 0, 0, 0666); + written_to_rejname = true; + } + else + append_to_file (TMPREJNAME, rejname); + } + else + move_file (TMPREJNAME, &TMPREJNAME_needs_removal, 0, + rej, 0666, false); } if (!rejname) free (rej); diff --git a/patch.man b/patch.man index a31e78c..a0359ba 100644 --- a/patch.man +++ b/patch.man @@ -514,7 +514,7 @@ Put rejects into .I rejectfile instead of the default .B \&.rej -file. +file. When \fIrejectfile\fP is \fB\-\fP, discard rejects. .TP \fB\-R\fP or \fB\*=reverse\fP Assume that this patch was created with the old and new files swapped. diff --git a/tests/global-reject-files b/tests/global-reject-files new file mode 100755 index 0000000..be06bec --- /dev/null +++ b/tests/global-reject-files @@ -0,0 +1,90 @@ +#! /bin/bash + +# Copyright (C) 2009 Free Software Foundation, Inc. +# +# Copying and distribution of this file, with or without modification, +# in any medium, are permitted without royalty provided the copyright +# notice and this notice are preserved. + +# More than one reject to + +. $srcdir/test-lib.sh + +require_cat +require_diff +use_local_patch +use_tmpdir + +# ============================================================== + +cat > ab.diff < a +echo three > b + +check 'patch -p0 < ab.diff' <