# Copyright (C) 2009, 2011-2012 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.

. $srcdir/test-lib.sh

require cat
require sed
use_local_patch
use_tmpdir

# ==============================================================

# Bug #25970: intuit_diff_type: Assertion `i0 != NONE' failed.

cat > f.diff <<EOF
--- f
+++ f
@@ -1 +0,0 @@
-one
EOF
echo one > f

check 'patch -f -p1 < f.diff || echo "Status: $?"' <<EOF
can't find file to patch at input line 3
Perhaps you used the wrong -p or --strip option?
The text leading up to this was:
--------------------------
|--- f
|+++ f
--------------------------
No file to patch.  Skipping patch.
1 out of 1 hunk ignored
Status: 1
EOF

# ==============================================================

cat > f.diff <<EOF
--- /dev/null
+++ f
@@ -0,0 +1 @@
+one
EOF
echo one > f

check 'patch -f < f.diff || echo "Status: $?"' <<EOF
The next patch would create the file f,
which already exists!  Applying it anyway.
patching file f
Hunk #1 FAILED at 1.
1 out of 1 hunk FAILED -- saving rejects to file f.rej
Status: 1
EOF

# ==============================================================

cat > f.diff <<EOF
--- f
+++ /dev/null
@@ -1 +0,0 @@
-one
EOF
rm -f f

preserve_trailing_blank=

check 'patch -p0 < f.diff || echo "Status: $?"' <<EOF
The next patch would delete the file f,
which does not exist!  Assume -R? [n] $preserve_trailing_blank
Apply anyway? [n] $preserve_trailing_blank
Skipping patch.
1 out of 1 hunk ignored
Status: 1
EOF

# --------------------------------------------------------------

rm -f f

check 'patch -p0 -f < f.diff || echo "Status: $?"' <<EOF
The next patch would delete the file f,
which does not exist!  Applying it anyway.
patching file f
Hunk #1 FAILED at 1.
1 out of 1 hunk FAILED -- saving rejects to file f.rej
Status: 1
EOF

# --------------------------------------------------------------
# Check the file mode of backups of new files

umask 022
rm -f f.orig f

check 'patch -f f < f.diff || echo "Status: $?"' <<EOF
The next patch would delete the file f,
which does not exist!  Applying it anyway.
patching file f
Hunk #1 FAILED at 1.
1 out of 1 hunk FAILED -- saving rejects to file f.rej
Status: 1
EOF

ncheck '! test -s f.orig'

# ==============================================================

cat > f.diff <<EOF
--- f.orig
+++ f
@@ -1 +0,0 @@
-f
EOF
echo f > f.orig
: > f

check 'patch -f < f.diff || echo "Status: $?"' <<EOF
The next patch would empty out the file f,
which is already empty!  Applying it anyway.
patching file f
Hunk #1 FAILED at 1.
1 out of 1 hunk FAILED -- saving rejects to file f.rej
Status: 1
EOF

# ==============================================================

cat > f.diff <<EOF
--- f
+++ f.orig
@@ -1 +0,0 @@
-f
EOF
rm -f f.orig
: > f

check 'patch -p0 -t < f.diff || echo "Status: $?"' <<EOF
The next patch would empty out the file f,
which is already empty!  Assuming -R.
patching file f
EOF

check 'cat f' <<EOF
f
EOF

# --------------------------------------------------------------

cat > f.diff <<EOF
--- f
+++ /dev/null
@@ -1 +0,0 @@
-f
EOF
rm -f f.orig
: > f

# FIXME: "The next patch would delete the file f" would be better here
check 'patch -p0 -t < f.diff || echo "Status: $?"' <<EOF
The next patch would empty out the file f,
which is already empty!  Assuming -R.
patching file f
EOF

check 'cat f' <<EOF
f
EOF

# ================================================================
# File expected to become empty doesn't become empty

seq 2 > target
cat > p.diff <<EOF
--- target
+++ /dev/null
@@ -1 +0,0 @@
-1
EOF

check 'patch -p0 < p.diff || echo status: $?' <<EOF
patching file target
Not deleting file target as content differs from patch
status: 1
EOF

check 'cat target' <<EOF
2
EOF

# --------------------------------------------------------------

seq 2 > target
cat > p.diff <<EOF
--- target	2012-09-25 03:09:15
+++ target	1970-01-01 00:00:00
@@ -1 +0,0 @@
-1
EOF

check 'patch -p0 < p.diff || echo status: $?' <<EOF
patching file target
Not deleting file target as content differs from patch
status: 1
EOF

check 'cat target' <<EOF
2
EOF

# --------------------------------------------------------------
printf '\0' > target
if ! test -s target; then
    echo "Failed to printf a null character; skipping this test"
else
    cat > p.diff <<EOF
diff --git a/target b/target
deleted file mode 100644
index f76dd23..0000000
Binary files a/target and /dev/null differ
EOF

    check 'patch -p1 -b < p.diff || echo status: $?' <<EOF
patching file target
Not deleting file target as content differs from patch
status: 1
EOF

    ncheck 'test -e target'

# Patch creates a backup file even when the original file remains unchanged:
    ncheck 'test -e target.orig'
fi

# ================================================================
# File not expected to become empty does become empty

echo 1 > target
cat > p.diff <<EOF
--- target
+++ target
@@ -1 +0,0 @@
-1
EOF

check 'patch -p0 < p.diff || echo status: $?' <<EOF
patching file target
EOF

check 'cat target' <<EOF
EOF

echo 1 > target
check 'patch -p0 -E < p.diff || echo status: $?' <<EOF
patching file target
EOF

ncheck 'test ! -e target'

# ================================================================
# File expected to become empty does become empty

echo data > target
cat > p.diff <<EOF
diff --git a/target b/target
deleted file mode 100644
index 1269488..0000000
--- a/target
+++ /dev/null
@@ -1 +0,0 @@
-data
EOF

check 'patch -p1 -b -f < p.diff || echo status: $?' <<EOF
patching file target
EOF

ncheck 'test ! -e target'

check 'cat target.orig' <<EOF
data
EOF

echo data > target

check 'patch -p1 -b --posix -f < p.diff || echo status: $?' <<EOF
patching file target
EOF

check 'cat target' <<EOF
EOF

check 'cat target.orig' <<EOF
data
EOF
