Fix checkout methods that use notifyBlock/GTCheckoutStrategySafe by dleehr · Pull Request #510 · libgit2/objective-git (original) (raw)

Previously, performCheckout method moved HEAD to the target, then called git_checkout_head(). This does not match behavior in git core, and prevents the checkoutStrategy and notifyCallback parameters from being used correctly. The only way to reliably checkout a branch or commit was to use GTCheckoutStrategyForce, which throws away conflicts. Also, the HEAD could be moved even if the checkout fails, resulting in a confused state.

This PR corrects the checkout behavior with two changes:

  1. HEAD is not moved until after the checkout succeeds.
  2. git_checkout_tree() is used instead of git_checkout_head(). The tree that is checked out is the target object (commit or ref).

Included tests demonstrate the correct behavior catching a conflicted file. The notifyBlock is called to report the conflict, and with GTCheckoutStrategySafe, the checkout fails because of the conflict as expected.