Skip to content

Commit

Permalink
fix(git): fixed unable to checkout remote branch issue crawlab-team/c…
Browse files Browse the repository at this point in the history
  • Loading branch information
tikazyq committed Sep 2, 2022
1 parent 8f90293 commit e3ff72b
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 13 deletions.
1 change: 1 addition & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ var (
ErrUnableToGetCurrentBranch = errors.New("unable to get current branch")
ErrUnableToCloneWithEmptyRemoteUrl = errors.New("unable to clone with empty remote url")
ErrInvalidHeadRef = errors.New("invalid head ref")
ErrNoMatchedRemoteBranch = errors.New("no matched remote branch")
)
42 changes: 37 additions & 5 deletions git.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,10 @@ func (c *GitClient) GetRemoteRefs(remoteName string) (gitRefs []GitRef, err erro
// refs
refs, err := r.List(&git.ListOptions{Auth: auth})
if err != nil {
return nil, trace.TraceError(err)
if err != transport.ErrEmptyRemoteRepository {
return nil, trace.TraceError(err)
}
return nil, nil
}

// iterate refs
Expand Down Expand Up @@ -902,12 +905,22 @@ func (c *GitClient) createBranch(branch, remote string, ref *plumbing.Reference)
if err := c.r.CreateBranch(&cfg); err != nil {
return err
}

// if ref is nil, set to HEAD
// if ref is nil
if ref == nil {
ref, err = c.r.Head()
// try to set to remote ref of branch first
ref, err = c.getBranchHashRef(branch, remote)

// if no matched remote branch, set to HEAD
if err == ErrNoMatchedRemoteBranch {
ref, err = c.r.Head()
if err != nil {
return trace.TraceError(err)
}
}

// error
if err != nil {
return err
return trace.TraceError(err)
}
}

Expand All @@ -925,6 +938,25 @@ func (c *GitClient) createBranch(branch, remote string, ref *plumbing.Reference)
return nil
}

func (c *GitClient) getBranchHashRef(branch, remote string) (hashRef *plumbing.Reference, err error) {
refs, err := c.GetRemoteRefs(remote)
if err != nil {
return nil, err
}
var branchRef *GitRef
for _, r := range refs {
if r.Name == branch {
branchRef = &r
break
}
}
if branchRef == nil {
return nil, ErrNoMatchedRemoteBranch
}
branchHashRef := plumbing.NewHashReference(plumbing.NewBranchReferenceName(branch), plumbing.NewHash(branchRef.Hash))
return branchHashRef, nil
}

func NewGitClient(opts ...GitOption) (c *GitClient, err error) {
// client
c = &GitClient{
Expand Down
17 changes: 9 additions & 8 deletions test/credential.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package test

type Credential struct {
Username string `json:"username"`
Password string `json:"password"`
TestRepoHttpUrl string `json:"test_repo_http_url"`
SshUsername string `json:"ssh_username"`
SshPassword string `json:"ssh_password"`
TestRepoSshUrl string `json:"test_repo_ssh_url"`
PrivateKey string `json:"private_key"`
PrivateKeyPath string `json:"private_key_path"`
Username string `json:"username"`
Password string `json:"password"`
TestRepoHttpUrl string `json:"test_repo_http_url"`
TestRepoMultiBranchUrl string `json:"test_repo_multi_branch_url"`
SshUsername string `json:"ssh_username"`
SshPassword string `json:"ssh_password"`
TestRepoSshUrl string `json:"test_repo_ssh_url"`
PrivateKey string `json:"private_key"`
PrivateKeyPath string `json:"private_key_path"`
}
57 changes: 57 additions & 0 deletions test/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ func TestGitClient_CommitAllAndCheckoutBranch(t *testing.T) {
branch, err := T.LocalRepo.GetCurrentBranch()
require.Nil(t, err)
require.Equal(t, T.TestBranchName, branch)

// dispose
err = T.LocalRepo.Dispose()
require.Nil(t, err)
}

func TestGitClient_Push(t *testing.T) {
Expand Down Expand Up @@ -251,6 +255,59 @@ func TestGitClient_PullWithHttpAuth(t *testing.T) {
require.Nil(t, err)
}

func TestGitClient_CheckoutRemoteBranchWithHttpAuth(t *testing.T) {
var err error
T.Setup(t)

// get credentials
var cred Credential
data, err := ioutil.ReadFile("credentials.json")
require.Nil(t, err)
err = json.Unmarshal(data, &cred)
require.Nil(t, err)

// create new git client
c, err := vcs.NewGitClient(
vcs.WithPath(T.AuthRepoPath),
vcs.WithRemoteUrl(cred.TestRepoMultiBranchUrl),
vcs.WithAuthType(vcs.GitAuthTypeHTTP),
vcs.WithUsername(cred.Username),
vcs.WithPassword(cred.Password),
)
require.Nil(t, err)

// pull
err = c.Pull(
vcs.WithRemoteNamePull(vcs.GitRemoteNameOrigin),
vcs.WithBranchNamePull(vcs.GitBranchNameMain),
)
require.Nil(t, err)

// validate
_, err = ioutil.ReadFile(path.Join(T.AuthRepoPath, "MAIN"))
require.Nil(t, err)

// checkout remote branch
err = c.CheckoutBranchWithRemote(vcs.GitBranchNameRelease, vcs.GitRemoteNameOrigin, nil)
require.Nil(t, err)

// validate
_, err = ioutil.ReadFile(path.Join(T.AuthRepoPath, "RELEASE"))
require.Nil(t, err)

// checkout remote branch
err = c.CheckoutBranchWithRemote(vcs.GitBranchNameDevelop, vcs.GitRemoteNameOrigin, nil)
require.Nil(t, err)

// validate
_, err = ioutil.ReadFile(path.Join(T.AuthRepoPath, "DEVELOP"))
require.Nil(t, err)

// dispose
err = c.Dispose()
require.Nil(t, err)
}

func TestGitClient_InitWithSshAuth_PrivateKey(t *testing.T) {
var err error
T.Setup(t)
Expand Down

0 comments on commit e3ff72b

Please sign in to comment.