From f54fd52b36f6d5e779a3d28d04688bcfbac2a78c Mon Sep 17 00:00:00 2001 From: "kamal.krami" Date: Wed, 26 Mar 2025 20:26:30 +0000 Subject: [PATCH 1/4] feat(interfaces): introduce BranchSelector interface to enhance branch message selection --- main.go | 2 +- services/openai/ai.go | 14 ++++---- services/utils/tools/commit_helper.go | 47 +++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 7 deletions(-) diff --git a/main.go b/main.go index ad81701..5526ef6 100644 --- a/main.go +++ b/main.go @@ -38,7 +38,7 @@ func main() { return case "-b": - ai.GetBranchNames(os.Args[2]) + tools.RunBranch(os.Args[2]) return case "help", "h", "-h", "--help": diff --git a/services/openai/ai.go b/services/openai/ai.go index 2f5d485..0ba70d7 100644 --- a/services/openai/ai.go +++ b/services/openai/ai.go @@ -41,6 +41,10 @@ type CommitMessageSelector interface { SelectCommitMessage(messages []string) error } +type BranchSelector interface { + SelectBranchMessage(messages []string, context string) error +} + func GetCommitMessage(content string, selector CommitMessageSelector, token string) string { var url = utils.ComitURL + "/commit" + "?token=" + token payload := RequestData{ @@ -81,7 +85,7 @@ func GetCommitMessage(content string, selector CommitMessageSelector, token stri return "Ok" } -func GetBranchNames(context string) string { +func GetBranchNames(context string, selector BranchSelector) string { var url = utils.ComitURL + "/branch" payload := RequestBranchName{ Context: context, @@ -117,12 +121,10 @@ func GetBranchNames(context string) string { return "Error: " + err.Error() } - fmt.Println("\nBranches:") - - for _, branch := range data.Branch { - fmt.Printf(" - " + branch + "\n") + if err := selector.SelectBranchMessage(data.Branch, context); err != nil { + return "Error: " + err.Error() } - return "" + return "Ok" } func GetPromptResponse(prompt string) { diff --git a/services/utils/tools/commit_helper.go b/services/utils/tools/commit_helper.go index c8b8576..9accdaf 100644 --- a/services/utils/tools/commit_helper.go +++ b/services/utils/tools/commit_helper.go @@ -52,6 +52,45 @@ func (RealSelector) SelectCommitMessage(commitMessages []string) error { return nil } +func (RealSelector) SelectBranchMessage(branchMessages []string, context string) error { + if len(branchMessages) == 0 || allEmpty(branchMessages) { + RunBranch(context) + return nil + } + + prompt := promptui.Select{ + Label: "Select commit message", + Items: append([]string{"Refresh"}, branchMessages...), + } + + _, result, err := prompt.Run() + if err != nil { + return err + } + + if result == "Refresh" { + RunBranch(context) + return nil + } + + var cmd *exec.Cmd + + switch runtime.GOOS { + case "windows": + cmd = exec.Command("powershell", "-Command", fmt.Sprintf("git checkout -b %q", result)) + case "darwin": + cmd = exec.Command("sh", "-c", fmt.Sprintf("git checkout -b %q", result)) + default: + cmd = exec.Command("powershell", "-Command", fmt.Sprintf("git checkout -b %q", result)) + } + fmt.Printf("You executed: git checkout -b %q\n", result) + _, err = cmd.Output() + if err != nil { + return err + } + return nil +} + func CheckStage() (string, error) { cmd := exec.Command("git", "--no-pager", "diff", "--staged") @@ -67,6 +106,14 @@ func CheckStage() (string, error) { return string(output), nil } +func RunBranch(context string) { + messageStatus := ai.GetBranchNames(context, RealSelector{}) + if messageStatus != "Ok" { + fmt.Println("Something went wrong please try again") + return + } +} + func RunCommit() { output, err := CheckStage() if err != nil { From 9d8ddf0314ae11c4a2da60625a4cc58a125abb6d Mon Sep 17 00:00:00 2001 From: "kamal.krami" Date: Wed, 26 Mar 2025 20:30:44 +0000 Subject: [PATCH 2/4] fix(commit_helper): update label for branch message selection --- services/utils/tools/commit_helper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/utils/tools/commit_helper.go b/services/utils/tools/commit_helper.go index 9accdaf..96743e5 100644 --- a/services/utils/tools/commit_helper.go +++ b/services/utils/tools/commit_helper.go @@ -59,7 +59,7 @@ func (RealSelector) SelectBranchMessage(branchMessages []string, context string) } prompt := promptui.Select{ - Label: "Select commit message", + Label: "Select Branch message", Items: append([]string{"Refresh"}, branchMessages...), } From 8ff5ea44c7d4f83e8e3bf97baa9c5f0e3fd7cef1 Mon Sep 17 00:00:00 2001 From: "kamal.krami" Date: Wed, 26 Mar 2025 21:12:09 +0000 Subject: [PATCH 3/4] fix: correct casing in prompt label for branch message selection --- services/utils/tools/commit_helper.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/utils/tools/commit_helper.go b/services/utils/tools/commit_helper.go index 96743e5..ce16d8c 100644 --- a/services/utils/tools/commit_helper.go +++ b/services/utils/tools/commit_helper.go @@ -59,7 +59,7 @@ func (RealSelector) SelectBranchMessage(branchMessages []string, context string) } prompt := promptui.Select{ - Label: "Select Branch message", + Label: "Select branch message", Items: append([]string{"Refresh"}, branchMessages...), } From 04afff06e7d776757222a045800722b5cfb3e222 Mon Sep 17 00:00:00 2001 From: Issam Haidaoui Date: Tue, 8 Apr 2025 22:10:32 +0200 Subject: [PATCH 4/4] chore: update version to 0.9 in config --- services/utils/config.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/utils/config.go b/services/utils/config.go index 207535c..9cab1d1 100644 --- a/services/utils/config.go +++ b/services/utils/config.go @@ -1,6 +1,6 @@ package utils -var Version = "0.8.5" +var Version = "0.9" var ( UpdateLink = "https://github.com/issamoxix/Comit/releases/download/%s/%s"