(ctx *snap.Context)
| 2655 | } |
| 2656 | |
| 2657 | func prepareCommit(ctx *snap.Context) (*commitPayload, error) { |
| 2658 | if err := ensureGitRepository(); err != nil { |
| 2659 | return nil, err |
| 2660 | } |
| 2661 | |
| 2662 | apiKey, err := resolveOpenAIKey(ctx.Context()) |
| 2663 | if err != nil { |
| 2664 | return nil, reportError(ctx, err) |
| 2665 | } |
| 2666 | |
| 2667 | if err := runGitCommandStreaming(ctx, "add", "."); err != nil { |
| 2668 | return nil, reportError(ctx, fmt.Errorf("git add .: %w", err)) |
| 2669 | } |
| 2670 | |
| 2671 | diffOutput, err := exec.Command("git", "diff", "--cached").CombinedOutput() |
| 2672 | if err != nil { |
| 2673 | return nil, reportError(ctx, fmt.Errorf("git diff --cached: %w", err)) |
| 2674 | } |
| 2675 | |
| 2676 | diff := string(diffOutput) |
| 2677 | if strings.TrimSpace(diff) == "" { |
| 2678 | return nil, reportError(ctx, fmt.Errorf("no staged changes to commit; stage files with git add")) |
| 2679 | } |
| 2680 | |
| 2681 | trimmedDiff, truncated := truncateDiffForCommit(diff) |
| 2682 | |
| 2683 | statusOutput, statusErr := exec.Command("git", "status", "--short").CombinedOutput() |
| 2684 | status := "" |
| 2685 | if statusErr == nil { |
| 2686 | status = string(statusOutput) |
| 2687 | } |
| 2688 | |
| 2689 | message, err := generateCommitMessage(ctx.Context(), apiKey, trimmedDiff, status, truncated) |
| 2690 | if err != nil { |
| 2691 | return nil, reportError(ctx, err) |
| 2692 | } |
| 2693 | |
| 2694 | message = strings.TrimSpace(trimMatchingQuotes(message)) |
| 2695 | if message == "" { |
| 2696 | return nil, reportError(ctx, fmt.Errorf("commit message is empty")) |
| 2697 | } |
| 2698 | paragraphs := splitCommitMessageParagraphs(message) |
| 2699 | if len(paragraphs) == 0 { |
| 2700 | return nil, reportError(ctx, fmt.Errorf("commit message is empty after formatting")) |
| 2701 | } |
| 2702 | |
| 2703 | return &commitPayload{message: message, paragraphs: paragraphs}, nil |
| 2704 | } |
| 2705 | |
| 2706 | func commitWithPayload(ctx *snap.Context, payload *commitPayload) error { |
| 2707 | args := []string{"commit"} |
no test coverage detected