Is there a way to `push --force-with-lease`?
This type of force is extremely useful because it prevents pushing over changes that have been pushed by others since one's branch was last pulled. (relevant git docs page)
That is a newer extension that is not currently understood the library's network code.
Would it be possible to add this, so any tools built on top of libgit2 could utilise this much safer method of force pushing?
@tiennou - I suspect that you're the person most familiar with this part of the stack. Do you have any idea what we might need to do to support this? How much effort it would take?
I can confirm tiennou's suggested approach works! I just implemented something functional based on those instructions.
The actual code I wrote is not shareable—it's mostly a lot of indirection, where the actual work is done in a Swift callback—but the gist of it is:
- Before having libgit2 perform the push (e.g. your
git_remote_uploadcall), read theoidof the current head of the remote branch you're pushing. Pass it into your pushoptions.payload. - In your
push_negotiationcallback:- Use
git_remote_lson the passed-in remote to list the heads, and find the one you were pushing. (Compare them based on theirname. Some string massaging might be necessary to successfully compare.) - Check that that head's
oidis the same as the one you've stored at the beginning. In not, abort the push by returning an error (e.g. -1).
- Use
All in all, it turns out to be not that much code!