Skip to content

Commit 1086deb

Browse files
0ghnyStratus3D
authored andcommitted
refactor: native-git-client fix tests and some minor changes (#2013)
1 parent 2b9ffd9 commit 1086deb

File tree

2 files changed

+35
-22
lines changed

2 files changed

+35
-22
lines changed

internal/git/git.go

+34-21
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,29 +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()
122-
checkoutOptions = git.CheckoutOptions{Branch: branch, Keep: true}
123-
} else {
124-
// Checkout ref if provided
125-
checkoutOptions = git.CheckoutOptions{Hash: plumbing.NewHash(ref), Keep: true}
126138
}
127139

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

148-
fmt.Printf("stdErr2.String() %#+v\n", stdErr2.String())
149-
repo, err = gitOpen(r.Directory)
150-
newHash, err := repo.ResolveRevision(plumbing.Revision("HEAD"))
163+
newHash, err := repo.Head()
151164
if err != nil {
152-
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)
153166
}
154-
return ref, oldHash.String(), newHash.String(), nil
167+
return ref, oldHash.String(), newHash.Hash().String(), nil
155168
}
156169

157170
func stdErrToErrMsg(stdErr string) string {

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
}

0 commit comments

Comments
 (0)