# [PATCH 0/4] fix regression that "git status" doesn't refresh the index

15 messages from 2010-04-02 to 2010-04-06. Participants: Markus Heidelberg, Jeff King, Junio C Hamano.
Thread: https://gitlist.dev/t/23301

## Markus Heidelberg, 2010-04-02 12:27

Subject: [PATCH 0/4] fix regression that "git status" doesn't refresh the index
Message-ID: <1270211241-10795-1-git-send-email-markus.heidelberg@web.de>
URL: https://gitlist.dev/e/1270211241-10795-1-git-send-email-markus.heidelberg%40web.de

```
Patches 1 and 2 are minor unrelated fixes, noticed while working on the regression.
Patches 3 and 4 add a test and fix the regression.

Markus Heidelberg (4):
  builtin/commit: fix duplicated sentence in a comment
  builtin/commit: remove unnecessary variable definition
  t7508: add test for "git status" refreshing the index
  git status: refresh the index

 builtin/commit.c  |   11 ++++++++---
 t/t7508-status.sh |   10 ++++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)

```

## Markus Heidelberg, 2010-04-02 12:27

Subject: [PATCH 1/4] builtin/commit: fix duplicated sentence in a comment
Message-ID: <1270211241-10795-2-git-send-email-markus.heidelberg@web.de>
URL: https://gitlist.dev/e/1270211241-10795-2-git-send-email-markus.heidelberg%40web.de
In-Reply-To: <1270211241-10795-1-git-send-email-markus.heidelberg@web.de>

```

Signed-off-by: Markus Heidelberg <markus.heidelberg@web.de>
---
 builtin/commit.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index 8dd104e..8cc9293 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -322,8 +322,8 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
 	 *
 	 * (1) return the name of the real index file.
 	 *
-	 * The caller should run hooks on the real index, and run
-	 * hooks on the real index, and create commit from the_index.
+	 * The caller should run hooks on the real index,
+	 * and create commit from the_index.
 	 * We still need to refresh the index here.
 	 */
 	if (!pathspec || !*pathspec) {
-- 
1.7.0.4.300.gc535b

```

## Markus Heidelberg, 2010-04-02 12:27

Subject: [PATCH 2/4] builtin/commit: remove unnecessary variable definition
Message-ID: <1270211241-10795-3-git-send-email-markus.heidelberg@web.de>
URL: https://gitlist.dev/e/1270211241-10795-3-git-send-email-markus.heidelberg%40web.de
In-Reply-To: <1270211241-10795-1-git-send-email-markus.heidelberg@web.de>

```
The file descriptor is already defined at the beginning of the function.

Signed-off-by: Markus Heidelberg <markus.heidelberg@web.de>
---
 builtin/commit.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index 8cc9293..c5ab683 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -307,7 +307,7 @@ static char *prepare_index(int argc, const char **argv, const char *prefix, int
 	 * (B) on failure, rollback the real index.
 	 */
 	if (all || (also && pathspec && *pathspec)) {
-		int fd = hold_locked_index(&index_lock, 1);
+		fd = hold_locked_index(&index_lock, 1);
 		add_files_to_cache(also ? prefix : NULL, pathspec, 0);
 		refresh_cache_or_die(refresh_flags);
 		if (write_cache(fd, active_cache, active_nr) ||
-- 
1.7.0.4.300.gc535b

```

## Markus Heidelberg, 2010-04-02 12:27

Subject: [PATCH 3/4] t7508: add test for "git status" refreshing the index
Message-ID: <1270211241-10795-4-git-send-email-markus.heidelberg@web.de>
URL: https://gitlist.dev/e/1270211241-10795-4-git-send-email-markus.heidelberg%40web.de
In-Reply-To: <1270211241-10795-1-git-send-email-markus.heidelberg@web.de>

```

Signed-off-by: Markus Heidelberg <markus.heidelberg@web.de>
---
 t/t7508-status.sh |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/t/t7508-status.sh b/t/t7508-status.sh
index 556d0fa..086ec3a 100755
--- a/t/t7508-status.sh
+++ b/t/t7508-status.sh
@@ -496,6 +496,16 @@ test_expect_success 'dry-run of partial commit excluding new file in index' '
 	test_cmp expect output
 '
 
+cat >expect <<EOF
+:100644 100644 e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 0000000000000000000000000000000000000000 M	dir1/modified
+EOF
+test_expect_failure 'status refreshes the index' '
+	touch dir2/added &&
+	git status &&
+	git diff-files >output &&
+	test_cmp expect output
+'
+
 test_expect_success 'setup status submodule summary' '
 	test_create_repo sm && (
 		cd sm &&
-- 
1.7.0.4.300.gc535b

```

## Markus Heidelberg, 2010-04-02 12:27

Subject: [PATCH 4/4] git status: refresh the index
Message-ID: <1270211241-10795-5-git-send-email-markus.heidelberg@web.de>
URL: https://gitlist.dev/e/1270211241-10795-5-git-send-email-markus.heidelberg%40web.de
In-Reply-To: <1270211241-10795-1-git-send-email-markus.heidelberg@web.de>

```
This was already the case before commit 9e4b7ab6 (git status: not
"commit --dry-run" anymore, 2009-08-15) and got lost during the
conversion, which was meant to only change behaviour when invoked with
arguments.

Signed-off-by: Markus Heidelberg <markus.heidelberg@web.de>
---
 builtin/commit.c  |    5 +++++
 t/t7508-status.sh |    2 +-
 2 files changed, 6 insertions(+), 1 deletions(-)

diff --git a/builtin/commit.c b/builtin/commit.c
index c5ab683..2262734 100644
--- a/builtin/commit.c
+++ b/builtin/commit.c
@@ -1017,6 +1017,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
 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,10 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 
 	read_cache_preload(s.pathspec);
 	refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
+	fd = hold_locked_index(&index_lock, 1);
+	if (write_cache(fd, active_cache, active_nr) ||
+	    commit_locked_index(&index_lock))
+		die("unable to write new_index file");
 	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 @@ test_expect_success 'dry-run of partial commit excluding new file in index' '
 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 &&
-- 
1.7.0.4.300.gc535b

```

## Jeff King, 2010-04-02 16:57

Subject: Re: [PATCH 4/4] git status: refresh the index
Message-ID: <20100402165759.GB18576@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20100402165759.GB18576%40coredump.intra.peff.net
In-Reply-To: <1270211241-10795-5-git-send-email-markus.heidelberg@web.de>

```
On Fri, Apr 02, 2010 at 02:27:21PM +0200, Markus Heidelberg wrote:

> +	fd = hold_locked_index(&index_lock, 1);
> +	if (write_cache(fd, active_cache, active_nr) ||
> +	    commit_locked_index(&index_lock))
> +		die("unable to write new_index file");

Does this mean we will fail to run in a read-only repository? I think
that status, like diff, should refresh the index on disk if it _can_,
but as that refresh is a side effect of the main purpose (which is to
output information), it should not be fatal if it cannot do so.

-Peff

```

## Junio C Hamano, 2010-04-02 18:56

Subject: Re: [PATCH 4/4] git status: refresh the index
Message-ID: <7v6349bs52.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v6349bs52.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1270211241-10795-5-git-send-email-markus.heidelberg@web.de>

```
Markus Heidelberg <markus.heidelberg@web.de> writes:

> This was already the case before commit 9e4b7ab6 (git status: not
> "commit --dry-run" anymore, 2009-08-15) and got lost during the
> conversion, which was meant to only change behaviour when invoked with
> arguments.
>
> Signed-off-by: Markus Heidelberg <markus.heidelberg@web.de>
> ---
>  builtin/commit.c  |    5 +++++
>  t/t7508-status.sh |    2 +-
>  2 files changed, 6 insertions(+), 1 deletions(-)
>
> diff --git a/builtin/commit.c b/builtin/commit.c
> index c5ab683..2262734 100644
> --- a/builtin/commit.c
> +++ b/builtin/commit.c
> @@ -1017,6 +1017,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
>  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,10 @@ int cmd_status(int argc, const char **argv, const char *prefix)
>  
>  	read_cache_preload(s.pathspec);
>  	refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
> +	fd = hold_locked_index(&index_lock, 1);
> +	if (write_cache(fd, active_cache, active_nr) ||
> +	    commit_locked_index(&index_lock))
> +		die("unable to write new_index file");

This is a regression, I think.

The first two patches are trivially correct and I'll queue them to
'master'.

Thanks.

```

## Markus Heidelberg, 2010-04-02 20:37

Subject: Re: [PATCH 4/4] git status: refresh the index
Message-ID: <201004022237.04130.markus.heidelberg@web.de>
URL: https://gitlist.dev/e/201004022237.04130.markus.heidelberg%40web.de
In-Reply-To: <20100402165759.GB18576@coredump.intra.peff.net>

```
Jeff King, 2010-04-02 18:57:
> On Fri, Apr 02, 2010 at 02:27:21PM +0200, Markus Heidelberg wrote:
> 
> > +	fd = hold_locked_index(&index_lock, 1);
> > +	if (write_cache(fd, active_cache, active_nr) ||
> > +	    commit_locked_index(&index_lock))
> > +		die("unable to write new_index file");
> 
> Does this mean we will fail to run in a read-only repository?

You're right.
But that was already the case when "status" was "commit --dry-run".
I have to admit, I didn't think about this scenario, but simply looked
for the differences between these two commands.

> I think
> that status, like diff, should refresh the index on disk if it _can_,
> but as that refresh is a side effect of the main purpose (which is to
> output information), it should not be fatal if it cannot do so.

Sounds sensible.

Markus

```

## Markus Heidelberg, 2010-04-02 20:39

Subject: Re: [PATCH 4/4] git status: refresh the index
Message-ID: <201004022239.46139.markus.heidelberg@web.de>
URL: https://gitlist.dev/e/201004022239.46139.markus.heidelberg%40web.de
In-Reply-To: <7v6349bs52.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano, 2010-04-02 20:56:
> Markus Heidelberg <markus.heidelberg@web.de> writes:
> 
> > This was already the case before commit 9e4b7ab6 (git status: not
> > "commit --dry-run" anymore, 2009-08-15) and got lost during the
> > conversion, which was meant to only change behaviour when invoked with
> > arguments.
> >
> > Signed-off-by: Markus Heidelberg <markus.heidelberg@web.de>
> > ---
> >  builtin/commit.c  |    5 +++++
> >  t/t7508-status.sh |    2 +-
> >  2 files changed, 6 insertions(+), 1 deletions(-)
> >
> > diff --git a/builtin/commit.c b/builtin/commit.c
> > index c5ab683..2262734 100644
> > --- a/builtin/commit.c
> > +++ b/builtin/commit.c
> > @@ -1017,6 +1017,7 @@ static int git_status_config(const char *k, const char *v, void *cb)
> >  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,10 @@ int cmd_status(int argc, const char **argv, const char *prefix)
> >  
> >  	read_cache_preload(s.pathspec);
> >  	refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED, s.pathspec, NULL, NULL);
> > +	fd = hold_locked_index(&index_lock, 1);
> > +	if (write_cache(fd, active_cache, active_nr) ||
> > +	    commit_locked_index(&index_lock))
> > +		die("unable to write new_index file");
> 
> This is a regression, I think.

A regressions in comparison with the current behaviour, but not with the
former "commit --dry-run".

I'll send a second attempt without regression.

Markus

```

## Jeff King, 2010-04-02 21:21

Subject: Re: [PATCH 4/4] git status: refresh the index
Message-ID: <20100402212120.GA28352@coredump.intra.peff.net>
URL: https://gitlist.dev/e/20100402212120.GA28352%40coredump.intra.peff.net
In-Reply-To: <201004022237.04130.markus.heidelberg@web.de>

```
On Fri, Apr 02, 2010 at 09:37:03PM +0100, Markus Heidelberg wrote:

> > Does this mean we will fail to run in a read-only repository?
> 
> You're right.
> But that was already the case when "status" was "commit --dry-run".
> I have to admit, I didn't think about this scenario, but simply looked
> for the differences between these two commands.

Sort of. See ab68545 (status: don't require the repository to be
writable, 2010-01-19), which went onto maint while the "status is no
longer commit --dry-run" topic was cooking elsewhere. So I think it
would be a regression from 1.6.6.2 onwards.

At any rate, I think we all agree on what it _should_ do, so if you're
willing to do an updated patch, that would be great. The patch from
ab68545 may be helpful, as the code it changed is quite similar to what
you posted in this series.

-Peff

```

## Markus Heidelberg, 2010-04-02 21:44

Subject: [PATCH v2 4/4] git status: refresh the index if possible
Message-ID: <1270244661-24173-1-git-send-email-markus.heidelberg@web.de>
URL: https://gitlist.dev/e/1270244661-24173-1-git-send-email-markus.heidelberg%40web.de
In-Reply-To: <1270211241-10795-5-git-send-email-markus.heidelberg@web.de>

```
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>
---

v2:
Doesn't die when writing the index fails and so works for read-only
repositories.
Is rollback_lock_file(&index_lock) necessary? It isn't used in
"git commit --dry-run" when commit_style is COMMIT_AS_IS.

 builtin/commit.c  |    9 +++++++++
 t/t7508-status.sh |    2 +-
 2 files changed, 10 insertions(+), 1 deletions(-)

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 @@ static int git_status_config(const char *k, const char *v, void *cb)
 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 @@ int cmd_status(int argc, const char **argv, const char *prefix)
 
 	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 @@ test_expect_success 'dry-run of partial commit excluding new file in index' '
 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 &&
-- 
1.7.0.4.300.ge0630

```

## Junio C Hamano, 2010-04-03 04:33

Subject: Re: [PATCH v2 4/4] git status: refresh the index if possible
Message-ID: <7v1vex9mur.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7v1vex9mur.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1270244661-24173-1-git-send-email-markus.heidelberg@web.de>

```
Markus Heidelberg <markus.heidelberg@web.de> writes:

> Is rollback_lock_file(&index_lock) necessary? It isn't used in
> "git commit --dry-run" when commit_style is COMMIT_AS_IS.

That is because AS_IS commit does not even lock anything for writing, as
AS_IS means just that: "git commit" does not touch the index but just
writes tree out of the index.

Upon program exit (unless you get an uncontrolled crash), the lockfile API
arranges atexit(3) to roll back the lockfiles, so it probably may not make
much of a difference if you omitted rollback_lock_file(&index_lock)
yourself, but it is a good idea to clean up the mess you made after you
are done, especially if the mess is not something the operating system
will clean up for us (e.g. open file descriptors, malloc'ed region of
memory etc.)

To make sure that the failure case is covered, you may also want to add a
test case where you run "chmod a-w $GIT_DIR" and then run status (but that
test needs to be conditional on POSIXPERM).

Thanks.

```

## Markus Heidelberg, 2010-04-03 10:11

Subject: [PATCH] t7508: add a test for "git status" in a read-only repository
Message-ID: <1270289517-32680-1-git-send-email-markus.heidelberg@web.de>
URL: https://gitlist.dev/e/1270289517-32680-1-git-send-email-markus.heidelberg%40web.de
In-Reply-To: <7v1vex9mur.fsf@alter.siamese.dyndns.org>

```

Signed-off-by: Markus Heidelberg <markus.heidelberg@web.de>
---
 t/t7508-status.sh |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)

diff --git a/t/t7508-status.sh b/t/t7508-status.sh
index c317bde..baa8d7b 100755
--- a/t/t7508-status.sh
+++ b/t/t7508-status.sh
@@ -703,4 +703,14 @@ test_expect_success 'commit --dry-run submodule summary (--amend)' '
 	test_cmp expect output
 '
 
+test_expect_success POSIXPERM 'status succeeds in a read-only repository' '
+	(
+		chmod a-w .git &&
+		git status
+	)
+	status=$?
+	chmod 775 .git
+	(exit $status)
+'
+
 test_done
-- 
1.7.0.4.304.gc2d16

```

## Markus Heidelberg, 2010-04-03 10:33

Subject: Re: [PATCH v2 4/4] git status: refresh the index if possible
Message-ID: <201004031233.46258.markus.heidelberg@web.de>
URL: https://gitlist.dev/e/201004031233.46258.markus.heidelberg%40web.de
In-Reply-To: <7v1vex9mur.fsf@alter.siamese.dyndns.org>

```
Junio C Hamano, 2010-04-03 06:33:
> Markus Heidelberg <markus.heidelberg@web.de> writes:
> 
> > Is rollback_lock_file(&index_lock) necessary? It isn't used in
> > "git commit --dry-run" when commit_style is COMMIT_AS_IS.
> 
> That is because AS_IS commit does not even lock anything for writing, as
> AS_IS means just that: "git commit" does not touch the index but just
> writes tree out of the index.

Hmm, it does lock and write the index, doesn't it?

	/*
	 * As-is commit.
	 *
	 * (1) return the name of the real index file.
	 *
	 * The caller should run hooks on the real index,
	 * and create commit from the_index.
	 * We still need to refresh the index here.
	 */
	if (!pathspec || !*pathspec) {
		fd = hold_locked_index(&index_lock, 1);
		refresh_cache_or_die(refresh_flags);
		if (write_cache(fd, active_cache, active_nr) ||
		    commit_locked_index(&index_lock))
			die("unable to write new_index file");
		commit_style = COMMIT_AS_IS;
		return get_index_file();
	}

$ stat .git/index
Access: 2010-04-03 12:31:11.000000000 +0200
Modify: 2010-04-03 12:31:11.000000000 +0200
Change: 2010-04-03 12:31:11.000000000 +0200
$ git commit --dry-run
$ stat .git/index
Access: 2010-04-03 12:31:52.000000000 +0200
Modify: 2010-04-03 12:31:52.000000000 +0200
Change: 2010-04-03 12:31:52.000000000 +0200

$ chmod a-w .git
$ git commit --dry-run
fatal: Unable to create '/home/markus/git/git/.git/index.lock': Permission denied

> Upon program exit (unless you get an uncontrolled crash), the lockfile API
> arranges atexit(3) to roll back the lockfiles, so it probably may not make
> much of a difference if you omitted rollback_lock_file(&index_lock)
> yourself, but it is a good idea to clean up the mess you made after you
> are done, especially if the mess is not something the operating system
> will clean up for us (e.g. open file descriptors, malloc'ed region of
> memory etc.)

Thanks for the explanation!

> To make sure that the failure case is covered, you may also want to add a
> test case where you run "chmod a-w $GIT_DIR" and then run status (but that
> test needs to be conditional on POSIXPERM).

Patch has been sent.

Markus

```

## Junio C Hamano, 2010-04-06 06:20

Subject: Re: [PATCH] t7508: add a test for "git status" in a read-only repository
Message-ID: <7vochxje5k.fsf@alter.siamese.dyndns.org>
URL: https://gitlist.dev/e/7vochxje5k.fsf%40alter.siamese.dyndns.org
In-Reply-To: <1270289517-32680-1-git-send-email-markus.heidelberg@web.de>

```
Thanks, will queue with a minor tweak.

```
