From 650944301d0648e4b3766c5b1fab7abb35bb7940 Mon Sep 17 00:00:00 2001 From: Michael Hoang Date: Fri, 19 May 2023 10:22:25 -0400 Subject: [PATCH] use git checkout instead of switch Signed-off-by: Michael Hoang --- pkg/git/git.go | 2 +- pkg/git/git_test.go | 63 ++++++++++++++++++++++++++++++++++++++++++--- pkg/git/mock.go | 7 +++-- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/pkg/git/git.go b/pkg/git/git.go index 94c7680e..07cbcab6 100644 --- a/pkg/git/git.go +++ b/pkg/git/git.go @@ -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 { diff --git a/pkg/git/git_test.go b/pkg/git/git_test.go index 09116fef..83dc3902 100644 --- a/pkg/git/git_test.go +++ b/pkg/git/git_test.go @@ -400,8 +400,6 @@ func Test_IsPublic(t *testing.T) { } func Test_CloneGitRepo(t *testing.T) { - tempInvalidDir := t.TempDir() - invalidGitUrl := GitUrl{ Protocol: "", Host: "", @@ -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 @@ -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) diff --git a/pkg/git/mock.go b/pkg/git/mock.go index 458ab369..b75cbbcf 100644 --- a/pkg/git/mock.go +++ b/pkg/git/mock.go @@ -20,7 +20,6 @@ import ( "net/url" "os" "path/filepath" - "strings" ) type MockGitUrl struct { @@ -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 { @@ -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) }