|
| 1 | +package branch |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + |
| 6 | + "github.com/recrsn/git-ai/pkg/config" |
| 7 | + "github.com/recrsn/git-ai/pkg/git" |
| 8 | + "github.com/recrsn/git-ai/pkg/llm" |
| 9 | + "github.com/recrsn/git-ai/pkg/logger" |
| 10 | + "github.com/recrsn/git-ai/pkg/ui" |
| 11 | + "github.com/spf13/cobra" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + autoApprove bool |
| 16 | + description string |
| 17 | +) |
| 18 | + |
| 19 | +// Cmd represents the branch command |
| 20 | +var Cmd = &cobra.Command{ |
| 21 | + Use: "branch [description]", |
| 22 | + Short: "Generate a meaningful Git branch name", |
| 23 | + Long: `Analyzes your input and existing branches to generate a meaningful branch name.`, |
| 24 | + Args: cobra.MaximumNArgs(1), |
| 25 | + Run: func(cmd *cobra.Command, args []string) { |
| 26 | + // If description is provided as an argument, use it |
| 27 | + if len(args) > 0 { |
| 28 | + description = args[0] |
| 29 | + } |
| 30 | + |
| 31 | + // If no description is provided, prompt for one |
| 32 | + if description == "" { |
| 33 | + var err error |
| 34 | + description, err = ui.PromptForInput("Enter a brief description of your branch:", "") |
| 35 | + if err != nil { |
| 36 | + logger.Fatal("Error prompting for description: %v", err) |
| 37 | + } |
| 38 | + if description == "" { |
| 39 | + logger.Error("Description cannot be empty.") |
| 40 | + os.Exit(1) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + executeBranch(description) |
| 45 | + }, |
| 46 | +} |
| 47 | + |
| 48 | +func init() { |
| 49 | + Cmd.Flags().BoolVar(&autoApprove, "auto", false, "Automatically approve the generated branch name without prompting") |
| 50 | + Cmd.Flags().StringVarP(&description, "description", "d", "", "Brief description of the branch purpose") |
| 51 | +} |
| 52 | + |
| 53 | +func executeBranch(description string) { |
| 54 | + cfg, err := config.LoadConfig() |
| 55 | + if err != nil { |
| 56 | + logger.Fatal("Failed to load config: %v", err) |
| 57 | + } |
| 58 | + |
| 59 | + // Generate branch name - with spinner |
| 60 | + spinner, err := ui.ShowSpinner("Generating branch name with LLM...") |
| 61 | + if err != nil { |
| 62 | + logger.Error("Failed to start spinner: %v", err) |
| 63 | + } |
| 64 | + |
| 65 | + branchName, err := llm.GenerateBranchName(cfg, description) |
| 66 | + if err != nil { |
| 67 | + if spinner != nil { |
| 68 | + spinner.Fail("Failed to generate branch name!") |
| 69 | + } |
| 70 | + logger.Fatal("Failed to generate branch name: %v", err) |
| 71 | + } |
| 72 | + |
| 73 | + if spinner != nil { |
| 74 | + spinner.Success("Branch name generated!") |
| 75 | + } |
| 76 | + |
| 77 | + // If auto-approve flag is not set, ask user to confirm or edit |
| 78 | + var proceed bool |
| 79 | + if !autoApprove { |
| 80 | + ui.DisplayBox("Generated Branch Name", branchName) |
| 81 | + |
| 82 | + options := []string{"Create branch", "Edit name", "Print name only", "Cancel"} |
| 83 | + selectedOption, err := ui.PromptForSelection(options, "Create branch", "What would you like to do?") |
| 84 | + if err != nil { |
| 85 | + logger.Fatal("Error prompting for selection: %v", err) |
| 86 | + } |
| 87 | + |
| 88 | + switch selectedOption { |
| 89 | + case "Create branch": |
| 90 | + proceed = true |
| 91 | + case "Edit name": |
| 92 | + // Use text input for editing with pre-filled value |
| 93 | + branchName, err = ui.PromptForInput("Edit branch name:", branchName) |
| 94 | + if err != nil { |
| 95 | + logger.Fatal("Error prompting for input: %v", err) |
| 96 | + } |
| 97 | + if branchName == "" { |
| 98 | + logger.Error("Branch name cannot be empty.") |
| 99 | + os.Exit(1) |
| 100 | + } |
| 101 | + proceed = true |
| 102 | + case "Print name only": |
| 103 | + logger.PrintMessage(branchName) |
| 104 | + os.Exit(0) |
| 105 | + case "Cancel": |
| 106 | + logger.PrintMessage("Branch creation cancelled.") |
| 107 | + os.Exit(0) |
| 108 | + } |
| 109 | + } else { |
| 110 | + proceed = true |
| 111 | + } |
| 112 | + |
| 113 | + if proceed { |
| 114 | + // Create the branch |
| 115 | + err = git.CreateBranch(branchName) |
| 116 | + if err != nil { |
| 117 | + logger.Fatal("Failed to create branch: %v", err) |
| 118 | + } |
| 119 | + |
| 120 | + logger.PrintMessagef("Branch '%s' created successfully!", branchName) |
| 121 | + } |
| 122 | +} |
0 commit comments