threads / patch / 54917

patchrefs: allow @{n} to work with n-sized reflog

Subject: [PATCH] refs: allow @{n} to work with n-sized reflog

## tl;dr

21 messages between Jan 2, 2021 and Jan 12, 2021. Diffs are folded; open one to read it.

replies: 20people: 5as markdown or json

Denton Liu· Jan 2, 2021, 01:36 UTC · lore
This sequence works
	$ git checkout -b newbranch
	$ git commit --allow-empty -m one
	$ git show -s newbranch@{1}

and shows the state that was immediately after the newbranch was created.

But then if you do
	$ git reflog expire --expire=now refs/heads/newbranch
	$ git commit --allow=empty -m two
	$ git show -s newbranch@{1}
you'd be scolded with
	fatal: log for 'newbranch' only has 1 entries

While it is true that it has only 1 entry, we have enough information in that single entry that records the transition between the state in which the tip of the branch was pointing at commit 'one' to the new commit 'two' built on it, so we should be able to answer "what object newbranch was pointing at?". But we refuse to do so.

Make @{0} the special case where we use the new side to look up that entry. Otherwise, look up @{n} using the old side of the (n-1)th entry of the reflog.

Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 refs.c                      | 48 +++++++++++++++++++++++++++----------
 t/t1503-rev-parse-verify.sh | 17 +++++++++++--
 2 files changed, 50 insertions(+), 15 deletions(-)
Show changes to 2 files +50 −15

refs.c, t/t1503-rev-parse-verify.sh

diff --git a/refs.c b/refs.c
index 13dc2c3291..c35c61a009 100644
--- a/refs.c
+++ b/refs.c
@@ -887,12 +887,16 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
 		const char *message, void *cb_data)
 {
 	struct read_ref_at_cb *cb = cb_data;
+	int at_indexed_ent;
 
 	cb->reccnt++;
 	cb->tz = tz;
 	cb->date = timestamp;
 
-	if (timestamp <= cb->at_time || cb->cnt == 0) {
+	if (cb->cnt > 0)
+		cb->cnt--;
+	at_indexed_ent = cb->cnt == 0 && !is_null_oid(ooid);
+	if (timestamp <= cb->at_time || at_indexed_ent) {
 		if (cb->msg)
 			*cb->msg = xstrdup(message);
 		if (cb->cutoff_time)
@@ -905,28 +909,41 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
 		 * we have not yet updated cb->[n|o]oid so they still
 		 * hold the values for the previous record.
 		 */
-		if (!is_null_oid(&cb->ooid)) {
-			oidcpy(cb->oid, noid);
-			if (!oideq(&cb->ooid, noid))
-				warning(_("log for ref %s has gap after %s"),
+		if (!is_null_oid(&cb->ooid) && !oideq(&cb->ooid, noid))
+			warning(_("log for ref %s has gap after %s"),
 					cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
-		}
-		else if (cb->date == cb->at_time)
+		if (at_indexed_ent)
+			oidcpy(cb->oid, ooid);
+		else if (!is_null_oid(&cb->ooid) || cb->date == cb->at_time)
 			oidcpy(cb->oid, noid);
 		else if (!oideq(noid, cb->oid))
 			warning(_("log for ref %s unexpectedly ended on %s"),
 				cb->refname, show_date(cb->date, cb->tz,
 						       DATE_MODE(RFC2822)));
-		oidcpy(&cb->ooid, ooid);
-		oidcpy(&cb->noid, noid);
 		cb->found_it = 1;
-		return 1;
 	}
 	oidcpy(&cb->ooid, ooid);
 	oidcpy(&cb->noid, noid);
-	if (cb->cnt > 0)
-		cb->cnt--;
-	return 0;
+	return cb->found_it;
+}
+
+static int read_ref_at_ent_newest(struct object_id *ooid, struct object_id *noid,
+				  const char *email, timestamp_t timestamp,
+				  int tz, const char *message, void *cb_data)
+{
+	struct read_ref_at_cb *cb = cb_data;
+
+	if (cb->msg)
+		*cb->msg = xstrdup(message);
+	if (cb->cutoff_time)
+		*cb->cutoff_time = timestamp;
+	if (cb->cutoff_tz)
+		*cb->cutoff_tz = tz;
+	if (cb->cutoff_cnt)
+		*cb->cutoff_cnt = cb->reccnt;
+	oidcpy(cb->oid, noid);
+	/* We just want the first entry */
+	return 1;
 }
 
 static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
@@ -967,6 +984,11 @@ int read_ref_at(struct ref_store *refs, const char *refname,
 	cb.cutoff_cnt = cutoff_cnt;
 	cb.oid = oid;
 
+	if (cb.cnt == 0) {
+		refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent_newest, &cb);
+		return 0;
+	}
+
 	refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent, &cb);
 
 	if (!cb.reccnt) {
diff --git a/t/t1503-rev-parse-verify.sh b/t/t1503-rev-parse-verify.sh
index dc9fe3cbf1..ed4a366e85 100755
--- a/t/t1503-rev-parse-verify.sh
+++ b/t/t1503-rev-parse-verify.sh
@@ -86,8 +86,8 @@ test_expect_success 'fails silently when using -q' '
 test_expect_success 'fails silently when using -q with deleted reflogs' '
 	ref=$(git rev-parse HEAD) &&
 	git update-ref --create-reflog -m "message for refs/test" refs/test "$ref" &&
-	git reflog delete --updateref --rewrite refs/test@{0} &&
-	test_must_fail git rev-parse -q --verify refs/test@{0} >error 2>&1 &&
+	git reflog delete --updateref --rewrite refs/test@{1} &&
+	test_must_fail git rev-parse -q --verify refs/test@{1} >error 2>&1 &&
 	test_must_be_empty error
 '
 
@@ -139,6 +139,19 @@ test_expect_success 'master@{n} for various n' '
 	test_must_fail git rev-parse --verify master@{$Np1}
 '
 
+test_expect_success '@{1} works with only one reflog entry' '
+	git checkout -B newbranch &&
+	git reflog expire --expire=now refs/heads/newbranch &&
+	git commit --allow-empty -mexpired &&
+	git rev-parse --verify newbranch@{1}
+'
+
+test_expect_success '@{0} works with empty reflog' '
+	git checkout -B newbranch &&
+	git reflog expire --expire=now refs/heads/newbranch &&
+	git rev-parse --verify newbranch@{0}
+'
+
 test_expect_success SYMLINKS 'ref resolution not confused by broken symlinks' '
 	ln -s does-not-exist .git/refs/heads/broken &&
 	test_must_fail git rev-parse --verify broken
-- 
2.30.0
Martin Ågren· Jan 2, 2021, 22:30 UTC · re: Denton Liu · lore

Re: [PATCH] refs: allow @{n} to work with n-sized reflog

On Sat, 2 Jan 2021 at 02:41, Denton Liu <liu.denton@gmail.com> wrote:
Show 17 quoted lines
>
> But then if you do
>
>         $ git reflog expire --expire=now refs/heads/newbranch
>         $ git commit --allow=empty -m two
>         $ git show -s newbranch@{1}
>
> you'd be scolded with
>
>         fatal: log for 'newbranch' only has 1 entries
>
> While it is true that it has only 1 entry, we have enough
> information in that single entry that records the transition between
> the state in which the tip of the branch was pointing at commit
> 'one' to the new commit 'two' built on it, so we should be able to
> answer "what object newbranch was pointing at?". But we refuse to
> do so.
The basic idea seems to make sense to me...
> Make @{0} the special case where we use the new side to look up that
> entry. Otherwise, look up @{n} using the old side of the (n-1)th entry
> of the reflog.
Show 17 quoted lines
> --- a/refs.c
> +++ b/refs.c
> @@ -887,12 +887,16 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
>                 const char *message, void *cb_data)
>  {
>         struct read_ref_at_cb *cb = cb_data;
> +       int at_indexed_ent;
>
>         cb->reccnt++;
>         cb->tz = tz;
>         cb->date = timestamp;
>
> -       if (timestamp <= cb->at_time || cb->cnt == 0) {
> +       if (cb->cnt > 0)
> +               cb->cnt--;
> +       at_indexed_ent = cb->cnt == 0 && !is_null_oid(ooid);
> +       if (timestamp <= cb->at_time || at_indexed_ent) {
... but I can't really say anything about the implementation.
> +test_expect_success '@{1} works with only one reflog entry' '
> +       git checkout -B newbranch &&
> +       git reflog expire --expire=now refs/heads/newbranch &&
> +       git commit --allow-empty -mexpired &&
Minor nit: not sure about "expired" -- maybe "first after expiration".
> +       git rev-parse --verify newbranch@{1}
> +'

Should this capture the output and compare it to, e.g., `git rev-parse newbranch^`?

Show 5 quoted lines
> +test_expect_success '@{0} works with empty reflog' '
> +       git checkout -B newbranch &&
> +       git reflog expire --expire=now refs/heads/newbranch &&
> +       git rev-parse --verify newbranch@{0}
> +'

Same here, but comparing to `git rev-parse newbranch`? Both of these checks seem worthwhile to make sure that we don't just answer *something*, but that we actually get the right answer, as per your "redefinition".

Speaking of redefinition, does this warrant an update of the documentation? That's a genuine question -- having browsed git-reflog(1) and gitrevisions(7) a bit, I'm not sure.

Martin
Denton Liu· Jan 3, 2021, 01:24 UTC · re: Denton Liu · lore

Re: [PATCH] refs: allow @{n} to work with n-sized reflog

On Fri, Jan 01, 2021 at 05:36:06PM -0800, Denton Liu wrote:
Show 32 quoted lines
> This sequence works
> 
> 	$ git checkout -b newbranch
> 	$ git commit --allow-empty -m one
> 	$ git show -s newbranch@{1}
> 
> and shows the state that was immediately after the newbranch was
> created.
> 
> But then if you do
> 
> 	$ git reflog expire --expire=now refs/heads/newbranch
> 	$ git commit --allow=empty -m two
> 	$ git show -s newbranch@{1}
> 
> you'd be scolded with
> 
> 	fatal: log for 'newbranch' only has 1 entries
> 
> While it is true that it has only 1 entry, we have enough
> information in that single entry that records the transition between
> the state in which the tip of the branch was pointing at commit
> 'one' to the new commit 'two' built on it, so we should be able to
> answer "what object newbranch was pointing at?". But we refuse to
> do so.
> 
> Make @{0} the special case where we use the new side to look up that
> entry. Otherwise, look up @{n} using the old side of the (n-1)th entry
> of the reflog.
> 
> Suggested-by: Junio C Hamano <gitster@pobox.com>
> Signed-off-by: Denton Liu <liu.denton@gmail.com>

I forgot to mention that the original thread that spawned this idea is here:

	https://lore.kernel.org/git/xmqqzh8zgcfp.fsf@gitster.c.googlers.com/
SZEDER Gábor· Jan 5, 2021, 08:52 UTC · re: Denton Liu · lore

Re: [PATCH] refs: allow @{n} to work with n-sized reflog

On Fri, Jan 01, 2021 at 05:36:06PM -0800, Denton Liu wrote:
Show 25 quoted lines
> This sequence works
> 
> 	$ git checkout -b newbranch
> 	$ git commit --allow-empty -m one
> 	$ git show -s newbranch@{1}
> 
> and shows the state that was immediately after the newbranch was
> created.
> 
> But then if you do
> 
> 	$ git reflog expire --expire=now refs/heads/newbranch
> 	$ git commit --allow=empty -m two
> 	$ git show -s newbranch@{1}
> 
> you'd be scolded with
> 
> 	fatal: log for 'newbranch' only has 1 entries
> 
> While it is true that it has only 1 entry, we have enough
> information in that single entry that records the transition between
> the state in which the tip of the branch was pointing at commit
> 'one' to the new commit 'two' built on it, so we should be able to
> answer "what object newbranch was pointing at?". But we refuse to
> do so.

Great! I've run into this issue quite a while ago with regular 'git gc' expiring too old reflog entries, and wondered while the @{N} notation errored out while the information was clearly still there in the reflog.

https://public-inbox.org/git/20130619125059.GD20052@goldbirke/T/#u
Show 16 quoted lines
> @@ -139,6 +139,19 @@ test_expect_success 'master@{n} for various n' '
>  	test_must_fail git rev-parse --verify master@{$Np1}
>  '
>  
> +test_expect_success '@{1} works with only one reflog entry' '
> +	git checkout -B newbranch &&
> +	git reflog expire --expire=now refs/heads/newbranch &&
> +	git commit --allow-empty -mexpired &&
> +	git rev-parse --verify newbranch@{1}
> +'
> +
> +test_expect_success '@{0} works with empty reflog' '
> +	git checkout -B newbranch &&
> +	git reflog expire --expire=now refs/heads/newbranch &&
> +	git rev-parse --verify newbranch@{0}
> +'

I agree with Martin about these tests: not failing is one thing, but we should make sure that the right value is printed.

Show 6 quoted lines
>  test_expect_success SYMLINKS 'ref resolution not confused by broken symlinks' '
>  	ln -s does-not-exist .git/refs/heads/broken &&
>  	test_must_fail git rev-parse --verify broken
> -- 
> 2.30.0
> 
Junio C Hamano· Jan 6, 2021, 05:55 UTC · re: Denton Liu · lore

Re: [PATCH] refs: allow @{n} to work with n-sized reflog

Denton Liu <liu.denton@gmail.com> writes:
Show 25 quoted lines
> This sequence works
>
> 	$ git checkout -b newbranch
> 	$ git commit --allow-empty -m one
> 	$ git show -s newbranch@{1}
>
> and shows the state that was immediately after the newbranch was
> created.
>
> But then if you do
>
> 	$ git reflog expire --expire=now refs/heads/newbranch
> 	$ git commit --allow=empty -m two
> 	$ git show -s newbranch@{1}
>
> you'd be scolded with
>
> 	fatal: log for 'newbranch' only has 1 entries
>
> While it is true that it has only 1 entry, we have enough
> information in that single entry that records the transition between
> the state in which the tip of the branch was pointing at commit
> 'one' to the new commit 'two' built on it, so we should be able to
> answer "what object newbranch was pointing at?". But we refuse to
> do so.
Yeah, I am often hit and irritated by this behaviour.
> Make @{0} the special case where we use the new side to look up that
> entry. Otherwise, look up @{n} using the old side of the (n-1)th entry
> of the reflog.
OK.
Show 18 quoted lines
> diff --git a/refs.c b/refs.c
> index 13dc2c3291..c35c61a009 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -887,12 +887,16 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
>  		const char *message, void *cb_data)
>  {
>  	struct read_ref_at_cb *cb = cb_data;
> +	int at_indexed_ent;
>  
>  	cb->reccnt++;
>  	cb->tz = tz;
>  	cb->date = timestamp;
>  
> -	if (timestamp <= cb->at_time || cb->cnt == 0) {
> +	if (cb->cnt > 0)
> +		cb->cnt--;
> +	at_indexed_ent = cb->cnt == 0 && !is_null_oid(ooid);

The code treats two cases identically (i.e. the case where cb->cnt was originally zero, and one). Is that intended?

I thought the code was to special case only <ref>@{0}, but with this conditional decrement, cb->cnt==0 would not be usable by the rest of the code as the "we must read the new side instead" signal. Is that why null-ness of ooid is also tested here? It is hard to tell the intention because "at_indexed_ent" does not quite tell me what the code wants to use the variable for.

Show 56 quoted lines
> +	if (timestamp <= cb->at_time || at_indexed_ent) {
>  		if (cb->msg)
>  			*cb->msg = xstrdup(message);
>  		if (cb->cutoff_time)
> @@ -905,28 +909,41 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
>  		 * we have not yet updated cb->[n|o]oid so they still
>  		 * hold the values for the previous record.
>  		 */
> -		if (!is_null_oid(&cb->ooid)) {
> -			oidcpy(cb->oid, noid);
> -			if (!oideq(&cb->ooid, noid))
> -				warning(_("log for ref %s has gap after %s"),
> +		if (!is_null_oid(&cb->ooid) && !oideq(&cb->ooid, noid))
> +			warning(_("log for ref %s has gap after %s"),
>  					cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
> -		}
> -		else if (cb->date == cb->at_time)
> +		if (at_indexed_ent)
> +			oidcpy(cb->oid, ooid);
> +		else if (!is_null_oid(&cb->ooid) || cb->date == cb->at_time)
>  			oidcpy(cb->oid, noid);
>  		else if (!oideq(noid, cb->oid))
>  			warning(_("log for ref %s unexpectedly ended on %s"),
>  				cb->refname, show_date(cb->date, cb->tz,
>  						       DATE_MODE(RFC2822)));
> -		oidcpy(&cb->ooid, ooid);
> -		oidcpy(&cb->noid, noid);
>  		cb->found_it = 1;
> -		return 1;
>  	}
>  	oidcpy(&cb->ooid, ooid);
>  	oidcpy(&cb->noid, noid);
> -	if (cb->cnt > 0)
> -		cb->cnt--;
> -	return 0;
> +	return cb->found_it;
> +}
> +
> +static int read_ref_at_ent_newest(struct object_id *ooid, struct object_id *noid,
> +				  const char *email, timestamp_t timestamp,
> +				  int tz, const char *message, void *cb_data)
> +{
> +	struct read_ref_at_cb *cb = cb_data;
> +
> +	if (cb->msg)
> +		*cb->msg = xstrdup(message);
> +	if (cb->cutoff_time)
> +		*cb->cutoff_time = timestamp;
> +	if (cb->cutoff_tz)
> +		*cb->cutoff_tz = tz;
> +	if (cb->cutoff_cnt)
> +		*cb->cutoff_cnt = cb->reccnt;
> +	oidcpy(cb->oid, noid);
> +	/* We just want the first entry */
> +	return 1;
>  }

The similarity of this to read_ref_at_ent_oldest is somehow striking. Do we really need to invent a new callback?

Show 48 quoted lines
>  static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
> @@ -967,6 +984,11 @@ int read_ref_at(struct ref_store *refs, const char *refname,
>  	cb.cutoff_cnt = cutoff_cnt;
>  	cb.oid = oid;
>  
> +	if (cb.cnt == 0) {
> +		refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent_newest, &cb);
> +		return 0;
> +	}
> +
>  	refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent, &cb);
>  
>  	if (!cb.reccnt) {
> diff --git a/t/t1503-rev-parse-verify.sh b/t/t1503-rev-parse-verify.sh
> index dc9fe3cbf1..ed4a366e85 100755
> --- a/t/t1503-rev-parse-verify.sh
> +++ b/t/t1503-rev-parse-verify.sh
> @@ -86,8 +86,8 @@ test_expect_success 'fails silently when using -q' '
>  test_expect_success 'fails silently when using -q with deleted reflogs' '
>  	ref=$(git rev-parse HEAD) &&
>  	git update-ref --create-reflog -m "message for refs/test" refs/test "$ref" &&
> -	git reflog delete --updateref --rewrite refs/test@{0} &&
> -	test_must_fail git rev-parse -q --verify refs/test@{0} >error 2>&1 &&
> +	git reflog delete --updateref --rewrite refs/test@{1} &&
> +	test_must_fail git rev-parse -q --verify refs/test@{1} >error 2>&1 &&
>  	test_must_be_empty error
>  '
>  
> @@ -139,6 +139,19 @@ test_expect_success 'master@{n} for various n' '
>  	test_must_fail git rev-parse --verify master@{$Np1}
>  '
>  
> +test_expect_success '@{1} works with only one reflog entry' '
> +	git checkout -B newbranch &&
> +	git reflog expire --expire=now refs/heads/newbranch &&
> +	git commit --allow-empty -mexpired &&
> +	git rev-parse --verify newbranch@{1}
> +'
> +
> +test_expect_success '@{0} works with empty reflog' '
> +	git checkout -B newbranch &&
> +	git reflog expire --expire=now refs/heads/newbranch &&
> +	git rev-parse --verify newbranch@{0}
> +'
> +
>  test_expect_success SYMLINKS 'ref resolution not confused by broken symlinks' '
>  	ln -s does-not-exist .git/refs/heads/broken &&
>  	test_must_fail git rev-parse --verify broken
Denton Liu· Jan 6, 2021, 08:25 UTC · re: Junio C Hamano · lore

Re: [PATCH] refs: allow @{n} to work with n-sized reflog

Hi Junio,
On Tue, Jan 05, 2021 at 09:55:29PM -0800, Junio C Hamano wrote:
Show 29 quoted lines
> Denton Liu <liu.denton@gmail.com> writes:
> 
> > This sequence works
> >
> > 	$ git checkout -b newbranch
> > 	$ git commit --allow-empty -m one
> > 	$ git show -s newbranch@{1}
> >
> > and shows the state that was immediately after the newbranch was
> > created.
> >
> > But then if you do
> >
> > 	$ git reflog expire --expire=now refs/heads/newbranch
> > 	$ git commit --allow=empty -m two
> > 	$ git show -s newbranch@{1}
> >
> > you'd be scolded with
> >
> > 	fatal: log for 'newbranch' only has 1 entries
> >
> > While it is true that it has only 1 entry, we have enough
> > information in that single entry that records the transition between
> > the state in which the tip of the branch was pointing at commit
> > 'one' to the new commit 'two' built on it, so we should be able to
> > answer "what object newbranch was pointing at?". But we refuse to
> > do so.
> 
> Yeah, I am often hit and irritated by this behaviour.
Yep, this was inspired by one of your emails ;)
Show 21 quoted lines
> > diff --git a/refs.c b/refs.c
> > index 13dc2c3291..c35c61a009 100644
> > --- a/refs.c
> > +++ b/refs.c
> > @@ -887,12 +887,16 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
> >  		const char *message, void *cb_data)
> >  {
> >  	struct read_ref_at_cb *cb = cb_data;
> > +	int at_indexed_ent;
> >  
> >  	cb->reccnt++;
> >  	cb->tz = tz;
> >  	cb->date = timestamp;
> >  
> > -	if (timestamp <= cb->at_time || cb->cnt == 0) {
> > +	if (cb->cnt > 0)
> > +		cb->cnt--;
> > +	at_indexed_ent = cb->cnt == 0 && !is_null_oid(ooid);
> 
> The code treats two cases identically (i.e. the case where cb->cnt
> was originally zero, and one).  Is that intended?

It shouldn't be possible for cb->cnt == 0 on the first iteration because there's a special-case check at [0]. As a result, it can only be -1 or >= 1 on the first iteration.

The -1 case happens when we're doing date-based lookup and that's what this if is intended to handle.

In the case where it's >= 1, we will always enter the if and it will always pre-decrement. This essentially gets us the n-1 behaviour.

Show 6 quoted lines
> I thought the code was to special case only <ref>@{0}, but with this
> conditional decrement, cb->cnt==0 would not be usable by the rest
> of the code as the "we must read the new side instead" signal. Is
> that why null-ness of ooid is also tested here?  It is hard to tell
> the intention because "at_indexed_ent" does not quite tell me what
> the code wants to use the variable for.

The null-ness of the ooid is needed because on the last entry of the reflog, ooid will be null so we should skip that.

"at_indexed_ent" is meant to signal when we are indexing the reflog numerically (as opposed to by date), we have arrived at the correct entry. If you have a more fitting name, I'm open to suggestions.

Show 59 quoted lines
> > +	if (timestamp <= cb->at_time || at_indexed_ent) {
> >  		if (cb->msg)
> >  			*cb->msg = xstrdup(message);
> >  		if (cb->cutoff_time)
> > @@ -905,28 +909,41 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
> >  		 * we have not yet updated cb->[n|o]oid so they still
> >  		 * hold the values for the previous record.
> >  		 */
> > -		if (!is_null_oid(&cb->ooid)) {
> > -			oidcpy(cb->oid, noid);
> > -			if (!oideq(&cb->ooid, noid))
> > -				warning(_("log for ref %s has gap after %s"),
> > +		if (!is_null_oid(&cb->ooid) && !oideq(&cb->ooid, noid))
> > +			warning(_("log for ref %s has gap after %s"),
> >  					cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
> > -		}
> > -		else if (cb->date == cb->at_time)
> > +		if (at_indexed_ent)
> > +			oidcpy(cb->oid, ooid);
> > +		else if (!is_null_oid(&cb->ooid) || cb->date == cb->at_time)
> >  			oidcpy(cb->oid, noid);
> >  		else if (!oideq(noid, cb->oid))
> >  			warning(_("log for ref %s unexpectedly ended on %s"),
> >  				cb->refname, show_date(cb->date, cb->tz,
> >  						       DATE_MODE(RFC2822)));
> > -		oidcpy(&cb->ooid, ooid);
> > -		oidcpy(&cb->noid, noid);
> >  		cb->found_it = 1;
> > -		return 1;
> >  	}
> >  	oidcpy(&cb->ooid, ooid);
> >  	oidcpy(&cb->noid, noid);
> > -	if (cb->cnt > 0)
> > -		cb->cnt--;
> > -	return 0;
> > +	return cb->found_it;
> > +}
> > +
> > +static int read_ref_at_ent_newest(struct object_id *ooid, struct object_id *noid,
> > +				  const char *email, timestamp_t timestamp,
> > +				  int tz, const char *message, void *cb_data)
> > +{
> > +	struct read_ref_at_cb *cb = cb_data;
> > +
> > +	if (cb->msg)
> > +		*cb->msg = xstrdup(message);
> > +	if (cb->cutoff_time)
> > +		*cb->cutoff_time = timestamp;
> > +	if (cb->cutoff_tz)
> > +		*cb->cutoff_tz = tz;
> > +	if (cb->cutoff_cnt)
> > +		*cb->cutoff_cnt = cb->reccnt;
> > +	oidcpy(cb->oid, noid);
> > +	/* We just want the first entry */
> > +	return 1;
> >  }
> 
> The similarity of this to read_ref_at_ent_oldest is somehow
> striking.  Do we really need to invent a new callback?

Unfortunately, yes. The alternative is to add a flag into `struct read_ref_at_cb` and we could conditionally choose whether or not to copy noid or ooid but this seems like the lesser of two evils.

The duplicated part,
	if (cb->msg)
		*cb->msg = xstrdup(message);
	if (cb->cutoff_time)
		*cb->cutoff_time = timestamp;
	if (cb->cutoff_tz)
		*cb->cutoff_tz = tz;
	if (cb->cutoff_cnt)
		*cb->cutoff_cnt = cb->reccnt;

is actually repeated three times -- once in each of the callbacks. I considered extracting factoring it out into a function but I was on the fence because the function would still have some duplication since it'd still require cb, message, timestamp and tz to all be passed in.

Show 10 quoted lines
> >  static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
> > @@ -967,6 +984,11 @@ int read_ref_at(struct ref_store *refs, const char *refname,
> >  	cb.cutoff_cnt = cutoff_cnt;
> >  	cb.oid = oid;
> >  
> > +	if (cb.cnt == 0) {
> > +		refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent_newest, &cb);
> > +		return 0;
> > +	}
> > +
[0]
> >  	refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent, &cb);
> >  
> >  	if (!cb.reccnt) {

Thanks, Denton

Junio C Hamano· Jan 6, 2021, 21:02 UTC · re: Denton Liu · lore

Re: [PATCH] refs: allow @{n} to work with n-sized reflog

Denton Liu <liu.denton@gmail.com> writes:
Show 14 quoted lines
>> > -	if (timestamp <= cb->at_time || cb->cnt == 0) {
>> > +	if (cb->cnt > 0)
>> > +		cb->cnt--;
>> > +	at_indexed_ent = cb->cnt == 0 && !is_null_oid(ooid);
>> 
>> The code treats two cases identically (i.e. the case where cb->cnt
>> was originally zero, and one).  Is that intended?
>
> It shouldn't be possible for cb->cnt == 0 on the first iteration
> because there's a special-case check at [0]. As a result, it can only be
> -1 or >= 1 on the first iteration.
>
> The -1 case happens when we're doing date-based lookup and that's what
> this if is intended to handle.

I knew about -1; it wasn't apparent that the caller won't call us with cnt==0. Perhaps it deserves a mention in an in-code comment.

> "at_indexed_ent" is meant to signal when we are indexing the reflog
> numerically (as opposed to by date), we have arrived at the correct
> entry. If you have a more fitting name, I'm open to suggestions.

When querying for <ref>@{24}, all the entries are indexed numerically (counted), not just the 24th one, and that contributed to my puzzlement.

I offhand do not think of a "name", but "at target", "found", "reached count", are phrases that come to my mind as starting points.

Thanks.
Denton Liu· Jan 6, 2021, 09:01 UTC · re: Denton Liu · lore

[PATCH v2 0/2] refs: allow @{n} to work with n-sized reflog

When there is only one reflog entry (perhaps caused by expiring the reflog and then making a single commit) @{1} errors out even though there is technically enough information to do the lookup. Look at the old side of the reflog instead of the new side so that this does not fail. This is explained in more detail in the commit of the last patch.

This idea was given by Junio at [0].
[0]: https://lore.kernel.org/git/xmqqzh8zgcfp.fsf@gitster.c.googlers.com/
Changes since v1:
* Factor out set_read_ref_cutoffs()
* Check the output of rev-parse to ensure that the intended commit is
  returned
Denton Liu (2):
  refs: factor out set_read_ref_cutoffs()
  refs: allow @{n} to work with n-sized reflog
 refs.c                      | 118 ++++++++++++++++++++----------------
 t/t1503-rev-parse-verify.sh |   4 +-
 t/t1508-at-combinations.sh  |  16 +++++
 3 files changed, 84 insertions(+), 54 deletions(-)
Range-diff against v1:
-:  ---------- > 1:  8f14ec3997 refs: factor out set_read_ref_cutoffs()
1:  0c6885f15f ! 2:  18a35506b8 refs: allow @{n} to work with n-sized reflog
    @@ refs.c: static int read_ref_at_ent(struct object_id *ooid, struct object_id *noi
      	struct read_ref_at_cb *cb = cb_data;
     +	int at_indexed_ent;
      
    - 	cb->reccnt++;
      	cb->tz = tz;
      	cb->date = timestamp;
      
    @@ refs.c: static int read_ref_at_ent(struct object_id *ooid, struct object_id *noi
     +		cb->cnt--;
     +	at_indexed_ent = cb->cnt == 0 && !is_null_oid(ooid);
     +	if (timestamp <= cb->at_time || at_indexed_ent) {
    - 		if (cb->msg)
    - 			*cb->msg = xstrdup(message);
    - 		if (cb->cutoff_time)
    -@@ refs.c: static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
    + 		set_read_ref_cutoffs(cb, timestamp, tz, message);
    + 		/*
      		 * we have not yet updated cb->[n|o]oid so they still
      		 * hold the values for the previous record.
      		 */
    @@ refs.c: static int read_ref_at_ent(struct object_id *ooid, struct object_id *noi
      			warning(_("log for ref %s unexpectedly ended on %s"),
      				cb->refname, show_date(cb->date, cb->tz,
      						       DATE_MODE(RFC2822)));
    +-		cb->reccnt++;
     -		oidcpy(&cb->ooid, ooid);
     -		oidcpy(&cb->noid, noid);
      		cb->found_it = 1;
     -		return 1;
      	}
    + 	cb->reccnt++;
      	oidcpy(&cb->ooid, ooid);
      	oidcpy(&cb->noid, noid);
     -	if (cb->cnt > 0)
    @@ refs.c: static int read_ref_at_ent(struct object_id *ooid, struct object_id *noi
     +{
     +	struct read_ref_at_cb *cb = cb_data;
     +
    -+	if (cb->msg)
    -+		*cb->msg = xstrdup(message);
    -+	if (cb->cutoff_time)
    -+		*cb->cutoff_time = timestamp;
    -+	if (cb->cutoff_tz)
    -+		*cb->cutoff_tz = tz;
    -+	if (cb->cutoff_cnt)
    -+		*cb->cutoff_cnt = cb->reccnt;
    ++	set_read_ref_cutoffs(cb, timestamp, tz, message);
     +	oidcpy(cb->oid, noid);
     +	/* We just want the first entry */
     +	return 1;
    @@ t/t1503-rev-parse-verify.sh: test_expect_success 'fails silently when using -q'
      	test_must_be_empty error
      '
      
    -@@ t/t1503-rev-parse-verify.sh: test_expect_success 'master@{n} for various n' '
    - 	test_must_fail git rev-parse --verify master@{$Np1}
    - '
    +
    + ## t/t1508-at-combinations.sh ##
    +@@ t/t1508-at-combinations.sh: test_expect_success 'create path with @' '
    + check "@:normal" blob content
    + check "@:fun@ny" blob content
      
     +test_expect_success '@{1} works with only one reflog entry' '
    -+	git checkout -B newbranch &&
    ++	git checkout -B newbranch master &&
     +	git reflog expire --expire=now refs/heads/newbranch &&
    -+	git commit --allow-empty -mexpired &&
    -+	git rev-parse --verify newbranch@{1}
    ++	git commit --allow-empty -m "first after expiration" &&
    ++	git rev-parse newbranch~ >expect &&
    ++	git rev-parse newbranch@{1} >actual &&
    ++	test_cmp expect actual
     +'
     +
     +test_expect_success '@{0} works with empty reflog' '
    -+	git checkout -B newbranch &&
    ++	git checkout -B newbranch master &&
     +	git reflog expire --expire=now refs/heads/newbranch &&
    -+	git rev-parse --verify newbranch@{0}
    ++	git rev-parse newbranch >expect &&
    ++	git rev-parse newbranch@{0} >actual &&
    ++	test_cmp expect actual
     +'
    -+
    - test_expect_success SYMLINKS 'ref resolution not confused by broken symlinks' '
    - 	ln -s does-not-exist .git/refs/heads/broken &&
    - 	test_must_fail git rev-parse --verify broken
    + test_done
-- 
2.30.0
Denton Liu· Jan 6, 2021, 09:01 UTC · re: Denton Liu · lore

[PATCH v2 1/2] refs: factor out set_read_ref_cutoffs()

This block of code is duplicated twice. In a future commit, it will be duplicated for a third time. Factor out the common functionality into set_read_ref_cutoffs().

In the case of read_ref_at_ent(), we are incrementing `cb->reccnt` at the beginning of the function. Move these to right before the return so that the `cb->reccnt - 1` is changed to `cb->reccnt` and it can be cleanly factored out into set_read_ref_cutoffs(). The duplication of the increment statements will be removed in a future patch.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 refs.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)
Show changes to refs.c +17 −17
diff --git a/refs.c b/refs.c
index 13dc2c3291..bfdd04aefd 100644
--- a/refs.c
+++ b/refs.c
@@ -882,25 +882,30 @@ struct read_ref_at_cb {
 	int *cutoff_cnt;
 };
 
+static void set_read_ref_cutoffs(struct read_ref_at_cb *cb,
+		timestamp_t timestamp, int tz, const char *message)
+{
+	if (cb->msg)
+		*cb->msg = xstrdup(message);
+	if (cb->cutoff_time)
+		*cb->cutoff_time = timestamp;
+	if (cb->cutoff_tz)
+		*cb->cutoff_tz = tz;
+	if (cb->cutoff_cnt)
+		*cb->cutoff_cnt = cb->reccnt;
+}
+
 static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
 		const char *email, timestamp_t timestamp, int tz,
 		const char *message, void *cb_data)
 {
 	struct read_ref_at_cb *cb = cb_data;
 
-	cb->reccnt++;
 	cb->tz = tz;
 	cb->date = timestamp;
 
 	if (timestamp <= cb->at_time || cb->cnt == 0) {
-		if (cb->msg)
-			*cb->msg = xstrdup(message);
-		if (cb->cutoff_time)
-			*cb->cutoff_time = timestamp;
-		if (cb->cutoff_tz)
-			*cb->cutoff_tz = tz;
-		if (cb->cutoff_cnt)
-			*cb->cutoff_cnt = cb->reccnt - 1;
+		set_read_ref_cutoffs(cb, timestamp, tz, message);
 		/*
 		 * we have not yet updated cb->[n|o]oid so they still
 		 * hold the values for the previous record.
@@ -917,11 +922,13 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
 			warning(_("log for ref %s unexpectedly ended on %s"),
 				cb->refname, show_date(cb->date, cb->tz,
 						       DATE_MODE(RFC2822)));
+		cb->reccnt++;
 		oidcpy(&cb->ooid, ooid);
 		oidcpy(&cb->noid, noid);
 		cb->found_it = 1;
 		return 1;
 	}
+	cb->reccnt++;
 	oidcpy(&cb->ooid, ooid);
 	oidcpy(&cb->noid, noid);
 	if (cb->cnt > 0)
@@ -935,14 +942,7 @@ static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid
 {
 	struct read_ref_at_cb *cb = cb_data;
 
-	if (cb->msg)
-		*cb->msg = xstrdup(message);
-	if (cb->cutoff_time)
-		*cb->cutoff_time = timestamp;
-	if (cb->cutoff_tz)
-		*cb->cutoff_tz = tz;
-	if (cb->cutoff_cnt)
-		*cb->cutoff_cnt = cb->reccnt;
+	set_read_ref_cutoffs(cb, timestamp, tz, message);
 	oidcpy(cb->oid, ooid);
 	if (is_null_oid(cb->oid))
 		oidcpy(cb->oid, noid);
-- 
2.30.0
Denton Liu· Jan 6, 2021, 09:01 UTC · re: Denton Liu · lore

[PATCH v2 2/2] refs: allow @{n} to work with n-sized reflog

This sequence works
	$ git checkout -b newbranch
	$ git commit --allow-empty -m one
	$ git show -s newbranch@{1}

and shows the state that was immediately after the newbranch was created.

But then if you do
	$ git reflog expire --expire=now refs/heads/newbranch
	$ git commit --allow=empty -m two
	$ git show -s newbranch@{1}
you'd be scolded with
	fatal: log for 'newbranch' only has 1 entries

While it is true that it has only 1 entry, we have enough information in that single entry that records the transition between the state in which the tip of the branch was pointing at commit 'one' to the new commit 'two' built on it, so we should be able to answer "what object newbranch was pointing at?". But we refuse to do so.

Make @{0} the special case where we use the new side to look up that entry. Otherwise, look up @{n} using the old side of the (n-1)th entry of the reflog.

Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 refs.c                      | 42 ++++++++++++++++++++++++-------------
 t/t1503-rev-parse-verify.sh |  4 ++--
 t/t1508-at-combinations.sh  | 16 ++++++++++++++
 3 files changed, 46 insertions(+), 16 deletions(-)
Show changes to 3 files +46 −16

refs.c, t/t1503-rev-parse-verify.sh, t/t1508-at-combinations.sh

diff --git a/refs.c b/refs.c
index bfdd04aefd..9eb26d456d 100644
--- a/refs.c
+++ b/refs.c
@@ -900,40 +900,49 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
 		const char *message, void *cb_data)
 {
 	struct read_ref_at_cb *cb = cb_data;
+	int at_indexed_ent;
 
 	cb->tz = tz;
 	cb->date = timestamp;
 
-	if (timestamp <= cb->at_time || cb->cnt == 0) {
+	if (cb->cnt > 0)
+		cb->cnt--;
+	at_indexed_ent = cb->cnt == 0 && !is_null_oid(ooid);
+	if (timestamp <= cb->at_time || at_indexed_ent) {
 		set_read_ref_cutoffs(cb, timestamp, tz, message);
 		/*
 		 * we have not yet updated cb->[n|o]oid so they still
 		 * hold the values for the previous record.
 		 */
-		if (!is_null_oid(&cb->ooid)) {
-			oidcpy(cb->oid, noid);
-			if (!oideq(&cb->ooid, noid))
-				warning(_("log for ref %s has gap after %s"),
+		if (!is_null_oid(&cb->ooid) && !oideq(&cb->ooid, noid))
+			warning(_("log for ref %s has gap after %s"),
 					cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
-		}
-		else if (cb->date == cb->at_time)
+		if (at_indexed_ent)
+			oidcpy(cb->oid, ooid);
+		else if (!is_null_oid(&cb->ooid) || cb->date == cb->at_time)
 			oidcpy(cb->oid, noid);
 		else if (!oideq(noid, cb->oid))
 			warning(_("log for ref %s unexpectedly ended on %s"),
 				cb->refname, show_date(cb->date, cb->tz,
 						       DATE_MODE(RFC2822)));
-		cb->reccnt++;
-		oidcpy(&cb->ooid, ooid);
-		oidcpy(&cb->noid, noid);
 		cb->found_it = 1;
-		return 1;
 	}
 	cb->reccnt++;
 	oidcpy(&cb->ooid, ooid);
 	oidcpy(&cb->noid, noid);
-	if (cb->cnt > 0)
-		cb->cnt--;
-	return 0;
+	return cb->found_it;
+}
+
+static int read_ref_at_ent_newest(struct object_id *ooid, struct object_id *noid,
+				  const char *email, timestamp_t timestamp,
+				  int tz, const char *message, void *cb_data)
+{
+	struct read_ref_at_cb *cb = cb_data;
+
+	set_read_ref_cutoffs(cb, timestamp, tz, message);
+	oidcpy(cb->oid, noid);
+	/* We just want the first entry */
+	return 1;
 }
 
 static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
@@ -967,6 +976,11 @@ int read_ref_at(struct ref_store *refs, const char *refname,
 	cb.cutoff_cnt = cutoff_cnt;
 	cb.oid = oid;
 
+	if (cb.cnt == 0) {
+		refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent_newest, &cb);
+		return 0;
+	}
+
 	refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent, &cb);
 
 	if (!cb.reccnt) {
diff --git a/t/t1503-rev-parse-verify.sh b/t/t1503-rev-parse-verify.sh
index dc9fe3cbf1..a7e9b4863d 100755
--- a/t/t1503-rev-parse-verify.sh
+++ b/t/t1503-rev-parse-verify.sh
@@ -86,8 +86,8 @@ test_expect_success 'fails silently when using -q' '
 test_expect_success 'fails silently when using -q with deleted reflogs' '
 	ref=$(git rev-parse HEAD) &&
 	git update-ref --create-reflog -m "message for refs/test" refs/test "$ref" &&
-	git reflog delete --updateref --rewrite refs/test@{0} &&
-	test_must_fail git rev-parse -q --verify refs/test@{0} >error 2>&1 &&
+	git reflog delete --updateref --rewrite refs/test@{1} &&
+	test_must_fail git rev-parse -q --verify refs/test@{1} >error 2>&1 &&
 	test_must_be_empty error
 '
 
diff --git a/t/t1508-at-combinations.sh b/t/t1508-at-combinations.sh
index 4a9964e9dc..15aac6e77a 100755
--- a/t/t1508-at-combinations.sh
+++ b/t/t1508-at-combinations.sh
@@ -99,4 +99,20 @@ test_expect_success 'create path with @' '
 check "@:normal" blob content
 check "@:fun@ny" blob content
 
+test_expect_success '@{1} works with only one reflog entry' '
+	git checkout -B newbranch master &&
+	git reflog expire --expire=now refs/heads/newbranch &&
+	git commit --allow-empty -m "first after expiration" &&
+	git rev-parse newbranch~ >expect &&
+	git rev-parse newbranch@{1} >actual &&
+	test_cmp expect actual
+'
+
+test_expect_success '@{0} works with empty reflog' '
+	git checkout -B newbranch master &&
+	git reflog expire --expire=now refs/heads/newbranch &&
+	git rev-parse newbranch >expect &&
+	git rev-parse newbranch@{0} >actual &&
+	test_cmp expect actual
+'
 test_done
-- 
2.30.0
SZEDER Gábor· Jan 6, 2021, 09:59 UTC · re: Denton Liu · lore

Re: [PATCH v2 2/2] refs: allow @{n} to work with n-sized reflog

On Wed, Jan 06, 2021 at 01:01:54AM -0800, Denton Liu wrote:
Show 23 quoted lines
> diff --git a/t/t1508-at-combinations.sh b/t/t1508-at-combinations.sh
> index 4a9964e9dc..15aac6e77a 100755
> --- a/t/t1508-at-combinations.sh
> +++ b/t/t1508-at-combinations.sh
> @@ -99,4 +99,20 @@ test_expect_success 'create path with @' '
>  check "@:normal" blob content
>  check "@:fun@ny" blob content
>  
> +test_expect_success '@{1} works with only one reflog entry' '
> +	git checkout -B newbranch master &&
> +	git reflog expire --expire=now refs/heads/newbranch &&
> +	git commit --allow-empty -m "first after expiration" &&
> +	git rev-parse newbranch~ >expect &&
> +	git rev-parse newbranch@{1} >actual &&
> +	test_cmp expect actual
> +'
> +
> +test_expect_success '@{0} works with empty reflog' '
> +	git checkout -B newbranch master &&
> +	git reflog expire --expire=now refs/heads/newbranch &&
> +	git rev-parse newbranch >expect &&
> +	git rev-parse newbranch@{0} >actual &&
> +	test_cmp expect actual

You could use 'test_cmp_rev' in these two tests to spare a few lines and to get a bit friendlier error message on failure.

Denton Liu· Jan 7, 2021, 10:36 UTC · re: Denton Liu · lore

[PATCH v3 0/2] refs: allow @{n} to work with n-sized reflog

When there is only one reflog entry (perhaps caused by expiring the reflog and then making a single commit) @{1} errors out even though there is technically enough information to do the lookup. Look at the old side of the reflog instead of the new side so that this does not fail. This is explained in more detail in the commit of the last patch.

This idea was given by Junio at [0].
[0]: https://lore.kernel.org/git/xmqqzh8zgcfp.fsf@gitster.c.googlers.com/
Changes since v1:
* Factor out set_read_ref_cutoffs()
* Check the output of rev-parse to ensure that the intended commit is
  returned
Changes since v2:
* Rename at_indexed_ent -> reached_count
* Add an in-code comment to document that cb->cnt can't be 0 in the first
  iteration of read_ref_at_ent()
* Make test cases use test_cmp_rev() for brevity and better errors
Denton Liu (2):
  refs: factor out set_read_ref_cutoffs()
  refs: allow @{n} to work with n-sized reflog
 refs.c                      | 122 +++++++++++++++++++++---------------
 t/t1503-rev-parse-verify.sh |   4 +-
 t/t1508-at-combinations.sh  |  12 ++++
 3 files changed, 84 insertions(+), 54 deletions(-)
Range-diff against v2:
1:  8f14ec3997 = 1:  8f14ec3997 refs: factor out set_read_ref_cutoffs()
2:  18a35506b8 ! 2:  c88c997eab refs: allow @{n} to work with n-sized reflog
    @@ refs.c: static int read_ref_at_ent(struct object_id *ooid, struct object_id *noi
      		const char *message, void *cb_data)
      {
      	struct read_ref_at_cb *cb = cb_data;
    -+	int at_indexed_ent;
    ++	int reached_count;
      
      	cb->tz = tz;
      	cb->date = timestamp;
      
     -	if (timestamp <= cb->at_time || cb->cnt == 0) {
    ++	/*
    ++	 * It is not possible for cb->cnt == 0 on the first itertion because
    ++	 * that special case is handled in read_ref_at().
    ++	 */
     +	if (cb->cnt > 0)
     +		cb->cnt--;
    -+	at_indexed_ent = cb->cnt == 0 && !is_null_oid(ooid);
    -+	if (timestamp <= cb->at_time || at_indexed_ent) {
    ++	reached_count = cb->cnt == 0 && !is_null_oid(ooid);
    ++	if (timestamp <= cb->at_time || reached_count) {
      		set_read_ref_cutoffs(cb, timestamp, tz, message);
      		/*
      		 * we have not yet updated cb->[n|o]oid so they still
    @@ refs.c: static int read_ref_at_ent(struct object_id *ooid, struct object_id *noi
      					cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
     -		}
     -		else if (cb->date == cb->at_time)
    -+		if (at_indexed_ent)
    ++		if (reached_count)
     +			oidcpy(cb->oid, ooid);
     +		else if (!is_null_oid(&cb->ooid) || cb->date == cb->at_time)
      			oidcpy(cb->oid, noid);
    @@ t/t1508-at-combinations.sh: test_expect_success 'create path with @' '
     +	git checkout -B newbranch master &&
     +	git reflog expire --expire=now refs/heads/newbranch &&
     +	git commit --allow-empty -m "first after expiration" &&
    -+	git rev-parse newbranch~ >expect &&
    -+	git rev-parse newbranch@{1} >actual &&
    -+	test_cmp expect actual
    ++	test_cmp_rev newbranch~ newbranch@{1}
     +'
     +
     +test_expect_success '@{0} works with empty reflog' '
     +	git checkout -B newbranch master &&
     +	git reflog expire --expire=now refs/heads/newbranch &&
    -+	git rev-parse newbranch >expect &&
    -+	git rev-parse newbranch@{0} >actual &&
    -+	test_cmp expect actual
    ++	test_cmp_rev newbranch newbranch@{0}
     +'
      test_done
-- 
2.30.0
Denton Liu· Jan 7, 2021, 10:36 UTC · re: Denton Liu · lore

[PATCH v3 1/2] refs: factor out set_read_ref_cutoffs()

This block of code is duplicated twice. In a future commit, it will be duplicated for a third time. Factor out the common functionality into set_read_ref_cutoffs().

In the case of read_ref_at_ent(), we are incrementing `cb->reccnt` at the beginning of the function. Move these to right before the return so that the `cb->reccnt - 1` is changed to `cb->reccnt` and it can be cleanly factored out into set_read_ref_cutoffs(). The duplication of the increment statements will be removed in a future patch.

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 refs.c | 34 +++++++++++++++++-----------------
 1 file changed, 17 insertions(+), 17 deletions(-)
Show changes to refs.c +17 −17
diff --git a/refs.c b/refs.c
index 13dc2c3291..bfdd04aefd 100644
--- a/refs.c
+++ b/refs.c
@@ -882,25 +882,30 @@ struct read_ref_at_cb {
 	int *cutoff_cnt;
 };
 
+static void set_read_ref_cutoffs(struct read_ref_at_cb *cb,
+		timestamp_t timestamp, int tz, const char *message)
+{
+	if (cb->msg)
+		*cb->msg = xstrdup(message);
+	if (cb->cutoff_time)
+		*cb->cutoff_time = timestamp;
+	if (cb->cutoff_tz)
+		*cb->cutoff_tz = tz;
+	if (cb->cutoff_cnt)
+		*cb->cutoff_cnt = cb->reccnt;
+}
+
 static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
 		const char *email, timestamp_t timestamp, int tz,
 		const char *message, void *cb_data)
 {
 	struct read_ref_at_cb *cb = cb_data;
 
-	cb->reccnt++;
 	cb->tz = tz;
 	cb->date = timestamp;
 
 	if (timestamp <= cb->at_time || cb->cnt == 0) {
-		if (cb->msg)
-			*cb->msg = xstrdup(message);
-		if (cb->cutoff_time)
-			*cb->cutoff_time = timestamp;
-		if (cb->cutoff_tz)
-			*cb->cutoff_tz = tz;
-		if (cb->cutoff_cnt)
-			*cb->cutoff_cnt = cb->reccnt - 1;
+		set_read_ref_cutoffs(cb, timestamp, tz, message);
 		/*
 		 * we have not yet updated cb->[n|o]oid so they still
 		 * hold the values for the previous record.
@@ -917,11 +922,13 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
 			warning(_("log for ref %s unexpectedly ended on %s"),
 				cb->refname, show_date(cb->date, cb->tz,
 						       DATE_MODE(RFC2822)));
+		cb->reccnt++;
 		oidcpy(&cb->ooid, ooid);
 		oidcpy(&cb->noid, noid);
 		cb->found_it = 1;
 		return 1;
 	}
+	cb->reccnt++;
 	oidcpy(&cb->ooid, ooid);
 	oidcpy(&cb->noid, noid);
 	if (cb->cnt > 0)
@@ -935,14 +942,7 @@ static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid
 {
 	struct read_ref_at_cb *cb = cb_data;
 
-	if (cb->msg)
-		*cb->msg = xstrdup(message);
-	if (cb->cutoff_time)
-		*cb->cutoff_time = timestamp;
-	if (cb->cutoff_tz)
-		*cb->cutoff_tz = tz;
-	if (cb->cutoff_cnt)
-		*cb->cutoff_cnt = cb->reccnt;
+	set_read_ref_cutoffs(cb, timestamp, tz, message);
 	oidcpy(cb->oid, ooid);
 	if (is_null_oid(cb->oid))
 		oidcpy(cb->oid, noid);
-- 
2.30.0
Denton Liu· Jan 7, 2021, 10:36 UTC · re: Denton Liu · lore

[PATCH v3 2/2] refs: allow @{n} to work with n-sized reflog

This sequence works
	$ git checkout -b newbranch
	$ git commit --allow-empty -m one
	$ git show -s newbranch@{1}

and shows the state that was immediately after the newbranch was created.

But then if you do
	$ git reflog expire --expire=now refs/heads/newbranch
	$ git commit --allow=empty -m two
	$ git show -s newbranch@{1}
you'd be scolded with
	fatal: log for 'newbranch' only has 1 entries

While it is true that it has only 1 entry, we have enough information in that single entry that records the transition between the state in which the tip of the branch was pointing at commit 'one' to the new commit 'two' built on it, so we should be able to answer "what object newbranch was pointing at?". But we refuse to do so.

Make @{0} the special case where we use the new side to look up that entry. Otherwise, look up @{n} using the old side of the (n-1)th entry of the reflog.

Suggested-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 refs.c                      | 46 ++++++++++++++++++++++++++-----------
 t/t1503-rev-parse-verify.sh |  4 ++--
 t/t1508-at-combinations.sh  | 12 ++++++++++
 3 files changed, 46 insertions(+), 16 deletions(-)
Show changes to 3 files +46 −16

refs.c, t/t1503-rev-parse-verify.sh, t/t1508-at-combinations.sh

diff --git a/refs.c b/refs.c
index bfdd04aefd..e70dcd33f7 100644
--- a/refs.c
+++ b/refs.c
@@ -900,40 +900,53 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
 		const char *message, void *cb_data)
 {
 	struct read_ref_at_cb *cb = cb_data;
+	int reached_count;
 
 	cb->tz = tz;
 	cb->date = timestamp;
 
-	if (timestamp <= cb->at_time || cb->cnt == 0) {
+	/*
+	 * It is not possible for cb->cnt == 0 on the first itertion because
+	 * that special case is handled in read_ref_at().
+	 */
+	if (cb->cnt > 0)
+		cb->cnt--;
+	reached_count = cb->cnt == 0 && !is_null_oid(ooid);
+	if (timestamp <= cb->at_time || reached_count) {
 		set_read_ref_cutoffs(cb, timestamp, tz, message);
 		/*
 		 * we have not yet updated cb->[n|o]oid so they still
 		 * hold the values for the previous record.
 		 */
-		if (!is_null_oid(&cb->ooid)) {
-			oidcpy(cb->oid, noid);
-			if (!oideq(&cb->ooid, noid))
-				warning(_("log for ref %s has gap after %s"),
+		if (!is_null_oid(&cb->ooid) && !oideq(&cb->ooid, noid))
+			warning(_("log for ref %s has gap after %s"),
 					cb->refname, show_date(cb->date, cb->tz, DATE_MODE(RFC2822)));
-		}
-		else if (cb->date == cb->at_time)
+		if (reached_count)
+			oidcpy(cb->oid, ooid);
+		else if (!is_null_oid(&cb->ooid) || cb->date == cb->at_time)
 			oidcpy(cb->oid, noid);
 		else if (!oideq(noid, cb->oid))
 			warning(_("log for ref %s unexpectedly ended on %s"),
 				cb->refname, show_date(cb->date, cb->tz,
 						       DATE_MODE(RFC2822)));
-		cb->reccnt++;
-		oidcpy(&cb->ooid, ooid);
-		oidcpy(&cb->noid, noid);
 		cb->found_it = 1;
-		return 1;
 	}
 	cb->reccnt++;
 	oidcpy(&cb->ooid, ooid);
 	oidcpy(&cb->noid, noid);
-	if (cb->cnt > 0)
-		cb->cnt--;
-	return 0;
+	return cb->found_it;
+}
+
+static int read_ref_at_ent_newest(struct object_id *ooid, struct object_id *noid,
+				  const char *email, timestamp_t timestamp,
+				  int tz, const char *message, void *cb_data)
+{
+	struct read_ref_at_cb *cb = cb_data;
+
+	set_read_ref_cutoffs(cb, timestamp, tz, message);
+	oidcpy(cb->oid, noid);
+	/* We just want the first entry */
+	return 1;
 }
 
 static int read_ref_at_ent_oldest(struct object_id *ooid, struct object_id *noid,
@@ -967,6 +980,11 @@ int read_ref_at(struct ref_store *refs, const char *refname,
 	cb.cutoff_cnt = cutoff_cnt;
 	cb.oid = oid;
 
+	if (cb.cnt == 0) {
+		refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent_newest, &cb);
+		return 0;
+	}
+
 	refs_for_each_reflog_ent_reverse(refs, refname, read_ref_at_ent, &cb);
 
 	if (!cb.reccnt) {
diff --git a/t/t1503-rev-parse-verify.sh b/t/t1503-rev-parse-verify.sh
index dc9fe3cbf1..a7e9b4863d 100755
--- a/t/t1503-rev-parse-verify.sh
+++ b/t/t1503-rev-parse-verify.sh
@@ -86,8 +86,8 @@ test_expect_success 'fails silently when using -q' '
 test_expect_success 'fails silently when using -q with deleted reflogs' '
 	ref=$(git rev-parse HEAD) &&
 	git update-ref --create-reflog -m "message for refs/test" refs/test "$ref" &&
-	git reflog delete --updateref --rewrite refs/test@{0} &&
-	test_must_fail git rev-parse -q --verify refs/test@{0} >error 2>&1 &&
+	git reflog delete --updateref --rewrite refs/test@{1} &&
+	test_must_fail git rev-parse -q --verify refs/test@{1} >error 2>&1 &&
 	test_must_be_empty error
 '
 
diff --git a/t/t1508-at-combinations.sh b/t/t1508-at-combinations.sh
index 4a9964e9dc..528a77287c 100755
--- a/t/t1508-at-combinations.sh
+++ b/t/t1508-at-combinations.sh
@@ -99,4 +99,16 @@ test_expect_success 'create path with @' '
 check "@:normal" blob content
 check "@:fun@ny" blob content
 
+test_expect_success '@{1} works with only one reflog entry' '
+	git checkout -B newbranch master &&
+	git reflog expire --expire=now refs/heads/newbranch &&
+	git commit --allow-empty -m "first after expiration" &&
+	test_cmp_rev newbranch~ newbranch@{1}
+'
+
+test_expect_success '@{0} works with empty reflog' '
+	git checkout -B newbranch master &&
+	git reflog expire --expire=now refs/heads/newbranch &&
+	test_cmp_rev newbranch newbranch@{0}
+'
 test_done
-- 
2.30.0
Simon Ruderich· Jan 10, 2021, 20:31 UTC · re: Denton Liu · lore

Re: [PATCH v3 2/2] refs: allow @{n} to work with n-sized reflog

On Thu, Jan 07, 2021 at 02:36:59AM -0800, Denton Liu wrote:
Show 16 quoted lines
> diff --git a/refs.c b/refs.c
> index bfdd04aefd..e70dcd33f7 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -900,40 +900,53 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
>  		const char *message, void *cb_data)
>  {
>  	struct read_ref_at_cb *cb = cb_data;
> +	int reached_count;
>
>  	cb->tz = tz;
>  	cb->date = timestamp;
>
> -	if (timestamp <= cb->at_time || cb->cnt == 0) {
> +	/*
> +	 * It is not possible for cb->cnt == 0 on the first itertion because
s/itertion/iteration/

Regards Simon

-- 
+ privacy is necessary
+ using gnupg http://gnupg.org
+ public key id: 0x92FEFDB7E44C32F9
Denton Liu· Jan 12, 2021, 06:14 UTC · re: Simon Ruderich · lore

[PATCH v3] fixup! refs: allow @{n} to work with n-sized reflog

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 refs.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
Show changes to refs.c +1 −1
diff --git a/refs.c b/refs.c
index e70dcd33f7..03968ad787 100644
--- a/refs.c
+++ b/refs.c
@@ -906,7 +906,7 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
 	cb->date = timestamp;
 
 	/*
-	 * It is not possible for cb->cnt == 0 on the first itertion because
+	 * It is not possible for cb->cnt == 0 on the first iteration because
 	 * that special case is handled in read_ref_at().
 	 */
 	if (cb->cnt > 0)
-- 
2.30.0.284.gd98b1dd5ea
Denton Liu· Jan 12, 2021, 06:18 UTC · re: Denton Liu · lore

Re: [PATCH v3] fixup! refs: allow @{n} to work with n-sized reflog

Please disregard, I see that the fixup has already been included.
-Denton
On Mon, Jan 11, 2021 at 10:14:14PM -0800, Denton Liu wrote:
Show 21 quoted lines
> Signed-off-by: Denton Liu <liu.denton@gmail.com>
> ---
>  refs.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/refs.c b/refs.c
> index e70dcd33f7..03968ad787 100644
> --- a/refs.c
> +++ b/refs.c
> @@ -906,7 +906,7 @@ static int read_ref_at_ent(struct object_id *ooid, struct object_id *noid,
>  	cb->date = timestamp;
>  
>  	/*
> -	 * It is not possible for cb->cnt == 0 on the first itertion because
> +	 * It is not possible for cb->cnt == 0 on the first iteration because
>  	 * that special case is handled in read_ref_at().
>  	 */
>  	if (cb->cnt > 0)
> -- 
> 2.30.0.284.gd98b1dd5ea
> 
Junio C Hamano· Jan 12, 2021, 06:27 UTC · re: Denton Liu · lore

Re: [PATCH v3] fixup! refs: allow @{n} to work with n-sized reflog

Denton Liu <liu.denton@gmail.com> writes:
> Please disregard, I see that the fixup has already been included.

Being careful never hurts; it may make duplicated work from time to time, but that is better than fixes falling thru the cracks.

Thanks.
SZEDER Gábor· Jan 10, 2021, 14:44 UTC · re: Denton Liu · lore

Re: [PATCH v3 0/2] refs: allow @{n} to work with n-sized reflog

Junio,
On Thu, Jan 07, 2021 at 02:36:57AM -0800, Denton Liu wrote:
Show 5 quoted lines
> When there is only one reflog entry (perhaps caused by expiring the
> reflog and then making a single commit) @{1} errors out even though
> there is technically enough information to do the lookup. Look at the
> old side of the reflog instead of the new side so that this does not
> fail. This is explained in more detail in the commit of the last patch.
> Denton Liu (2):
>   refs: factor out set_read_ref_cutoffs()
>   refs: allow @{n} to work with n-sized reflog

Topic 'dl/reflog-with-single-entry', i.e. these two patches queued directly on top of v2.29.2, break the test case "61 - valid ref of the form "n", n < N" in 't3903-stash.sh'. Queueing them on top of something already containing commit 4f44c5659b (stash: simplify reflog emptiness check, 2020-10-24) fixes this issue.

Junio C Hamano· Jan 10, 2021, 20:24 UTC · re: SZEDER Gábor · lore

Re: [PATCH v3 0/2] refs: allow @{n} to work with n-sized reflog

SZEDER Gábor <szeder.dev@gmail.com> writes:
Show 18 quoted lines
> Junio,
>
> On Thu, Jan 07, 2021 at 02:36:57AM -0800, Denton Liu wrote:
>> When there is only one reflog entry (perhaps caused by expiring the
>> reflog and then making a single commit) @{1} errors out even though
>> there is technically enough information to do the lookup. Look at the
>> old side of the reflog instead of the new side so that this does not
>> fail. This is explained in more detail in the commit of the last patch.
>
>> Denton Liu (2):
>>   refs: factor out set_read_ref_cutoffs()
>>   refs: allow @{n} to work with n-sized reflog
>
> Topic 'dl/reflog-with-single-entry', i.e. these two patches queued
> directly on top of v2.29.2, break the test case "61 - valid ref of the
> form "n", n < N" in 't3903-stash.sh'.  Queueing them on top of
> something already containing commit 4f44c5659b (stash: simplify reflog
> emptiness check, 2020-10-24) fixes this issue.
Thanks for carefully watching ;-)

There is no reason why this fix needs to be backported down to 2.29 track, I would think.

Denton Liu· Jan 7, 2021, 10:43 UTC · re: Denton Liu · lore

[PATCH v3 3/2] fixup! refs: allow @{n} to work with n-sized reflog

Signed-off-by: Denton Liu <liu.denton@gmail.com>
---
 t/t1508-at-combinations.sh | 1 +
 1 file changed, 1 insertion(+)
Show changes to t/t1508-at-combinations.sh +1 −0
diff --git a/t/t1508-at-combinations.sh b/t/t1508-at-combinations.sh
index 528a77287c..e4521b7b97 100755
--- a/t/t1508-at-combinations.sh
+++ b/t/t1508-at-combinations.sh
@@ -111,4 +111,5 @@ test_expect_success '@{0} works with empty reflog' '
 	git reflog expire --expire=now refs/heads/newbranch &&
 	test_cmp_rev newbranch newbranch@{0}
 '
+
 test_done
-- 
2.30.0

← back to recent threads