Let function cat_to_new_file optionally backup the destination file
before overwriting it. This has two advantages:
* This avoids duplicating code on the caller's side. Functions header
  and refresh both need to perform a backup.
* This guarantees the consistency of the backup logic. At the moment,
  the refresh command would handle the backup of a symlinked file
  just fine while the header command would misbehave in that case.

Now that the header command handles symlinked patches properly, also
test it so that we don't break it accidentally later.
---
 quilt/header.in           |    4 +---
 quilt/refresh.in          |    9 +--------
 quilt/scripts/patchfns.in |   14 +++++++++++++-
 test/symlink.test         |   37 +++++++++++++++++++++++++++++++++++++
 4 files changed, 52 insertions(+), 12 deletions(-)

--- a/quilt/header.in
+++ b/quilt/header.in
@@ -159,9 +159,7 @@ else
 
        cat_file "$patch_file_or_null" | patch_body >> $tmp2 || exit 1
 
-       if ( [ -z "$QUILT_BACKUP" -o ! -e $patch_file ] || \
-              mv $patch_file $patch_file~ ) && \
-              cat_to_new_file $patch_file < $tmp2
+       if cat_to_new_file $patch_file $QUILT_BACKUP < $tmp2
        then
                if [ -z "$opt_append" ]
                then
--- a/quilt/refresh.in
+++ b/quilt/refresh.in
@@ -323,14 +323,7 @@ if [ -e "$patch_file" ] && \
    diff -q "$patch_file" $tmp_result > /dev/null
 then
        printf $"Patch %s is unchanged\n" "$(print_patch "$patch")"
-elif ( [ -z "$QUILT_BACKUP" -o ! -e "$patch_file" ] || \
-       if [ -L "$patch_file" ] ; \
-       then \
-          cp -p "$patch_file" "$patch_file~"; \
-       else \
-          mv "$patch_file" "$patch_file~"; \
-       fi ) && \
-     cat_to_new_file "$patch_file" < $tmp_result
+elif cat_to_new_file "$patch_file" $QUILT_BACKUP < $tmp_result
 then
        if [ -n "$opt_fork" ]
        then
--- a/quilt/scripts/patchfns.in
+++ b/quilt/scripts/patchfns.in
@@ -783,7 +783,19 @@ cat_file()
 
 cat_to_new_file()
 {
-       local filename="$1"
+       local filename=$1 backup=$2
+
+       # If the file exists, back it up if requested. Return immediately upon
+       # failure.
+       if [ -e "$filename" -a -n "$backup" ]
+       then
+               if [ -L "$filename" ]
+               then
+                       cp -p "$filename" "$filename~"
+               else
+                       mv "$filename" "$filename~"
+               fi || return
+       fi
 
        # If the destination file is a symbolic link, preserve it unless its
        # target is read-only. In other cases, delete the patch file first in
--- a/test/symlink.test
+++ b/test/symlink.test
@@ -55,6 +55,43 @@
 
        $ [ patches/test.diff~ -nt test.timeref ] && echo "mtimes differ"
 
+# Test the header update
+       $ quilt header -a
+       < A test patch
+       > Appended text to header of patch patches/test.diff
+
+       $ readlink patches/test.diff
+       > ../test.diff
+
+# Test the header update with a backup
+       $ touch -r patches/test.diff test.timeref
+       $ quilt header -r --backup
+       > Replaced header of patch patches/test.diff
+
+       $ readlink patches/test.diff
+       > ../test.diff
+
+       $ cat patches/test.diff
+       > --- a/foo
+       > +++ b/foo
+       > @@ -1 +1 @@
+       > -foo
+       > +foo changed 3
+
+       $ readlink patches/test.diff~
+       $ echo %{?}
+       > 1
+
+       $ cat patches/test.diff~
+       > A test patch
+       > --- a/foo
+       > +++ b/foo
+       > @@ -1 +1 @@
+       > -foo
+       > +foo changed 3
+
+       $ [ patches/test.diff~ -nt test.timeref ] && echo "mtimes differ"
+
 # Test the refresh when target is read-only
        $ chmod -w test.diff
        $ echo "foo changed 4" > foo

-- 
Jean Delvare
SUSE L3 Support


_______________________________________________
Quilt-dev mailing list
Quilt-dev@nongnu.org
https://lists.nongnu.org/mailman/listinfo/quilt-dev

Reply via email to