diff --git a/cmd/root.go b/cmd/root.go index ac7afdf..51d46ef 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -85,9 +85,9 @@ var rootCmd = &cobra.Command{ } var gitCommitError error action := func() { - // signOff := config.SignOff - // gitCommitError = commit(commitMessage, commitBody, signOff) - gitCommitError = config.GitCommit(".", commitMessage, commitBody) + signOff := config.SignOff + gitCommitError = commit(commitMessage, commitBody, signOff) + // gitCommitError = config.GitCommit(".", commitMessage, commitBody) } err = spinner.New(). diff --git a/pkg/config/git.go b/pkg/config/git.go index 0ab7df8..a6d3fcb 100644 --- a/pkg/config/git.go +++ b/pkg/config/git.go @@ -1,42 +1,46 @@ package config import ( - "fmt" - "os/exec" - "strings" - "github.com/charmbracelet/log" "github.com/go-git/go-git/v5" - "github.com/go-git/go-git/v5/plumbing" + // "github.com/go-git/go-git/v5" + // "github.com/go-git/go-git/v5/plumbing" ) func GitRepo() (string, error) { - revParse := exec.Command("git", "rev-parse", "--show-toplevel") - repoDirBytes, err := revParse.Output() + repo, err := git.PlainOpen(".") if err != nil { + if err == git.ErrRepositoryNotExists { + return "", err + } log.Fatalf("Error finding git root directory: %v", err) } - repoDir := strings.TrimRight(string(repoDirBytes), "\n") - return repoDir, nil -} -func (c *Config) GitCommit(repoPath, message, description string) error { - // Open the repository - repo, err := git.PlainOpen(repoPath) + repoPath, err := repo.Worktree() if err != nil { - return err + log.Fatalf("Error finding git root directory: %v", err) } - // Get the working tree - wt, err := repo.Worktree() - if err != nil { - return err - } + return repoPath.Filesystem.Root(), nil +} - // Commit the changes - _, err = wt.Commit(fmt.Sprintf("%s\n\n%s", message, description), &git.CommitOptions{ - Parents: []plumbing.Hash{}, - }) +// func (c *Config) GitCommit(repoPath, message, description string) error { +// // Open the repository +// repo, err := git.PlainOpen(repoPath) +// if err != nil { +// return err +// } - return err -} +// // Get the working tree +// wt, err := repo.Worktree() +// if err != nil { +// return err +// } + +// // Commit the changes +// _, err = wt.Commit(fmt.Sprintf("%s\n\n%s", message, description), &git.CommitOptions{ +// Parents: []plumbing.Hash{}, +// }) + +// return err +// } diff --git a/pkg/config/git_test.go b/pkg/config/git_test.go index ca032d5..cf62b20 100644 --- a/pkg/config/git_test.go +++ b/pkg/config/git_test.go @@ -3,13 +3,11 @@ package config import "testing" func TestGitRepo(t *testing.T) { - repo, err := GitRepo() - if err != nil { - t.Errorf("Error finding git root directory: %v", err) - } + t.Run("repo dir is found", func(t *testing.T) { + _, err := GitRepo() + if err != nil { + t.Errorf("Error finding git root directory: %v", err) - // Check if the repo path is not empty - if repo == "" { - t.Errorf("Expected a repository path, got an empty string") - } + } + }) }