Merge branch 'cmn/checkout-opts'

This commit is contained in:
Carlos Martín Nieto 2014-03-11 03:36:28 +01:00
commit ea909d8518
1 changed files with 26 additions and 17 deletions

View File

@ -2,10 +2,6 @@ package git
/* /*
#include <git2.h> #include <git2.h>
git_checkout_opts git_checkout_opts_init() {
git_checkout_opts ret = GIT_CHECKOUT_OPTS_INIT;
return ret;
}
*/ */
import "C" import "C"
import ( import (
@ -42,28 +38,34 @@ type CheckoutOpts struct {
FileOpenFlags int // Default is O_CREAT | O_TRUNC | O_WRONLY FileOpenFlags int // Default is O_CREAT | O_TRUNC | O_WRONLY
} }
// Convert the CheckoutOpts struct to the corresponding C-struct // Convert the CheckoutOpts struct to the corresponding
func populateCheckoutOpts(ptr *C.git_checkout_opts, opts *CheckoutOpts) { // C-struct. Returns a pointer to ptr, or nil if opts is nil, in order
*ptr = C.git_checkout_opts_init() // to help with what to pass.
func populateCheckoutOpts(ptr *C.git_checkout_options, opts *CheckoutOpts) *C.git_checkout_options {
if opts == nil { if opts == nil {
return return nil
} }
C.git_checkout_init_opts(ptr, 1)
ptr.checkout_strategy = C.uint(opts.Strategy) ptr.checkout_strategy = C.uint(opts.Strategy)
ptr.disable_filters = cbool(opts.DisableFilters) ptr.disable_filters = cbool(opts.DisableFilters)
ptr.dir_mode = C.uint(opts.DirMode.Perm()) ptr.dir_mode = C.uint(opts.DirMode.Perm())
ptr.file_mode = C.uint(opts.FileMode.Perm()) ptr.file_mode = C.uint(opts.FileMode.Perm())
return ptr
} }
// Updates files in the index and the working tree to match the content of // Updates files in the index and the working tree to match the content of
// the commit pointed at by HEAD. // the commit pointed at by HEAD. opts may be nil.
func (v *Repository) Checkout(opts *CheckoutOpts) error { func (v *Repository) CheckoutHead(opts *CheckoutOpts) error {
var copts C.git_checkout_opts var copts C.git_checkout_options
populateCheckoutOpts(&copts, opts)
ptr := populateCheckoutOpts(&copts, opts)
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ret := C.git_checkout_head(v.ptr, &copts) ret := C.git_checkout_head(v.ptr, ptr)
if ret < 0 { if ret < 0 {
return MakeGitError(ret) return MakeGitError(ret)
} }
@ -71,15 +73,22 @@ func (v *Repository) Checkout(opts *CheckoutOpts) error {
return nil return nil
} }
// Updates files in the working tree to match the content of the index. // Updates files in the working tree to match the content of the given
// index. If index is nil, the repository's index will be used. opts
// may be nil.
func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error { func (v *Repository) CheckoutIndex(index *Index, opts *CheckoutOpts) error {
var copts C.git_checkout_opts var copts C.git_checkout_options
populateCheckoutOpts(&copts, opts) ptr := populateCheckoutOpts(&copts, opts)
var iptr *C.git_index = nil
if index != nil {
iptr = index.ptr
}
runtime.LockOSThread() runtime.LockOSThread()
defer runtime.UnlockOSThread() defer runtime.UnlockOSThread()
ret := C.git_checkout_index(v.ptr, index.ptr, &copts) ret := C.git_checkout_index(v.ptr, iptr, ptr)
if ret < 0 { if ret < 0 {
return MakeGitError(ret) return MakeGitError(ret)
} }