diff options
Diffstat (limited to 'remote.go')
| -rw-r--r-- | remote.go | 260 |
1 files changed, 117 insertions, 143 deletions
@@ -69,6 +69,51 @@ type RemoteCallbacks struct { PushUpdateReferenceCallback } +type FetchPrune uint + +const ( + // Use the setting from the configuration + FetchPruneUnspecified FetchPrune = C.GIT_FETCH_PRUNE_UNSPECIFIED + // Force pruning on + FetchPruneOn FetchPrune = C.GIT_FETCH_PRUNE + // Force pruning off + FetchNoPrune FetchPrune = C.GIT_FETCH_NO_PRUNE +) + +type DownloadTags uint + +const ( + + // Use the setting from the configuration. + DownloadTagsUnspecified DownloadTags = C.GIT_REMOTE_DOWNLOAD_TAGS_UNSPECIFIED + // Ask the server for tags pointing to objects we're already + // downloading. + DownloadTagsAuto DownloadTags = C.GIT_REMOTE_DOWNLOAD_TAGS_AUTO + + // Don't ask for any tags beyond the refspecs. + DownloadTagsNone DownloadTags = C.GIT_REMOTE_DOWNLOAD_TAGS_NONE + + // Ask for the all the tags. + DownloadTagsAll DownloadTags = C.GIT_REMOTE_DOWNLOAD_TAGS_ALL +) + +type FetchOptions struct { + // Callbacks to use for this fetch operation + RemoteCallbacks RemoteCallbacks + // Whether to perform a prune after the fetch + Prune FetchPrune + // Whether to write the results to FETCH_HEAD. Defaults to + // on. Leave this default in order to behave like git. + UpdateFetchhead bool + + // Determines how to behave regarding tags on the remote, such + // as auto-downloading tags for objects we're downloading or + // downloading all of them. + // + // The default is to auto-follow tags. + DownloadTags DownloadTags +} + type Remote struct { ptr *C.git_remote callbacks RemoteCallbacks @@ -123,6 +168,12 @@ func newRemoteHeadFromC(ptr *C.git_remote_head) RemoteHead { } } +func untrackCalbacksPayload(callbacks *C.git_remote_callbacks) { + if callbacks != nil && callbacks.payload != nil { + pointerHandles.Untrack(callbacks.payload) + } +} + func populateRemoteCallbacks(ptr *C.git_remote_callbacks, callbacks *RemoteCallbacks) { C.git_remote_init_callbacks(ptr, C.GIT_REMOTE_CALLBACKS_VERSION) if callbacks == nil { @@ -267,41 +318,22 @@ func RemoteIsValidName(name string) bool { return false } -func (r *Remote) SetCallbacks(callbacks *RemoteCallbacks) error { - r.callbacks = *callbacks - - var ccallbacks C.git_remote_callbacks - populateRemoteCallbacks(&ccallbacks, &r.callbacks) - - runtime.LockOSThread() - defer runtime.UnlockOSThread() - - ecode := C.git_remote_set_callbacks(r.ptr, &ccallbacks) - if ecode < 0 { - return MakeGitError(ecode) - } - - return nil -} - func (r *Remote) Free() { runtime.SetFinalizer(r, nil) - - callbacks := C.git_remote_get_callbacks(r.ptr) - if callbacks != nil && callbacks.payload != nil { - pointerHandles.Untrack(callbacks.payload) - } - C.git_remote_free(r.ptr) } -func (repo *Repository) ListRemotes() ([]string, error) { +type RemoteCollection struct { + repo *Repository +} + +func (c *RemoteCollection) List() ([]string, error) { var r C.git_strarray runtime.LockOSThread() defer runtime.UnlockOSThread() - ecode := C.git_remote_list(&r, repo.ptr) + ecode := C.git_remote_list(&r, c.repo.ptr) if ecode < 0 { return nil, MakeGitError(ecode) } @@ -311,7 +343,7 @@ func (repo *Repository) ListRemotes() ([]string, error) { return remotes, nil } -func (repo *Repository) CreateRemote(name string, url string) (*Remote, error) { +func (c *RemoteCollection) Create(name string, url string) (*Remote, error) { remote := &Remote{} cname := C.CString(name) @@ -322,7 +354,7 @@ func (repo *Repository) CreateRemote(name string, url string) (*Remote, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_remote_create(&remote.ptr, repo.ptr, cname, curl) + ret := C.git_remote_create(&remote.ptr, c.repo.ptr, cname, curl) if ret < 0 { return nil, MakeGitError(ret) } @@ -330,21 +362,21 @@ func (repo *Repository) CreateRemote(name string, url string) (*Remote, error) { return remote, nil } -func (repo *Repository) DeleteRemote(name string) error { +func (c *RemoteCollection) Delete(name string) error { cname := C.CString(name) defer C.free(unsafe.Pointer(cname)) runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_remote_delete(repo.ptr, cname) + ret := C.git_remote_delete(c.repo.ptr, cname) if ret < 0 { return MakeGitError(ret) } return nil } -func (repo *Repository) CreateRemoteWithFetchspec(name string, url string, fetch string) (*Remote, error) { +func (c *RemoteCollection) CreateWithFetchspec(name string, url string, fetch string) (*Remote, error) { remote := &Remote{} cname := C.CString(name) @@ -357,7 +389,7 @@ func (repo *Repository) CreateRemoteWithFetchspec(name string, url string, fetch runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_remote_create_with_fetchspec(&remote.ptr, repo.ptr, cname, curl, cfetch) + ret := C.git_remote_create_with_fetchspec(&remote.ptr, c.repo.ptr, cname, curl, cfetch) if ret < 0 { return nil, MakeGitError(ret) } @@ -365,18 +397,16 @@ func (repo *Repository) CreateRemoteWithFetchspec(name string, url string, fetch return remote, nil } -func (repo *Repository) CreateAnonymousRemote(url, fetch string) (*Remote, error) { +func (c *RemoteCollection) CreateAnonymous(url string) (*Remote, error) { remote := &Remote{} curl := C.CString(url) defer C.free(unsafe.Pointer(curl)) - cfetch := C.CString(fetch) - defer C.free(unsafe.Pointer(cfetch)) runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_remote_create_anonymous(&remote.ptr, repo.ptr, curl, cfetch) + ret := C.git_remote_create_anonymous(&remote.ptr, c.repo.ptr, curl) if ret < 0 { return nil, MakeGitError(ret) } @@ -384,7 +414,7 @@ func (repo *Repository) CreateAnonymousRemote(url, fetch string) (*Remote, error return remote, nil } -func (repo *Repository) LookupRemote(name string) (*Remote, error) { +func (c *RemoteCollection) Lookup(name string) (*Remote, error) { remote := &Remote{} cname := C.CString(name) @@ -393,7 +423,7 @@ func (repo *Repository) LookupRemote(name string) (*Remote, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_remote_lookup(&remote.ptr, repo.ptr, cname) + ret := C.git_remote_lookup(&remote.ptr, c.repo.ptr, cname) if ret < 0 { return nil, MakeGitError(ret) } @@ -401,22 +431,6 @@ func (repo *Repository) LookupRemote(name string) (*Remote, error) { return remote, nil } -func (o *Remote) Save() error { - - runtime.LockOSThread() - defer runtime.UnlockOSThread() - - ret := C.git_remote_save(o.ptr) - if ret < 0 { - return MakeGitError(ret) - } - return nil -} - -func (o *Remote) Owner() Repository { - return Repository{C.git_remote_owner(o.ptr)} -} - func (o *Remote) Name() string { return C.GoString(C.git_remote_name(o.ptr)) } @@ -429,42 +443,48 @@ func (o *Remote) PushUrl() string { return C.GoString(C.git_remote_pushurl(o.ptr)) } -func (o *Remote) SetUrl(url string) error { +func (c *RemoteCollection) SetUrl(remote, url string) error { curl := C.CString(url) defer C.free(unsafe.Pointer(curl)) + cremote := C.CString(remote) + defer C.free(unsafe.Pointer(cremote)) runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_remote_set_url(o.ptr, curl) + ret := C.git_remote_set_url(c.repo.ptr, cremote, curl) if ret < 0 { return MakeGitError(ret) } return nil } -func (o *Remote) SetPushUrl(url string) error { +func (c *RemoteCollection) SetPushUrl(remote, url string) error { curl := C.CString(url) defer C.free(unsafe.Pointer(curl)) + cremote := C.CString(remote) + defer C.free(unsafe.Pointer(cremote)) runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_remote_set_pushurl(o.ptr, curl) + ret := C.git_remote_set_pushurl(c.repo.ptr, cremote, curl) if ret < 0 { return MakeGitError(ret) } return nil } -func (o *Remote) AddFetch(refspec string) error { +func (c *RemoteCollection) AddFetch(remote, refspec string) error { crefspec := C.CString(refspec) defer C.free(unsafe.Pointer(crefspec)) + cremote := C.CString(remote) + defer C.free(unsafe.Pointer(cremote)) runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_remote_add_fetch(o.ptr, crefspec) + ret := C.git_remote_add_fetch(c.repo.ptr, cremote, crefspec) if ret < 0 { return MakeGitError(ret) } @@ -525,30 +545,16 @@ func (o *Remote) FetchRefspecs() ([]string, error) { return refspecs, nil } -func (o *Remote) SetFetchRefspecs(refspecs []string) error { - crefspecs := C.git_strarray{} - crefspecs.count = C.size_t(len(refspecs)) - crefspecs.strings = makeCStringsFromStrings(refspecs) - defer freeStrarray(&crefspecs) - - runtime.LockOSThread() - defer runtime.UnlockOSThread() - - ret := C.git_remote_set_fetch_refspecs(o.ptr, &crefspecs) - if ret < 0 { - return MakeGitError(ret) - } - return nil -} - -func (o *Remote) AddPush(refspec string) error { +func (c *RemoteCollection) AddPush(remote, refspec string) error { crefspec := C.CString(refspec) defer C.free(unsafe.Pointer(crefspec)) + cremote := C.CString(remote) + defer C.free(unsafe.Pointer(cremote)) runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_remote_add_push(o.ptr, crefspec) + ret := C.git_remote_add_push(c.repo.ptr, cremote, crefspec) if ret < 0 { return MakeGitError(ret) } @@ -570,52 +576,26 @@ func (o *Remote) PushRefspecs() ([]string, error) { return refspecs, nil } -func (o *Remote) SetPushRefspecs(refspecs []string) error { - crefspecs := C.git_strarray{} - crefspecs.count = C.size_t(len(refspecs)) - crefspecs.strings = makeCStringsFromStrings(refspecs) - defer freeStrarray(&crefspecs) - - runtime.LockOSThread() - defer runtime.UnlockOSThread() - - ret := C.git_remote_set_push_refspecs(o.ptr, &crefspecs) - if ret < 0 { - return MakeGitError(ret) - } - return nil -} - -func (o *Remote) ClearRefspecs() { - C.git_remote_clear_refspecs(o.ptr) -} - func (o *Remote) RefspecCount() uint { return uint(C.git_remote_refspec_count(o.ptr)) } -func (o *Remote) SetUpdateFetchHead(val bool) { - C.git_remote_set_update_fetchhead(o.ptr, cbool(val)) -} - -func (o *Remote) UpdateFetchHead() bool { - return C.git_remote_update_fetchhead(o.ptr) > 0 +func populateFetchOptions(options *C.git_fetch_options, opts *FetchOptions) { + C.git_fetch_init_options(options, C.GIT_FETCH_OPTIONS_VERSION) + if opts == nil { + return; + } + populateRemoteCallbacks(&options.callbacks, &opts.RemoteCallbacks) + options.prune = C.git_fetch_prune_t(opts.Prune) + options.update_fetchhead = cbool(opts.UpdateFetchhead) + options.download_tags = C.git_remote_autotag_option_t(opts.DownloadTags) } // Fetch performs a fetch operation. refspecs specifies which refspecs // to use for this fetch, use an empty list to use the refspecs from -// the configuration; sig and msg specify what to use for the reflog -// entries. Leave nil and "" to use defaults. -func (o *Remote) Fetch(refspecs []string, sig *Signature, msg string) error { - - var csig *C.git_signature = nil - if sig != nil { - csig, err := sig.toC() - if err != nil { - return err - } - defer C.free(unsafe.Pointer(csig)) - } +// the configuration; msg specifies what to use for the reflog +// entries. Leave "" to use defaults. +func (o *Remote) Fetch(refspecs []string, opts *FetchOptions, msg string) error { var cmsg *C.char = nil if msg != "" { @@ -628,29 +608,36 @@ func (o *Remote) Fetch(refspecs []string, sig *Signature, msg string) error { crefspecs.strings = makeCStringsFromStrings(refspecs) defer freeStrarray(&crefspecs) + var coptions C.git_fetch_options + populateFetchOptions(&coptions, opts); + defer untrackCalbacksPayload(&coptions.callbacks) + runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_remote_fetch(o.ptr, &crefspecs, csig, cmsg) + ret := C.git_remote_fetch(o.ptr, &crefspecs, &coptions, cmsg) if ret < 0 { return MakeGitError(ret) } return nil } -func (o *Remote) ConnectFetch() error { - return o.Connect(ConnectDirectionFetch) +func (o *Remote) ConnectFetch(callbacks *RemoteCallbacks) error { + return o.Connect(ConnectDirectionFetch, callbacks) } -func (o *Remote) ConnectPush() error { - return o.Connect(ConnectDirectionPush) +func (o *Remote) ConnectPush(callbacks *RemoteCallbacks) error { + return o.Connect(ConnectDirectionPush, callbacks) } -func (o *Remote) Connect(direction ConnectDirection) error { +func (o *Remote) Connect(direction ConnectDirection, callbacks *RemoteCallbacks) error { + var ccallbacks C.git_remote_callbacks; + populateRemoteCallbacks(&ccallbacks, callbacks) + runtime.LockOSThread() defer runtime.UnlockOSThread() - if ret := C.git_remote_connect(o.ptr, C.git_direction(direction)); ret != 0 { + if ret := C.git_remote_connect(o.ptr, C.git_direction(direction), &ccallbacks); ret != 0 { return MakeGitError(ret) } return nil @@ -702,24 +689,7 @@ func (o *Remote) Ls(filterRefs ...string) ([]RemoteHead, error) { return heads, nil } -func (o *Remote) Push(refspecs []string, opts *PushOptions, sig *Signature, msg string) error { - var csig *C.git_signature = nil - if sig != nil { - csig, err := sig.toC() - if err != nil { - return err - } - defer C.free(unsafe.Pointer(csig)) - } - - var cmsg *C.char - if msg == "" { - cmsg = nil - } else { - cmsg = C.CString(msg) - defer C.free(unsafe.Pointer(cmsg)) - } - +func (o *Remote) Push(refspecs []string, opts *PushOptions) error { var copts C.git_push_options C.git_push_init_options(&copts, C.GIT_PUSH_OPTIONS_VERSION) if opts != nil { @@ -733,8 +703,9 @@ func (o *Remote) Push(refspecs []string, opts *PushOptions, sig *Signature, msg runtime.LockOSThread() defer runtime.UnlockOSThread() + defer untrackCalbacksPayload(&copts.callbacks) - ret := C.git_remote_push(o.ptr, &crefspecs, &copts, csig, cmsg) + ret := C.git_remote_push(o.ptr, &crefspecs, &copts) if ret < 0 { return MakeGitError(ret) } @@ -745,11 +716,14 @@ func (o *Remote) PruneRefs() bool { return C.git_remote_prune_refs(o.ptr) > 0 } -func (o *Remote) Prune() error { +func (o *Remote) Prune(callbacks *RemoteCallbacks) error { + var ccallbacks C.git_remote_callbacks; + populateRemoteCallbacks(&ccallbacks, callbacks) + runtime.LockOSThread() defer runtime.UnlockOSThread() - ret := C.git_remote_prune(o.ptr) + ret := C.git_remote_prune(o.ptr, &ccallbacks) if ret < 0 { return MakeGitError(ret) } |
