1
1
package git
2
2
3
3
import (
4
+ "context"
4
5
"errors"
5
6
"fmt"
6
7
stdioutil "io/ioutil"
@@ -168,19 +169,36 @@ func Open(s storage.Storer, worktree billy.Filesystem) (*Repository, error) {
168
169
169
170
// Clone a repository into the given Storer and worktree Filesystem with the
170
171
// 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.
172
177
func 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 ) {
173
191
r , err := Init (s , worktree )
174
192
if err != nil {
175
193
return nil , err
176
194
}
177
195
178
- return r , r .clone (o )
196
+ return r , r .clone (ctx , o )
179
197
}
180
198
181
199
// PlainInit create an empty git repository at the given path. isBare defines
182
200
// 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.
184
202
func PlainInit (path string , isBare bool ) (* Repository , error ) {
185
203
var wt , dot billy.Filesystem
186
204
@@ -279,14 +297,25 @@ func dotGitFileToOSFilesystem(path string, fs billy.Filesystem) (billy.Filesyste
279
297
280
298
// PlainClone a repository into the path with the given options, isBare defines
281
299
// if the new repository will be bare or normal. If the path is not empty
282
- // ErrRepositoryAlreadyExists is returned
300
+ // ErrRepositoryAlreadyExists is returned.
283
301
func 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 ) {
284
313
r , err := PlainInit (path , isBare )
285
314
if err != nil {
286
315
return nil , err
287
316
}
288
317
289
- return r , r .clone (o )
318
+ return r , r .clone (ctx , o )
290
319
}
291
320
292
321
func newRepository (s storage.Storer , worktree billy.Filesystem ) * Repository {
@@ -372,7 +401,7 @@ func (r *Repository) DeleteRemote(name string) error {
372
401
}
373
402
374
403
// Clone clones a remote repository
375
- func (r * Repository ) clone (o * CloneOptions ) error {
404
+ func (r * Repository ) clone (ctx context. Context , o * CloneOptions ) error {
376
405
if err := o .Validate (); err != nil {
377
406
return err
378
407
}
@@ -386,7 +415,7 @@ func (r *Repository) clone(o *CloneOptions) error {
386
415
return err
387
416
}
388
417
389
- head , err := r .fetchAndUpdateReferences (& FetchOptions {
418
+ head , err := r .fetchAndUpdateReferences (ctx , & FetchOptions {
390
419
RefSpecs : r .cloneRefSpec (o , c ),
391
420
Depth : o .Depth ,
392
421
Auth : o .Auth ,
@@ -469,7 +498,7 @@ func (r *Repository) updateRemoteConfigIfNeeded(o *CloneOptions, c *config.Remot
469
498
}
470
499
471
500
func (r * Repository ) fetchAndUpdateReferences (
472
- o * FetchOptions , ref plumbing.ReferenceName ,
501
+ ctx context. Context , o * FetchOptions , ref plumbing.ReferenceName ,
473
502
) (* plumbing.Reference , error ) {
474
503
475
504
if err := o .Validate (); err != nil {
@@ -482,7 +511,7 @@ func (r *Repository) fetchAndUpdateReferences(
482
511
}
483
512
484
513
objsUpdated := true
485
- remoteRefs , err := remote .fetch (o )
514
+ remoteRefs , err := remote .fetch (ctx , o )
486
515
if err == NoErrAlreadyUpToDate {
487
516
objsUpdated = false
488
517
} else if err != nil {
@@ -581,10 +610,25 @@ func updateReferenceStorerIfNeeded(
581
610
return false , nil
582
611
}
583
612
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
+ //
585
616
// Returns nil if the operation is successful, NoErrAlreadyUpToDate if there are
586
617
// no changes to be fetched, or an error.
587
618
func (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 {
588
632
if err := o .Validate (); err != nil {
589
633
return err
590
634
}
@@ -594,11 +638,24 @@ func (r *Repository) Fetch(o *FetchOptions) error {
594
638
return err
595
639
}
596
640
597
- return remote .Fetch ( o )
641
+ return remote .FetchContext ( ctx , o )
598
642
}
599
643
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.
601
647
func (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 {
602
659
if err := o .Validate (); err != nil {
603
660
return err
604
661
}
@@ -608,7 +665,7 @@ func (r *Repository) Push(o *PushOptions) error {
608
665
return err
609
666
}
610
667
611
- return remote .Push ( o )
668
+ return remote .PushContext ( ctx , o )
612
669
}
613
670
614
671
// Log returns the commit history from the given LogOptions.
0 commit comments