aicommit

package
v0.0.0-...-d9dc390 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 13, 2026 License: Apache-2.0 Imports: 9 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CommitCmd = &cobra.Command{
	Use:   "commit",
	Short: "Auto-generate conventional Git commit messages using AI",
	Long:  `Inspects your Git diff, generates a conventional commit message via AI, and commits changes upon approval.`,
	Run: func(cmd *cobra.Command, args []string) {
		fmt.Println("🔍 Analyzing git changes...")

		if err := exec.Command("git", "rev-parse", "--is-inside-work-tree").Run(); err != nil {
			fmt.Println("❌ Error: Not inside a Git repository.")
			return
		}

		diffBytes, err := exec.Command("git", "diff", "--cached").Output()
		if err != nil {
			fmt.Printf("❌ Error running git diff: %v\n", err)
			return
		}
		diffStr := strings.TrimSpace(string(diffBytes))

		if diffStr == "" {
			diffBytes, _ = exec.Command("git", "diff").Output()
			diffStr = strings.TrimSpace(string(diffBytes))
		}

		if diffStr == "" {
			fmt.Println("ℹ️ No git changes detected. Stage or modify files first!")
			return
		}

		if len(diffStr) > 4000 {
			diffStr = diffStr[:4000] + "\n... [diff truncated]"
		}

		cfg, _ := config.LoadConfig()
		provider, err := providers.GetProvider(cfg)
		if err != nil || provider == nil {
			provider = &providers.OpenAIProvider{}
			provider.Configure(cfg)
		}

		fmt.Println("🤖 Generating AI commit message...")

		prompt := fmt.Sprintf("Generate a single-line conventional git commit message (e.g. 'feat: ...' or 'fix: ...') for the following diff. Output ONLY the commit title line and nothing else.\n\nDiff:\n%s", diffStr)

		msgs := []ai.Message{{Role: "user", Content: prompt}}
		resp, err := provider.Send(msgs)
		if err != nil {
			fmt.Printf("❌ AI generation error: %v\n", err)
			return
		}

		commitMsg := strings.TrimSpace(resp)

		commitMsg = strings.Trim(commitMsg, "`\"'")

		fmt.Println("\n=======================================================")
		fmt.Printf("✨ Suggested Commit Message:\n   %s\n", commitMsg)
		fmt.Println("=======================================================")

		fmt.Print("\nDo you want to apply this commit? [Y/n/e (edit)]: ")
		reader := bufio.NewReader(os.Stdin)
		input, _ := reader.ReadString('\n')
		input = strings.TrimSpace(strings.ToLower(input))

		if input == "e" {
			fmt.Print("Enter custom commit message: ")
			customMsg, _ := reader.ReadString('\n')
			customMsg = strings.TrimSpace(customMsg)
			if customMsg != "" {
				commitMsg = customMsg
			}
			input = "y"
		}

		if input == "" || input == "y" {

			if len(diffBytes) == 0 {
				exec.Command("git", "add", ".").Run()
			}
			cCmd := exec.Command("git", "commit", "-m", commitMsg)
			if out, err := cCmd.CombinedOutput(); err != nil {
				fmt.Printf("❌ Git commit failed: %s\n", string(out))
			} else {
				fmt.Println("✅ Successfully committed changes!")
				fmt.Println(string(out))
			}
		} else {
			fmt.Println("Cancelled commit.")
		}
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL