From 81e470bd3c499fd51aead75abaf920a0a65ecdb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zine=20Moualhi=20=F0=9F=87=B5=F0=9F=87=B8?= <114167214+muandane@users.noreply.github.com> Date: Wed, 5 Jun 2024 22:56:11 +0100 Subject: [PATCH] Fix/commit output (#51) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * test 🧪(commit): testing commit output Signed-off-by: moualhi zine el abidine --------- Signed-off-by: moualhi zine el abidine --- .goji.json | 4 +-- cmd/root.go | 64 ++++++++++++++++++++++++++++++++++-------------- cmd/root_test.go | 39 ----------------------------- go.mod | 3 ++- go.sum | 2 ++ main.go | 3 +++ 6 files changed, 55 insertions(+), 60 deletions(-) diff --git a/.goji.json b/.goji.json index 39cfd4a..2bc7dcd 100644 --- a/.goji.json +++ b/.goji.json @@ -5,7 +5,7 @@ "accounts", "ci" ], - "signoff": false, + "signoff": true, "skipquestions": null, "subjectmaxlength": 100, "types": [ @@ -76,4 +76,4 @@ "name": "package" } ] -} +} \ No newline at end of file diff --git a/cmd/root.go b/cmd/root.go index f45683a..62a6b47 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -6,6 +6,7 @@ import ( "os" + "github.com/alessio/shellescape" "github.com/charmbracelet/huh/spinner" "github.com/charmbracelet/log" "github.com/fatih/color" @@ -15,11 +16,12 @@ import ( ) var ( - version string - versionFlag bool - typeFlag string - scopeFlag string - messageFlag string + version string + versionFlag bool + noVerifyFlag bool + typeFlag string + scopeFlag string + messageFlag string ) var rootCmd = &cobra.Command{ @@ -90,8 +92,15 @@ var rootCmd = &cobra.Command{ var gitCommitError error action := func() { signOff := config.SignOff - gitCommitError = commit(commitMessage, commitBody, signOff) - // gitCommitError = config.GitCommit(".", commitMessage, commitBody) + var extraArgs []string + if noVerifyFlag { + extraArgs = append(extraArgs, "--no-verify") + } + command, commandString := buildCommitCommand(commitMessage, commitBody, signOff, extraArgs) + fmt.Printf("Executing command: %s\n", commandString) + if err := commit(command); err != nil { + log.Fatalf("Error committing changes: %v\n", err) + } } err = spinner.New(). @@ -107,11 +116,24 @@ var rootCmd = &cobra.Command{ }, } +// init initializes the flags for the root command. +// +// This function sets up the flags for the root command, which are used to specify the type, scope, message, +// and options for the command. The flags are defined using the `rootCmd.Flags()` method. +// +// - `typeFlag` is a string flag that specifies the type from the config file. +// - `scopeFlag` is a string flag that specifies a custom scope. +// - `messageFlag` is a string flag that specifies a commit message. +// - `noVerifyFlag` is a boolean flag that bypasses pre-commit and commit-msg hooks. +// - `versionFlag` is a boolean flag that displays version information. +// +// There are no parameters or return values for this function. func init() { - rootCmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "Display version information") rootCmd.Flags().StringVarP(&typeFlag, "type", "t", "", "Specify the type from the config file") rootCmd.Flags().StringVarP(&scopeFlag, "scope", "s", "", "Specify a custom scope") rootCmd.Flags().StringVarP(&messageFlag, "message", "m", "", "Specify a commit message") + rootCmd.Flags().BoolVarP(&noVerifyFlag, "no-verify", "n", false, "bypass pre-commit and commit-msg hooks") + rootCmd.Flags().BoolVarP(&versionFlag, "version", "v", false, "Display version information") } func Execute() { @@ -130,17 +152,23 @@ func Execute() { // // Returns: // - error: an error if the git commit execution fails. -func commit(message, body string, sign bool) error { - args := []string{"commit", "-m", message, "-m", body} +func buildCommitCommand(message string, body string, sign bool, extraArgs []string) ([]string, string) { if sign { - args = append(args, "--signoff") + extraArgs = append(extraArgs, "--signoff") } - gitCmd := exec.Command("git", args...) - - output, err := gitCmd.CombinedOutput() - if err != nil { - return fmt.Errorf("error executing git commit: %v\noutput: %s", err, output) + args := append([]string{"commit", "-m", message}, extraArgs...) + if body != "" { + args = append(args, "-m", body) } - fmt.Printf("Git commit output: %s\n", string(output)) - return nil + return args, fmt.Sprintf("git %v", shellescape.QuoteCommand(args)) +} + +// commit commits the changes to git +func commit(command []string) error { + cmd := exec.Command("git", command...) + cmd.Stdin = os.Stdin + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + + return cmd.Run() } diff --git a/cmd/root_test.go b/cmd/root_test.go index 6d18366..b25f816 100644 --- a/cmd/root_test.go +++ b/cmd/root_test.go @@ -5,7 +5,6 @@ import ( "fmt" "io" "os" - "os/exec" "testing" "github.com/alecthomas/assert/v2" @@ -13,44 +12,6 @@ import ( "github.com/spf13/cobra" ) -func TestCommit(t *testing.T) { - t.Run("success", func(t *testing.T) { - if testing.Short() { - t.Skip("skipping test in short mode.") - } - - tempDir, err := os.MkdirTemp("", "git-commit-test") - if err != nil { - t.Fatalf("error creating temp dir: %v", err) - } - defer os.RemoveAll(tempDir) - - if err := os.Chdir(tempDir); err != nil { - t.Fatalf("error changing to temp dir: %v", err) - } - - if err := exec.Command("git", "init").Run(); err != nil { - t.Fatalf("error initializing git repo: %v", err) - } - - if err := os.WriteFile("testfile", []byte("test content"), 0644); err != nil { - t.Fatalf("error writing testfile: %v", err) - } - - if err := exec.Command("git", "add", "testfile").Run(); err != nil { - t.Fatalf("error adding testfile to index: %v", err) - } - - if err := commit("test commit", "test commit body", false); err != nil { - t.Fatalf("error committing: %v", err) - } - - if err := exec.Command("git", "log", "-1", "--pretty=%s").Run(); err != nil { - t.Fatalf("error checking commit: %v", err) - } - }) -} - func TestRootCmd_VersionFlag(t *testing.T) { var versionFlag bool diff --git a/go.mod b/go.mod index f646a16..3ecf926 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,9 @@ module github.com/muandane/goji -go 1.22.2 +go 1.22.4 require ( + github.com/alessio/shellescape v1.4.2 github.com/alecthomas/assert/v2 v2.10.0 github.com/charmbracelet/glamour v0.7.0 github.com/charmbracelet/huh v0.4.2 diff --git a/go.sum b/go.sum index 9d4e7fd..545ef90 100644 --- a/go.sum +++ b/go.sum @@ -2,6 +2,8 @@ github.com/alecthomas/assert/v2 v2.10.0 h1:jjRCHsj6hBJhkmhznrCzoNpbA3zqy0fYiUcYZ github.com/alecthomas/assert/v2 v2.10.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k= github.com/alecthomas/chroma/v2 v2.8.0 h1:w9WJUjFFmHHB2e8mRpL9jjy3alYDlU0QLDezj1xE264= github.com/alecthomas/chroma/v2 v2.8.0/go.mod h1:yrkMI9807G1ROx13fhe1v6PN2DDeaR73L3d+1nmYQtw= +github.com/alessio/shellescape v1.4.2 h1:MHPfaU+ddJ0/bYWpgIeUnQUqKrlJ1S7BfEYPM4uEoM0= +github.com/alessio/shellescape v1.4.2/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30= github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc= github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4= github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4= diff --git a/main.go b/main.go index 25c2e91..024f6fe 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,9 @@ import ( "github.com/muandane/goji/cmd" ) +// main is the entry point of the Go program. +// +// It calls the Execute function from the cmd package to execute the command. func main() { cmd.Execute() }