Strict pattern match for diffstat output
Remove unused code
Insert blank line between header and diffstat
Tests for diffstat integration

Index: quilt/quilt/refresh.in
===================================================================
--- quilt.orig/quilt/refresh.in	2005-08-08 10:08:18.%N +1000
+++ quilt/quilt/refresh.in	2005-08-17 02:07:21.%N +1000
@@ -258,26 +258,27 @@
 then
 	diffstat="$(@DIFFSTAT@ -p$opt_strip_level $tmp_patch)" || die 1
 	@AWK@ '
-	    function print_diffstat(arr, i) {
+	    function print_diffstat() {
 	      split(diffstat, arr, "\n")
 	      for (i=1; i in arr; i++)
 		print prefix arr[i]
 	    }
-	    BEGIN	{ split(diffstat, ds_arr, /\n/) }
 			{ prefix=""
 			  if (index($0, "#") == 1)
 			    prefix="#"
 			}
-	    /^#? .* \| /	{ eat = eat $0 "\n"
+	    /^#? .* \|  *[1-9][0-9]* [+-][+-]*/	{ eat = eat $0 "\n"
 			  next }
 	    /^#? .* files? changed(, .* insertions?\(\+\))?(, .* deletions?\(-\))?/ \
 			{ print_diffstat()
 			  diffstat = "" ; eat = ""
 			  next }
 			{ print eat $0
-			  eat = "" }
+			  eat = ""
+			  add_nl=(length($0) > 1) }
 	    END		{ printf "%s", eat
 	    		  if (diffstat != "") {
+			    if (add_nl) print prefix
 			    print_diffstat()
 			    print prefix
 			  }
Index: quilt/test/diffstat.test
===================================================================
--- /dev/null	1970-01-01 00:00:00.000000000 +0000
+++ quilt/test/diffstat.test	2005-08-17 02:02:00.%N +1000
@@ -0,0 +1,77 @@
+	# Test diffstat in header
+
+	$ mkdir d
+	$ cd d
+
+	$ quilt new diffstat.diff
+	> Patch patches/diffstat.diff is now on top
+
+	$ quilt add b c
+	> File b added to patch patches/diffstat.diff
+	> File c added to patch patches/diffstat.diff
+
+	$ echo b > b
+	$ echo c > c
+	$ quilt refresh --diffstat
+	> Refreshed patch patches/diffstat.diff
+
+	$ head -5 patches/diffstat.diff
+	>  b |    1 +
+	>  c |    1 +
+	>  2 files changed, 2 insertions(+)
+	>
+	> Index: d/b
+
+	$ quilt header -a
+	< test
+	> Appended text to header of patch patches/diffstat.diff
+
+	# header is not appended before the diffstat
+
+	$ head -6 patches/diffstat.diff
+	>  b |    1 +
+	>  c |    1 +
+	>  2 files changed, 2 insertions(+)
+	>
+	> test
+	> Index: d/b
+
+	# test that diffstat adds a trailing blank line in the header
+
+	$ quilt header -r
+	< test
+	> Replaced header of patch patches/diffstat.diff
+
+	$ quilt refresh --diffstat
+	> Refreshed patch patches/diffstat.diff
+
+	$ head -7 patches/diffstat.diff
+	> test
+	>
+	>  b |    1 +
+	>  c |    1 +
+	>  2 files changed, 2 insertions(+)
+	>
+	> Index: d/b
+
+	# test adding a trailing blank line in the header
+
+	$ quilt header -r
+	< test
+	<
+	> Replaced header of patch patches/diffstat.diff
+
+	$ quilt refresh --diffstat
+	> Refreshed patch patches/diffstat.diff
+
+	$ head -7 patches/diffstat.diff
+	> test
+	>
+	>  b |    1 +
+	>  c |    1 +
+	>  2 files changed, 2 insertions(+)
+	>
+	> Index: d/b
+
+	$ cd ..
+	$ rm -rf d
