update-ref: add forward command to safely fast-forward refs
PATCH 1/1 update-ref: add forward command to safely fast-forward refs
forward is an update-ref command that behaves similarly to update, but takes an additional argument, <ancestor>, and verifies that the new value is a descendent of ancestor before updating. This is useful for fast-forwarding prefetched refs.
---
I've found git fetch --prefetch to be useful in my workflow for asynchronously updating git repos, but I'd really like a nice way to update my remote tracking branches using the latest prefetched refs without querying the remote in case the remote is temporarily unavailable or I'm on a plane or something. I've settled on using
$ git for-each-ref refs/prefetch/ --format='update refs/%(refname:lstrip=2) %(refname)'
to prepare a transaction for git update-ref, but it has a defect: if I have fetched the remote more recently than the last prefetch my remote tracking branches will be sent into the past. "forward" is a convenience feature for update-ref that will save the trouble of looping through each ref and finding the merge base so it can be fast-forwarded.
Documentation/git-update-ref.txt | 5 ++++ builtin/update-ref.c | 49 ++++++++++++++++++++++++++++++++ t/t1400-update-ref.sh | 29 +++++++++++++++++++ 3 files changed, 83 insertions(+)
Documentation/git-update-ref.txt +5 -0
@@ -66,6 +66,7 @@ performs all modifications together. Specify commands of the form: delete SP <ref> [SP <oldvalue>] LF verify SP <ref> [SP <oldvalue>] LF option SP <opt> LF + forward SP <ref> SP <newvalue> SP <ancestor> [SP <oldvalue>] LF start LF prepare LF commit LF
@@ -105,6 +106,10 @@ update:: after the update and/or a zero <oldvalue> to make sure the ref does not exist before the update. +forward:: + Set <ref> to <newvalue> after verifying <oldvalue> only if + <newvalue> is a descendent of <ancestor>. + create:: Create <ref> with <newvalue> after verifying it does not exist. The given <newvalue> may not be zero.
builtin/update-ref.c +49 -0
84 lines not on this page · raw message
t/t1400-update-ref.sh +29 -0
@@ -785,6 +785,35 @@ test_expect_success 'stdin update ref works with right old value' ' test_cmp expect actual ' +test_expect_success 'stdin forward ref fails with wrong ancestor value' ' + echo "forward $c $m~1 $m" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "forward $c: not a fast-forward" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin forward ref fails with bad ancestor value' ' + echo "forward $c $m does-not-exist" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: forward $c: invalid <ancestor>: does-not-exist" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin forward ref fails with bad old value' ' + echo "forward $c $m $m~1 does-not-exist" >stdin && + test_must_fail git update-ref --stdin <stdin 2>err && + grep "fatal: forward $c: invalid <oldvalue>: does-not-exist" err && + test_must_fail git rev-parse --verify -q $c +' + +test_expect_success 'stdin forward ref works with right ancestor value' ' + echo "forward $b $m~1 $m~2" >stdin && + git update-ref --stdin <stdin && + git rev-parse $m~1 >expect && + git rev-parse $b >actual && + test_cmp expect actual +' + test_expect_success 'stdin delete ref fails with wrong old value' ' echo "delete $a $m~1" >stdin && test_must_fail git update-ref --stdin <stdin 2>err &&
Signature · 1 line
2.39.2
Quoted 4 lines · Ronan Pigott
forward is an update-ref command that behaves similarly to update, but takes an additional argument, <ancestor>, and verifies that the new value is a descendent of ancestor before updating. This is useful for fast-forwarding prefetched refs.
Why is this necessary?
* Do you expect that you may not know the ancestry relationship between the <newvalue> and <ancestor> values when you need to compute them, in order to formulate the 'forward' command?
* Is there a case where the relationship between <newvalue> and <ancestor> that was fast-forward when you formulated the 'forward' command changes by the time the 'forward' command gets executed?
I do not see the reason why this new command is needed, unless one or both of the above is what you are trying to address.
For example, existing "delete SP <ref> SP <oldvalue>" is to protect the ref you want to delete, that used to have the oldvalue back when you created the 'delete' command, from getting deleted when somebody else changed it from the sidelines. We can do
oldvalue=$(git rev-parse refs/to/be/deleted^{object})
echo delete refs/to/be/deleted $oldvalue | ...and let the command notice if somebody else changed refs/to/be/deleted in between. It is similar to the second one between the two I cited above, to make sure that your command does not overwrite what somebody else did.
It may make some sense if the new <ancestor> thing is to replace the <oldvalue> thing, though. That is, if there were a three-commit chain
A---B---C
where the ref you are trying to update currently points at A and you want to update it to C. You would observe that the current value is A, and formulate the command line:
update ref/to/be/updated C A
with the current system, and updating the ref is allowed only when nobody touched the ref in the meantime. It is _conceivable_ to say that we are OK as long as the ref points at a decendant of A (instead of pointing exactly at A), and is an ancestor of C (the value we are updating to), with
fast-forward ref/to/be/fast-forwarded C A
Then somebody else _could_ update the ref to B from the sideline, but we notice that it is a descendant of A and an ancestor of C, and we are still allowed to update it to C.
Even in that case, I am not sure how useful it would be, but at least that use case I can see why it may make sense.
If you can write <newvalue> and <ancestor> on the command line, you certainly should be able to compute "git merge-base". And because the commits are immutable, it won't change in the middle.
So, I am not very impressed. Unless I am missing something, this does not seem to be adding anything we cannot already do.
I wonder if
git fetch . "+refs/prefetch/remotes/origin/*:refs/remotes/origin/*"
(with or without the '+' prefix, depending) what you are really going after, though.
Quoted 2 lines
Unless I am missing something, this
does not seem to be adding anything we cannot already do.
Correct. In my case it replaces:
MERGE_BASE=$(git merge-base refs/remotes/origin/master refs/prefetch/remotes/origin/master) update refs/remotes/origin/master refs/prefetch/remotes/origin/master $MERGE_BASE
I just thought the "forward" form was a little more convenient since it could output it all in one for-each-ref invocation. I mean, I could always open .git/refs/* in vim but that doesn't mean I want to...
Originally I left it as a three argument command but figured it might as well be possible to update to the descendent of any given ref, so I separated <ancestor> from <refname>.
Quoted 6 lines
I wonder if
git fetch . "+refs/prefetch/remotes/origin/*:refs/remotes/origin/*"
(with or without the '+' prefix, depending) what you are really going after, though.
Oh, that's perfect! I didn't consider that it was possible to "fetch" from the local repo. I think the refspec 'refs/prefetch/remotes/origin/*:refs/remotes/origin/*' does what I want — fast-forwarding my remote tracking branches to their latest prefetches.
In that case, if you don't feel the forward feature is useful feel free to drop it.
Thanks,
Ronan