Skip to contentSkip to conversation

Annoyance wrt ref@{1} and reflog expiry

DISCUSSION Junio C Hamano 3 participants 6 messages Jun 19, 2020

lore
Share & tools
lore
#0 lore raw

I have been disturbed by this for a long time, but not strongly enough to do anything to it.

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.

And it is unintuitive. It is understandable to the users that all the ref history before "reflog expire" is lost---it was what the end user asked Git to do. But after creating one commit on the state (or do anything else that moves the ref) and finding it regrettable, "git reset --hard @{1}" should be a viable way to recover from the mistake made _after_ the reflog entries were expired.

Opinions?

#1 lore raw
Quoted 29 lines · Junio C Hamano

I have been disturbed by this for a long time, but not strongly enough to do anything to it.

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.

Quoted 8 lines

And it is unintuitive. It is understandable to the users that all the ref history before "reflog expire" is lost---it was what the end user asked Git to do. But after creating one commit on the state (or do anything else that moves the ref) and finding it regrettable, "git reset --hard @{1}" should be a viable way to recover from the mistake made _after_ the reflog entries were expired.

Opinions?

Makes sense. The first solution that comes to mind is immediately record current state after "reflog expire", so that there will be 2 entries for the case in question.

-- Sergey

#2 lore raw
Quoted 12 lines · Sergey Organov
Quoted 12 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}
...
And it is unintuitive.  It is understandable to the users that all
the ref history before "reflog expire" is lost---it was what the end
user asked Git to do.  But after creating one commit on the state
(or do anything else that moves the ref) and finding it regrettable,
"git reset --hard @{1}" should be a viable way to recover from the
mistake made _after_ the reflog entries were expired.

And the expiration does not have to be --expire=now; what happens more often is when I expire entries older than (say) a week, the reflog for a topic branch that hasn't seen any activity may become empty. Then I "git am" the new round on the same base, compare and then update, perhaps like so:

... git reflog expire has emptied the log for so/topic ... $ git checkout so/topic
$ git log master.. ;# remind myself what the previous round had $ git checkout master... ;# detach HEAD at the previous base $ git am -s ./+so-v2-topic ;# apply
$ git range-diff @{-1}... ;# compare
$ git checkout -B so/topic

Now, I'm used to see this work after the above:

    $ git range-diff @{1}... ;# compare again just to be sure

but because there is only one entry in the reflog, which was created when the last "checkout -B" updated the so/topic branch, "there is only one entry" error kicks in.

Quoted 3 lines

Makes sense. The first solution that comes to mind is immediately record current state after "reflog expire", so that there will be 2 entries for the case in question.

Perhaps.

Or we could change the lookup side to use the value of the ref itself when asked for @{0}, and use the "old" side of the only entry when asked for @{1}. That way, we do not need to play games with an artificial entry at all, which may be a better solution.

I dunno.

#3 lore raw
Quoted 4 lines · Junio C Hamano

Or we could change the lookup side to use the value of the ref itself when asked for @{0}, and use the "old" side of the only entry when asked for @{1}. That way, we do not need to play games with an artificial entry at all, which may be a better solution.

In other words, looking up @{0} will be the only special case. A natural consequence of the above way to look up @{1} is to look at the old side of (N-1)th entry in the reflog for @{N}.

I am starting to like this idea, but I am not sure how tricky the actual implementation would be.

#4 lore raw
In reply to Junio C Hamano
Quoted 41 lines · Junio C Hamano
Quoted 12 lines · Sergey Organov
Quoted 12 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}
...
And it is unintuitive.  It is understandable to the users that all
the ref history before "reflog expire" is lost---it was what the end
user asked Git to do.  But after creating one commit on the state
(or do anything else that moves the ref) and finding it regrettable,
"git reset --hard @{1}" should be a viable way to recover from the
mistake made _after_ the reflog entries were expired.

And the expiration does not have to be --expire=now; what happens more often is when I expire entries older than (say) a week, the reflog for a topic branch that hasn't seen any activity may become empty. Then I "git am" the new round on the same base, compare and then update, perhaps like so:

... git reflog expire has emptied the log for so/topic ... $ git checkout so/topic
$ git log master.. ;# remind myself what the previous round had $ git checkout master... ;# detach HEAD at the previous base $ git am -s ./+so-v2-topic ;# apply
$ git range-diff @{-1}... ;# compare
$ git checkout -B so/topic

Now, I'm used to see this work after the above:

    $ git range-diff @{1}... ;# compare again just to be sure

but because there is only one entry in the reflog, which was created when the last "checkout -B" updated the so/topic branch, "there is only one entry" error kicks in.

Quoted 3 lines

Makes sense. The first solution that comes to mind is immediately record current state after "reflog expire", so that there will be 2 entries for the case in question.

Quoted 6 lines

Perhaps.

Or we could change the lookup side to use the value of the ref itself when asked for @{0}, and use the "old" side of the only entry when asked for @{1}. That way, we do not need to play games with an artificial entry at all, which may be a better solution.

Or more generally, use the old side from @{n} when asked for @{n+1} when there are only n entries in the reflog.

Mike

#5 lore raw
Quoted 7 lines · Mike Hommey
Quoted 4 lines

Or we could change the lookup side to use the value of the ref itself when asked for @{0}, and use the "old" side of the only entry when asked for @{1}. That way, we do not need to play games with an artificial entry at all, which may be a better solution.

Or more generally, use the old side from @{n} when asked for @{n+1} when there are only n entries in the reflog.

Yup, I agree that it is the natural consequence.