libgit2 icon indicating copy to clipboard operation
libgit2 copied to clipboard

Is there a way to `push --force-with-lease`?

Open Mr-Wallet opened this issue 9 years ago • 4 comments

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)

Mr-Wallet avatar Sep 30 '16 23:09 Mr-Wallet

That is a newer extension that is not currently understood the library's network code.

carlosmn avatar Oct 02 '16 19:10 carlosmn

Would it be possible to add this, so any tools built on top of libgit2 could utilise this much safer method of force pushing?

yonaskolb avatar Feb 24 '18 21:02 yonaskolb

@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?

ethomson avatar Jan 17 '20 10:01 ethomson

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:

  1. Before having libgit2 perform the push (e.g. your git_remote_upload call), read the oid of the current head of the remote branch you're pushing. Pass it into your push options.payload.
  2. In your push_negotiation callback:
    1. Use git_remote_ls on the passed-in remote to list the heads, and find the one you were pushing. (Compare them based on their name. Some string massaging might be necessary to successfully compare.)
    2. Check that that head's oid is the same as the one you've stored at the beginning. In not, abort the push by returning an error (e.g. -1).

All in all, it turns out to be not that much code!

Cykelero avatar Jan 29 '24 18:01 Cykelero