git status: refresh the index if possible

This was already the case before commit 9e4b7ab6 (git status: not
"commit --dry-run" anymore, 2009-08-15) with the difference that it died
at failure.
It got lost during the new implementation of "git status", which was
meant to only change behaviour when invoked with arguments.

Signed-off-by: Markus Heidelberg <markus.heidelberg@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
diff --git a/builtin/commit.c b/builtin/commit.c
index c5ab683..3c14ade 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1017,6 +1017,7 @@
 int cmd_status(int argc, const char **argv, const char *prefix)
 {
 	struct wt_status s;
+	int fd;
 	unsigned char sha1[20];
 	static struct option builtin_status_options[] = {
 		OPT__VERBOSE(&verbose),
@@ -1050,6 +1051,14 @@
 
 	read_cache_preload(s.pathspec);
 	refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
+
+	fd = hold_locked_index(&index_lock, 0);
+	if (0 <= fd) {
+		if (!write_cache(fd, active_cache, active_nr))
+			commit_locked_index(&index_lock);
+		rollback_lock_file(&index_lock);
+	}
+
 	s.is_initial = get_sha1(s.reference, sha1) ? 1 : 0;
 	s.in_merge = in_merge;
 	wt_status_collect(&s);
diff --git a/t/t7508-status.sh b/t/t7508-status.sh
index 086ec3a..c317bde 100755
--- a/t/t7508-status.sh
+++ b/t/t7508-status.sh
@@ -499,7 +499,7 @@
 cat >expect <<EOF
 :100644 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0000000000000000000000000000000000000000 M	dir1/modified
 EOF
-test_expect_failure 'status refreshes the index' '
+test_expect_success 'status refreshes the index' '
 	touch dir2/added &&
 	git status &&
 	git diff-files >output &&