You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We may be good with just a simple key/value map. The key should be based on the filename. If a filename is queued again, we can just update that key's value. The last 3 items just depend on the ability to figure out what is being pushed, so we can look around for git media blobs.
One other option: Change push so it requires the remote and branch (which should be provided by the pre-push hook. The pre-push hook gets this in STDIN:
It seems to me that instead of recording the filename, it would be more robust and easier to handle the SHA-1 of the blob representing the pointer file. If and only if that blob has to be pushed to the git server, then the corresponding git-media blob has to be pushed to the media server.
Ok, I'm leveling up my git game here, want to check in to make sure I'm on the right track. Here's a scenario:
I've got 3 commits I want to push and sync, efd7, da5e, and 60a0. When I run git push origin master, the pre-commit hook will get: origin https://github.com/rubyist/mediarepo on its command line, and refs/heads/master efd7 refs/head/master 9b77 on its stdin. Cool.
Now, I want to figure out what objects it's about to push so I know what to sync. I know that I could do git rev-list --objects efd7 ^9b77 and get a list of objects. Is this an accurate representation of what it's about to push? I'm thinking you just trawl through that list, ignore anything that isn't a blob, and 💥 you've got your list of objects to sync.
Does that sound legit? Is there a better way to get that list?
git rev-list --objects efd7 ^9b77 will include all of the objects being pushed, but it can also include far more objects, namely objects that already exist on the remote because they are reachable from a different branch. For example, if I create a new commit and push it:
and your rev-list command will list all of the commits on mybranch even though those commits are on master, and many of them are likely already present at origin.
(Actually, your command would fail because ^0000000000000000000000000000000000000000 is not a valid argument to git rev-list. So in this case the analogous command would be
git rev-parse efd7
. But you know what I mean.)
AFAIK there is no way to get at the information you want (namely, the list of objects being newly pushed to the remote) without either hacking git or duplicating git's negotiation with the remote server 😦 Essentially you would need to know the value of all of the remote server's references before your push.
If we're pushing a new branch, we'd want to find the nearest ref (probably master, but it'd depend on where the push originated from).
@rubyist: Having a "dry run" option would be useful here. Have git media push --dry-run list the media blobs it intends to push. Maybe a --debug option could show more detailed info about the git refs it used to come up with that list.
In the case of a new branch where git really is pushing all of the objects, we can only know whether or not we should sync a git media file by asking the git media server. The git media server should already prevent uploading a full blob it already knows about, so maybe it's OK to use the full list that rev-parse gives? We could perhaps add a remote-list like command of our own to avoid all the roundtrips that just 201 or whatever.
There's not really a trick here. The closest simple thing is probably git rev-list --objects $all_the_things_you_are_pushing --not --remotes=$remote_you_are_pushing_to. That is slightly different than what git generates because we are using what's in refs/remotes (i.e., the cache from the last time we fetched), whereas git push will actually ask the upstream for all of its ref tips. So it falls down in a few cases:
We might not be pushing to a named ref with tracking branches at all (e.g., git push $url).
The tracking branches might not be up to date. Usually this means we have a subset of what the other side has, and we'll err on the side of thinking we have to send more objects. But if the other side has rewound a ref, we could err in the other direction.
The other side may have refs not included in what we've fetched (e.g., refs/pull, or even .have tips from alternates repositories).
You can get closer with:
# Find out what the other side has, but omit any objects that we don't
# have ourselves. Give them a `^`-negation, and then use the
# result to limit our object list.
git ls-remote $remote |
cut -f1 |
git cat-file --batch-check |
perl -lne 'print "^$1" if /^([0-9a-f]{40})/' |
git rev-list --objects $stuff_you_are_pushing --stdin
That's still not quite right. The server side of a push may advertise slightly more refs than ls-remote will report, as it will advertise tips for alternates repos it has access to. We don't currently do that ourselves, but other servers might.
Git doesn't actually do any other fancy negotiation to find common refs during a push, like it does for a fetch. The assumption is that you're typically building on what the other side has, so one of their refs should be a subset of what you already have. That could change in the future, but I don't think anybody has ever proposed to make it so.
@peff, does the server also report peeled versions of tags?
It sounds like a git ls-remote --haves $remote command or something like that might be useful here.
Though really, it is a shame to have to run git ls-remote separately. It costs another round-trip, and possibly opens up race conditions between the pre-push script trying to second-guess what will be sent vs. the git push itself which knows exactly what it will send. For this purpose it would be better to have a hook that gets, on stdin, a list of all objects that are about to be pushed.
@mhagger Yeah, I never noticed that, but we don't advertise peeled tags for pushes. Another issue: receive-pack does not necessarily advertise the same set of refs as upload-pack. Specifically, our servers do not advertise refs/pull for pushes (because it confuses --mirror pushers).
I agree that ls-remote --haves would be useful and would solve all of these problems, but that's going to mean a dependency on a newer version of git. If we're going that route, we might as well go with a separate sent-objects hook, which has all the advantages you noted.
Activity
rubyist commentedon Aug 17, 2014
How much data are we talking? More than an in-memory map and writing to disk with
gob?technoweenie commentedon Aug 17, 2014
We may be good with just a simple key/value map. The key should be based on the filename. If a filename is queued again, we can just update that key's value. The last 3 items just depend on the ability to figure out what is being pushed, so we can look around for git media blobs.
technoweenie commentedon Aug 20, 2014
One other option: Change push so it requires the remote and branch (which should be provided by the pre-push hook. The pre-push hook gets this in STDIN:
We can get this easily with:
mhagger commentedon Aug 21, 2014
Easy, yes, but don't forget that
git ls-remoterequires a round-trip to the upstream repo so it is a bit expensive.mhagger commentedon Aug 21, 2014
It seems to me that instead of recording the filename, it would be more robust and easier to handle the SHA-1 of the blob representing the pointer file. If and only if that blob has to be pushed to the git server, then the corresponding git-media blob has to be pushed to the media server.
[-]add a smarter queue[/-][+]improve push command, kill the queue[/+]rubyist commentedon Sep 16, 2014
Ok, I'm leveling up my git game here, want to check in to make sure I'm on the right track. Here's a scenario:
I've got 3 commits I want to push and sync,
efd7,da5e, and60a0. When I rungit push origin master, the pre-commit hook will get:origin https://github.com/rubyist/mediarepoon its command line, andrefs/heads/master efd7 refs/head/master 9b77on its stdin. Cool.Now, I want to figure out what objects it's about to push so I know what to sync. I know that I could do
git rev-list --objects efd7 ^9b77and get a list of objects. Is this an accurate representation of what it's about to push? I'm thinking you just trawl through that list, ignore anything that isn't ablob, and 💥 you've got your list of objects to sync.Does that sound legit? Is there a better way to get that list?
mhagger commentedon Sep 16, 2014
s/pre-commit/pre-push/, as you obviously know.git rev-list --objects efd7 ^9b77will include all of the objects being pushed, but it can also include far more objects, namely objects that already exist on the remote because they are reachable from a different branch. For example, if I create a new commit and push it:then the hook script will get
and your
rev-listcommand will list all of the commits onmybrancheven though those commits are on master, and many of them are likely already present at origin.(Actually, your command would fail because
^0000000000000000000000000000000000000000is not a valid argument togit rev-list. So in this case the analogous command would be. But you know what I mean.)
AFAIK there is no way to get at the information you want (namely, the list of objects being newly pushed to the remote) without either hacking git or duplicating git's negotiation with the remote server 😦 Essentially you would need to know the value of all of the remote server's references before your push.
Maybe @peff knows a trick.
technoweenie commentedon Sep 16, 2014
This should work, right?
If we're pushing a new branch, we'd want to find the nearest ref (probably master, but it'd depend on where the push originated from).
@rubyist: Having a "dry run" option would be useful here. Have
git media push --dry-runlist the media blobs it intends to push. Maybe a--debugoption could show more detailed info about the git refs it used to come up with that list.rubyist commentedon Sep 16, 2014
In the case of a new branch where git really is pushing all of the objects, we can only know whether or not we should sync a git media file by asking the git media server. The git media server should already prevent uploading a full blob it already knows about, so maybe it's OK to use the full list that
rev-parsegives? We could perhaps add aremote-listlike command of our own to avoid all the roundtrips that just 201 or whatever.We'll definitely add a
--dry-runoption.peff commentedon Sep 17, 2014
There's not really a trick here. The closest simple thing is probably
git rev-list --objects $all_the_things_you_are_pushing --not --remotes=$remote_you_are_pushing_to. That is slightly different than what git generates because we are using what's inrefs/remotes(i.e., the cache from the last time we fetched), whereasgit pushwill actually ask the upstream for all of its ref tips. So it falls down in a few cases:refs/pull, or even.havetips from alternates repositories).You can get closer with:
That's still not quite right. The server side of a push may advertise slightly more refs than
ls-remotewill report, as it will advertise tips for alternates repos it has access to. We don't currently do that ourselves, but other servers might.Git doesn't actually do any other fancy negotiation to find common refs during a push, like it does for a fetch. The assumption is that you're typically building on what the other side has, so one of their refs should be a subset of what you already have. That could change in the future, but I don't think anybody has ever proposed to make it so.
mhagger commentedon Sep 17, 2014
@peff, does the server also report peeled versions of tags?
It sounds like a
git ls-remote --haves $remotecommand or something like that might be useful here.Though really, it is a shame to have to run
git ls-remoteseparately. It costs another round-trip, and possibly opens up race conditions between thepre-pushscript trying to second-guess what will be sent vs. thegit pushitself which knows exactly what it will send. For this purpose it would be better to have a hook that gets, on stdin, a list of all objects that are about to be pushed.peff commentedon Sep 17, 2014
@mhagger Yeah, I never noticed that, but we don't advertise peeled tags for pushes. Another issue:
receive-packdoes not necessarily advertise the same set of refs asupload-pack. Specifically, our servers do not advertiserefs/pullfor pushes (because it confuses--mirrorpushers).I agree that
ls-remote --haveswould be useful and would solve all of these problems, but that's going to mean a dependency on a newer version of git. If we're going that route, we might as well go with a separatesent-objectshook, which has all the advantages you noted.