Skip to content

Commit

Permalink
use git checkout instead of switch
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Hoang <mhoang@redhat.com>
  • Loading branch information
mike-hoang committed May 19, 2023
1 parent f041d79 commit 6509443
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ func (g *GitUrl) CloneGitRepo(destDir string) error {
}

if g.Revision != "" {
_, err := execute(destDir, "git", "switch", "--detach", "origin/"+g.Revision)
_, err := execute(destDir, "git", "checkout", g.Revision)
if err != nil {
err = os.RemoveAll(destDir)
if err != nil {
Expand Down
63 changes: 59 additions & 4 deletions pkg/git/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,6 @@ func Test_IsPublic(t *testing.T) {
}

func Test_CloneGitRepo(t *testing.T) {
tempInvalidDir := t.TempDir()

invalidGitUrl := GitUrl{
Protocol: "",
Host: "",
Expand All @@ -419,9 +417,42 @@ func Test_CloneGitRepo(t *testing.T) {
token: "fake-github-token",
}

validGitHubRepoBranch := GitUrl{
Protocol: "https",
Host: "github.com",
Owner: "devfile-resources",
Repo: "python-src-docker",
Revision: "testbranch",
}

validGitHubRepoCommit := GitUrl{
Protocol: "https",
Host: "github.com",
Owner: "devfile-resources",
Repo: "python-src-docker",
Revision: "bb00eeffc638f2657a0c752ef934a9b6dc87e2c1",
}

validGitHubRepoInvalidCommit := GitUrl{
Protocol: "https",
Host: "github.com",
Owner: "devfile-resources",
Repo: "python-src-docker",
Revision: "lkjatbasdlkfja0c752ef93faskj4bowdf1",
}

validGitHubRepoTag := GitUrl{
Protocol: "https",
Host: "github.com",
Owner: "OpenLiberty",
Repo: "devfile-stack",
Revision: "maven-0.7.0",
}

privateRepoBadTokenErr := "failed to clone repo with token*"
publicRepoInvalidUrlErr := "failed to clone repo without a token"
missingDestDirErr := "failed to clone repo, destination directory*"
switchRevisionErr := "failed to switch repo to revision*"

tests := []struct {
name string
Expand All @@ -438,19 +469,43 @@ func Test_CloneGitRepo(t *testing.T) {
{
name: "should fail with invalid git url",
gitUrl: invalidGitUrl,
destDir: tempInvalidDir,
destDir: "",
wantErr: publicRepoInvalidUrlErr,
},
{
name: "should fail to clone invalid private git url with a bad token",
gitUrl: invalidPrivateGitHubRepo,
destDir: tempInvalidDir,
destDir: "",
wantErr: privateRepoBadTokenErr,
},
{
name: "should clone valid git url with branch revision",
gitUrl: validGitHubRepoBranch,
destDir: "",
},
{
name: "should clone valid git url with commit revision",
gitUrl: validGitHubRepoCommit,
destDir: "",
},
{
name: "should clone valid git url with tag revision",
gitUrl: validGitHubRepoTag,
destDir: "",
},
{
name: "should fail to clone valid git url with invalid commit",
gitUrl: validGitHubRepoInvalidCommit,
destDir: "",
wantErr: switchRevisionErr,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.destDir == "" {
tt.destDir = t.TempDir()
}
err := tt.gitUrl.CloneGitRepo(tt.destDir)
if (err != nil) != (tt.wantErr != "") {
t.Errorf("Unxpected error: %t, want: %v", err, tt.wantErr)
Expand Down
7 changes: 3 additions & 4 deletions pkg/git/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"net/url"
"os"
"path/filepath"
"strings"
)

type MockGitUrl struct {
Expand Down Expand Up @@ -70,8 +69,8 @@ var mockExecute = func(baseDir string, cmd CommandType, args ...string) ([]byte,
return []byte(""), nil
}

if len(args) > 0 && args[0] == "switch" {
revision := strings.TrimPrefix(args[2], "origin/")
if len(args) > 0 && args[0] == "checkout" {
revision := args[1]
if revision != "invalid-revision" {
resourceFile, err := os.OpenFile(filepath.Clean(baseDir)+"/resource.file", os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
Expand Down Expand Up @@ -122,7 +121,7 @@ func (m *MockGitUrl) CloneGitRepo(destDir string) error {
}

if m.Revision != "" {
_, err := mockExecute(destDir, "git", "switch", "--detach", "origin/"+m.Revision)
_, err := mockExecute(destDir, "git", "checkout", m.Revision)
if err != nil {
return fmt.Errorf("failed to switch repo to revision. repo dir: %v, revision: %v", destDir, m.Revision)
}
Expand Down

0 comments on commit 6509443

Please sign in to comment.