Skip to content

Commit 1d669ac

Browse files
committed
refactor: move formatGitCommand to utils
Consolidate git command formatting logic into utils package for reuse.
1 parent d5b0e80 commit 1d669ac

File tree

3 files changed

+37
-33
lines changed

3 files changed

+37
-33
lines changed

cmd/actions.go

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -87,37 +87,6 @@ func (a *App) generateCommitMessage(ctx context.Context, diff string, cfg *ai.Co
8787
return msg, nil
8888
}
8989

90-
// formatGitCommand formats the git commit command for display based on message content.
91-
// Handles both single-line and multi-line commit messages.
92-
func formatGitCommand(msg string) string {
93-
lines := strings.Split(msg, "\n")
94-
nonEmptyLines := make([]string, 0, len(lines))
95-
96-
// Filter out empty lines
97-
for _, line := range lines {
98-
if trimmed := strings.TrimSpace(line); trimmed != "" {
99-
nonEmptyLines = append(nonEmptyLines, trimmed)
100-
}
101-
}
102-
103-
if len(nonEmptyLines) == 0 {
104-
return "git commit -m \"\""
105-
}
106-
107-
if len(nonEmptyLines) == 1 {
108-
return fmt.Sprintf("git commit -m \"%s\"", nonEmptyLines[0])
109-
}
110-
111-
var builder strings.Builder
112-
builder.WriteString(fmt.Sprintf("git commit -m \"%s\"", nonEmptyLines[0]))
113-
114-
for _, line := range nonEmptyLines[1:] {
115-
builder.WriteString(fmt.Sprintf(" \\\n -m \"%s\"", line))
116-
}
117-
118-
return builder.String()
119-
}
120-
12190
// CommitAction handles the generation of commit messages
12291
func (a *App) CommitAction(c *cli.Context) error {
12392
if c.NArg() > 0 {
@@ -163,7 +132,7 @@ func (a *App) CommitAction(c *cli.Context) error {
163132

164133
// Display the generated command
165134
fmt.Println("✅ Commit message generated. You can now run:")
166-
fmt.Printf(" %s\n", formatGitCommand(msg))
135+
fmt.Printf(" %s\n", utils.FormatGitCommand(msg))
167136

168137
return nil
169138
}
@@ -269,6 +238,9 @@ func (a *App) updateConfigFromFlags(cfg *config.Config, c *cli.Context) {
269238
if commitType := c.String("commit-type"); commitType != "" {
270239
cfg.CommitType = commitType
271240
}
241+
if temperature := c.Float64("temperature"); temperature != 0 {
242+
cfg.Temperature = temperature
243+
}
272244
if customConvention := c.String("custom-convention"); customConvention != "" {
273245
cfg.CustomConvention = customConvention
274246
}

internal/git/diff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func (s *gitServiceImpl) StageFiles(ctx context.Context, files []string) error {
165165
return nil
166166
}
167167

168-
cmd := exec.CommandContext(ctx, "git", append([]string{"add"}, files...)...)
168+
cmd := exec.CommandContext(ctx, "git", append([]string{"add", "--"}, files...)...)
169169
if output, err := cmd.CombinedOutput(); err != nil {
170170
return fmt.Errorf("git add failed: %s", string(output))
171171
}

pkg/utils/prompt.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,5 +64,37 @@ func getConventionInstruction(convention string) string {
6464
if convention != "" {
6565
return fmt.Sprintf("Follow custom convention: %s", convention)
6666
}
67+
6768
return "Follow Conventional Commits"
6869
}
70+
71+
// formatGitCommand formats the git commit command for display based on message content.
72+
// Handles both single-line and multi-line commit messages.
73+
func FormatGitCommand(msg string) string {
74+
lines := strings.Split(msg, "\n")
75+
nonEmptyLines := make([]string, 0, len(lines))
76+
77+
// Filter out empty lines
78+
for _, line := range lines {
79+
if trimmed := strings.TrimSpace(line); trimmed != "" {
80+
nonEmptyLines = append(nonEmptyLines, trimmed)
81+
}
82+
}
83+
84+
if len(nonEmptyLines) == 0 {
85+
return "git commit -m \"\""
86+
}
87+
88+
if len(nonEmptyLines) == 1 {
89+
return fmt.Sprintf("git commit -m \"%s\"", nonEmptyLines[0])
90+
}
91+
92+
var builder strings.Builder
93+
builder.WriteString(fmt.Sprintf("git commit -m \"%s\"", nonEmptyLines[0]))
94+
95+
for _, line := range nonEmptyLines[1:] {
96+
builder.WriteString(fmt.Sprintf(" \\\n -m \"%s\"", line))
97+
}
98+
99+
return builder.String()
100+
}

0 commit comments

Comments
 (0)