11package git
22
33import (
4+ "context"
45 "errors"
56 "fmt"
67 stdioutil "io/ioutil"
@@ -168,19 +169,36 @@ func Open(s storage.Storer, worktree billy.Filesystem) (*Repository, error) {
168169
169170// Clone a repository into the given Storer and worktree Filesystem with the
170171// given options, if worktree is nil a bare repository is created. If the given
171- // storer is not empty ErrRepositoryAlreadyExists is returned
172+ // storer is not empty ErrRepositoryAlreadyExists is returned.
173+ //
174+ // The provided Context must be non-nil. If the context expires before the
175+ // operation is complete, an error is returned. The context only affects to the
176+ // transport operations.
172177func Clone (s storage.Storer , worktree billy.Filesystem , o * CloneOptions ) (* Repository , error ) {
178+ return CloneContext (context .Background (), s , worktree , o )
179+ }
180+
181+ // CloneContext a repository into the given Storer and worktree Filesystem with
182+ // the given options, if worktree is nil a bare repository is created. If the
183+ // given storer is not empty ErrRepositoryAlreadyExists is returned.
184+ //
185+ // The provided Context must be non-nil. If the context expires before the
186+ // operation is complete, an error is returned. The context only affects to the
187+ // transport operations.
188+ func CloneContext (
189+ ctx context.Context , s storage.Storer , worktree billy.Filesystem , o * CloneOptions ,
190+ ) (* Repository , error ) {
173191 r , err := Init (s , worktree )
174192 if err != nil {
175193 return nil , err
176194 }
177195
178- return r , r .clone (o )
196+ return r , r .clone (ctx , o )
179197}
180198
181199// PlainInit create an empty git repository at the given path. isBare defines
182200// if the repository will have worktree (non-bare) or not (bare), if the path
183- // is not empty ErrRepositoryAlreadyExists is returned
201+ // is not empty ErrRepositoryAlreadyExists is returned.
184202func PlainInit (path string , isBare bool ) (* Repository , error ) {
185203 var wt , dot billy.Filesystem
186204
@@ -279,14 +297,25 @@ func dotGitFileToOSFilesystem(path string, fs billy.Filesystem) (billy.Filesyste
279297
280298// PlainClone a repository into the path with the given options, isBare defines
281299// if the new repository will be bare or normal. If the path is not empty
282- // ErrRepositoryAlreadyExists is returned
300+ // ErrRepositoryAlreadyExists is returned.
283301func PlainClone (path string , isBare bool , o * CloneOptions ) (* Repository , error ) {
302+ return PlainCloneContext (context .Background (), path , isBare , o )
303+ }
304+
305+ // PlainCloneContext a repository into the path with the given options, isBare
306+ // defines if the new repository will be bare or normal. If the path is not empty
307+ // ErrRepositoryAlreadyExists is returned.
308+ //
309+ // The provided Context must be non-nil. If the context expires before the
310+ // operation is complete, an error is returned. The context only affects to the
311+ // transport operations.
312+ func PlainCloneContext (ctx context.Context , path string , isBare bool , o * CloneOptions ) (* Repository , error ) {
284313 r , err := PlainInit (path , isBare )
285314 if err != nil {
286315 return nil , err
287316 }
288317
289- return r , r .clone (o )
318+ return r , r .clone (ctx , o )
290319}
291320
292321func newRepository (s storage.Storer , worktree billy.Filesystem ) * Repository {
@@ -372,7 +401,7 @@ func (r *Repository) DeleteRemote(name string) error {
372401}
373402
374403// Clone clones a remote repository
375- func (r * Repository ) clone (o * CloneOptions ) error {
404+ func (r * Repository ) clone (ctx context. Context , o * CloneOptions ) error {
376405 if err := o .Validate (); err != nil {
377406 return err
378407 }
@@ -386,7 +415,7 @@ func (r *Repository) clone(o *CloneOptions) error {
386415 return err
387416 }
388417
389- head , err := r .fetchAndUpdateReferences (& FetchOptions {
418+ head , err := r .fetchAndUpdateReferences (ctx , & FetchOptions {
390419 RefSpecs : r .cloneRefSpec (o , c ),
391420 Depth : o .Depth ,
392421 Auth : o .Auth ,
@@ -469,7 +498,7 @@ func (r *Repository) updateRemoteConfigIfNeeded(o *CloneOptions, c *config.Remot
469498}
470499
471500func (r * Repository ) fetchAndUpdateReferences (
472- o * FetchOptions , ref plumbing.ReferenceName ,
501+ ctx context. Context , o * FetchOptions , ref plumbing.ReferenceName ,
473502) (* plumbing.Reference , error ) {
474503
475504 if err := o .Validate (); err != nil {
@@ -482,7 +511,7 @@ func (r *Repository) fetchAndUpdateReferences(
482511 }
483512
484513 objsUpdated := true
485- remoteRefs , err := remote .fetch (o )
514+ remoteRefs , err := remote .fetch (ctx , o )
486515 if err == NoErrAlreadyUpToDate {
487516 objsUpdated = false
488517 } else if err != nil {
@@ -581,10 +610,25 @@ func updateReferenceStorerIfNeeded(
581610 return false , nil
582611}
583612
584- // Fetch fetches changes from a remote repository.
613+ // Fetch fetches references along with the objects necessary to complete
614+ // their histories, from the remote named as FetchOptions.RemoteName.
615+ //
585616// Returns nil if the operation is successful, NoErrAlreadyUpToDate if there are
586617// no changes to be fetched, or an error.
587618func (r * Repository ) Fetch (o * FetchOptions ) error {
619+ return r .FetchContext (context .Background (), o )
620+ }
621+
622+ // FetchContext fetches references along with the objects necessary to complete
623+ // their histories, from the remote named as FetchOptions.RemoteName.
624+ //
625+ // Returns nil if the operation is successful, NoErrAlreadyUpToDate if there are
626+ // no changes to be fetched, or an error.
627+ //
628+ // The provided Context must be non-nil. If the context expires before the
629+ // operation is complete, an error is returned. The context only affects to the
630+ // transport operations.
631+ func (r * Repository ) FetchContext (ctx context.Context , o * FetchOptions ) error {
588632 if err := o .Validate (); err != nil {
589633 return err
590634 }
@@ -594,11 +638,24 @@ func (r *Repository) Fetch(o *FetchOptions) error {
594638 return err
595639 }
596640
597- return remote .Fetch ( o )
641+ return remote .FetchContext ( ctx , o )
598642}
599643
600- // Push pushes changes to a remote.
644+ // Push performs a push to the remote. Returns NoErrAlreadyUpToDate if
645+ // the remote was already up-to-date, from the remote named as
646+ // FetchOptions.RemoteName.
601647func (r * Repository ) Push (o * PushOptions ) error {
648+ return r .PushContext (context .Background (), o )
649+ }
650+
651+ // PushContext performs a push to the remote. Returns NoErrAlreadyUpToDate if
652+ // the remote was already up-to-date, from the remote named as
653+ // FetchOptions.RemoteName.
654+ //
655+ // The provided Context must be non-nil. If the context expires before the
656+ // operation is complete, an error is returned. The context only affects to the
657+ // transport operations.
658+ func (r * Repository ) PushContext (ctx context.Context , o * PushOptions ) error {
602659 if err := o .Validate (); err != nil {
603660 return err
604661 }
@@ -608,7 +665,7 @@ func (r *Repository) Push(o *PushOptions) error {
608665 return err
609666 }
610667
611- return remote .Push ( o )
668+ return remote .PushContext ( ctx , o )
612669}
613670
614671// Log returns the commit history from the given LogOptions.
0 commit comments