threads / patch / 23301

patch, 4 partsfix regression that "git status" doesn't refresh the index

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

## tl;dr

15 messages between Apr 2, 2010 and Apr 6, 2010. Diffs are folded; open one to read it.

replies: 14people: 3as markdown or json

Markus Heidelberg· Apr 2, 2010, 12:27 UTC · lore

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· Apr 2, 2010, 12:27 UTC · re: Markus Heidelberg · lore

[PATCH 1/4] builtin/commit: fix duplicated sentence in a comment

Signed-off-by: Markus Heidelberg <markus.heidelberg@web.de>
---
 builtin/commit.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
Show changes to builtin/commit.c +2 −2
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· Apr 2, 2010, 12:27 UTC · re: Markus Heidelberg · lore

[PATCH 2/4] builtin/commit: remove unnecessary variable definition

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(-)
Show changes to builtin/commit.c +1 −1
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· Apr 2, 2010, 12:27 UTC · re: Markus Heidelberg · lore

[PATCH 3/4] t7508: add test for "git status" refreshing the index

Signed-off-by: Markus Heidelberg <markus.heidelberg@web.de>
---
 t/t7508-status.sh |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
Show changes to t/t7508-status.sh +10 −0
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· Apr 2, 2010, 12:27 UTC · re: Markus Heidelberg · lore

[PATCH 4/4] git status: refresh the index

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(-)
Show changes to 2 files +6 −1

builtin/commit.c, t/t7508-status.sh

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· Apr 2, 2010, 16:57 UTC · re: Markus Heidelberg · lore

Re: [PATCH 4/4] git status: refresh the index

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
Markus Heidelberg· Apr 2, 2010, 20:37 UTC · re: Jeff King · lore

Re: [PATCH 4/4] git status: refresh the index

Jeff King, 2010-04-02 18:57:
Show 8 quoted lines
> 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
Jeff King· Apr 2, 2010, 21:21 UTC · re: Markus Heidelberg · lore

Re: [PATCH 4/4] git status: refresh the index

On Fri, Apr 02, 2010 at 09:37:03PM +0100, Markus Heidelberg wrote:
Show 6 quoted lines
> > 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
Junio C Hamano· Apr 2, 2010, 18:56 UTC · re: Markus Heidelberg · lore

Re: [PATCH 4/4] git status: refresh the index

Markus Heidelberg <markus.heidelberg@web.de> writes:
Show 31 quoted lines
> 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· Apr 2, 2010, 20:39 UTC · re: Junio C Hamano · lore

Re: [PATCH 4/4] git status: refresh the index

Junio C Hamano, 2010-04-02 20:56:
Show 35 quoted lines
> 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
Markus Heidelberg· Apr 2, 2010, 21:44 UTC · re: Markus Heidelberg · lore

[PATCH v2 4/4] 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>
---

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(-)
Show changes to 2 files +10 −1

builtin/commit.c, t/t7508-status.sh

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· Apr 3, 2010, 04:33 UTC · re: Markus Heidelberg · lore

Re: [PATCH v2 4/4] git status: refresh the index if possible

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· Apr 3, 2010, 10:11 UTC · re: Junio C Hamano · lore

[PATCH] t7508: add a test for "git status" in a read-only repository

Signed-off-by: Markus Heidelberg <markus.heidelberg@web.de>
---
 t/t7508-status.sh |   10 ++++++++++
 1 files changed, 10 insertions(+), 0 deletions(-)
Show changes to t/t7508-status.sh +10 −0
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· Apr 3, 2010, 10:33 UTC · re: Junio C Hamano · lore

Re: [PATCH v2 4/4] git status: refresh the index if possible

Junio C Hamano, 2010-04-03 06:33:
Show 8 quoted lines
> 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

Show 7 quoted lines
> 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

← back to recent threads