Description: ensure that all mtime of modified files are equal when pushing
 This is intended to avoid time skew in build systems in some cases.
 .
 See the discussion
 http://lists.debian.org/debian-policy/2008/02/msg00030.html for more
 context information.
Bug-Debian: http://bugs.debian.org/466360
Upstream-status: to be submitted 

---
 quilt/push.in                 |   22 ++++++++--
 quilt/scripts/backup-files.in |    3 -
 test/push_timeskew.test       |   86 ++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 105 insertions(+), 6 deletions(-)

Index: b/quilt/push.in
===================================================================
--- a/quilt/push.in
+++ b/quilt/push.in
@@ -30,7 +30,8 @@
 specified number of patches.  When a patch name is specified, apply
 all patches up to and including the specified patch.  Patch names may
 include the patches/ prefix, which means that filename completion can
-be used.
+be used. The mtime of all touched files will be exactly the same to
+prevent time skews.
 
 -a	Apply all patches in the series file.
 
@@ -209,18 +210,29 @@
 			touch "$QUILT_PC/$patch~refresh"
 		fi
 
-		if [ -e "$QUILT_PC/$patch" ]
+		if ! [ -e "$QUILT_PC/$patch" ]
 		then
-			touch "$QUILT_PC/$patch/.timestamp"
-		else
 			mkdir "$QUILT_PC/$patch"
 		fi
+		touch "$QUILT_PC/$patch/.timestamp"
+
+		# Store the list of files to process
+		NONEMPTY_FILES=$(gen_tempfile)
+		trap "rm -f \"$NONEMPTY_FILES\"" EXIT
+		find "$QUILT_PC/$patch" -type f \
+		      -a ! -path "$QUILT_PC/$patch/.timestamp" -size +0 -print0 > "$NONEMPTY_FILES"
+
+		if [ -s "$NONEMPTY_FILES" ]; then
+			xargs -0 touch -c -r "$QUILT_PC/$patch/.timestamp" < "$NONEMPTY_FILES"
+		fi
+
+		rm -f $NONEMPTY_FILES
 
 		if ! [ -e "$patch_file" ]
 		then
 			printf $"Patch %s does not exist; applied empty patch\n" \
 			       "$(print_patch "$patch")"
-		elif [ -z "$(shopt -s nullglob ; echo "$QUILT_PC/$patch/"*)" ]
+		elif [ "$(shopt -s nullglob ; echo "$QUILT_PC/$patch/"*)" = "$QUILT_PC/$patch/.timestamp" ]
 		then
 			printf $"Patch %s appears to be empty; applied\n" \
 			       "$(print_patch "$patch")"
Index: b/quilt/scripts/backup-files.in
===================================================================
--- a/quilt/scripts/backup-files.in
+++ b/quilt/scripts/backup-files.in
@@ -181,8 +181,9 @@
 			done < "$NONEMPTY_FILES"
 		fi
 
+		modif_time=`date +%m%d%H%M.%S`
 		if [ -n "$OPT_TOUCH" ]; then
-			xargs -0 touch -c < "$NONEMPTY_FILES"
+			xargs -0 touch -t $modif_time -c < "$NONEMPTY_FILES"
 		fi
 	fi
 
Index: b/test/push_timeskew.test
===================================================================
--- /dev/null
+++ b/test/push_timeskew.test
@@ -0,0 +1,86 @@
+This test enforces that files touched by a patch have the exact same
+mtime when pushing and poping the patch.
+(To run, type `./run push_timeskew.test' in this directory.)
+
+	$ mkdir patches d
+
+	$ quilt new patch1
+	> Patch %{P}patch1 is now on top
+
+	$ cd d
+	$ mkdir dir
+	$ echo "This is file one." > dir/file1
+	$ quilt add dir/file1
+	> File d/dir/file1 added to patch %{_P}patch1
+
+	$ echo "This is file two." > dir/file2
+	$ quilt add dir/file2
+	> File d/dir/file2 added to patch %{_P}patch1
+
+	$ echo "More content to file one." >> dir/file1
+	$ echo "More content to file two." >> dir/file2
+	$ quilt refresh
+	> Refreshed patch %{_P}patch1
+
+	$ quilt pop -q
+	> Removing patch %{_P}patch1
+	> No patches applied
+
+	$ test dir/file1 -nt dir/file2 && echo "timeskew!"
+	$ test dir/file2 -nt dir/file1 && echo "timeskew!"
+
+	$ quilt push -q
+	> Applying patch %{_P}patch1
+	> Now at patch %{_P}patch1
+
+	$ test dir/file1 -nt dir/file2 && echo "timeskew!"
+	$ test dir/file2 -nt dir/file1 && echo "timeskew!"
+
+
+
+	# And now, enforces that this timestamp fixup don't create unwanted files
+	$ quilt new patch2.diff
+	> Patch %{_P}patch2.diff is now on top
+
+	$ echo "some content" > dir/file_removed
+	$ quilt add dir/file_removed
+	> File d/dir/file_removed added to patch %{_P}patch2.diff
+
+	$ quilt add dir/file_created
+	> File d/dir/file_created added to patch %{_P}patch2.diff
+
+	$ rm dir/file_removed
+	$ echo "some content" > dir/file_created
+	$ quilt refresh
+	> Refreshed patch %{_P}patch2.diff
+
+	$ quilt diff --no-timestamps --no-index -p ab
+	> --- /dev/null
+	> +++ b/d/dir/file_created
+	> @@ -0,0 +1 @@
+	> +some content
+	> --- a/d/dir/file_removed
+	> +++ /dev/null
+	> @@ -1 +0,0 @@
+	> -some content
+
+	$ quilt pop
+	> Removing patch %{_P}patch2.diff
+	> Removing d/dir/file_created
+	> Restoring d/dir/file_removed
+	>
+	> Now at patch %{_P}patch1
+
+	$ test   -e dir/file_created && echo "Created file should not exist when patch is poped!"
+	$ test ! -e dir/file_removed && echo "Deleted file should exist when patch is poped!"
+
+	$ quilt push
+	> Applying patch %{_P}patch2.diff
+	> patching file d/dir/file_created
+	> patching file d/dir/file_removed
+	>
+	> Now at patch %{_P}patch2.diff
+
+	$ test ! -e dir/file_created && echo "Created file should exist when patch is pushed!"
+	$ test   -e dir/file_removed && echo "Deleted file should not exist when patch is pushed!"
+
