Skip to content

Commit 8203cc5

Browse files
authored
refactor: native-git-client fix tests and some minor changes (#2013)
1 parent 5fdbae0 commit 8203cc5

File tree

3 files changed

+36
-19
lines changed

3 files changed

+36
-19
lines changed

Diff for: internal/git/git.go

+34-17
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,30 @@ func (r Repo) RemoteURL() (string, error) {
9292
return remotes[0].Config().URLs[0], nil
9393
}
9494

95+
func (r Repo) RemoteDefaultBranch() (string, error) {
96+
// @jiminychris - https://github.com/go-git/go-git/issues/510#issuecomment-2560116147
97+
repo, err := gitOpen(r.Directory)
98+
if err != nil {
99+
return "", err
100+
}
101+
102+
// Get the remote you want to find the default for
103+
remote, err := repo.Remote("origin")
104+
if err != nil {
105+
return "", err
106+
}
107+
108+
references, _ := remote.List(&git.ListOptions{})
109+
// Search through the list of references in that remote for a symbolic reference named HEAD;
110+
// Its target should be the default branch name.
111+
for _, reference := range references {
112+
if reference.Name() == "HEAD" && reference.Type() == plumbing.SymbolicReference {
113+
return reference.Target().String(), nil
114+
}
115+
}
116+
return "", fmt.Errorf("unable to find default branch for git directory %s", r.Directory)
117+
}
118+
95119
// Update updates the plugin's Git repository to the ref if provided, or the
96120
// latest commit on the current branch
97121
func (r Repo) Update(ref string) (string, string, string, error) {
@@ -100,25 +124,17 @@ func (r Repo) Update(ref string) (string, string, string, error) {
100124
return "", "", "", err
101125
}
102126

103-
oldHash, err := repo.ResolveRevision(plumbing.Revision("HEAD"))
127+
oldHash, err := repo.Head()
104128
if err != nil {
105129
return "", "", "", err
106130
}
107131

108-
if ref == "" {
109-
// If no ref is provided checkout latest commit on current branch
110-
head, err := repo.Head()
132+
// If no ref is provided we take the default branch of the remote
133+
if strings.TrimSpace(ref) == "" {
134+
ref, err = r.RemoteDefaultBranch()
111135
if err != nil {
112136
return "", "", "", err
113137
}
114-
115-
if !head.Name().IsBranch() {
116-
return "", "", "", fmt.Errorf("not on a branch, unable to update")
117-
}
118-
119-
// If on a branch checkout the latest version of it from the remote
120-
branch := head.Name()
121-
ref = branch.String()
122138
}
123139

124140
commonOpts := []string{"--git-dir", filepath.Join(r.Directory, ".git"), "--work-tree", r.Directory}
@@ -140,14 +156,15 @@ func (r Repo) Update(ref string) (string, string, string, error) {
140156
var stdErr2 strings.Builder
141157
cmd.Stderr = &stdErr2
142158
err = cmd.Run()
159+
if err != nil {
160+
return "", "", "", errors.New(stdErrToErrMsg(stdErr2.String()))
161+
}
143162

144-
fmt.Printf("stdErr2.String() %#+v\n", stdErr2.String())
145-
repo, err = gitOpen(r.Directory)
146-
newHash, err := repo.ResolveRevision(plumbing.Revision("HEAD"))
163+
newHash, err := repo.Head()
147164
if err != nil {
148-
return ref, oldHash.String(), newHash.String(), errors.New(stdErrToErrMsg(stdErr2.String()))
165+
return ref, oldHash.String(), newHash.Hash().String(), fmt.Errorf("unable to resolve plugin new Git HEAD: %w", err)
149166
}
150-
return ref, oldHash.String(), newHash.String(), nil
167+
return ref, oldHash.String(), newHash.Hash().String(), nil
151168
}
152169

153170
func stdErrToErrMsg(stdErr string) string {

Diff for: internal/repotest/repotest.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func getModuleRoot() (string, error) {
118118
func createGitRepo(location string) (string, error) {
119119
// Definitely some opportunities to refactor here. This code might be
120120
// simplified by switching to the Go git library
121-
err := runCmd("git", "-C", location, "init", "-q")
121+
err := runCmd("git", "-C", location, "init", "-q", "-b", "master")
122122
if err != nil {
123123
return location, err
124124
}

Diff for: internal/versions/versions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ const (
2424
systemVersion = "system"
2525
latestVersion = "latest"
2626
uninstallableVersionMsg = "uninstallable version: %s"
27-
latestFilterRegex = "(?i)(^Available versions:|-src|-dev|-latest|-stm|[-\\.]rc|-milestone|-alpha|-beta|[-\\.]pre|-next|(a|b|c)[0-9]+|snapshot|master)"
27+
latestFilterRegex = "(?i)(^Available versions:|-src|-dev|-latest|-stm|[-\\.]rc|-milestone|-alpha|-beta|[-\\.]pre|-next|(a|b|c)[0-9]+|snapshot|master|main)"
2828
noLatestVersionErrMsg = "no latest version found"
2929
)
3030

0 commit comments

Comments
 (0)