# 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
#
# ----------------------------------------------------------------

# This test deletes the target file.

echo data > target
cat > p.diff <<EOF
diff --git a/target b/target
index 1..0
EOF

check 'patch -p1 -b < p.diff || echo status: $?' <<EOF
patching file target
File target is not empty after patch, as expected
EOF

ncheck 'test ! -e target'

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