Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/commit output #51

Merged
merged 19 commits into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .goji.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"accounts",
"ci"
],
"signoff": false,
"signoff": true,
"skipquestions": null,
"subjectmaxlength": 100,
"types": [
Expand Down Expand Up @@ -76,4 +76,4 @@
"name": "package"
}
]
}
}
64 changes: 46 additions & 18 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"os"

"github.com/alessio/shellescape"
"github.com/charmbracelet/huh/spinner"
"github.com/charmbracelet/log"
"github.com/fatih/color"
Expand All @@ -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{
Expand Down Expand Up @@ -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().
Expand All @@ -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() {
Expand All @@ -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()
}
39 changes: 0 additions & 39 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,13 @@ import (
"fmt"
"io"
"os"
"os/exec"
"testing"

"github.com/alecthomas/assert/v2"
"github.com/fatih/color"
"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
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Loading