From 7da223640f8311800fe09d43b65d77ccf8167efb Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Wed, 26 Apr 2023 16:29:24 -0700 Subject: [PATCH 01/27] Add gitbranch and sha into spaces payload --- cli/internal/ci/vendors.go | 22 +++++++++++---- cli/internal/runsummary/run_summary.go | 1 + cli/internal/runsummary/spaces.go | 39 ++++++++++++++++++++++++-- cli/internal/scm/scm.go | 22 +++++++++++++++ 4 files changed, 76 insertions(+), 8 deletions(-) diff --git a/cli/internal/ci/vendors.go b/cli/internal/ci/vendors.go index 13bce77dc1805..f619140bc8ef4 100644 --- a/cli/internal/ci/vendors.go +++ b/cli/internal/ci/vendors.go @@ -15,6 +15,12 @@ type Vendor struct { Env vendorEnvs // EvalEnv is key/value map of environment variables that can be used to quickly determine the vendor EvalEnv map[string]string + + // The name of the environment variable that contains the current git sha + ShaEnvVar string + + // The name of the environment variable that contains the current checked out branch + BranchEnvVar string } // Vendors is a list of common CI/CD vendors (from https://github.com/watson/ci-info/blob/master/vendors.json) @@ -107,9 +113,11 @@ var Vendors = []Vendor{ Env: vendorEnvs{Any: []string{"EAS_BUILD"}}, }, { - Name: "GitHub Actions", - Constant: "GITHUB_ACTIONS", - Env: vendorEnvs{Any: []string{"GITHUB_ACTIONS"}}, + Name: "GitHub Actions", + Constant: "GITHUB_ACTIONS", + Env: vendorEnvs{Any: []string{"GITHUB_ACTIONS"}}, + ShaEnvVar: "GITHUB_SHA", + BranchEnvVar: "GITHUB_REF_NAME", }, { Name: "GitLab CI", @@ -224,9 +232,11 @@ var Vendors = []Vendor{ Env: vendorEnvs{Any: []string{"TRAVIS"}}, }, { - Name: "Vercel", - Constant: "VERCEL", - Env: vendorEnvs{Any: []string{"NOW_BUILDER", "VERCEL"}}, + Name: "Vercel", + Constant: "VERCEL", + Env: vendorEnvs{Any: []string{"NOW_BUILDER", "VERCEL"}}, + ShaEnvVar: "VERCEL_GIT_COMMIT_SHA", + BranchEnvVar: "VERCEL_GIT_COMMIT_REF", }, { Name: "Visual Studio App Center", diff --git a/cli/internal/runsummary/run_summary.go b/cli/internal/runsummary/run_summary.go index a297114bd8495..9ceeefa97735e 100644 --- a/cli/internal/runsummary/run_summary.go +++ b/cli/internal/runsummary/run_summary.go @@ -244,6 +244,7 @@ func (rsm *Meta) record() (string, []error) { response := &spacesRunResponse{} payload := rsm.newSpacesRunCreatePayload() + fmt.Printf("[debug] payload %#v\n", payload) if startPayload, err := json.Marshal(payload); err == nil { if resp, err := rsm.apiClient.JSONPost(createRunEndpoint, startPayload); err != nil { errs = append(errs, fmt.Errorf("POST %s: %w", createRunEndpoint, err)) diff --git a/cli/internal/runsummary/spaces.go b/cli/internal/runsummary/spaces.go index bf19941806a43..7306b28c50b60 100644 --- a/cli/internal/runsummary/spaces.go +++ b/cli/internal/runsummary/spaces.go @@ -2,6 +2,8 @@ package runsummary import ( "github.com/vercel/turbo/cli/internal/ci" + "github.com/vercel/turbo/cli/internal/env" + "github.com/vercel/turbo/cli/internal/scm" ) // spacesRunResponse deserialized the response from POST Run endpoint @@ -19,11 +21,11 @@ type spacesRunPayload struct { Command string `json:"command,omitempty"` // the thing that kicked off the turbo run RepositoryPath string `json:"repositoryPath,omitempty"` // where the command was invoked from Context string `json:"context,omitempty"` // the host on which this Run was executed (e.g. Github Action, Vercel, etc) + GitBranch string `json:"gitBranch"` + GitSha string `json:"gitSha"` // TODO: we need to add these in // originationUser string - // gitBranch string - // gitSha string } // spacesCacheStatus is the same as TaskCacheSummary so we can convert @@ -57,6 +59,10 @@ func (rsm *Meta) newSpacesRunCreatePayload() *spacesRunPayload { if name := ci.Constant(); name != "" { context = name } + + // Get a list of env vars + sha, branch := getGitMetadata() + return &spacesRunPayload{ StartTime: startTime, Status: "running", @@ -64,6 +70,8 @@ func (rsm *Meta) newSpacesRunCreatePayload() *spacesRunPayload { RepositoryPath: rsm.repoPath.ToString(), Type: "TURBO", Context: context, + GitBranch: branch, + GitSha: sha, } } @@ -94,3 +102,30 @@ func newSpacesTaskPayload(taskSummary *TaskSummary) *spacesTask { Logs: string(taskSummary.GetLogs()), } } + +func getGitMetadata() (string, string) { + allEnvVars := env.GetEnvMap() + + var sha string + var branch string + + // If we're in CI, try to get the values we need from environment variables + if ci.IsCi() { + vendor := ci.Info() + branchVarName := vendor.BranchEnvVar + shaVarName := vendor.ShaEnvVar + vars := env.FromKeys(allEnvVars, []string{shaVarName, branchVarName}) + sha = vars[shaVarName] + branch = vars[branchVarName] + } + + // Otherwise fallback to using `git` + if branch == "" { + branch = scm.GetCurrentBranch() + } + if sha == "" { + sha = scm.GetCurrentSha() + } + + return sha, branch +} diff --git a/cli/internal/scm/scm.go b/cli/internal/scm/scm.go index e7f17c8b7374a..2e9a677f28ece 100644 --- a/cli/internal/scm/scm.go +++ b/cli/internal/scm/scm.go @@ -7,6 +7,8 @@ package scm import ( + "os/exec" + "github.com/pkg/errors" "github.com/vercel/turbo/cli/internal/turbopath" @@ -51,3 +53,23 @@ func FromInRepo(repoRoot turbopath.AbsoluteSystemPath) (SCM, error) { } return newFallback(dotGitDir.Dir()) } + +// GetCurrentBranch returns the current branch +func GetCurrentBranch() string { + command := []string{"rev-parse", "--abbrev-ref", "HEAD"} + out, err := exec.Command("git", command...).CombinedOutput() + if err != nil { + return "" + } + return string(out) +} + +// GetCurrentSha returns the current SHA +func GetCurrentSha() string { + command := []string{"rev-parse", "HEAD"} + out, err := exec.Command("git", command...).CombinedOutput() + if err != nil { + return "" + } + return string(out) +} From 305be404d97003af4f045c7fec35cf03e0c0bbef Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Thu, 27 Apr 2023 15:49:41 -0700 Subject: [PATCH 02/27] rm debug --- cli/internal/runsummary/run_summary.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/internal/runsummary/run_summary.go b/cli/internal/runsummary/run_summary.go index 9ceeefa97735e..a297114bd8495 100644 --- a/cli/internal/runsummary/run_summary.go +++ b/cli/internal/runsummary/run_summary.go @@ -244,7 +244,6 @@ func (rsm *Meta) record() (string, []error) { response := &spacesRunResponse{} payload := rsm.newSpacesRunCreatePayload() - fmt.Printf("[debug] payload %#v\n", payload) if startPayload, err := json.Marshal(payload); err == nil { if resp, err := rsm.apiClient.JSONPost(createRunEndpoint, startPayload); err != nil { errs = append(errs, fmt.Errorf("POST %s: %w", createRunEndpoint, err)) From 084a1e79085797bcc30e752267c67cd885146d0b Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Thu, 27 Apr 2023 16:07:08 -0700 Subject: [PATCH 03/27] comments --- cli/internal/runsummary/spaces.go | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cli/internal/runsummary/spaces.go b/cli/internal/runsummary/spaces.go index 7306b28c50b60..48381cfb430be 100644 --- a/cli/internal/runsummary/spaces.go +++ b/cli/internal/runsummary/spaces.go @@ -61,7 +61,7 @@ func (rsm *Meta) newSpacesRunCreatePayload() *spacesRunPayload { } // Get a list of env vars - sha, branch := getGitMetadata() + sha, branch := getStateOfRepo() return &spacesRunPayload{ StartTime: startTime, @@ -103,7 +103,10 @@ func newSpacesTaskPayload(taskSummary *TaskSummary) *spacesTask { } } -func getGitMetadata() (string, string) { +// getStateOfRepo returns the sha and branch when in a git repo +// Otherwise it should return empty strings right now. +// We my add handling of other scms and non-git tracking in the future. +func getStateOfRepo() (string, string) { allEnvVars := env.GetEnvMap() var sha string @@ -114,6 +117,7 @@ func getGitMetadata() (string, string) { vendor := ci.Info() branchVarName := vendor.BranchEnvVar shaVarName := vendor.ShaEnvVar + // Get the values of the vars vars := env.FromKeys(allEnvVars, []string{shaVarName, branchVarName}) sha = vars[shaVarName] branch = vars[branchVarName] From 8cfeb180e0f9dba9e1c5f369411359b113247592 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Fri, 28 Apr 2023 15:19:12 -0700 Subject: [PATCH 04/27] return struct instead of tuple --- cli/internal/runsummary/spaces.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/cli/internal/runsummary/spaces.go b/cli/internal/runsummary/spaces.go index 48381cfb430be..9dae5edaf0972 100644 --- a/cli/internal/runsummary/spaces.go +++ b/cli/internal/runsummary/spaces.go @@ -61,7 +61,7 @@ func (rsm *Meta) newSpacesRunCreatePayload() *spacesRunPayload { } // Get a list of env vars - sha, branch := getStateOfRepo() + gitstate := getStateOfRepo() return &spacesRunPayload{ StartTime: startTime, @@ -70,8 +70,8 @@ func (rsm *Meta) newSpacesRunCreatePayload() *spacesRunPayload { RepositoryPath: rsm.repoPath.ToString(), Type: "TURBO", Context: context, - GitBranch: branch, - GitSha: sha, + GitBranch: gitstate.Branch, + GitSha: gitstate.Sha, } } @@ -103,14 +103,18 @@ func newSpacesTaskPayload(taskSummary *TaskSummary) *spacesTask { } } +type gitState struct { + Sha string `json:"gitSha"` + Branch string `json:"branch"` +} + // getStateOfRepo returns the sha and branch when in a git repo // Otherwise it should return empty strings right now. // We my add handling of other scms and non-git tracking in the future. -func getStateOfRepo() (string, string) { +func getStateOfRepo() *gitState { allEnvVars := env.GetEnvMap() - var sha string - var branch string + gitstate := &gitState{} // If we're in CI, try to get the values we need from environment variables if ci.IsCi() { @@ -119,17 +123,17 @@ func getStateOfRepo() (string, string) { shaVarName := vendor.ShaEnvVar // Get the values of the vars vars := env.FromKeys(allEnvVars, []string{shaVarName, branchVarName}) - sha = vars[shaVarName] - branch = vars[branchVarName] + gitstate.Sha = vars[shaVarName] + gitstate.Branch = vars[branchVarName] } // Otherwise fallback to using `git` - if branch == "" { - branch = scm.GetCurrentBranch() + if gitstate.Branch == "" { + gitstate.Branch = scm.GetCurrentBranch() } - if sha == "" { - sha = scm.GetCurrentSha() + if gitstate.Sha == "" { + gitstate.Sha = scm.GetCurrentSha() } - return sha, branch + return gitstate } From 213b9f5cc65135aa3a95a0c91b558dba703c7d61 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Fri, 28 Apr 2023 15:21:45 -0700 Subject: [PATCH 05/27] Use branch --show-current to get branch name instead --- cli/internal/scm/scm.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/internal/scm/scm.go b/cli/internal/scm/scm.go index 2e9a677f28ece..da034bbea7299 100644 --- a/cli/internal/scm/scm.go +++ b/cli/internal/scm/scm.go @@ -56,7 +56,7 @@ func FromInRepo(repoRoot turbopath.AbsoluteSystemPath) (SCM, error) { // GetCurrentBranch returns the current branch func GetCurrentBranch() string { - command := []string{"rev-parse", "--abbrev-ref", "HEAD"} + command := []string{"branch", "--show-current"} out, err := exec.Command("git", command...).CombinedOutput() if err != nil { return "" From 0d57d8c5b2e7460cb65b116d231537bd49e295fa Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Fri, 28 Apr 2023 15:22:24 -0700 Subject: [PATCH 06/27] ONly use stdout, ignore stderr --- cli/internal/scm/scm.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/internal/scm/scm.go b/cli/internal/scm/scm.go index da034bbea7299..ff717162da3e9 100644 --- a/cli/internal/scm/scm.go +++ b/cli/internal/scm/scm.go @@ -57,7 +57,7 @@ func FromInRepo(repoRoot turbopath.AbsoluteSystemPath) (SCM, error) { // GetCurrentBranch returns the current branch func GetCurrentBranch() string { command := []string{"branch", "--show-current"} - out, err := exec.Command("git", command...).CombinedOutput() + out, err := exec.Command("git", command...).Output() if err != nil { return "" } @@ -67,7 +67,7 @@ func GetCurrentBranch() string { // GetCurrentSha returns the current SHA func GetCurrentSha() string { command := []string{"rev-parse", "HEAD"} - out, err := exec.Command("git", command...).CombinedOutput() + out, err := exec.Command("git", command...).Output() if err != nil { return "" } From 8d1db8dfbeedfd1167e184da80a99e9cdb763469 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Fri, 28 Apr 2023 15:26:55 -0700 Subject: [PATCH 07/27] git state --- cli/internal/runsummary/spaces.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/internal/runsummary/spaces.go b/cli/internal/runsummary/spaces.go index 9dae5edaf0972..9ae1f2c913145 100644 --- a/cli/internal/runsummary/spaces.go +++ b/cli/internal/runsummary/spaces.go @@ -61,7 +61,7 @@ func (rsm *Meta) newSpacesRunCreatePayload() *spacesRunPayload { } // Get a list of env vars - gitstate := getStateOfRepo() + gitstate := getGitState() return &spacesRunPayload{ StartTime: startTime, @@ -108,10 +108,10 @@ type gitState struct { Branch string `json:"branch"` } -// getStateOfRepo returns the sha and branch when in a git repo +// getGitState returns the sha and branch when in a git repo // Otherwise it should return empty strings right now. // We my add handling of other scms and non-git tracking in the future. -func getStateOfRepo() *gitState { +func getGitState() *gitState { allEnvVars := env.GetEnvMap() gitstate := &gitState{} From 4de2dbf20b0f2e53c506c99784bfec0ed5d525c2 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Fri, 28 Apr 2023 15:28:51 -0700 Subject: [PATCH 08/27] look up env vars more simply --- cli/internal/runsummary/spaces.go | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/cli/internal/runsummary/spaces.go b/cli/internal/runsummary/spaces.go index 9ae1f2c913145..c600645330f5a 100644 --- a/cli/internal/runsummary/spaces.go +++ b/cli/internal/runsummary/spaces.go @@ -119,12 +119,8 @@ func getGitState() *gitState { // If we're in CI, try to get the values we need from environment variables if ci.IsCi() { vendor := ci.Info() - branchVarName := vendor.BranchEnvVar - shaVarName := vendor.ShaEnvVar - // Get the values of the vars - vars := env.FromKeys(allEnvVars, []string{shaVarName, branchVarName}) - gitstate.Sha = vars[shaVarName] - gitstate.Branch = vars[branchVarName] + gitstate.Sha = allEnvVars[vendor.ShaEnvVar] + gitstate.Branch = allEnvVars[vendor.BranchEnvVar] } // Otherwise fallback to using `git` From c8553ff0fe6166077219414da1aeaebe8cea02c9 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Fri, 28 Apr 2023 15:30:23 -0700 Subject: [PATCH 09/27] rm comment --- cli/internal/runsummary/spaces.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cli/internal/runsummary/spaces.go b/cli/internal/runsummary/spaces.go index c600645330f5a..2bb5186e17aed 100644 --- a/cli/internal/runsummary/spaces.go +++ b/cli/internal/runsummary/spaces.go @@ -60,7 +60,6 @@ func (rsm *Meta) newSpacesRunCreatePayload() *spacesRunPayload { context = name } - // Get a list of env vars gitstate := getGitState() return &spacesRunPayload{ From ef0621be97754b6eca70056f89ab130a6eae1957 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Mon, 1 May 2023 23:31:59 -0700 Subject: [PATCH 10/27] Move gitState to run summary instead of just spaces --- cli/internal/runsummary/format_json.go | 1 + cli/internal/runsummary/repo_summary.go | 38 ++++++++++++++++++++++++ cli/internal/runsummary/run_summary.go | 2 ++ cli/internal/runsummary/spaces.go | 39 ++----------------------- 4 files changed, 43 insertions(+), 37 deletions(-) create mode 100644 cli/internal/runsummary/repo_summary.go diff --git a/cli/internal/runsummary/format_json.go b/cli/internal/runsummary/format_json.go index 76a0a40f7fdcb..8ef26b2328880 100644 --- a/cli/internal/runsummary/format_json.go +++ b/cli/internal/runsummary/format_json.go @@ -63,4 +63,5 @@ type nonMonorepoRunSummary struct { EnvMode util.EnvMode `json:"envMode"` ExecutionSummary *executionSummary `json:"execution,omitempty"` Tasks []*TaskSummary `json:"tasks"` + GitState *gitState `json:"git"` } diff --git a/cli/internal/runsummary/repo_summary.go b/cli/internal/runsummary/repo_summary.go new file mode 100644 index 0000000000000..eed39a34e6579 --- /dev/null +++ b/cli/internal/runsummary/repo_summary.go @@ -0,0 +1,38 @@ +package runsummary + +import ( + "github.com/vercel/turbo/cli/internal/ci" + "github.com/vercel/turbo/cli/internal/env" + "github.com/vercel/turbo/cli/internal/scm" +) + +type gitState struct { + Sha string `json:"gitSha"` + Branch string `json:"branch"` +} + +// getGitState returns the sha and branch when in a git repo +// Otherwise it should return empty strings right now. +// We my add handling of other scms and non-git tracking in the future. +func getGitState() *gitState { + allEnvVars := env.GetEnvMap() + + gitstate := &gitState{} + + // If we're in CI, try to get the values we need from environment variables + if ci.IsCi() { + vendor := ci.Info() + gitstate.Sha = allEnvVars[vendor.ShaEnvVar] + gitstate.Branch = allEnvVars[vendor.BranchEnvVar] + } + + // Otherwise fallback to using `git` + if gitstate.Branch == "" { + gitstate.Branch = scm.GetCurrentBranch() + } + if gitstate.Sha == "" { + gitstate.Sha = scm.GetCurrentSha() + } + + return gitstate +} diff --git a/cli/internal/runsummary/run_summary.go b/cli/internal/runsummary/run_summary.go index a297114bd8495..6b2ae7f2bc26e 100644 --- a/cli/internal/runsummary/run_summary.go +++ b/cli/internal/runsummary/run_summary.go @@ -64,6 +64,7 @@ type RunSummary struct { EnvMode util.EnvMode `json:"envMode"` ExecutionSummary *executionSummary `json:"execution,omitempty"` Tasks []*TaskSummary `json:"tasks"` + GitState *gitState `json:"git"` } // NewRunSummary returns a RunSummary instance @@ -105,6 +106,7 @@ func NewRunSummary( EnvMode: globalEnvMode, Tasks: []*TaskSummary{}, GlobalHashSummary: globalHashSummary, + GitState: getGitState(), }, ui: ui, runType: runType, diff --git a/cli/internal/runsummary/spaces.go b/cli/internal/runsummary/spaces.go index 2bb5186e17aed..5d45f4b56f8c5 100644 --- a/cli/internal/runsummary/spaces.go +++ b/cli/internal/runsummary/spaces.go @@ -2,8 +2,6 @@ package runsummary import ( "github.com/vercel/turbo/cli/internal/ci" - "github.com/vercel/turbo/cli/internal/env" - "github.com/vercel/turbo/cli/internal/scm" ) // spacesRunResponse deserialized the response from POST Run endpoint @@ -60,8 +58,6 @@ func (rsm *Meta) newSpacesRunCreatePayload() *spacesRunPayload { context = name } - gitstate := getGitState() - return &spacesRunPayload{ StartTime: startTime, Status: "running", @@ -69,8 +65,8 @@ func (rsm *Meta) newSpacesRunCreatePayload() *spacesRunPayload { RepositoryPath: rsm.repoPath.ToString(), Type: "TURBO", Context: context, - GitBranch: gitstate.Branch, - GitSha: gitstate.Sha, + GitBranch: rsm.RunSummary.GitState.Branch, + GitSha: rsm.RunSummary.GitState.Sha, } } @@ -101,34 +97,3 @@ func newSpacesTaskPayload(taskSummary *TaskSummary) *spacesTask { Logs: string(taskSummary.GetLogs()), } } - -type gitState struct { - Sha string `json:"gitSha"` - Branch string `json:"branch"` -} - -// getGitState returns the sha and branch when in a git repo -// Otherwise it should return empty strings right now. -// We my add handling of other scms and non-git tracking in the future. -func getGitState() *gitState { - allEnvVars := env.GetEnvMap() - - gitstate := &gitState{} - - // If we're in CI, try to get the values we need from environment variables - if ci.IsCi() { - vendor := ci.Info() - gitstate.Sha = allEnvVars[vendor.ShaEnvVar] - gitstate.Branch = allEnvVars[vendor.BranchEnvVar] - } - - // Otherwise fallback to using `git` - if gitstate.Branch == "" { - gitstate.Branch = scm.GetCurrentBranch() - } - if gitstate.Sha == "" { - gitstate.Sha = scm.GetCurrentSha() - } - - return gitstate -} From da9917afd4047e85d4867d7485467544fac917b5 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Tue, 2 May 2023 00:02:21 -0700 Subject: [PATCH 11/27] add tests, fix key, remove newline char --- cli/internal/runsummary/repo_summary.go | 2 +- cli/internal/scm/scm.go | 5 +++-- .../integration/tests/dry_json/monorepo.t | 1 + .../tests/dry_json/single_package.t | 6 +++++- .../tests/dry_json/single_package_no_config.t | 6 +++++- .../tests/dry_json/single_package_with_deps.t | 6 +++++- .../integration/tests/run_summary/monorepo.t | 19 +++++++++++++++++++ .../tests/run_summary/single-package.t | 18 ++++++++++++++++++ 8 files changed, 57 insertions(+), 6 deletions(-) diff --git a/cli/internal/runsummary/repo_summary.go b/cli/internal/runsummary/repo_summary.go index eed39a34e6579..4db54208c9b27 100644 --- a/cli/internal/runsummary/repo_summary.go +++ b/cli/internal/runsummary/repo_summary.go @@ -7,7 +7,7 @@ import ( ) type gitState struct { - Sha string `json:"gitSha"` + Sha string `json:"sha"` Branch string `json:"branch"` } diff --git a/cli/internal/scm/scm.go b/cli/internal/scm/scm.go index ff717162da3e9..90b6504f05e2f 100644 --- a/cli/internal/scm/scm.go +++ b/cli/internal/scm/scm.go @@ -8,6 +8,7 @@ package scm import ( "os/exec" + "strings" "github.com/pkg/errors" @@ -61,7 +62,7 @@ func GetCurrentBranch() string { if err != nil { return "" } - return string(out) + return strings.TrimRight(string(out), "\n") } // GetCurrentSha returns the current SHA @@ -71,5 +72,5 @@ func GetCurrentSha() string { if err != nil { return "" } - return string(out) + return strings.TrimRight(string(out), "\n") } diff --git a/turborepo-tests/integration/tests/dry_json/monorepo.t b/turborepo-tests/integration/tests/dry_json/monorepo.t index e7c6c1f53882a..1bf9fb2283cfc 100644 --- a/turborepo-tests/integration/tests/dry_json/monorepo.t +++ b/turborepo-tests/integration/tests/dry_json/monorepo.t @@ -73,6 +73,7 @@ Setup $ cat tmpjson.log | jq 'keys' [ "envMode", + "git", "globalCacheInputs", "id", "packages", diff --git a/turborepo-tests/integration/tests/dry_json/single_package.t b/turborepo-tests/integration/tests/dry_json/single_package.t index 3b746da67a04e..2a4d02f59f3d8 100644 --- a/turborepo-tests/integration/tests/dry_json/single_package.t +++ b/turborepo-tests/integration/tests/dry_json/single_package.t @@ -82,5 +82,9 @@ Setup "globalPassthrough": null } } - ] + ], + "git": { + "sha": "[a-z0-9]+", (re) + "branch": "[a-zA-Z0-9]+" (re) + } } diff --git a/turborepo-tests/integration/tests/dry_json/single_package_no_config.t b/turborepo-tests/integration/tests/dry_json/single_package_no_config.t index a3dfe79985c4b..a520ae6eab0b7 100644 --- a/turborepo-tests/integration/tests/dry_json/single_package_no_config.t +++ b/turborepo-tests/integration/tests/dry_json/single_package_no_config.t @@ -76,5 +76,9 @@ Setup "globalPassthrough": null } } - ] + ], + "git": { + "sha": "[a-z0-9]+", (re) + "branch": "[a-zA-Z0-9]+" (re) + } } diff --git a/turborepo-tests/integration/tests/dry_json/single_package_with_deps.t b/turborepo-tests/integration/tests/dry_json/single_package_with_deps.t index 0664cd02699d3..c3c23c8d3413b 100644 --- a/turborepo-tests/integration/tests/dry_json/single_package_with_deps.t +++ b/turborepo-tests/integration/tests/dry_json/single_package_with_deps.t @@ -143,5 +143,9 @@ Setup "globalPassthrough": null } } - ] + ], + "git": { + "sha": "[a-z0-9]+", (re) + "branch": "[a-zA-Z0-9]+" (re) + } } diff --git a/turborepo-tests/integration/tests/run_summary/monorepo.t b/turborepo-tests/integration/tests/run_summary/monorepo.t index 99967d4656243..9bb9d52a25763 100644 --- a/turborepo-tests/integration/tests/run_summary/monorepo.t +++ b/turborepo-tests/integration/tests/run_summary/monorepo.t @@ -29,7 +29,26 @@ Setup $ FIRST=$(/bin/ls .turbo/runs/*.json | head -n1) $ SECOND=$(/bin/ls .turbo/runs/*.json | tail -n1) + $ cat $FIRST | jq 'keys' + [ + "envMode", + "execution", + "git", + "globalCacheInputs", + "id", + "packages", + "tasks", + "turboVersion", + "version" + ] + # some top level run summary validation + $ cat $FIRST | jq '.git' + { + "sha": "[a-z0-9]+", (re) + "branch": "[a-zA-Z0-9]+" (re) + } + $ cat $FIRST | jq '.tasks | length' 2 $ cat $FIRST | jq '.version' diff --git a/turborepo-tests/integration/tests/run_summary/single-package.t b/turborepo-tests/integration/tests/run_summary/single-package.t index fdf3c4206b2dc..b4a077cd24ae6 100644 --- a/turborepo-tests/integration/tests/run_summary/single-package.t +++ b/turborepo-tests/integration/tests/run_summary/single-package.t @@ -29,6 +29,24 @@ Check "success" ] + $ cat $SUMMARY | jq 'keys' + [ + "envMode", + "execution", + "git", + "globalCacheInputs", + "id", + "tasks", + "turboVersion", + "version" + ] + + $ cat $SUMMARY | jq '.git' + { + "sha": "[a-z0-9]+", (re) + "branch": "[a-zA-Z0-9]+" (re) + } + $ cat $SUMMARY | jq '.execution.exitCode' 0 $ cat $SUMMARY | jq '.execution.attempted' From 1e9d30df9922128803a9685b1b092e9973386a68 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Tue, 2 May 2023 00:22:18 -0700 Subject: [PATCH 12/27] Set CWD when getting git info --- cli/internal/runsummary/repo_summary.go | 8 +++++--- cli/internal/runsummary/run_summary.go | 2 +- cli/internal/scm/scm.go | 16 ++++++++++------ 3 files changed, 16 insertions(+), 10 deletions(-) diff --git a/cli/internal/runsummary/repo_summary.go b/cli/internal/runsummary/repo_summary.go index 4db54208c9b27..9cbc84c4ca787 100644 --- a/cli/internal/runsummary/repo_summary.go +++ b/cli/internal/runsummary/repo_summary.go @@ -4,6 +4,7 @@ import ( "github.com/vercel/turbo/cli/internal/ci" "github.com/vercel/turbo/cli/internal/env" "github.com/vercel/turbo/cli/internal/scm" + "github.com/vercel/turbo/cli/internal/turbopath" ) type gitState struct { @@ -14,7 +15,7 @@ type gitState struct { // getGitState returns the sha and branch when in a git repo // Otherwise it should return empty strings right now. // We my add handling of other scms and non-git tracking in the future. -func getGitState() *gitState { +func getGitState(dir turbopath.AbsoluteSystemPath) *gitState { allEnvVars := env.GetEnvMap() gitstate := &gitState{} @@ -26,12 +27,13 @@ func getGitState() *gitState { gitstate.Branch = allEnvVars[vendor.BranchEnvVar] } + dirString = dir.ToString() // Otherwise fallback to using `git` if gitstate.Branch == "" { - gitstate.Branch = scm.GetCurrentBranch() + gitstate.Branch = scm.GetCurrentBranch(dirString) } if gitstate.Sha == "" { - gitstate.Sha = scm.GetCurrentSha() + gitstate.Sha = scm.GetCurrentSha(dirString) } return gitstate diff --git a/cli/internal/runsummary/run_summary.go b/cli/internal/runsummary/run_summary.go index 6b2ae7f2bc26e..d6e1b0b5cea66 100644 --- a/cli/internal/runsummary/run_summary.go +++ b/cli/internal/runsummary/run_summary.go @@ -106,7 +106,7 @@ func NewRunSummary( EnvMode: globalEnvMode, Tasks: []*TaskSummary{}, GlobalHashSummary: globalHashSummary, - GitState: getGitState(), + GitState: getGitState(repoRoot), }, ui: ui, runType: runType, diff --git a/cli/internal/scm/scm.go b/cli/internal/scm/scm.go index 90b6504f05e2f..3bd194f97fb5c 100644 --- a/cli/internal/scm/scm.go +++ b/cli/internal/scm/scm.go @@ -56,9 +56,11 @@ func FromInRepo(repoRoot turbopath.AbsoluteSystemPath) (SCM, error) { } // GetCurrentBranch returns the current branch -func GetCurrentBranch() string { - command := []string{"branch", "--show-current"} - out, err := exec.Command("git", command...).Output() +func GetCurrentBranch(dir string) string { + cmd := exec.Command("git", []string{"branch", "--show-current"}...) + cmd.Dir = dir + + out, err := cmd.Output() if err != nil { return "" } @@ -66,9 +68,11 @@ func GetCurrentBranch() string { } // GetCurrentSha returns the current SHA -func GetCurrentSha() string { - command := []string{"rev-parse", "HEAD"} - out, err := exec.Command("git", command...).Output() +func GetCurrentSha(dir string) string { + cmd := exec.Command("git", []string{"rev-parse", "HEAD"}...) + cmd.Dir = dir + + out, err := cmd.Output() if err != nil { return "" } From dd4a603ecf729a1eafeaeb9e5ea8b1bd0d1a87bc Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Tue, 2 May 2023 00:23:34 -0700 Subject: [PATCH 13/27] compile err --- cli/internal/runsummary/repo_summary.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/internal/runsummary/repo_summary.go b/cli/internal/runsummary/repo_summary.go index 9cbc84c4ca787..0f23f3db41a3c 100644 --- a/cli/internal/runsummary/repo_summary.go +++ b/cli/internal/runsummary/repo_summary.go @@ -27,7 +27,7 @@ func getGitState(dir turbopath.AbsoluteSystemPath) *gitState { gitstate.Branch = allEnvVars[vendor.BranchEnvVar] } - dirString = dir.ToString() + dirString := dir.ToString() // Otherwise fallback to using `git` if gitstate.Branch == "" { gitstate.Branch = scm.GetCurrentBranch(dirString) From c1e3d6886c42b8fcad05bc87b5da3bc7c20bcef7 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Tue, 2 May 2023 00:49:30 -0700 Subject: [PATCH 14/27] branch names can be anything --- turborepo-tests/integration/tests/dry_json/single_package.t | 2 +- .../integration/tests/dry_json/single_package_no_config.t | 2 +- .../integration/tests/dry_json/single_package_with_deps.t | 2 +- turborepo-tests/integration/tests/run_summary/monorepo.t | 2 +- turborepo-tests/integration/tests/run_summary/single-package.t | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/turborepo-tests/integration/tests/dry_json/single_package.t b/turborepo-tests/integration/tests/dry_json/single_package.t index 2a4d02f59f3d8..2a3eeaee14663 100644 --- a/turborepo-tests/integration/tests/dry_json/single_package.t +++ b/turborepo-tests/integration/tests/dry_json/single_package.t @@ -85,6 +85,6 @@ Setup ], "git": { "sha": "[a-z0-9]+", (re) - "branch": "[a-zA-Z0-9]+" (re) + "branch": ".+" (re) } } diff --git a/turborepo-tests/integration/tests/dry_json/single_package_no_config.t b/turborepo-tests/integration/tests/dry_json/single_package_no_config.t index a520ae6eab0b7..7c6ceffb71d24 100644 --- a/turborepo-tests/integration/tests/dry_json/single_package_no_config.t +++ b/turborepo-tests/integration/tests/dry_json/single_package_no_config.t @@ -79,6 +79,6 @@ Setup ], "git": { "sha": "[a-z0-9]+", (re) - "branch": "[a-zA-Z0-9]+" (re) + "branch": ".+" (re) } } diff --git a/turborepo-tests/integration/tests/dry_json/single_package_with_deps.t b/turborepo-tests/integration/tests/dry_json/single_package_with_deps.t index c3c23c8d3413b..8f90da427cda1 100644 --- a/turborepo-tests/integration/tests/dry_json/single_package_with_deps.t +++ b/turborepo-tests/integration/tests/dry_json/single_package_with_deps.t @@ -146,6 +146,6 @@ Setup ], "git": { "sha": "[a-z0-9]+", (re) - "branch": "[a-zA-Z0-9]+" (re) + "branch": ".+" (re) } } diff --git a/turborepo-tests/integration/tests/run_summary/monorepo.t b/turborepo-tests/integration/tests/run_summary/monorepo.t index 9bb9d52a25763..8f3e763e89625 100644 --- a/turborepo-tests/integration/tests/run_summary/monorepo.t +++ b/turborepo-tests/integration/tests/run_summary/monorepo.t @@ -46,7 +46,7 @@ Setup $ cat $FIRST | jq '.git' { "sha": "[a-z0-9]+", (re) - "branch": "[a-zA-Z0-9]+" (re) + "branch": ".+" (re) } $ cat $FIRST | jq '.tasks | length' diff --git a/turborepo-tests/integration/tests/run_summary/single-package.t b/turborepo-tests/integration/tests/run_summary/single-package.t index b4a077cd24ae6..18e474ff5c9f6 100644 --- a/turborepo-tests/integration/tests/run_summary/single-package.t +++ b/turborepo-tests/integration/tests/run_summary/single-package.t @@ -44,7 +44,7 @@ Check $ cat $SUMMARY | jq '.git' { "sha": "[a-z0-9]+", (re) - "branch": "[a-zA-Z0-9]+" (re) + "branch": ".+" (re) } $ cat $SUMMARY | jq '.execution.exitCode' From bffe3ce5457b90089797b565882824c6ab2a2e13 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Tue, 2 May 2023 09:07:01 -0700 Subject: [PATCH 15/27] stringify at the end --- cli/internal/runsummary/repo_summary.go | 5 ++--- cli/internal/scm/scm.go | 8 ++++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/cli/internal/runsummary/repo_summary.go b/cli/internal/runsummary/repo_summary.go index 0f23f3db41a3c..143c2e0f183f2 100644 --- a/cli/internal/runsummary/repo_summary.go +++ b/cli/internal/runsummary/repo_summary.go @@ -27,13 +27,12 @@ func getGitState(dir turbopath.AbsoluteSystemPath) *gitState { gitstate.Branch = allEnvVars[vendor.BranchEnvVar] } - dirString := dir.ToString() // Otherwise fallback to using `git` if gitstate.Branch == "" { - gitstate.Branch = scm.GetCurrentBranch(dirString) + gitstate.Branch = scm.GetCurrentBranch(dir) } if gitstate.Sha == "" { - gitstate.Sha = scm.GetCurrentSha(dirString) + gitstate.Sha = scm.GetCurrentSha(dir) } return gitstate diff --git a/cli/internal/scm/scm.go b/cli/internal/scm/scm.go index 3bd194f97fb5c..825401b79faf3 100644 --- a/cli/internal/scm/scm.go +++ b/cli/internal/scm/scm.go @@ -56,9 +56,9 @@ func FromInRepo(repoRoot turbopath.AbsoluteSystemPath) (SCM, error) { } // GetCurrentBranch returns the current branch -func GetCurrentBranch(dir string) string { +func GetCurrentBranch(dir turbopath.AbsoluteSystemPath) string { cmd := exec.Command("git", []string{"branch", "--show-current"}...) - cmd.Dir = dir + cmd.Dir = dir.ToString() out, err := cmd.Output() if err != nil { @@ -68,9 +68,9 @@ func GetCurrentBranch(dir string) string { } // GetCurrentSha returns the current SHA -func GetCurrentSha(dir string) string { +func GetCurrentSha(dir turbopath.AbsoluteSystemPath) string { cmd := exec.Command("git", []string{"rev-parse", "HEAD"}...) - cmd.Dir = dir + cmd.Dir = dir.ToString() out, err := cmd.Output() if err != nil { From 21ff8595dc1d1a5cb6bd94af05b80e11a0d57abd Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Tue, 2 May 2023 09:11:52 -0700 Subject: [PATCH 16/27] Use scm key/struct naming for run summary, keep git fields for spaces --- cli/internal/runsummary/format_json.go | 2 +- cli/internal/runsummary/run_summary.go | 4 ++-- .../{repo_summary.go => scm_summary.go} | 24 ++++++++++--------- cli/internal/runsummary/spaces.go | 4 ++-- 4 files changed, 18 insertions(+), 16 deletions(-) rename cli/internal/runsummary/{repo_summary.go => scm_summary.go} (57%) diff --git a/cli/internal/runsummary/format_json.go b/cli/internal/runsummary/format_json.go index 8ef26b2328880..b4e6da9e2dd55 100644 --- a/cli/internal/runsummary/format_json.go +++ b/cli/internal/runsummary/format_json.go @@ -63,5 +63,5 @@ type nonMonorepoRunSummary struct { EnvMode util.EnvMode `json:"envMode"` ExecutionSummary *executionSummary `json:"execution,omitempty"` Tasks []*TaskSummary `json:"tasks"` - GitState *gitState `json:"git"` + SCM *scmState `json:"scm"` } diff --git a/cli/internal/runsummary/run_summary.go b/cli/internal/runsummary/run_summary.go index d6e1b0b5cea66..10c0e27e94a4a 100644 --- a/cli/internal/runsummary/run_summary.go +++ b/cli/internal/runsummary/run_summary.go @@ -64,7 +64,7 @@ type RunSummary struct { EnvMode util.EnvMode `json:"envMode"` ExecutionSummary *executionSummary `json:"execution,omitempty"` Tasks []*TaskSummary `json:"tasks"` - GitState *gitState `json:"git"` + SCM *scmState `json:"scm"` } // NewRunSummary returns a RunSummary instance @@ -106,7 +106,7 @@ func NewRunSummary( EnvMode: globalEnvMode, Tasks: []*TaskSummary{}, GlobalHashSummary: globalHashSummary, - GitState: getGitState(repoRoot), + SCM: getSCMState(repoRoot), }, ui: ui, runType: runType, diff --git a/cli/internal/runsummary/repo_summary.go b/cli/internal/runsummary/scm_summary.go similarity index 57% rename from cli/internal/runsummary/repo_summary.go rename to cli/internal/runsummary/scm_summary.go index 143c2e0f183f2..217e05a365ae0 100644 --- a/cli/internal/runsummary/repo_summary.go +++ b/cli/internal/runsummary/scm_summary.go @@ -7,33 +7,35 @@ import ( "github.com/vercel/turbo/cli/internal/turbopath" ) -type gitState struct { +type scmState struct { + Type string `json:"type"` Sha string `json:"sha"` Branch string `json:"branch"` } -// getGitState returns the sha and branch when in a git repo +// getSCMState returns the sha and branch when in a git repo // Otherwise it should return empty strings right now. // We my add handling of other scms and non-git tracking in the future. -func getGitState(dir turbopath.AbsoluteSystemPath) *gitState { +func getSCMState(dir turbopath.AbsoluteSystemPath) *scmState { allEnvVars := env.GetEnvMap() - gitstate := &gitState{} + state := &scmState{Type: "git"} // If we're in CI, try to get the values we need from environment variables if ci.IsCi() { vendor := ci.Info() - gitstate.Sha = allEnvVars[vendor.ShaEnvVar] - gitstate.Branch = allEnvVars[vendor.BranchEnvVar] + state.Sha = allEnvVars[vendor.ShaEnvVar] + state.Branch = allEnvVars[vendor.BranchEnvVar] } // Otherwise fallback to using `git` - if gitstate.Branch == "" { - gitstate.Branch = scm.GetCurrentBranch(dir) + if state.Branch == "" { + state.Branch = scm.GetCurrentBranch(dir) } - if gitstate.Sha == "" { - gitstate.Sha = scm.GetCurrentSha(dir) + + if state.Sha == "" { + state.Sha = scm.GetCurrentSha(dir) } - return gitstate + return state } diff --git a/cli/internal/runsummary/spaces.go b/cli/internal/runsummary/spaces.go index 5d45f4b56f8c5..c6e07b436b981 100644 --- a/cli/internal/runsummary/spaces.go +++ b/cli/internal/runsummary/spaces.go @@ -65,8 +65,8 @@ func (rsm *Meta) newSpacesRunCreatePayload() *spacesRunPayload { RepositoryPath: rsm.repoPath.ToString(), Type: "TURBO", Context: context, - GitBranch: rsm.RunSummary.GitState.Branch, - GitSha: rsm.RunSummary.GitState.Sha, + GitBranch: rsm.RunSummary.SCM.Branch, + GitSha: rsm.RunSummary.SCM.Sha, } } From f1daab19070755c92dd41b1045d1643fcf726e4b Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Tue, 2 May 2023 10:25:33 -0700 Subject: [PATCH 17/27] Fix tests --- turborepo-tests/integration/tests/dry_json/monorepo.t | 2 +- turborepo-tests/integration/tests/dry_json/single_package.t | 3 ++- .../integration/tests/dry_json/single_package_no_config.t | 3 ++- .../integration/tests/dry_json/single_package_with_deps.t | 3 ++- turborepo-tests/integration/tests/run_summary/monorepo.t | 5 +++-- .../integration/tests/run_summary/single-package.t | 5 +++-- 6 files changed, 13 insertions(+), 8 deletions(-) diff --git a/turborepo-tests/integration/tests/dry_json/monorepo.t b/turborepo-tests/integration/tests/dry_json/monorepo.t index 1bf9fb2283cfc..b4f6a9da4d667 100644 --- a/turborepo-tests/integration/tests/dry_json/monorepo.t +++ b/turborepo-tests/integration/tests/dry_json/monorepo.t @@ -73,10 +73,10 @@ Setup $ cat tmpjson.log | jq 'keys' [ "envMode", - "git", "globalCacheInputs", "id", "packages", + "scm", "tasks", "turboVersion", "version" diff --git a/turborepo-tests/integration/tests/dry_json/single_package.t b/turborepo-tests/integration/tests/dry_json/single_package.t index 2a3eeaee14663..8a21295ec0a8c 100644 --- a/turborepo-tests/integration/tests/dry_json/single_package.t +++ b/turborepo-tests/integration/tests/dry_json/single_package.t @@ -83,7 +83,8 @@ Setup } } ], - "git": { + "scm": { + "type": "git", "sha": "[a-z0-9]+", (re) "branch": ".+" (re) } diff --git a/turborepo-tests/integration/tests/dry_json/single_package_no_config.t b/turborepo-tests/integration/tests/dry_json/single_package_no_config.t index 7c6ceffb71d24..f80e6068a9656 100644 --- a/turborepo-tests/integration/tests/dry_json/single_package_no_config.t +++ b/turborepo-tests/integration/tests/dry_json/single_package_no_config.t @@ -77,7 +77,8 @@ Setup } } ], - "git": { + "scm": { + "type": "git", "sha": "[a-z0-9]+", (re) "branch": ".+" (re) } diff --git a/turborepo-tests/integration/tests/dry_json/single_package_with_deps.t b/turborepo-tests/integration/tests/dry_json/single_package_with_deps.t index 8f90da427cda1..2d6a8a5ca6a70 100644 --- a/turborepo-tests/integration/tests/dry_json/single_package_with_deps.t +++ b/turborepo-tests/integration/tests/dry_json/single_package_with_deps.t @@ -144,7 +144,8 @@ Setup } } ], - "git": { + "scm": { + "type": "git", "sha": "[a-z0-9]+", (re) "branch": ".+" (re) } diff --git a/turborepo-tests/integration/tests/run_summary/monorepo.t b/turborepo-tests/integration/tests/run_summary/monorepo.t index 8f3e763e89625..e083cad4e1548 100644 --- a/turborepo-tests/integration/tests/run_summary/monorepo.t +++ b/turborepo-tests/integration/tests/run_summary/monorepo.t @@ -33,18 +33,19 @@ Setup [ "envMode", "execution", - "git", "globalCacheInputs", "id", "packages", + "scm", "tasks", "turboVersion", "version" ] # some top level run summary validation - $ cat $FIRST | jq '.git' + $ cat $FIRST | jq '.scm' { + "type": "git", "sha": "[a-z0-9]+", (re) "branch": ".+" (re) } diff --git a/turborepo-tests/integration/tests/run_summary/single-package.t b/turborepo-tests/integration/tests/run_summary/single-package.t index 18e474ff5c9f6..03c16cb1a0141 100644 --- a/turborepo-tests/integration/tests/run_summary/single-package.t +++ b/turborepo-tests/integration/tests/run_summary/single-package.t @@ -33,16 +33,17 @@ Check [ "envMode", "execution", - "git", "globalCacheInputs", "id", + "scm", "tasks", "turboVersion", "version" ] - $ cat $SUMMARY | jq '.git' + $ cat $SUMMARY | jq '.scm' { + "type": "git", "sha": "[a-z0-9]+", (re) "branch": ".+" (re) } From c480e158cfc327fd63bd62945c360e4eeddce421 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Tue, 2 May 2023 14:22:52 -0700 Subject: [PATCH 18/27] add unit tests for git branch/sha --- cli/internal/scm/scm_test.go | 100 +++++++++++++++++++++++++++ cli/internal/scm/testdata/myrepo/foo | 0 2 files changed, 100 insertions(+) create mode 100644 cli/internal/scm/scm_test.go create mode 100644 cli/internal/scm/testdata/myrepo/foo diff --git a/cli/internal/scm/scm_test.go b/cli/internal/scm/scm_test.go new file mode 100644 index 0000000000000..94ba0dc2fb959 --- /dev/null +++ b/cli/internal/scm/scm_test.go @@ -0,0 +1,100 @@ +package scm + +import ( + "os" + "os/exec" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/vercel/turbo/cli/internal/fs" + "github.com/vercel/turbo/cli/internal/turbopath" +) + +func TestGetCurrentBranchMain(t *testing.T) { + targetbranch := "main" + testDir := getTestDir(t, "myrepo") + gitSetup(t, testDir) + gitCheckoutBranch(t, testDir, targetbranch) + branch := GetCurrentBranch(testDir) + assert.Equal(t, branch, targetbranch) + gitRm(t, testDir) +} + +func TestGetCurrentBranchNonMain(t *testing.T) { + targetbranch := "mybranch" + testDir := getTestDir(t, "myrepo") + gitSetup(t, testDir) + gitCheckoutBranch(t, testDir, targetbranch) + branch := GetCurrentBranch(testDir) + assert.Equal(t, branch, targetbranch) + gitRm(t, testDir) +} + +func TestGetCurrentSHA(t *testing.T) { + testDir := getTestDir(t, "myrepo") + gitSetup(t, testDir) + + // initial sha is blank because there are no commits + initSha := GetCurrentSha(testDir) + assert.True(t, initSha == "", "initial sha is empty") + + // create new commit + gitCommit(t, testDir) + sha1 := GetCurrentSha(testDir) + assert.True(t, sha1 != "sha on commit 1 is not empty") + gitCommit(t, testDir) + + // second sha + sha2 := GetCurrentSha(testDir) + assert.True(t, sha2 != "", "sha on commit 2 is not empty") + assert.True(t, sha2 != sha1, "sha on commit 2 changes from commit 1") + + // cleanup + gitRm(t, testDir) +} + +func getTestDir(t *testing.T, testName string) turbopath.AbsoluteSystemPath { + defaultCwd, err := os.Getwd() + if err != nil { + t.Errorf("failed to get cwd: %v", err) + } + cwd, err := fs.CheckedToAbsoluteSystemPath(defaultCwd) + if err != nil { + t.Fatalf("cwd is not an absolute directory %v: %v", defaultCwd, err) + } + + return cwd.UntypedJoin("testdata", testName) +} + +func gitRm(t *testing.T, dir turbopath.AbsoluteSystemPath) { + cmd := exec.Command("rm", []string{"-rf", ".git"}...) + cmd.Dir = dir.ToString() + if _, err := cmd.Output(); err != nil { + t.Fatalf("Failed to cleanup git dir: %v", err) + } +} + +func gitSetup(t *testing.T, dir turbopath.AbsoluteSystemPath) { + cmd := exec.Command("git", []string{"init"}...) + cmd.Dir = dir.ToString() + if _, err := cmd.Output(); err != nil { + t.Fatalf("Failed to checkout new branch in fixture repo: %v", err) + } +} + +func gitCommit(t *testing.T, dir turbopath.AbsoluteSystemPath) { + cmd := exec.Command("git", []string{"commit", "--allow-empty", "-am", "new commit"}...) + cmd.Dir = dir.ToString() + if _, err := cmd.Output(); err != nil { + t.Fatalf("Failed to checkout new branch in fixture repo: %v", err) + } +} + +func gitCheckoutBranch(t *testing.T, dir turbopath.AbsoluteSystemPath, branchname string) { + // using capital -B instead of -b, so we avoid "branch already exists errors" + cmd := exec.Command("git", []string{"checkout", "-B", branchname}...) + cmd.Dir = dir.ToString() + if _, err := cmd.Output(); err != nil { + t.Fatalf("Failed to checkout new branch in fixture repo: %v", err) + } +} diff --git a/cli/internal/scm/testdata/myrepo/foo b/cli/internal/scm/testdata/myrepo/foo new file mode 100644 index 0000000000000..e69de29bb2d1d From ced34689fedb6dcf5197448e12f3dc9a6a02eda7 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Tue, 2 May 2023 14:53:56 -0700 Subject: [PATCH 19/27] more logging on fail --- cli/internal/scm/scm_test.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/cli/internal/scm/scm_test.go b/cli/internal/scm/scm_test.go index 94ba0dc2fb959..b8b9731906c23 100644 --- a/cli/internal/scm/scm_test.go +++ b/cli/internal/scm/scm_test.go @@ -69,24 +69,24 @@ func getTestDir(t *testing.T, testName string) turbopath.AbsoluteSystemPath { func gitRm(t *testing.T, dir turbopath.AbsoluteSystemPath) { cmd := exec.Command("rm", []string{"-rf", ".git"}...) cmd.Dir = dir.ToString() - if _, err := cmd.Output(); err != nil { - t.Fatalf("Failed to cleanup git dir: %v", err) + if out, err := cmd.Output(); err != nil { + t.Fatalf("Failed to cleanup git dir: %v\n%v", out, err) } } func gitSetup(t *testing.T, dir turbopath.AbsoluteSystemPath) { cmd := exec.Command("git", []string{"init"}...) cmd.Dir = dir.ToString() - if _, err := cmd.Output(); err != nil { - t.Fatalf("Failed to checkout new branch in fixture repo: %v", err) + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("Failed to setup git: %v\n%v", out, err) } } func gitCommit(t *testing.T, dir turbopath.AbsoluteSystemPath) { cmd := exec.Command("git", []string{"commit", "--allow-empty", "-am", "new commit"}...) cmd.Dir = dir.ToString() - if _, err := cmd.Output(); err != nil { - t.Fatalf("Failed to checkout new branch in fixture repo: %v", err) + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("Failed to create new commit: %v\n%v", out, err) } } @@ -94,7 +94,7 @@ func gitCheckoutBranch(t *testing.T, dir turbopath.AbsoluteSystemPath, branchnam // using capital -B instead of -b, so we avoid "branch already exists errors" cmd := exec.Command("git", []string{"checkout", "-B", branchname}...) cmd.Dir = dir.ToString() - if _, err := cmd.Output(); err != nil { - t.Fatalf("Failed to checkout new branch in fixture repo: %v", err) + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("Failed to checkout new branch: %v\n%v", out, err) } } From 23c13acb67323de823957e847c1e3cf8b8b450f9 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Tue, 2 May 2023 15:18:26 -0700 Subject: [PATCH 20/27] print byte string correctly --- cli/internal/scm/scm_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/internal/scm/scm_test.go b/cli/internal/scm/scm_test.go index b8b9731906c23..bbfb25f3f126d 100644 --- a/cli/internal/scm/scm_test.go +++ b/cli/internal/scm/scm_test.go @@ -70,7 +70,7 @@ func gitRm(t *testing.T, dir turbopath.AbsoluteSystemPath) { cmd := exec.Command("rm", []string{"-rf", ".git"}...) cmd.Dir = dir.ToString() if out, err := cmd.Output(); err != nil { - t.Fatalf("Failed to cleanup git dir: %v\n%v", out, err) + t.Fatalf("Failed to cleanup git dir: %s\n%v", out, err) } } @@ -78,7 +78,7 @@ func gitSetup(t *testing.T, dir turbopath.AbsoluteSystemPath) { cmd := exec.Command("git", []string{"init"}...) cmd.Dir = dir.ToString() if out, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("Failed to setup git: %v\n%v", out, err) + t.Fatalf("Failed to setup git: %s\n%v", out, err) } } @@ -86,7 +86,7 @@ func gitCommit(t *testing.T, dir turbopath.AbsoluteSystemPath) { cmd := exec.Command("git", []string{"commit", "--allow-empty", "-am", "new commit"}...) cmd.Dir = dir.ToString() if out, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("Failed to create new commit: %v\n%v", out, err) + t.Fatalf("Failed to create new commit: %s\n%v", out, err) } } @@ -95,6 +95,6 @@ func gitCheckoutBranch(t *testing.T, dir turbopath.AbsoluteSystemPath, branchnam cmd := exec.Command("git", []string{"checkout", "-B", branchname}...) cmd.Dir = dir.ToString() if out, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("Failed to checkout new branch: %v\n%v", out, err) + t.Fatalf("Failed to checkout new branch: %s\n%v", out, err) } } From cdda67c30e1533b927af761f82d27786961406b1 Mon Sep 17 00:00:00 2001 From: Turbobot Date: Tue, 2 May 2023 15:44:29 -0700 Subject: [PATCH 21/27] setup git first --- cli/internal/scm/scm_test.go | 53 ++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 27 deletions(-) diff --git a/cli/internal/scm/scm_test.go b/cli/internal/scm/scm_test.go index bbfb25f3f126d..3144678dfe9d1 100644 --- a/cli/internal/scm/scm_test.go +++ b/cli/internal/scm/scm_test.go @@ -13,36 +13,52 @@ import ( func TestGetCurrentBranchMain(t *testing.T) { targetbranch := "main" testDir := getTestDir(t, "myrepo") - gitSetup(t, testDir) - gitCheckoutBranch(t, testDir, targetbranch) + + // Setup git + gitCommand(t, testDir, []string{"config", "--global", "user.email", "turbo@vercel.com"}) + gitCommand(t, testDir, []string{"config", "--global", "user.name", "Turbobot"}) + gitCommand(t, testDir, []string{"init"}) + + gitCommand(t, testDir, []string{"checkout", "-B", targetbranch}) branch := GetCurrentBranch(testDir) assert.Equal(t, branch, targetbranch) + + // cleanup gitRm(t, testDir) } func TestGetCurrentBranchNonMain(t *testing.T) { targetbranch := "mybranch" testDir := getTestDir(t, "myrepo") - gitSetup(t, testDir) - gitCheckoutBranch(t, testDir, targetbranch) + // Setup git + gitCommand(t, testDir, []string{"config", "--global", "user.email", "turbo@vercel.com"}) + gitCommand(t, testDir, []string{"config", "--global", "user.name", "Turbobot"}) + gitCommand(t, testDir, []string{"init"}) + gitCommand(t, testDir, []string{"checkout", "-B", targetbranch}) + branch := GetCurrentBranch(testDir) assert.Equal(t, branch, targetbranch) + + // cleanup gitRm(t, testDir) } func TestGetCurrentSHA(t *testing.T) { testDir := getTestDir(t, "myrepo") - gitSetup(t, testDir) + // Setup git + gitCommand(t, testDir, []string{"config", "--global", "user.email", "turbo@vercel.com"}) + gitCommand(t, testDir, []string{"config", "--global", "user.name", "Turbobot"}) + gitCommand(t, testDir, []string{"init"}) // initial sha is blank because there are no commits initSha := GetCurrentSha(testDir) assert.True(t, initSha == "", "initial sha is empty") // create new commit - gitCommit(t, testDir) + gitCommand(t, testDir, []string{"commit", "--allow-empty", "-am", "new commit"}) sha1 := GetCurrentSha(testDir) assert.True(t, sha1 != "sha on commit 1 is not empty") - gitCommit(t, testDir) + gitCommand(t, testDir, []string{"commit", "--allow-empty", "-am", "new commit"}) // second sha sha2 := GetCurrentSha(testDir) @@ -74,26 +90,9 @@ func gitRm(t *testing.T, dir turbopath.AbsoluteSystemPath) { } } -func gitSetup(t *testing.T, dir turbopath.AbsoluteSystemPath) { - cmd := exec.Command("git", []string{"init"}...) - cmd.Dir = dir.ToString() - if out, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("Failed to setup git: %s\n%v", out, err) - } -} - -func gitCommit(t *testing.T, dir turbopath.AbsoluteSystemPath) { - cmd := exec.Command("git", []string{"commit", "--allow-empty", "-am", "new commit"}...) - cmd.Dir = dir.ToString() - if out, err := cmd.CombinedOutput(); err != nil { - t.Fatalf("Failed to create new commit: %s\n%v", out, err) - } -} - -func gitCheckoutBranch(t *testing.T, dir turbopath.AbsoluteSystemPath, branchname string) { - // using capital -B instead of -b, so we avoid "branch already exists errors" - cmd := exec.Command("git", []string{"checkout", "-B", branchname}...) - cmd.Dir = dir.ToString() +func gitCommand(t *testing.T, cwd turbopath.AbsoluteSystemPath, args []string) { + cmd := exec.Command("git", args...) + cmd.Dir = cwd.ToString() if out, err := cmd.CombinedOutput(); err != nil { t.Fatalf("Failed to checkout new branch: %s\n%v", out, err) } From 6e5aa1c44d37b79a7671090d76cf33d440c0bfdb Mon Sep 17 00:00:00 2001 From: Turbobot Date: Tue, 2 May 2023 16:38:51 -0700 Subject: [PATCH 22/27] yes --- cli/internal/scm/scm_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cli/internal/scm/scm_test.go b/cli/internal/scm/scm_test.go index 3144678dfe9d1..5fece5c3442fb 100644 --- a/cli/internal/scm/scm_test.go +++ b/cli/internal/scm/scm_test.go @@ -54,13 +54,13 @@ func TestGetCurrentSHA(t *testing.T) { initSha := GetCurrentSha(testDir) assert.True(t, initSha == "", "initial sha is empty") - // create new commit + // first commit gitCommand(t, testDir, []string{"commit", "--allow-empty", "-am", "new commit"}) sha1 := GetCurrentSha(testDir) assert.True(t, sha1 != "sha on commit 1 is not empty") - gitCommand(t, testDir, []string{"commit", "--allow-empty", "-am", "new commit"}) - // second sha + // second commit + gitCommand(t, testDir, []string{"commit", "--allow-empty", "-am", "new commit"}) sha2 := GetCurrentSha(testDir) assert.True(t, sha2 != "", "sha on commit 2 is not empty") assert.True(t, sha2 != sha1, "sha on commit 2 changes from commit 1") From fae2b8830a7a550405b831f9b43c55406c79a11b Mon Sep 17 00:00:00 2001 From: Turbobot Date: Tue, 2 May 2023 17:14:35 -0700 Subject: [PATCH 23/27] only set local git user in test, so it doesn't blow up dev machines --- cli/internal/scm/scm_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cli/internal/scm/scm_test.go b/cli/internal/scm/scm_test.go index 5fece5c3442fb..340cbb8b2574b 100644 --- a/cli/internal/scm/scm_test.go +++ b/cli/internal/scm/scm_test.go @@ -15,8 +15,8 @@ func TestGetCurrentBranchMain(t *testing.T) { testDir := getTestDir(t, "myrepo") // Setup git - gitCommand(t, testDir, []string{"config", "--global", "user.email", "turbo@vercel.com"}) - gitCommand(t, testDir, []string{"config", "--global", "user.name", "Turbobot"}) + gitCommand(t, testDir, []string{"config", "--local", "user.email", "turbo@vercel.com"}) + gitCommand(t, testDir, []string{"config", "--local", "user.name", "Turbobot"}) gitCommand(t, testDir, []string{"init"}) gitCommand(t, testDir, []string{"checkout", "-B", targetbranch}) @@ -31,8 +31,8 @@ func TestGetCurrentBranchNonMain(t *testing.T) { targetbranch := "mybranch" testDir := getTestDir(t, "myrepo") // Setup git - gitCommand(t, testDir, []string{"config", "--global", "user.email", "turbo@vercel.com"}) - gitCommand(t, testDir, []string{"config", "--global", "user.name", "Turbobot"}) + gitCommand(t, testDir, []string{"config", "--local", "user.email", "turbo@vercel.com"}) + gitCommand(t, testDir, []string{"config", "--local", "user.name", "Turbobot"}) gitCommand(t, testDir, []string{"init"}) gitCommand(t, testDir, []string{"checkout", "-B", targetbranch}) @@ -46,8 +46,8 @@ func TestGetCurrentBranchNonMain(t *testing.T) { func TestGetCurrentSHA(t *testing.T) { testDir := getTestDir(t, "myrepo") // Setup git - gitCommand(t, testDir, []string{"config", "--global", "user.email", "turbo@vercel.com"}) - gitCommand(t, testDir, []string{"config", "--global", "user.name", "Turbobot"}) + gitCommand(t, testDir, []string{"config", "--local", "user.email", "turbo@vercel.com"}) + gitCommand(t, testDir, []string{"config", "--local", "user.name", "Turbobot"}) gitCommand(t, testDir, []string{"init"}) // initial sha is blank because there are no commits From c443620a2532cd5e47e157bd76aab0a5c23cd344 Mon Sep 17 00:00:00 2001 From: Turbobot Date: Tue, 2 May 2023 18:20:58 -0700 Subject: [PATCH 24/27] set local without flag --- cli/internal/scm/scm_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cli/internal/scm/scm_test.go b/cli/internal/scm/scm_test.go index 340cbb8b2574b..69d4884016a06 100644 --- a/cli/internal/scm/scm_test.go +++ b/cli/internal/scm/scm_test.go @@ -15,8 +15,8 @@ func TestGetCurrentBranchMain(t *testing.T) { testDir := getTestDir(t, "myrepo") // Setup git - gitCommand(t, testDir, []string{"config", "--local", "user.email", "turbo@vercel.com"}) - gitCommand(t, testDir, []string{"config", "--local", "user.name", "Turbobot"}) + gitCommand(t, testDir, []string{"config", "user.email", "turbo@vercel.com"}) + gitCommand(t, testDir, []string{"config", "user.name", "Turbobot"}) gitCommand(t, testDir, []string{"init"}) gitCommand(t, testDir, []string{"checkout", "-B", targetbranch}) @@ -31,8 +31,8 @@ func TestGetCurrentBranchNonMain(t *testing.T) { targetbranch := "mybranch" testDir := getTestDir(t, "myrepo") // Setup git - gitCommand(t, testDir, []string{"config", "--local", "user.email", "turbo@vercel.com"}) - gitCommand(t, testDir, []string{"config", "--local", "user.name", "Turbobot"}) + gitCommand(t, testDir, []string{"config", "user.email", "turbo@vercel.com"}) + gitCommand(t, testDir, []string{"config", "user.name", "Turbobot"}) gitCommand(t, testDir, []string{"init"}) gitCommand(t, testDir, []string{"checkout", "-B", targetbranch}) @@ -46,8 +46,8 @@ func TestGetCurrentBranchNonMain(t *testing.T) { func TestGetCurrentSHA(t *testing.T) { testDir := getTestDir(t, "myrepo") // Setup git - gitCommand(t, testDir, []string{"config", "--local", "user.email", "turbo@vercel.com"}) - gitCommand(t, testDir, []string{"config", "--local", "user.name", "Turbobot"}) + gitCommand(t, testDir, []string{"config", "user.email", "turbo@vercel.com"}) + gitCommand(t, testDir, []string{"config", "user.name", "Turbobot"}) gitCommand(t, testDir, []string{"init"}) // initial sha is blank because there are no commits From 29410e3804a037023828dac1471ed462d98b88f1 Mon Sep 17 00:00:00 2001 From: Mehul Kar Date: Wed, 3 May 2023 00:50:05 -0700 Subject: [PATCH 25/27] set --global git config in tests and reset after --- cli/internal/scm/scm_test.go | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/cli/internal/scm/scm_test.go b/cli/internal/scm/scm_test.go index 69d4884016a06..7b7cd00031101 100644 --- a/cli/internal/scm/scm_test.go +++ b/cli/internal/scm/scm_test.go @@ -13,10 +13,12 @@ import ( func TestGetCurrentBranchMain(t *testing.T) { targetbranch := "main" testDir := getTestDir(t, "myrepo") + originalEmail := gitCommand(t, testDir, []string{"config", "--global", "user.email"}) + originalName := gitCommand(t, testDir, []string{"config", "--global", "user.name"}) // Setup git - gitCommand(t, testDir, []string{"config", "user.email", "turbo@vercel.com"}) - gitCommand(t, testDir, []string{"config", "user.name", "Turbobot"}) + gitCommand(t, testDir, []string{"config", "--global", "user.email", "turbo@vercel.com"}) + gitCommand(t, testDir, []string{"config", "--global", "user.name", "Turbobot"}) gitCommand(t, testDir, []string{"init"}) gitCommand(t, testDir, []string{"checkout", "-B", targetbranch}) @@ -25,14 +27,20 @@ func TestGetCurrentBranchMain(t *testing.T) { // cleanup gitRm(t, testDir) + gitCommand(t, testDir, []string{"config", "--global", "user.email", originalEmail}) + gitCommand(t, testDir, []string{"config", "--global", "user.name", originalName}) } func TestGetCurrentBranchNonMain(t *testing.T) { targetbranch := "mybranch" testDir := getTestDir(t, "myrepo") + + originalEmail := gitCommand(t, testDir, []string{"config", "--global", "user.email"}) + originalName := gitCommand(t, testDir, []string{"config", "--global", "user.name"}) + // Setup git - gitCommand(t, testDir, []string{"config", "user.email", "turbo@vercel.com"}) - gitCommand(t, testDir, []string{"config", "user.name", "Turbobot"}) + gitCommand(t, testDir, []string{"config", "--global", "user.email", "turbo@vercel.com"}) + gitCommand(t, testDir, []string{"config", "--global", "user.name", "Turbobot"}) gitCommand(t, testDir, []string{"init"}) gitCommand(t, testDir, []string{"checkout", "-B", targetbranch}) @@ -41,13 +49,18 @@ func TestGetCurrentBranchNonMain(t *testing.T) { // cleanup gitRm(t, testDir) + gitCommand(t, testDir, []string{"config", "--global", "user.email", originalEmail}) + gitCommand(t, testDir, []string{"config", "--global", "user.name", originalName}) } func TestGetCurrentSHA(t *testing.T) { testDir := getTestDir(t, "myrepo") + originalEmail := gitCommand(t, testDir, []string{"config", "--global", "user.email"}) + originalName := gitCommand(t, testDir, []string{"config", "--global", "user.name"}) + // Setup git - gitCommand(t, testDir, []string{"config", "user.email", "turbo@vercel.com"}) - gitCommand(t, testDir, []string{"config", "user.name", "Turbobot"}) + gitCommand(t, testDir, []string{"config", "--global", "user.email", "turbo@vercel.com"}) + gitCommand(t, testDir, []string{"config", "--global", "user.name", "Turbobot"}) gitCommand(t, testDir, []string{"init"}) // initial sha is blank because there are no commits @@ -67,6 +80,8 @@ func TestGetCurrentSHA(t *testing.T) { // cleanup gitRm(t, testDir) + gitCommand(t, testDir, []string{"config", "--global", "user.email", originalEmail}) + gitCommand(t, testDir, []string{"config", "--global", "user.name", originalName}) } func getTestDir(t *testing.T, testName string) turbopath.AbsoluteSystemPath { @@ -90,10 +105,14 @@ func gitRm(t *testing.T, dir turbopath.AbsoluteSystemPath) { } } -func gitCommand(t *testing.T, cwd turbopath.AbsoluteSystemPath, args []string) { +func gitCommand(t *testing.T, cwd turbopath.AbsoluteSystemPath, args []string) string { cmd := exec.Command("git", args...) cmd.Dir = cwd.ToString() - if out, err := cmd.CombinedOutput(); err != nil { + out, err := cmd.CombinedOutput() + + if err != nil { t.Fatalf("Failed to checkout new branch: %s\n%v", out, err) } + + return string(out) } From 467feb240a179afb925a91099ee24aa383f66274 Mon Sep 17 00:00:00 2001 From: Turbobot Date: Wed, 3 May 2023 10:31:49 -0700 Subject: [PATCH 26/27] show which cmd failed --- cli/internal/scm/scm_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/internal/scm/scm_test.go b/cli/internal/scm/scm_test.go index 7b7cd00031101..e4f3e23129e6a 100644 --- a/cli/internal/scm/scm_test.go +++ b/cli/internal/scm/scm_test.go @@ -111,7 +111,7 @@ func gitCommand(t *testing.T, cwd turbopath.AbsoluteSystemPath, args []string) s out, err := cmd.CombinedOutput() if err != nil { - t.Fatalf("Failed to checkout new branch: %s\n%v", out, err) + t.Fatalf("Failed git command %s: %s\n%v", args, out, err) } return string(out) From 90ee2f5d77e66a30f5052fbbcd3c1d7e6a111978 Mon Sep 17 00:00:00 2001 From: Turbobot Date: Wed, 3 May 2023 12:47:21 -0700 Subject: [PATCH 27/27] Ignore errors when no global config exists --- cli/internal/scm/scm_test.go | 34 ++++++++++++++++++++++++++-------- 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/cli/internal/scm/scm_test.go b/cli/internal/scm/scm_test.go index e4f3e23129e6a..e0982aecc259e 100644 --- a/cli/internal/scm/scm_test.go +++ b/cli/internal/scm/scm_test.go @@ -13,8 +13,7 @@ import ( func TestGetCurrentBranchMain(t *testing.T) { targetbranch := "main" testDir := getTestDir(t, "myrepo") - originalEmail := gitCommand(t, testDir, []string{"config", "--global", "user.email"}) - originalName := gitCommand(t, testDir, []string{"config", "--global", "user.name"}) + originalName, originalEmail := getOriginalConfig(testDir) // Setup git gitCommand(t, testDir, []string{"config", "--global", "user.email", "turbo@vercel.com"}) @@ -35,8 +34,7 @@ func TestGetCurrentBranchNonMain(t *testing.T) { targetbranch := "mybranch" testDir := getTestDir(t, "myrepo") - originalEmail := gitCommand(t, testDir, []string{"config", "--global", "user.email"}) - originalName := gitCommand(t, testDir, []string{"config", "--global", "user.name"}) + originalName, originalEmail := getOriginalConfig(testDir) // Setup git gitCommand(t, testDir, []string{"config", "--global", "user.email", "turbo@vercel.com"}) @@ -55,8 +53,7 @@ func TestGetCurrentBranchNonMain(t *testing.T) { func TestGetCurrentSHA(t *testing.T) { testDir := getTestDir(t, "myrepo") - originalEmail := gitCommand(t, testDir, []string{"config", "--global", "user.email"}) - originalName := gitCommand(t, testDir, []string{"config", "--global", "user.name"}) + originalName, originalEmail := getOriginalConfig(testDir) // Setup git gitCommand(t, testDir, []string{"config", "--global", "user.email", "turbo@vercel.com"}) @@ -84,6 +81,7 @@ func TestGetCurrentSHA(t *testing.T) { gitCommand(t, testDir, []string{"config", "--global", "user.name", originalName}) } +// Helper functions func getTestDir(t *testing.T, testName string) turbopath.AbsoluteSystemPath { defaultCwd, err := os.Getwd() if err != nil { @@ -105,14 +103,34 @@ func gitRm(t *testing.T, dir turbopath.AbsoluteSystemPath) { } } +func getOriginalConfig(cwd turbopath.AbsoluteSystemPath) (string, string) { + // Ignore errors. If there was an error, it's likely because there was no value for these + // configs (e.g. in CI), so git is returning non-zero status code. This is ok, and we'll use the + // zero-value empty strings. + name, _ := _gitCommand(cwd, []string{"config", "--global", "user.name"}) + email, _ := _gitCommand(cwd, []string{"config", "--global", "user.name"}) + + return name, email +} + func gitCommand(t *testing.T, cwd turbopath.AbsoluteSystemPath, args []string) string { + out, err := _gitCommand(cwd, args) + + if err != nil { + t.Fatalf("Failed git command %s: %s\n%v", args, out, err) + } + + return string(out) +} + +func _gitCommand(cwd turbopath.AbsoluteSystemPath, args []string) (string, error) { cmd := exec.Command("git", args...) cmd.Dir = cwd.ToString() out, err := cmd.CombinedOutput() if err != nil { - t.Fatalf("Failed git command %s: %s\n%v", args, out, err) + return "", err } - return string(out) + return string(out), nil }