git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
* [RFC PATCH] pack-refs: fail on falsely sorted packed-refs
@ 2019-01-30 23:13 Max Kirillov
  2019-01-30 23:31 ` Eric Sunshine
  2019-02-13 10:08 ` [RFC PATCH] " Ævar Arnfjörð Bjarmason
  0 siblings, 2 replies; 12+ messages in thread
From: Max Kirillov @ 2019-01-30 23:13 UTC (permalink / raw)
  To: Michael Haggerty; +Cc: Max Kirillov, git

If packed-refs is marked as sorted but not really sorted it causes
very hard to comprehend misbehavior of reference resolving - a reference
is reported as not found.

As the scope of the issue is not clear, make it visible by failing
pack-refs command - the one which would not suffer performance penalty
to verify the sortedness - when it encounters not really sorted existing
data.

Signed-off-by: Max Kirillov <max@max630.net>
---
I happened to have a not really sorted packed-refs file. As you might guess,
it was quite wtf-ing experience. It worked, mostly, but there was one branch
which just did not resolve, regardless of existing and being presented in
for-each-refs output.

I don't know where the corruption came from. I should admit it could even be a manual
editing but last time I did it (in that reporitory) was several years ago so it is unlikely.

I am not sure what should be the proper fix. I did a minimal detection, so that
it does not go unnoticed. Probably next step would be either fixing in `git fsck` call.

 refs/packed-backend.c               | 15 +++++++++++++++
 t/t3212-pack-refs-broken-sorting.sh | 26 ++++++++++++++++++++++++++
 2 files changed, 41 insertions(+)
 create mode 100755 t/t3212-pack-refs-broken-sorting.sh

diff --git a/refs/packed-backend.c b/refs/packed-backend.c
index c01c7f5901..505f4535b5 100644
--- a/refs/packed-backend.c
+++ b/refs/packed-backend.c
@@ -1088,6 +1088,7 @@ static int write_with_updates(struct packed_ref_store *refs,
 	FILE *out;
 	struct strbuf sb = STRBUF_INIT;
 	char *packed_refs_path;
+	struct strbuf prev_ref = STRBUF_INIT;
 
 	if (!is_lock_file_locked(&refs->lock))
 		BUG("write_with_updates() called while unlocked");
@@ -1137,6 +1138,20 @@ static int write_with_updates(struct packed_ref_store *refs,
 		struct ref_update *update = NULL;
 		int cmp;
 
+		if (iter)
+		{
+			if (prev_ref.len &&  strcmp(prev_ref.buf, iter->refname) > 0)
+			{
+				strbuf_addf(err, "broken sorting in packed-refs: '%s' > '%s'",
+					    prev_ref.buf,
+					    iter->refname);
+				goto error;
+			}
+
+			strbuf_init(&prev_ref, 0);
+			strbuf_addstr(&prev_ref, iter->refname);
+		}
+
 		if (i >= updates->nr) {
 			cmp = -1;
 		} else {
diff --git a/t/t3212-pack-refs-broken-sorting.sh b/t/t3212-pack-refs-broken-sorting.sh
new file mode 100755
index 0000000000..37a98a6fb1
--- /dev/null
+++ b/t/t3212-pack-refs-broken-sorting.sh
@@ -0,0 +1,26 @@
+#!/bin/sh
+
+test_description='tests for the falsely sorted refs'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	git commit --allow-empty -m commit &&
+	for num in $(test_seq 10)
+	do
+		git branch b$(printf "%02d" $num) || break
+	done &&
+	git pack-refs --all &&
+	head_object=$(git rev-parse HEAD) &&
+	printf "$head_object refs/heads/b00\\n" >>.git/packed-refs &&
+	git branch b11
+'
+
+test_expect_success 'off-order branch not found' '
+	! git show-ref --verify --quiet refs/heads/b00
+'
+
+test_expect_success 'subsequent pack-refs fails' '
+	! git pack-refs --all
+'
+
+test_done
-- 
2.19.0.1202.g68e1e8f04e


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [RFC PATCH] pack-refs: fail on falsely sorted packed-refs
  2019-01-30 23:13 [RFC PATCH] pack-refs: fail on falsely sorted packed-refs Max Kirillov
@ 2019-01-30 23:31 ` Eric Sunshine
  2019-01-31  8:21   ` Max Kirillov
  2019-02-08 21:22   ` [PATCH v2] " Max Kirillov
  2019-02-13 10:08 ` [RFC PATCH] " Ævar Arnfjörð Bjarmason
  1 sibling, 2 replies; 12+ messages in thread
From: Eric Sunshine @ 2019-01-30 23:31 UTC (permalink / raw)
  To: Max Kirillov; +Cc: Michael Haggerty, Git List

On Wed, Jan 30, 2019 at 6:21 PM Max Kirillov <max@max630.net> wrote:
> If packed-refs is marked as sorted but not really sorted it causes
> very hard to comprehend misbehavior of reference resolving - a reference
> is reported as not found.
>
> As the scope of the issue is not clear, make it visible by failing
> pack-refs command - the one which would not suffer performance penalty
> to verify the sortedness - when it encounters not really sorted existing
> data.
>
> Signed-off-by: Max Kirillov <max@max630.net>
> ---
> diff --git a/refs/packed-backend.c b/refs/packed-backend.c
> @@ -1088,6 +1088,7 @@ static int write_with_updates(struct packed_ref_store *refs,
> +       struct strbuf prev_ref = STRBUF_INIT;
> @@ -1137,6 +1138,20 @@ static int write_with_updates(struct packed_ref_store *refs,
> +               if (iter)
> +               {
> +                       if (prev_ref.len &&  strcmp(prev_ref.buf, iter->refname) > 0)
> +                       {
> +                               strbuf_addf(err, "broken sorting in packed-refs: '%s' > '%s'",
> +                                           prev_ref.buf,
> +                                           iter->refname);

strbuf_release(&prev_ref) either here or after the "error" label.

> +                               goto error;
> +                       }
> +
> +                       strbuf_init(&prev_ref, 0);
> +                       strbuf_addstr(&prev_ref, iter->refname);
> +               }
> diff --git a/t/t3212-pack-refs-broken-sorting.sh b/t/t3212-pack-refs-broken-sorting.sh
> @@ -0,0 +1,26 @@
> +test_expect_success 'setup' '
> +       git commit --allow-empty -m commit &&
> +       for num in $(test_seq 10)
> +       do
> +               git branch b$(printf "%02d" $num) || break

This should probably be "|| return 1" rather than "|| break" in order
to fail the test immediately.

> +       done &&
> +       git pack-refs --all &&
> +       head_object=$(git rev-parse HEAD) &&
> +       printf "$head_object refs/heads/b00\\n" >>.git/packed-refs &&
> +       git branch b11
> +'
> +
> +test_expect_success 'off-order branch not found' '
> +       ! git show-ref --verify --quiet refs/heads/b00
> +'

Use test_must_fail() rather than '!' when expecting a Git command to fail.

> +test_expect_success 'subsequent pack-refs fails' '
> +       ! git pack-refs --all
> +'

Ditto.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [RFC PATCH] pack-refs: fail on falsely sorted packed-refs
  2019-01-30 23:31 ` Eric Sunshine
@ 2019-01-31  8:21   ` Max Kirillov
  2019-02-08 21:22   ` [PATCH v2] " Max Kirillov
  1 sibling, 0 replies; 12+ messages in thread
From: Max Kirillov @ 2019-01-31  8:21 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Max Kirillov, Michael Haggerty, Git List

On Wed, Jan 30, 2019 at 06:31:34PM -0500, Eric Sunshine wrote:
> On Wed, Jan 30, 2019 at 6:21 PM Max Kirillov <max@max630.net> wrote:
>> +                               strbuf_addf(err, "broken sorting in packed-refs: '%s' > '%s'",
>> +                                           prev_ref.buf,
>> +                                           iter->refname);

> strbuf_release(&prev_ref) either here or after the "error" label.

Thanks! I seem to forget about it.

> > +               git branch b$(printf "%02d" $num) || break

> This should probably be "|| return 1" rather than "|| break" in order
> to fail the test immediately.

I've been looking for the correct way, and have seen the
break somewhere. Now I see the "return 1" is mostly user.
Thanks, will fix.

> Use test_must_fail() rather than '!' when expecting a Git command to fail.

Will fix in both places

^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v2] pack-refs: fail on falsely sorted packed-refs
  2019-01-30 23:31 ` Eric Sunshine
  2019-01-31  8:21   ` Max Kirillov
@ 2019-02-08 21:22   ` Max Kirillov
  2019-02-08 21:40     ` Eric Sunshine
       [not found]     ` <CAMy9T_EX_L80-V4zD626nFCxw6qa90+pZwcbd6wHw9ZHcj2rNA@mail.gmail.com>
  1 sibling, 2 replies; 12+ messages in thread
From: Max Kirillov @ 2019-02-08 21:22 UTC (permalink / raw)
  To: Git List, Eric Sunshine; +Cc: Max Kirillov, Michael Haggerty

If packed-refs is marked as sorted but not really sorted it causes
very hard to comprehend misbehavior of reference resolving - a reference
is reported as not found, though it is listed by commands which output
the references list.

As the scope of the issue is not clear, make it visible by failing
pack-refs command - the one which would not suffer performance penalty
to verify the sortedness - when it encounters not really sorted existing
data.

Signed-off-by: Max Kirillov <max@max630.net>
---
Fixed the notes
 refs/packed-backend.c               | 18 ++++++++++++++++++
 t/t3212-pack-refs-broken-sorting.sh | 26 ++++++++++++++++++++++++++
 2 files changed, 44 insertions(+)
 create mode 100755 t/t3212-pack-refs-broken-sorting.sh

diff --git a/refs/packed-backend.c b/refs/packed-backend.c
index c01c7f5901..c89a5eb899 100644
--- a/refs/packed-backend.c
+++ b/refs/packed-backend.c
@@ -1088,6 +1088,7 @@ static int write_with_updates(struct packed_ref_store *refs,
 	FILE *out;
 	struct strbuf sb = STRBUF_INIT;
 	char *packed_refs_path;
+	struct strbuf prev_ref = STRBUF_INIT;
 
 	if (!is_lock_file_locked(&refs->lock))
 		BUG("write_with_updates() called while unlocked");
@@ -1137,6 +1138,21 @@ static int write_with_updates(struct packed_ref_store *refs,
 		struct ref_update *update = NULL;
 		int cmp;
 
+		if (iter)
+		{
+			if (prev_ref.len &&  strcmp(prev_ref.buf, iter->refname) > 0)
+			{
+				strbuf_addf(err, "broken sorting in packed-refs: '%s' > '%s'",
+					    prev_ref.buf,
+					    iter->refname);
+				strbuf_release(&prev_ref);
+				goto error;
+			}
+
+			strbuf_init(&prev_ref, 0);
+			strbuf_addstr(&prev_ref, iter->refname);
+		}
+
 		if (i >= updates->nr) {
 			cmp = -1;
 		} else {
@@ -1240,6 +1256,8 @@ static int write_with_updates(struct packed_ref_store *refs,
 		}
 	}
 
+	strbuf_release(&prev_ref);
+
 	if (ok != ITER_DONE) {
 		strbuf_addstr(err, "unable to write packed-refs file: "
 			      "error iterating over old contents");
diff --git a/t/t3212-pack-refs-broken-sorting.sh b/t/t3212-pack-refs-broken-sorting.sh
new file mode 100755
index 0000000000..a44785c8fc
--- /dev/null
+++ b/t/t3212-pack-refs-broken-sorting.sh
@@ -0,0 +1,26 @@
+#!/bin/sh
+
+test_description='tests for the falsely sorted refs'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	git commit --allow-empty -m commit &&
+	for num in $(test_seq 10)
+	do
+		git branch b$(printf "%02d" $num) || return 1
+	done &&
+	git pack-refs --all &&
+	head_object=$(git rev-parse HEAD) &&
+	printf "$head_object refs/heads/b00\\n" >>.git/packed-refs &&
+	git branch b11
+'
+
+test_expect_success 'off-order branch not found' '
+	test_must_fail git show-ref --verify --quiet refs/heads/b00
+'
+
+test_expect_success 'subsequent pack-refs fails' '
+	test_must_fail git pack-refs --all
+'
+
+test_done
-- 
2.19.0.1202.g68e1e8f04e


^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH v2] pack-refs: fail on falsely sorted packed-refs
  2019-02-08 21:22   ` [PATCH v2] " Max Kirillov
@ 2019-02-08 21:40     ` Eric Sunshine
  2019-02-13  4:24       ` Max Kirillov
       [not found]     ` <CAMy9T_EX_L80-V4zD626nFCxw6qa90+pZwcbd6wHw9ZHcj2rNA@mail.gmail.com>
  1 sibling, 1 reply; 12+ messages in thread
From: Eric Sunshine @ 2019-02-08 21:40 UTC (permalink / raw)
  To: Max Kirillov; +Cc: Git List, Michael Haggerty

On Fri, Feb 8, 2019 at 4:22 PM Max Kirillov <max@max630.net> wrote:
> If packed-refs is marked as sorted but not really sorted it causes
> very hard to comprehend misbehavior of reference resolving - a reference
> is reported as not found, though it is listed by commands which output
> the references list.
>
> As the scope of the issue is not clear, make it visible by failing
> pack-refs command - the one which would not suffer performance penalty
> to verify the sortedness - when it encounters not really sorted existing
> data.
>
> Signed-off-by: Max Kirillov <max@max630.net>
> ---
> diff --git a/refs/packed-backend.c b/refs/packed-backend.c
> @@ -1137,6 +1138,21 @@ static int write_with_updates(struct packed_ref_store *refs,
> +               if (iter)
> +               {
> +                       if (prev_ref.len &&  strcmp(prev_ref.buf, iter->refname) > 0)
> +                       {
> +                               [...]
> +                               strbuf_release(&prev_ref);
> +                               goto error;
> +                       }
> +
> +                       strbuf_init(&prev_ref, 0);
> +                       strbuf_addstr(&prev_ref, iter->refname);
> +               }

The call to strbuf_init() is leaking the allocated strbuf buffer each
time through the loop. The typical way to re-use a strbuf, and the way
you should do it here, is strbuf_reset().

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2] pack-refs: fail on falsely sorted packed-refs
       [not found]     ` <CAMy9T_EX_L80-V4zD626nFCxw6qa90+pZwcbd6wHw9ZHcj2rNA@mail.gmail.com>
@ 2019-02-13  4:23       ` Max Kirillov
  0 siblings, 0 replies; 12+ messages in thread
From: Max Kirillov @ 2019-02-13  4:23 UTC (permalink / raw)
  To: Michael Haggerty; +Cc: Max Kirillov, Git List, Eric Sunshine

On Mon, Feb 11, 2019 at 08:24:46PM +0100, Michael Haggerty wrote:
> The change to `write_with_updates()` doesn't only affect `pack-refs`.
> That function is also called when the `packed-refs` file has to be
> rewritten when a packed reference is deleted. This is another thing
> that you could test.

Ok, I'll check ti and add to the tests.

> But that also means that fairly common commands like `git branch -d`
> could be slowed down by this change. I doubt that the slowdown is
> prohibitive, but it would be great to see numbers to prove it. For
> example, create a repository with a lot (say 10000) references, pack
> them, then run `git branch -d` to delete one of them. Benchmark that
> once with master and once with your modification and document the
> difference.

At my hardware, with 1M references, "branch -d" takes 0.31s
of user time before change vs 0.38 after change. Should I
mention it in the commit message?

>> +test_expect_success 'off-order branch not found' '
>> +       test_must_fail git show-ref --verify --quiet refs/heads/b00
>> +'
> 
> I don't think that the above test makes sense. We don't *guarantee*
> that an out-of-order reference won't be found. That is an
> implementation detail that we are free to change. I think that it
> would be OK to just omit this test.

Thanks, will remove this one

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2] pack-refs: fail on falsely sorted packed-refs
  2019-02-08 21:40     ` Eric Sunshine
@ 2019-02-13  4:24       ` Max Kirillov
  0 siblings, 0 replies; 12+ messages in thread
From: Max Kirillov @ 2019-02-13  4:24 UTC (permalink / raw)
  To: Eric Sunshine; +Cc: Max Kirillov, Git List, Michael Haggerty

On Fri, Feb 08, 2019 at 04:40:28PM -0500, Eric Sunshine wrote:
> The call to strbuf_init() is leaking the allocated strbuf buffer each
> time through the loop. The typical way to re-use a strbuf, and the way
> you should do it here, is strbuf_reset().

Thank you! Will fix it

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [RFC PATCH] pack-refs: fail on falsely sorted packed-refs
  2019-01-30 23:13 [RFC PATCH] pack-refs: fail on falsely sorted packed-refs Max Kirillov
  2019-01-30 23:31 ` Eric Sunshine
@ 2019-02-13 10:08 ` Ævar Arnfjörð Bjarmason
  2019-02-13 10:56   ` SZEDER Gábor
                     ` (2 more replies)
  1 sibling, 3 replies; 12+ messages in thread
From: Ævar Arnfjörð Bjarmason @ 2019-02-13 10:08 UTC (permalink / raw)
  To: Max Kirillov; +Cc: Michael Haggerty, git


On Thu, Jan 31 2019, Max Kirillov wrote:

> If packed-refs is marked as sorted but not really sorted it causes
> very hard to comprehend misbehavior of reference resolving - a reference
> is reported as not found.
>
> As the scope of the issue is not clear, make it visible by failing
> pack-refs command - the one which would not suffer performance penalty
> to verify the sortedness - when it encounters not really sorted existing
> data.
>
> Signed-off-by: Max Kirillov <max@max630.net>
> ---
> I happened to have a not really sorted packed-refs file. As you might guess,
> it was quite wtf-ing experience. It worked, mostly, but there was one branch
> which just did not resolve, regardless of existing and being presented in
> for-each-refs output.
>
> I don't know where the corruption came from. I should admit it could even be a manual
> editing but last time I did it (in that reporitory) was several years ago so it is unlikely.
>
> I am not sure what should be the proper fix. I did a minimal detection, so that
> it does not go unnoticed. Probably next step would be either fixing in `git fsck` call.
>
>  refs/packed-backend.c               | 15 +++++++++++++++
>  t/t3212-pack-refs-broken-sorting.sh | 26 ++++++++++++++++++++++++++
>  2 files changed, 41 insertions(+)
>  create mode 100755 t/t3212-pack-refs-broken-sorting.sh

This is not an area I'm very familiar with. So mostly commeting on
cosmetic issues with the patch. FWIW the "years back" issue you had
could be that an issue didn't manifest until now, i.e. in a sorted file
format you can get lucky and not see corruption for a while with a
random insert.

> diff --git a/refs/packed-backend.c b/refs/packed-backend.c
> index c01c7f5901..505f4535b5 100644
> --- a/refs/packed-backend.c
> +++ b/refs/packed-backend.c
> @@ -1088,6 +1088,7 @@ static int write_with_updates(struct packed_ref_store *refs,
>  	FILE *out;
>  	struct strbuf sb = STRBUF_INIT;
>  	char *packed_refs_path;
> +	struct strbuf prev_ref = STRBUF_INIT;
>
>  	if (!is_lock_file_locked(&refs->lock))
>  		BUG("write_with_updates() called while unlocked");
> @@ -1137,6 +1138,20 @@ static int write_with_updates(struct packed_ref_store *refs,
>  		struct ref_update *update = NULL;
>  		int cmp;
>
> +		if (iter)
> +		{
> +			if (prev_ref.len &&  strcmp(prev_ref.buf, iter->refname) > 0)

You have an extra two whitespaces after "&&" there.

> +			{
> +				strbuf_addf(err, "broken sorting in packed-refs: '%s' > '%s'",
> +					    prev_ref.buf,
> +					    iter->refname);
> +				goto error;
> +			}
> +
> +			strbuf_init(&prev_ref, 0);
> +			strbuf_addstr(&prev_ref, iter->refname);
> +		}
> +
>  		if (i >= updates->nr) {
>  			cmp = -1;
>  		} else {
> diff --git a/t/t3212-pack-refs-broken-sorting.sh b/t/t3212-pack-refs-broken-sorting.sh
> new file mode 100755
> index 0000000000..37a98a6fb1
> --- /dev/null
> +++ b/t/t3212-pack-refs-broken-sorting.sh
> @@ -0,0 +1,26 @@
> +#!/bin/sh
> +
> +test_description='tests for the falsely sorted refs'
> +. ./test-lib.sh
> +
> +test_expect_success 'setup' '
> +	git commit --allow-empty -m commit &&

Looks like just "test_commit A" would do here.

> +	for num in $(test_seq 10)
> +	do
> +		git branch b$(printf "%02d" $num) || break
> +	done &&

We can fail in these sorts of loops. There's a few ways to deal with
that. Doing it like this with "break" will still silently hide errors:

    $ for i in $(seq 1 3); do if test $i = 2; then false || break; else echo $i; fi; done && echo success
    1
    success

One way to deal with that is to e.g. before the loop say "had_fail=",
then set "had_fail=t" in that "||" case, and test for it after the loop.

But perhaps in this case we're better off e.g. running for-each-ref
after and either using test_cmp or test_line_count to see that we
created the refs successfully?

> +	git pack-refs --all &&
> +	head_object=$(git rev-parse HEAD) &&
> +	printf "$head_object refs/heads/b00\\n" >>.git/packed-refs &&

Looks like just "echo" here would be simpler since we only use printf to
add a newline.

> +	git branch b11
> +'
> +
> +test_expect_success 'off-order branch not found' '
> +	! git show-ref --verify --quiet refs/heads/b00
> +'
> +
> +test_expect_success 'subsequent pack-refs fails' '
> +	! git pack-refs --all
> +'

Instead of "! git ..." use "test_must_fail git ...". See t/README. This
will hide e.g. segfaults.

Also, perhaps:

    test_must_fail git ... 2>stderr &&
    grep "broken sorting in packed-refs" stderr

Would make this more obvious/self-documenting so we know we failed due
to that issue in particular.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [RFC PATCH] pack-refs: fail on falsely sorted packed-refs
  2019-02-13 10:08 ` [RFC PATCH] " Ævar Arnfjörð Bjarmason
@ 2019-02-13 10:56   ` SZEDER Gábor
  2019-02-23  7:10     ` Max Kirillov
  2019-02-14  6:06   ` Jeff King
  2019-02-23  7:09   ` Max Kirillov
  2 siblings, 1 reply; 12+ messages in thread
From: SZEDER Gábor @ 2019-02-13 10:56 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: Max Kirillov, Michael Haggerty, git

On Wed, Feb 13, 2019 at 11:08:01AM +0100, Ævar Arnfjörð Bjarmason wrote:
> 
> On Thu, Jan 31 2019, Max Kirillov wrote:

> >  refs/packed-backend.c               | 15 +++++++++++++++
> >  t/t3212-pack-refs-broken-sorting.sh | 26 ++++++++++++++++++++++++++
> >  2 files changed, 41 insertions(+)
> >  create mode 100755 t/t3212-pack-refs-broken-sorting.sh
> 
> This is not an area I'm very familiar with. So mostly commeting on
> cosmetic issues with the patch. 

Just two quick comments in addition to Ævar's:

> > @@ -1137,6 +1138,20 @@ static int write_with_updates(struct packed_ref_store *refs,
> >  		struct ref_update *update = NULL;
> >  		int cmp;
> >
> > +		if (iter)
> > +		{

According to our CodingGuidelines, the opening bracket should go on
the same line as the condition, i.e.

  if (iter) {

> > diff --git a/t/t3212-pack-refs-broken-sorting.sh b/t/t3212-pack-refs-broken-sorting.sh
> > new file mode 100755
> > index 0000000000..37a98a6fb1
> > --- /dev/null
> > +++ b/t/t3212-pack-refs-broken-sorting.sh
> > @@ -0,0 +1,26 @@
> > +#!/bin/sh
> > +
> > +test_description='tests for the falsely sorted refs'
> > +. ./test-lib.sh
> > +
> > +test_expect_success 'setup' '
> > +	git commit --allow-empty -m commit &&
> 
> Looks like just "test_commit A" would do here.
> 
> > +	for num in $(test_seq 10)
> > +	do
> > +		git branch b$(printf "%02d" $num) || break
> > +	done &&
> 
> We can fail in these sorts of loops. There's a few ways to deal with
> that. Doing it like this with "break" will still silently hide errors:
> 
>     $ for i in $(seq 1 3); do if test $i = 2; then false || break; else echo $i; fi; done && echo success
>     1
>     success
> 
> One way to deal with that is to e.g. before the loop say "had_fail=",
> then set "had_fail=t" in that "||" case, and test for it after the loop.

No, you can simply do 'cmd1 && cmd2 || return 1' in the body of the
for loop; that's why we have a separate test_eval_inner() helper
function in test-lib.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [RFC PATCH] pack-refs: fail on falsely sorted packed-refs
  2019-02-13 10:08 ` [RFC PATCH] " Ævar Arnfjörð Bjarmason
  2019-02-13 10:56   ` SZEDER Gábor
@ 2019-02-14  6:06   ` Jeff King
  2019-02-23  7:09   ` Max Kirillov
  2 siblings, 0 replies; 12+ messages in thread
From: Jeff King @ 2019-02-14  6:06 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: Max Kirillov, Michael Haggerty, git

On Wed, Feb 13, 2019 at 11:08:01AM +0100, Ævar Arnfjörð Bjarmason wrote:

> > I happened to have a not really sorted packed-refs file. As you might guess,
> > it was quite wtf-ing experience. It worked, mostly, but there was one branch
> > which just did not resolve, regardless of existing and being presented in
> > for-each-refs output.
> >
> > I don't know where the corruption came from. I should admit it could even be a manual
> > editing but last time I did it (in that reporitory) was several years ago so it is unlikely.
> >
> > I am not sure what should be the proper fix. I did a minimal detection, so that
> > it does not go unnoticed. Probably next step would be either fixing in `git fsck` call.
> >
> >  refs/packed-backend.c               | 15 +++++++++++++++
> >  t/t3212-pack-refs-broken-sorting.sh | 26 ++++++++++++++++++++++++++
> >  2 files changed, 41 insertions(+)
> >  create mode 100755 t/t3212-pack-refs-broken-sorting.sh
> 
> This is not an area I'm very familiar with. So mostly commeting on
> cosmetic issues with the patch. FWIW the "years back" issue you had
> could be that an issue didn't manifest until now, i.e. in a sorted file
> format you can get lucky and not see corruption for a while with a
> random insert.

It actually shouldn't be that old a breakage. Until 02b920f3f7
(read_packed_refs(): ensure that references are ordered when read,
2017-09-25), we did not assume the file was sorted (even though we
always wrote it out sorted). And we continue to not assume the file is
sorted unless it is written out with an explicit "sorted" trait in the
header (which we started doing in that commit, too).

So a years-old manual edit would not have the "sorted" trait, and should
not have manifested as a problem, even now.  Likewise for a years-old
bug. It would have to be a bug in a _new_ writer which writes out the
sorted trait.  If there is such a bug in our implementation, this would
be the first report we've seen. Given the number of times pack-refs has
been run, without further evidence I'm inclined to think it was some
weird manual edit, or maybe an alternate implementation (though one
would _hope_ they would not write out the sorted trait without actually
sorting!).

I agree with all of the cosmetic issues you mentioned. As far as what
the patch itself does, I think it's OK. We could probably go further and
actually sort it (or even just write it out without a "sorted" trait,
which means the next read would load it all into memory and sort it).
That's a little friendlier, since just dying leaves the user to fix it
up themselves. But given that we expect this code to trigger
approximately never, it's probably not worth spending much time on a
fancy solution.

-Peff

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [RFC PATCH] pack-refs: fail on falsely sorted packed-refs
  2019-02-13 10:08 ` [RFC PATCH] " Ævar Arnfjörð Bjarmason
  2019-02-13 10:56   ` SZEDER Gábor
  2019-02-14  6:06   ` Jeff King
@ 2019-02-23  7:09   ` Max Kirillov
  2 siblings, 0 replies; 12+ messages in thread
From: Max Kirillov @ 2019-02-23  7:09 UTC (permalink / raw)
  To: Ævar Arnfjörð Bjarmason
  Cc: Max Kirillov, Michael Haggerty, git

On Wed, Feb 13, 2019 at 11:08:01AM +0100, Ævar Arnfjörð Bjarmason wrote:
> You have an extra two whitespaces after "&&" there.

Thanks, will check it.

>> +	git commit --allow-empty -m commit &&
> Looks like just "test_commit A" would do here.

About this I'm not sure. AFAIK test_commit does lots of stuff,
so can it be considered "just" compared to "commit
--allow-empty" or the opposite? I could replace it with
test_commit for uniformity reason though.

> We can fail in these sorts of loops. There's a few ways to deal with
> that. Doing it like this with "break" will still silently hide errors:

Thanks, this was pointed point

>> +	printf "$head_object refs/heads/b00\\n" >>.git/packed-refs &&
> 
> Looks like just "echo" here would be simpler since we only use printf to
> add a newline.

Could it happen so that "echo" adds '\r\n' at Windows? I
could use echo.

> Instead of "! git ..." use "test_must_fail git ...". See t/README. This
> will hide e.g. segfaults.

Thanks, this was pointed point

> Also, perhaps:
> 
>     test_must_fail git ... 2>stderr &&
>     grep "broken sorting in packed-refs" stderr
> 
> Would make this more obvious/self-documenting so we know we failed due
> to that issue in particular.

Thanks, will change it

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [RFC PATCH] pack-refs: fail on falsely sorted packed-refs
  2019-02-13 10:56   ` SZEDER Gábor
@ 2019-02-23  7:10     ` Max Kirillov
  0 siblings, 0 replies; 12+ messages in thread
From: Max Kirillov @ 2019-02-23  7:10 UTC (permalink / raw)
  To: SZEDER Gábor
  Cc: Ævar Arnfjörð Bjarmason, Max Kirillov,
	Michael Haggerty, git

On Wed, Feb 13, 2019 at 11:56:16AM +0100, SZEDER Gábor wrote:
>>> +		if (iter)
>>> +		{
> 
> According to our CodingGuidelines, the opening bracket should go on
> the same line as the condition, i.e.
> 
>   if (iter) {

Oh, thanks. I must have been professionally deformed.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2019-02-23  7:11 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-30 23:13 [RFC PATCH] pack-refs: fail on falsely sorted packed-refs Max Kirillov
2019-01-30 23:31 ` Eric Sunshine
2019-01-31  8:21   ` Max Kirillov
2019-02-08 21:22   ` [PATCH v2] " Max Kirillov
2019-02-08 21:40     ` Eric Sunshine
2019-02-13  4:24       ` Max Kirillov
     [not found]     ` <CAMy9T_EX_L80-V4zD626nFCxw6qa90+pZwcbd6wHw9ZHcj2rNA@mail.gmail.com>
2019-02-13  4:23       ` Max Kirillov
2019-02-13 10:08 ` [RFC PATCH] " Ævar Arnfjörð Bjarmason
2019-02-13 10:56   ` SZEDER Gábor
2019-02-23  7:10     ` Max Kirillov
2019-02-14  6:06   ` Jeff King
2019-02-23  7:09   ` Max Kirillov

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).