From 5d1222feabdfb72e4b2bcef2dda23fbe4045181e Mon Sep 17 00:00:00 2001 From: "paul.t" Date: Thu, 19 Jan 2023 06:07:36 +0000 Subject: [PATCH 1/5] refactor based on mvdan.sh --- internal/git/testing.go | 4 +-- internal/git/utils.go | 35 ++++++++++++++++----- internal/git/utils_test.go | 4 +-- internal/task/nextcommit/nextcommit_test.go | 8 ++--- 4 files changed, 35 insertions(+), 16 deletions(-) diff --git a/internal/git/testing.go b/internal/git/testing.go index 70e70e83..08fa7617 100644 --- a/internal/git/testing.go +++ b/internal/git/testing.go @@ -118,7 +118,7 @@ func EmptyCommit(t *testing.T, commit string) string { "commit", "--allow-empty", "-m", - commit, + fmt.Sprintf("'%s'", commit), } _, err := Run(args...) @@ -228,7 +228,7 @@ func TimeBasedTagSeries(t *testing.T, tags []string) []TimedTag { "commit", "--allow-empty", "-m", - fmt.Sprintf("feat: %d", c), + fmt.Sprintf("'feat: %d'", c), "--date", dtf, } diff --git a/internal/git/utils.go b/internal/git/utils.go index 68b04138..75024adc 100644 --- a/internal/git/utils.go +++ b/internal/git/utils.go @@ -23,14 +23,18 @@ SOFTWARE. package git import ( + "bytes" + "context" "errors" "fmt" "net/url" - "os/exec" + "os" "strings" "github.com/gembaadvantage/codecommit-sign/pkg/translate" "github.com/gembaadvantage/uplift/internal/semver" + "mvdan.cc/sh/v3/interp" + "mvdan.cc/sh/v3/syntax" ) // SCM is used for identifying the source code management tool used by the current @@ -87,12 +91,27 @@ func (c CommitDetails) String() string { // Run executes a git command and returns its output or errors func Run(args ...string) (string, error) { - cmd := exec.Command("git", args...) - out, err := cmd.CombinedOutput() + args = append([]string{"git"}, args...) + + p, err := syntax.NewParser().Parse(strings.NewReader(strings.Join(args, " ")), "") + if err != nil { + return "", err + } + + var buf bytes.Buffer + r, err := interp.New( + interp.StdIO(os.Stdin, &buf, &buf), + ) if err != nil { - return "", errors.New(string(out)) + return "", errors.New(buf.String()) } - return string(out), nil + + err = r.Run(context.Background(), p) + if err != nil { + return "", errors.New(buf.String()) + } + + return buf.String(), nil } // IsInstalled identifies whether git is installed under the current $PATH @@ -305,7 +324,7 @@ func Log(tag string) (string, error) { } func commitLog(srch string) (string, error) { - out, err := Clean(Run("log", "--no-decorate", "--no-color", srch)) + out, err := Clean(Run("log", "--no-decorate", "--no-color", fmt.Sprintf("'%s'", srch))) if err != nil { return "", err } @@ -343,7 +362,7 @@ func AnnotatedTag(tag string, cd CommitDetails) error { tag, "-f", "-m", - cd.Message, + fmt.Sprintf("'%s'", cd.Message), } if _, err := Clean(Run(args...)); err != nil { @@ -395,7 +414,7 @@ func Commit(cd CommitDetails) error { fmt.Sprintf("user.email='%s'", cd.Email), "commit", "-m", - cd.Message, + fmt.Sprintf("'%s'", cd.Message), } // If GPG commit signing is enabled, append the -S flag to the args diff --git a/internal/git/utils_test.go b/internal/git/utils_test.go index a293dec6..dd8fde56 100644 --- a/internal/git/utils_test.go +++ b/internal/git/utils_test.go @@ -544,7 +544,7 @@ func TestAuthor(t *testing.T) { func TestAuthorNoNameSet(t *testing.T) { InitRepo(t) // Setting it to an empty string is the equivalent of it not existing - Run("config", "user.name", "") + Run("config", "user.name", "''") Run("config", "user.email", "uplift@test.com") details := Author() @@ -556,7 +556,7 @@ func TestAuthorNoEmailSet(t *testing.T) { InitRepo(t) // Setting it to an empty string is the equivalent of it not existing Run("config", "user.name", "uplift") - Run("config", "user.email", "") + Run("config", "user.email", "''") details := Author() assert.Equal(t, "uplift", details.Name) diff --git a/internal/task/nextcommit/nextcommit_test.go b/internal/task/nextcommit/nextcommit_test.go index f75f08d9..4ebb4c63 100644 --- a/internal/task/nextcommit/nextcommit_test.go +++ b/internal/task/nextcommit/nextcommit_test.go @@ -46,8 +46,8 @@ func TestSkip(t *testing.T) { func TestRun(t *testing.T) { git.InitRepo(t) // Unset git author config - git.SetConfig(t, "user.name", "") - git.SetConfig(t, "user.email", "") + git.SetConfig(t, "user.name", "''") + git.SetConfig(t, "user.email", "''") ctx := &context.Context{ NextVersion: semver.Version{ @@ -82,8 +82,8 @@ func TestRun_GitAuthorConfig(t *testing.T) { func TestRun_CustomCommitDetails(t *testing.T) { git.InitRepo(t) - git.SetConfig(t, "user.name", "") - git.SetConfig(t, "user.email", "") + git.SetConfig(t, "user.name", "''") + git.SetConfig(t, "user.email", "''") ctx := &context.Context{ Config: config.Uplift{ From b2900eba01e13a17d360c5b5a49008b2807f38c4 Mon Sep 17 00:00:00 2001 From: "paul.t" Date: Thu, 19 Jan 2023 06:20:24 +0000 Subject: [PATCH 2/5] improve the way the git cmd is built --- internal/git/utils.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/internal/git/utils.go b/internal/git/utils.go index 75024adc..94623826 100644 --- a/internal/git/utils.go +++ b/internal/git/utils.go @@ -91,9 +91,14 @@ func (c CommitDetails) String() string { // Run executes a git command and returns its output or errors func Run(args ...string) (string, error) { - args = append([]string{"git"}, args...) + var cmd strings.Builder + cmd.WriteString("git") + for _, arg := range args { + cmd.WriteString(" ") + cmd.WriteString(arg) + } - p, err := syntax.NewParser().Parse(strings.NewReader(strings.Join(args, " ")), "") + p, err := syntax.NewParser().Parse(strings.NewReader(cmd.String()), "") if err != nil { return "", err } From af8bae10b1b1a32492e609a5435cb7663a447a3f Mon Sep 17 00:00:00 2001 From: "paul.t" Date: Thu, 19 Jan 2023 19:35:29 +0000 Subject: [PATCH 3/5] debugging --- internal/task/bump/bump.go | 5 +++++ internal/task/bump/regex.go | 4 ++++ internal/task/bump/regex_test.go | 2 ++ 3 files changed, 11 insertions(+) diff --git a/internal/task/bump/bump.go b/internal/task/bump/bump.go index bb2b73fb..e9d28d55 100644 --- a/internal/task/bump/bump.go +++ b/internal/task/bump/bump.go @@ -23,6 +23,8 @@ SOFTWARE. package bump import ( + "fmt" + "github.com/apex/log" "github.com/gembaadvantage/uplift/internal/context" "github.com/gembaadvantage/uplift/internal/git" @@ -85,6 +87,9 @@ func (t Task) Run(ctx *context.Context) error { continue } + fmt.Println("About to stage file at path") + fmt.Println(resolvedBump) + if err := git.Stage(resolvedBump); err != nil { return err } diff --git a/internal/task/bump/regex.go b/internal/task/bump/regex.go index 5dc05daf..fa90c215 100644 --- a/internal/task/bump/regex.go +++ b/internal/task/bump/regex.go @@ -24,6 +24,7 @@ package bump import ( "errors" + "fmt" "os" "regexp" "strings" @@ -92,6 +93,9 @@ func regexBump(ctx *context.Context, path string, bumps []config.RegexBump) (boo return false, nil } + fmt.Println("Writing to file at path") + fmt.Println(path) + return true, os.WriteFile(path, []byte(str), 0o644) } diff --git a/internal/task/bump/regex_test.go b/internal/task/bump/regex_test.go index 69ea00b0..1208ee10 100644 --- a/internal/task/bump/regex_test.go +++ b/internal/task/bump/regex_test.go @@ -544,6 +544,8 @@ description: This is a test chart version: 0.1.0 appVersion: v0.1.0`) + fmt.Println(file) + ctx := &context.Context{ NextVersion: semver.Version{ Raw: "v0.1.1", From 2490f6a518e3b63e95136df6874001f92906c7e3 Mon Sep 17 00:00:00 2001 From: "paul.t" Date: Thu, 19 Jan 2023 20:03:44 +0000 Subject: [PATCH 4/5] escape path in stage command --- internal/git/utils.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/git/utils.go b/internal/git/utils.go index 94623826..55d5ef3b 100644 --- a/internal/git/utils.go +++ b/internal/git/utils.go @@ -458,7 +458,7 @@ func ConfigSet(values map[string]string) error { // Stage will ensure the specified file is staged for the next commit func Stage(path string) error { - if _, err := Clean(Run("add", path)); err != nil { + if _, err := Clean(Run("add", fmt.Sprintf("'%s'", path))); err != nil { return err } From 37defa1310a7a54c99713ebe0f030f1b53cc05cc Mon Sep 17 00:00:00 2001 From: "paul.t" Date: Thu, 19 Jan 2023 21:16:00 +0000 Subject: [PATCH 5/5] remove debugging for windows --- internal/task/bump/bump.go | 5 ----- internal/task/bump/bump_test.go | 3 --- internal/task/bump/regex.go | 4 ---- internal/task/bump/regex_test.go | 2 -- 4 files changed, 14 deletions(-) diff --git a/internal/task/bump/bump.go b/internal/task/bump/bump.go index e9d28d55..bb2b73fb 100644 --- a/internal/task/bump/bump.go +++ b/internal/task/bump/bump.go @@ -23,8 +23,6 @@ SOFTWARE. package bump import ( - "fmt" - "github.com/apex/log" "github.com/gembaadvantage/uplift/internal/context" "github.com/gembaadvantage/uplift/internal/git" @@ -87,9 +85,6 @@ func (t Task) Run(ctx *context.Context) error { continue } - fmt.Println("About to stage file at path") - fmt.Println(resolvedBump) - if err := git.Stage(resolvedBump); err != nil { return err } diff --git a/internal/task/bump/bump_test.go b/internal/task/bump/bump_test.go index e8dcb842..86985935 100644 --- a/internal/task/bump/bump_test.go +++ b/internal/task/bump/bump_test.go @@ -23,7 +23,6 @@ SOFTWARE. package bump import ( - "fmt" "os" "path/filepath" "testing" @@ -49,8 +48,6 @@ func TestRun_NotGitRepository(t *testing.T) { git.MkTmpDir(t) file := WriteTempFile(t, "version: 0.1.0") - fmt.Println(file) - ctx := &context.Context{ NextVersion: semver.Version{ Raw: "0.1.1", diff --git a/internal/task/bump/regex.go b/internal/task/bump/regex.go index fa90c215..5dc05daf 100644 --- a/internal/task/bump/regex.go +++ b/internal/task/bump/regex.go @@ -24,7 +24,6 @@ package bump import ( "errors" - "fmt" "os" "regexp" "strings" @@ -93,9 +92,6 @@ func regexBump(ctx *context.Context, path string, bumps []config.RegexBump) (boo return false, nil } - fmt.Println("Writing to file at path") - fmt.Println(path) - return true, os.WriteFile(path, []byte(str), 0o644) } diff --git a/internal/task/bump/regex_test.go b/internal/task/bump/regex_test.go index 1208ee10..69ea00b0 100644 --- a/internal/task/bump/regex_test.go +++ b/internal/task/bump/regex_test.go @@ -544,8 +544,6 @@ description: This is a test chart version: 0.1.0 appVersion: v0.1.0`) - fmt.Println(file) - ctx := &context.Context{ NextVersion: semver.Version{ Raw: "v0.1.1",