Skip to content
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
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
14 changes: 8 additions & 6 deletions services/openai/ai.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion services/utils/config.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
47 changes: 47 additions & 0 deletions services/utils/tools/commit_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 branch 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")
Expand All @@ -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 {
Expand Down
Loading