@@ -35,7 +35,8 @@ func GetConfigDir() (configDir string, err error) {
3535}
3636
3737// FetchLatestRepo fetches the hd-home as the config
38- func FetchLatestRepo (provider string , branch string , progress io.Writer ) (err error ) {
38+ func FetchLatestRepo (provider string , branch string ,
39+ progress io.Writer ) (err error ) {
3940 repoAddr , ok := configRepos [provider ]
4041 if ! ok {
4142 fmt .Printf ("not support '%s', use 'github' instead\n " , provider )
@@ -67,21 +68,31 @@ func FetchLatestRepo(provider string, branch string, progress io.Writer) (err er
6768 return
6869 }
6970
70- head , _ := repo .Head ()
71-
72- // avoid force push from remote
73- if err = wd .Reset (& git.ResetOptions {
74- Commit : head .Hash (),
75- Mode : git .HardReset ,
76- }); err != nil {
77- err = fmt .Errorf ("unable to reset to '%s'" , head .Hash ().String ())
71+ if err = repo .Fetch (& git.FetchOptions {
72+ RemoteName : remoteName ,
73+ Progress : progress ,
74+ Force : true ,
75+ }); err != nil && err != git .NoErrAlreadyUpToDate {
76+ err = fmt .Errorf ("failed to fetch '%s', error: %v" , remoteName , err )
7877 return
7978 }
8079
80+ head , _ := repo .Head ()
81+ if head != nil {
82+ // avoid force push from remote
83+ if err = wd .Reset (& git.ResetOptions {
84+ Commit : head .Hash (),
85+ Mode : git .HardReset ,
86+ }); err != nil {
87+ err = fmt .Errorf ("unable to reset to '%s'" , head .Hash ().String ())
88+ return
89+ }
90+ }
91+
8192 if err = wd .Checkout (& git.CheckoutOptions {
82- Branch : plumbing .NewBranchReferenceName ( branch ),
93+ Branch : plumbing .NewRemoteReferenceName ( remoteName , branch ),
8394 Create : false ,
84- Force : true ,
95+ Keep : true ,
8596 }); err != nil {
8697 err = fmt .Errorf ("unable to checkout git branch: %s, error: %v" , branch , err )
8798 return
@@ -101,6 +112,8 @@ func FetchLatestRepo(provider string, branch string, progress io.Writer) (err er
101112 err = fmt .Errorf ("failed to open git local repository, error: %v" , err )
102113 }
103114 } else {
115+ _ , _ = fmt .Fprintf (progress , "no local config exist, try to clone it\n " )
116+
104117 if _ , err = git .PlainClone (configDir , false , & git.CloneOptions {
105118 RemoteName : remoteName ,
106119 URL : repoAddr ,
0 commit comments