55 "fmt"
66 "io"
77 "os"
8- "path"
8+ "path/filepath "
99 "strings"
1010
1111 "github.com/go-git/go-git/v5/plumbing/object"
@@ -32,6 +32,7 @@ var configRepos = map[string]string{
3232// Fetcher is the interface of a fetcher which responses to fetch config files
3333type Fetcher interface {
3434 GetConfigDir () (configDir string , err error )
35+ GetHomeDir () (homeDir string , err error )
3536 FetchLatestRepo (provider string , branch string ,
3637 progress io.Writer ) (err error )
3738 SetContext (ctx context.Context )
@@ -45,12 +46,18 @@ type DefaultFetcher struct {
4546// GetConfigDir returns the directory of the config
4647func (f * DefaultFetcher ) GetConfigDir () (configDir string , err error ) {
4748 var userHome string
48- if userHome , err = homedir . Dir (); err == nil {
49- configDir = path .Join (userHome , fmt .Sprintf (".config%shd-home" , string (os .PathSeparator )))
49+ if userHome , err = f . GetHomeDir (); err == nil {
50+ configDir = filepath .Join (userHome , fmt .Sprintf (".config%shd-home" , string (os .PathSeparator )))
5051 }
5152 return
5253}
5354
55+ // GetHomeDir returns the user home directory
56+ func (f * DefaultFetcher ) GetHomeDir () (homeDir string , err error ) {
57+ homeDir , err = homedir .Dir ()
58+ return
59+ }
60+
5461// SetContext sets the context of the fetch
5562func (f * DefaultFetcher ) SetContext (ctx context.Context ) {
5663 f .ctx = ctx
@@ -125,6 +132,7 @@ func (f *DefaultFetcher) FetchLatestRepo(provider string, branch string,
125132 RemoteName : remoteName ,
126133 Progress : progress ,
127134 Force : true ,
135+ Depth : 1 ,
128136 }); err != nil && err != git .NoErrAlreadyUpToDate {
129137 err = fmt .Errorf ("failed to fetch '%s', error: %v" , remoteName , err )
130138 return
@@ -184,9 +192,10 @@ func (f *DefaultFetcher) FetchLatestRepo(provider string, branch string,
184192 _ , _ = fmt .Fprintf (progress , "no local config exist, try to clone it\n " )
185193
186194 if _ , err = git .PlainCloneContext (f .ctx , configDir , false , & git.CloneOptions {
187- RemoteName : remoteName ,
188- URL : repoAddr ,
189- Progress : progress ,
195+ RemoteName : remoteName ,
196+ URL : repoAddr ,
197+ Progress : progress ,
198+ SingleBranch : true ,
190199 }); err != nil {
191200 err = fmt .Errorf ("failed to clone git repository '%s' into '%s', error: %v" , repoAddr , configDir , err )
192201 }
@@ -213,6 +222,7 @@ func makeSureRemote(name, repoAddr string, repo *git.Repository) (err error) {
213222// FakeFetcher is a fake fetch. We expect to use it for unit test cases.
214223type FakeFetcher struct {
215224 ConfigDir string
225+ HomeDir string
216226 GetConfigDirErr error
217227 FetchLatestRepoErr error
218228}
@@ -224,6 +234,13 @@ func (f *FakeFetcher) GetConfigDir() (configDir string, err error) {
224234 return
225235}
226236
237+ // GetHomeDir is a fake method
238+ func (f * FakeFetcher ) GetHomeDir () (HomeDir string , err error ) {
239+ HomeDir = f .HomeDir
240+ err = f .GetConfigDirErr
241+ return
242+ }
243+
227244// FetchLatestRepo is a fake method
228245func (f * FakeFetcher ) FetchLatestRepo (provider string , branch string ,
229246 progress io.Writer ) (err error ) {
0 commit comments