git diffs: Support file copies and renames

* src/patch.c (main): Support git diffs which copy or rename files.
* tests/copy-rename: New test case.
* tests/Makefile.am (TESTS): Add test case.
This commit is contained in:
Andreas Gruenbacher 2010-04-29 15:48:57 +02:00
parent 502d77b81b
commit 9e39d9c995
4 changed files with 157 additions and 2 deletions

View File

@ -1,5 +1,9 @@
2009-04-29 Andreas Gruenbacher <agruen@suse.de>
* src/patch.c (main): Support git diffs which copy or rename files.
* tests/copy-rename: New test case.
* tests/Makefile.am (TESTS): Add test case.
* src/patch.c (main): Convert outname into a char const *.
* src/util.ch (create_backup, move_file, removedirs): Take char const *
filenames.

View File

@ -194,7 +194,13 @@ main (int argc, char **argv)
if (! skip_rest_of_patch)
{
outname = outfile ? outfile : inname;
if (outfile)
outname = outfile;
else if (pch_copy () || pch_rename ())
outname = pch_name (! strcmp (inname, pch_name (OLD)));
else
outname = inname;
if (! get_input_file (inname, outname, file_type))
{
skip_rest_of_patch = true;
@ -411,7 +417,8 @@ main (int argc, char **argv)
bool set_mode = new_mode && old_mode != new_mode;
/* Avoid replacing files when nothing has changed. */
if (failed < hunk || diff_type == ED_DIFF || set_mode)
if (failed < hunk || diff_type == ED_DIFF || set_mode
|| pch_copy () || pch_rename ())
{
enum file_attributes attr = FA_IDS | FA_MODE;
struct timespec new_time = pch_timestamp (! reverse);
@ -443,6 +450,14 @@ main (int argc, char **argv)
if (! inerrno)
set_file_attributes (outname, attr, &instat, mode, &new_time);
if (pch_rename ())
{
if (backup)
create_backup (inname, 0, 0, false);
else if (unlink (inname))
pfatal ("Can't remove file %s", quotearg (inname));
}
}
else if (backup)
create_backup (outname, 0, 0, true);

View File

@ -19,6 +19,7 @@
TESTS = \
asymmetric-hunks \
backup-prefix-suffix \
copy-rename \
corrupt-reject-files \
create-delete \
crlf-handling \

135
tests/copy-rename Normal file
View File

@ -0,0 +1,135 @@
# Copyright (C) 2010 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.
# git diffs: copy and rename tests
. $srcdir/test-lib.sh
require_cat
require_diff
use_local_patch
use_tmpdir
# ==============================================================
# Normal copy and rename patches
echo old > f
cat > copy.diff <<EOF
diff --git a/f b/g
copy from f
copy to g
--- a/f
+++ b/g
@@ -1 +1 @@
-old
+new
EOF
# FIXME: the message is confusing ...
check 'patch -p1 < copy.diff || echo "Status: $?"' <<EOF
patching file f
EOF
check 'cat f' <<EOF
old
EOF
check 'cat g' <<EOF
new
EOF
cat > rename.diff <<EOF
diff --git a/f b/h
rename from f
rename to h
--- a/f
+++ b/h
@@ -1 +1 @@
-old
+new
EOF
# FIXME: the message is confusing ...
check 'patch -p1 < rename.diff || echo "Status: $?"' <<EOF
patching file f
EOF
ncheck 'test ! -e f'
check 'cat h' <<EOF
new
EOF
# --------------------------------------------------------------
# Patches with no hunks
echo old > f
rm -f g h
cat > copy.diff <<EOF
diff --git a/f a/g
copy from f
copy to g
EOF
# FIXME: the message is confusing ...
check 'patch -p1 < copy.diff || echo "Status: $?"' <<EOF
patching file f
EOF
check 'cat f' <<EOF
old
EOF
check 'cat g' <<EOF
old
EOF
cat > rename.diff <<EOF
diff --git a/f a/h
rename from f
rename to h
EOF
# FIXME: the message is confusing ...
check 'patch -p1 < rename.diff || echo "Status: $?"' <<EOF
patching file f
EOF
ncheck 'test ! -e f'
check 'cat h' <<EOF
old
EOF
# --------------------------------------------------------------
# Backup file tests
echo old > f
rm -f g h
# FIXME: the message is confusing ...
check 'patch -p1 --backup < copy.diff || echo "Status: $?"' <<EOF
patching file f
EOF
ncheck 'test ! -e f.orig'
ncheck 'cat g.orig'
rm -f f.orig g.orig
# FIXME: the message is confusing ...
check 'patch -p1 --backup < rename.diff || echo "Status: $?"' <<EOF
patching file f
EOF
check 'cat f.orig' <<EOF
old
EOF
ncheck 'cat h.orig'