patch: fix --no-backup-if-mismatch regression

Problem reported by Sam James in:
https://lists.gnu.org/archive/html/bug-patch/2025-02/msg00014.html
https://bugs.gentoo.org/show_bug.cgi?id=949834
* src/patch.c (backup_if_mismatch_specified): New static var.
(get_some_switches): Set it.
(main): Default backup_if_mismatch only if not set on command line.
* tests/no-backup: New file.
* tests/Makefile.am (TESTS): Add it.
This commit is contained in:
Paul Eggert 2025-02-24 22:59:51 -08:00
parent 86ac7e2d7b
commit b5d2124e2e
3 changed files with 62 additions and 1 deletions

View File

@ -118,6 +118,7 @@ static bool merge;
static enum diff reject_format = NO_DIFF; /* automatic */
static bool make_backups;
static bool backup_if_mismatch;
static bool backup_if_mismatch_specified;
static char const *version_control;
static char const *version_control_context;
static bool remove_empty_files;
@ -196,7 +197,8 @@ main (int argc, char **argv)
if (set_utc && setenv ("TZ", "UTC0", 1) < 0)
pfatal ("setenv");
backup_if_mismatch = ! posixly_correct;
if (!backup_if_mismatch_specified)
backup_if_mismatch = !posixly_correct;
if (make_backups | backup_if_mismatch)
backup_type = get_version (version_control_context, version_control);
@ -1050,9 +1052,11 @@ get_some_switches (int argc, char **argv)
usage (stdout, EXIT_SUCCESS);
case CHAR_MAX + 5:
backup_if_mismatch = true;
backup_if_mismatch_specified = true;
break;
case CHAR_MAX + 6:
backup_if_mismatch = false;
backup_if_mismatch_specified = true;
break;
case CHAR_MAX + 7:
posixly_correct = true;

View File

@ -50,6 +50,7 @@ TESTS = \
mixed-patch-types \
munged-context-format \
need-filename \
no-backup \
no-mode-change-git-diff \
no-newline-triggers-assert \
preserve-c-function-names \

56
tests/no-backup Normal file
View File

@ -0,0 +1,56 @@
# Copyright 2025 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.
# Test the --no-backup-if-mismatch option
. $srcdir/test-lib.sh
require cat
use_local_patch
use_tmpdir
# ==============================================================
cat >my_file <<'EOF'
/* ... */
void baz();
void baz() {
/* ... */
}
int main() {
int foo;
int bar;
/* ... */
baz();
}
EOF
cat >my_file.patch <<'EOF'
--- my_file 2025-02-16 11:22:12.881765792 +0000
+++ my_file_new 2025-02-16 11:22:12.881796732 +0000
@@ -2,7 +2,7 @@
void baz();
void baz() {
- /* ... */
+ // ...
}
int main() {
EOF
unset POSIXLY_CORRECT
check 'patch -N --no-backup-if-mismatch <my_file.patch || echo "Status: $?"' <<'EOF'
patching file my_file
Hunk #1 succeeded at 3 with fuzz 1 (offset 1 line).
EOF
ncheck 'test ! -f my_file.orig'