@@ -35,7 +35,8 @@ func GetConfigDir() (configDir string, err error) {
35
35
}
36
36
37
37
// 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 ) {
39
40
repoAddr , ok := configRepos [provider ]
40
41
if ! ok {
41
42
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
67
68
return
68
69
}
69
70
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 )
78
77
return
79
78
}
80
79
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
+
81
92
if err = wd .Checkout (& git.CheckoutOptions {
82
- Branch : plumbing .NewBranchReferenceName ( branch ),
93
+ Branch : plumbing .NewRemoteReferenceName ( remoteName , branch ),
83
94
Create : false ,
84
- Force : true ,
95
+ Keep : true ,
85
96
}); err != nil {
86
97
err = fmt .Errorf ("unable to checkout git branch: %s, error: %v" , branch , err )
87
98
return
@@ -101,6 +112,8 @@ func FetchLatestRepo(provider string, branch string, progress io.Writer) (err er
101
112
err = fmt .Errorf ("failed to open git local repository, error: %v" , err )
102
113
}
103
114
} else {
115
+ _ , _ = fmt .Fprintf (progress , "no local config exist, try to clone it" )
116
+
104
117
if _ , err = git .PlainClone (configDir , false , & git.CloneOptions {
105
118
RemoteName : remoteName ,
106
119
URL : repoAddr ,
0 commit comments