@@ -13,7 +13,6 @@ import (
13
13
"strings"
14
14
"time"
15
15
16
- "golang.org/x/crypto/openpgp"
17
16
"github.com/go-git/go-git/v5/config"
18
17
"github.com/go-git/go-git/v5/internal/revision"
19
18
"github.com/go-git/go-git/v5/plumbing"
@@ -24,6 +23,8 @@ import (
24
23
"github.com/go-git/go-git/v5/storage"
25
24
"github.com/go-git/go-git/v5/storage/filesystem"
26
25
"github.com/go-git/go-git/v5/utils/ioutil"
26
+ "github.com/imdario/mergo"
27
+ "golang.org/x/crypto/openpgp"
27
28
28
29
"github.com/go-git/go-billy/v5"
29
30
"github.com/go-git/go-billy/v5/osfs"
@@ -155,7 +156,7 @@ func setConfigWorktree(r *Repository, worktree, storage billy.Filesystem) error
155
156
return nil
156
157
}
157
158
158
- cfg , err := r .Storer . Config ()
159
+ cfg , err := r .Config ()
159
160
if err != nil {
160
161
return err
161
162
}
@@ -439,9 +440,42 @@ func (r *Repository) Config() (*config.Config, error) {
439
440
return r .Storer .Config ()
440
441
}
441
442
443
+ // ConfigScoped returns the repository config, merged with requested scope and
444
+ // lower. For example if, config.GlobalScope is given the local and global config
445
+ // are returned merged in one config value.
446
+ func (r * Repository ) ConfigScoped (scope config.Scope ) (* config.Config , error ) {
447
+ // TODO(mcuadros): v6, add this as ConfigOptions.Scoped
448
+
449
+ var err error
450
+ system := config .NewConfig ()
451
+ if scope >= config .SystemScope {
452
+ system , err = config .LoadConfig (config .SystemScope )
453
+ if err != nil {
454
+ return nil , err
455
+ }
456
+ }
457
+
458
+ global := config .NewConfig ()
459
+ if scope >= config .GlobalScope {
460
+ global , err = config .LoadConfig (config .GlobalScope )
461
+ if err != nil {
462
+ return nil , err
463
+ }
464
+ }
465
+
466
+ local , err := r .Storer .Config ()
467
+ if err != nil {
468
+ return nil , err
469
+ }
470
+
471
+ _ = mergo .Merge (global , system )
472
+ _ = mergo .Merge (local , global )
473
+ return local , nil
474
+ }
475
+
442
476
// Remote return a remote if exists
443
477
func (r * Repository ) Remote (name string ) (* Remote , error ) {
444
- cfg , err := r .Storer . Config ()
478
+ cfg , err := r .Config ()
445
479
if err != nil {
446
480
return nil , err
447
481
}
@@ -456,7 +490,7 @@ func (r *Repository) Remote(name string) (*Remote, error) {
456
490
457
491
// Remotes returns a list with all the remotes
458
492
func (r * Repository ) Remotes () ([]* Remote , error ) {
459
- cfg , err := r .Storer . Config ()
493
+ cfg , err := r .Config ()
460
494
if err != nil {
461
495
return nil , err
462
496
}
@@ -480,7 +514,7 @@ func (r *Repository) CreateRemote(c *config.RemoteConfig) (*Remote, error) {
480
514
481
515
remote := NewRemote (r .Storer , c )
482
516
483
- cfg , err := r .Storer . Config ()
517
+ cfg , err := r .Config ()
484
518
if err != nil {
485
519
return nil , err
486
520
}
@@ -511,7 +545,7 @@ func (r *Repository) CreateRemoteAnonymous(c *config.RemoteConfig) (*Remote, err
511
545
512
546
// DeleteRemote delete a remote from the repository and delete the config
513
547
func (r * Repository ) DeleteRemote (name string ) error {
514
- cfg , err := r .Storer . Config ()
548
+ cfg , err := r .Config ()
515
549
if err != nil {
516
550
return err
517
551
}
@@ -526,7 +560,7 @@ func (r *Repository) DeleteRemote(name string) error {
526
560
527
561
// Branch return a Branch if exists
528
562
func (r * Repository ) Branch (name string ) (* config.Branch , error ) {
529
- cfg , err := r .Storer . Config ()
563
+ cfg , err := r .Config ()
530
564
if err != nil {
531
565
return nil , err
532
566
}
@@ -545,7 +579,7 @@ func (r *Repository) CreateBranch(c *config.Branch) error {
545
579
return err
546
580
}
547
581
548
- cfg , err := r .Storer . Config ()
582
+ cfg , err := r .Config ()
549
583
if err != nil {
550
584
return err
551
585
}
@@ -560,7 +594,7 @@ func (r *Repository) CreateBranch(c *config.Branch) error {
560
594
561
595
// DeleteBranch delete a Branch from the repository and delete the config
562
596
func (r * Repository ) DeleteBranch (name string ) error {
563
- cfg , err := r .Storer . Config ()
597
+ cfg , err := r .Config ()
564
598
if err != nil {
565
599
return err
566
600
}
@@ -835,7 +869,7 @@ func (r *Repository) cloneRefSpec(o *CloneOptions) []config.RefSpec {
835
869
}
836
870
837
871
func (r * Repository ) setIsBare (isBare bool ) error {
838
- cfg , err := r .Storer . Config ()
872
+ cfg , err := r .Config ()
839
873
if err != nil {
840
874
return err
841
875
}
@@ -851,7 +885,7 @@ func (r *Repository) updateRemoteConfigIfNeeded(o *CloneOptions, c *config.Remot
851
885
852
886
c .Fetch = r .cloneRefSpec (o )
853
887
854
- cfg , err := r .Storer . Config ()
888
+ cfg , err := r .Config ()
855
889
if err != nil {
856
890
return err
857
891
}
@@ -1541,7 +1575,7 @@ func (r *Repository) createNewObjectPack(cfg *RepackConfig) (h plumbing.Hash, er
1541
1575
return h , err
1542
1576
}
1543
1577
defer ioutil .CheckClose (wc , & err )
1544
- scfg , err := r .Storer . Config ()
1578
+ scfg , err := r .Config ()
1545
1579
if err != nil {
1546
1580
return h , err
1547
1581
}
0 commit comments