diff mbox series

[v4,1/2] describe: setup working tree for --dirty

Message ID 20190201135512.68220-1-koraktor@gmail.com (mailing list archive)
State New, archived
Headers show
Series [v4,1/2] describe: setup working tree for --dirty | expand

Commit Message

Sebastian Staudt Feb. 1, 2019, 1:55 p.m. UTC
We don't use NEED_WORK_TREE when running the git-describe builtin,
since you should be able to describe a commit even in a bare repository.
However, the --dirty flag does need a working tree. Since we don't call
setup_work_tree(), it uses whatever directory we happen to be in. That's
unlikely to match our index, meaning we'd say "dirty" even when the real
working tree is clean.

We can fix that by calling setup_work_tree() once we know that the user
has asked for --dirty.

The --broken option also needs a working tree. But because its
implementation calls git-diff-index we don‘t have to setup the working
tree in the git-describe process.

Signed-off-by: Sebastian Staudt <koraktor@gmail.com>
Helped-by: Jeff King <peff@peff.net>
---
 builtin/describe.c  |  1 +
 t/t6120-describe.sh | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+)

Comments

Eric Sunshine Feb. 1, 2019, 8:12 p.m. UTC | #1
On Fri, Feb 1, 2019 at 8:55 AM Sebastian Staudt <koraktor@gmail.com> wrote:
> We don't use NEED_WORK_TREE when running the git-describe builtin,
> since you should be able to describe a commit even in a bare repository.
> However, the --dirty flag does need a working tree. Since we don't call
> setup_work_tree(), it uses whatever directory we happen to be in. That's
> unlikely to match our index, meaning we'd say "dirty" even when the real
> working tree is clean.
> [...]
> Signed-off-by: Sebastian Staudt <koraktor@gmail.com>
> ---
> diff --git a/t/t6120-describe.sh b/t/t6120-describe.sh
> @@ -145,14 +145,38 @@ check_describe A-* HEAD
> +test_expect_success 'describe --dirty with --work-tree' '
> +       [...]
> +'
>
> +test_expect_success 'describe --dirty with --work-tree' '
> +       [...]
> +'

Can you give these two new tests different titles to make it easier to
narrow down a problem to one or the other if one of them does fail?
Perhaps the second test could be titled:

    test_expect_success 'describe --dirty with dirty --work-tree' '

or something.
Junio C Hamano Feb. 1, 2019, 8:52 p.m. UTC | #2
Sebastian Staudt <koraktor@gmail.com> writes:

> We don't use NEED_WORK_TREE when running the git-describe builtin,
> since you should be able to describe a commit even in a bare repository.
> However, the --dirty flag does need a working tree. Since we don't call
> setup_work_tree(), it uses whatever directory we happen to be in. That's
> unlikely to match our index, meaning we'd say "dirty" even when the real
> working tree is clean.
>
> We can fix that by calling setup_work_tree() once we know that the user
> has asked for --dirty.
>
> The --broken option also needs a working tree. But because its
> implementation calls git-diff-index we don‘t have to setup the working
> tree in the git-describe process.

Very nicely described.

>
> Signed-off-by: Sebastian Staudt <koraktor@gmail.com>
> Helped-by: Jeff King <peff@peff.net>
> ---
>  builtin/describe.c  |  1 +
>  t/t6120-describe.sh | 33 +++++++++++++++++++++++++++++++++
>  2 files changed, 34 insertions(+)
>
> diff --git a/builtin/describe.c b/builtin/describe.c
> index cc118448ee..b5b7abdc8f 100644
> --- a/builtin/describe.c
> +++ b/builtin/describe.c
> @@ -629,6 +629,7 @@ int cmd_describe(int argc, const char **argv, const char *prefix)
>  			struct argv_array args = ARGV_ARRAY_INIT;
>  			int fd, result;
>  
> +			setup_work_tree();
>  			read_cache();
>  			refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
>  				      NULL, NULL, NULL);

And the implementation is as promised in the proposed log message.
Quite straight-forward and obviously right ;-)

> diff --git a/t/t6120-describe.sh b/t/t6120-describe.sh
> index d639d94696..7cfed77c52 100755
> --- a/t/t6120-describe.sh
> +++ b/t/t6120-describe.sh
> @@ -145,14 +145,38 @@ check_describe A-* HEAD
>  
>  check_describe "A-*[0-9a-f]" --dirty
>  
> +test_expect_success 'describe --dirty with --work-tree' '
> +	(
> +		cd "$TEST_DIRECTORY" &&
> +		git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
> +	) &&
> +	grep "^A-[1-9][0-9]\?-g[0-9a-f]\+$" out
> +'
> +

Without the fix to the code, this test piece fails with "-dirty"
suffix in 'out'.  Good.

> +test_expect_success 'describe --dirty with --work-tree' '
> +	(
> +		cd "$TEST_DIRECTORY" &&
> +		git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
> +	) &&
> +	grep "^A-[1-9][0-9]\?-g[0-9a-f]\+-dirty$" out
> +'

This succeeds with or without the fix to the code; the buggy
behaviour was to claim "-dirty"-ness when the working tree files are
clean.  This new test is not about that buggy behaviour.  It is
rather about the updated code does not mistakenly claim cleanness in
a dirty working tree.  Good.

>  check_describe "A-*[0-9a-f].mod" --dirty=.mod
>  
> +test_expect_success 'describe --dirty=.mod with --work-tree' '
> +	(
> +		cd "$TEST_DIRECTORY" &&
> +		git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty=.mod >"$TRASH_DIRECTORY/out"
> +	) &&
> +	grep "^A-[1-9][0-9]\?-g[0-9a-f]\+.mod$" out
> +'
> +

Likewise.

>  test_expect_success 'describe --dirty HEAD' '
>  	test_must_fail git describe --dirty HEAD
>  '
> @@ -303,8 +327,17 @@ test_expect_success 'describe chokes on severely broken submodules' '
>  	mv .git/modules/sub1/ .git/modules/sub_moved &&
>  	test_must_fail git describe --dirty
>  '
> +
>  test_expect_success 'describe ignoring a broken submodule' '
>  	git describe --broken >out &&
> +	grep broken out
> +'
> +
> +test_expect_success 'describe with --work-tree ignoring a broken submodule' '
> +	(
> +		cd "$TEST_DIRECTORY" &&
> +		git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --broken >"$TRASH_DIRECTORY/out"
> +	) &&

OK, this checks the same repository as the existing test does, but
does so from outside the repository.

Looks good.

>  	test_when_finished "mv .git/modules/sub_moved .git/modules/sub1" &&
>  	grep broken out
>  '
Sebastian Staudt Feb. 2, 2019, 10:04 a.m. UTC | #3
Am Fr., 1. Feb. 2019 um 21:12 Uhr schrieb Eric Sunshine
<sunshine@sunshineco.com>:
>
> On Fri, Feb 1, 2019 at 8:55 AM Sebastian Staudt <koraktor@gmail.com> wrote:
> > We don't use NEED_WORK_TREE when running the git-describe builtin,
> > since you should be able to describe a commit even in a bare repository.
> > However, the --dirty flag does need a working tree. Since we don't call
> > setup_work_tree(), it uses whatever directory we happen to be in. That's
> > unlikely to match our index, meaning we'd say "dirty" even when the real
> > working tree is clean.
> > [...]
> > Signed-off-by: Sebastian Staudt <koraktor@gmail.com>
> > ---
> > diff --git a/t/t6120-describe.sh b/t/t6120-describe.sh
> > @@ -145,14 +145,38 @@ check_describe A-* HEAD
> > +test_expect_success 'describe --dirty with --work-tree' '
> > +       [...]
> > +'
> >
> > +test_expect_success 'describe --dirty with --work-tree' '
> > +       [...]
> > +'
>
> Can you give these two new tests different titles to make it easier to
> narrow down a problem to one or the other if one of them does fail?
> Perhaps the second test could be titled:
>
>     test_expect_success 'describe --dirty with dirty --work-tree' '
>
> or something.

Thanks, didn‘t notice this.
I‘d use a suffix (dirty) for my test titles. But this won‘t work for tests
using check_describe(). Any objections?
Eric Sunshine Feb. 3, 2019, 3:35 a.m. UTC | #4
On Sat, Feb 2, 2019 at 5:05 AM Sebastian Staudt <koraktor@gmail.com> wrote:
> Am Fr., 1. Feb. 2019 um 21:12 Uhr schrieb Eric Sunshine
> <sunshine@sunshineco.com>:
> > On Fri, Feb 1, 2019 at 8:55 AM Sebastian Staudt <koraktor@gmail.com> wrote:
> > > diff --git a/t/t6120-describe.sh b/t/t6120-describe.sh
> > > @@ -145,14 +145,38 @@ check_describe A-* HEAD
> > > +test_expect_success 'describe --dirty with --work-tree' '
> > > +       [...]
> > > +'
> > > +test_expect_success 'describe --dirty with --work-tree' '
> > > +       [...]
> > > +'
> >
> > Can you give these two new tests different titles to make it easier to
> > narrow down a problem to one or the other if one of them does fail?
> > Perhaps the second test could be titled:
> >
> >     test_expect_success 'describe --dirty with dirty --work-tree' '
> >
> > or something.
>
> Thanks, didn‘t notice this.
> I‘d use a suffix (dirty) for my test titles. But this won‘t work for tests
> using check_describe(). Any objections?

I have no objections to using suffix "(dirty)".

It's true that there are a few duplicate test titles due to
check_describe() invocations, however, this patch isn't introducing
any new callers, so it's not strictly a concern of this series. If you
did want to address that issue, one possibility would be to add a
'title' argument to check_describe() and adjust callers to use a
unique title, however, such a change would not be part of this
particular patch, and I'm not convinced that it's even worth the
effort and churn at this point.
diff mbox series

Patch

diff --git a/builtin/describe.c b/builtin/describe.c
index cc118448ee..b5b7abdc8f 100644
--- a/builtin/describe.c
+++ b/builtin/describe.c
@@ -629,6 +629,7 @@  int cmd_describe(int argc, const char **argv, const char *prefix)
 			struct argv_array args = ARGV_ARRAY_INIT;
 			int fd, result;
 
+			setup_work_tree();
 			read_cache();
 			refresh_index(&the_index, REFRESH_QUIET|REFRESH_UNMERGED,
 				      NULL, NULL, NULL);
diff --git a/t/t6120-describe.sh b/t/t6120-describe.sh
index d639d94696..7cfed77c52 100755
--- a/t/t6120-describe.sh
+++ b/t/t6120-describe.sh
@@ -145,14 +145,38 @@  check_describe A-* HEAD
 
 check_describe "A-*[0-9a-f]" --dirty
 
+test_expect_success 'describe --dirty with --work-tree' '
+	(
+		cd "$TEST_DIRECTORY" &&
+		git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
+	) &&
+	grep "^A-[1-9][0-9]\?-g[0-9a-f]\+$" out
+'
+
 test_expect_success 'set-up dirty work tree' '
 	echo >>file
 '
 
 check_describe "A-*[0-9a-f]-dirty" --dirty
 
+test_expect_success 'describe --dirty with --work-tree' '
+	(
+		cd "$TEST_DIRECTORY" &&
+		git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty >"$TRASH_DIRECTORY/out"
+	) &&
+	grep "^A-[1-9][0-9]\?-g[0-9a-f]\+-dirty$" out
+'
+
 check_describe "A-*[0-9a-f].mod" --dirty=.mod
 
+test_expect_success 'describe --dirty=.mod with --work-tree' '
+	(
+		cd "$TEST_DIRECTORY" &&
+		git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --dirty=.mod >"$TRASH_DIRECTORY/out"
+	) &&
+	grep "^A-[1-9][0-9]\?-g[0-9a-f]\+.mod$" out
+'
+
 test_expect_success 'describe --dirty HEAD' '
 	test_must_fail git describe --dirty HEAD
 '
@@ -303,8 +327,17 @@  test_expect_success 'describe chokes on severely broken submodules' '
 	mv .git/modules/sub1/ .git/modules/sub_moved &&
 	test_must_fail git describe --dirty
 '
+
 test_expect_success 'describe ignoring a broken submodule' '
 	git describe --broken >out &&
+	grep broken out
+'
+
+test_expect_success 'describe with --work-tree ignoring a broken submodule' '
+	(
+		cd "$TEST_DIRECTORY" &&
+		git --git-dir "$TRASH_DIRECTORY/.git" --work-tree "$TRASH_DIRECTORY" describe --broken >"$TRASH_DIRECTORY/out"
+	) &&
 	test_when_finished "mv .git/modules/sub_moved .git/modules/sub1" &&
 	grep broken out
 '