Skip to content

Commit 8950f39

Browse files
committed
Fix the potential fetch error
1 parent 6d93d84 commit 8950f39

File tree

3 files changed

+33
-20
lines changed

3 files changed

+33
-20
lines changed

cmd/install.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,15 @@ func (o *installOption) preRunE(cmd *cobra.Command, args []string) (err error) {
104104
if o.Category == "" {
105105
err = o.downloadOption.preRunE(cmd, args)
106106

107-
// try to find the real tool name
108-
if o.downloadOption.Package.TargetBinary != "" {
109-
o.tool = o.downloadOption.Package.TargetBinary
110-
} else if o.downloadOption.Package.Binary != "" {
111-
o.tool = o.downloadOption.Package.Binary
112-
} else {
113-
o.tool = o.downloadOption.Package.Repo
107+
if o.downloadOption.Package != nil {
108+
// try to find the real tool name
109+
if o.downloadOption.Package.TargetBinary != "" {
110+
o.tool = o.downloadOption.Package.TargetBinary
111+
} else if o.downloadOption.Package.Binary != "" {
112+
o.tool = o.downloadOption.Package.Binary
113+
} else {
114+
o.tool = o.downloadOption.Package.Repo
115+
}
114116
}
115117
} else {
116118
err = o.downloadOption.fetch()

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,6 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX
4444
cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0=
4545
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
4646
github.com/AlecAivazis/survey/v2 v2.2.2/go.mod h1:9FJRdMdDm8rnT+zHVbvQT2RTSTLq0Ttd6q3Vl2fahjk=
47-
github.com/AlecAivazis/survey/v2 v2.3.1 h1:lzkuHA60pER7L4eYL8qQJor4bUWlJe4V0gqAT19tdOA=
48-
github.com/AlecAivazis/survey/v2 v2.3.1/go.mod h1:TH2kPCDU3Kqq7pLbnCWwZXDBjnhZtmsCle5EiYDJ2fg=
4947
github.com/AlecAivazis/survey/v2 v2.3.2 h1:TqTB+aDDCLYhf9/bD2TwSO8u8jDSmMUd2SUVO4gCnU8=
5048
github.com/AlecAivazis/survey/v2 v2.3.2/go.mod h1:TH2kPCDU3Kqq7pLbnCWwZXDBjnhZtmsCle5EiYDJ2fg=
5149
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

pkg/installer/fetch.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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")
116+
104117
if _, err = git.PlainClone(configDir, false, &git.CloneOptions{
105118
RemoteName: remoteName,
106119
URL: repoAddr,

0 commit comments

Comments
 (0)