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
34 changes: 22 additions & 12 deletions cmd/edit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,21 @@ func TestConstructCommitMessageNoEmoji(t *testing.T) {
NoEmoji: true,
}

result := constructCommitMessage(cfg, "feat", "", "add feature")
expected := "feat: add feature"
if result != expected {
t.Errorf("Expected %q, got %q", expected, result)
}
t.Run("without scope", func(t *testing.T) {
result := constructCommitMessage(cfg, "feat", "", "add feature")
expected := "feat: add feature"
if result != expected {
t.Errorf("Expected %q, got %q", expected, result)
}
})

t.Run("with scope", func(t *testing.T) {
result := constructCommitMessage(cfg, "feat", "lang", "add Polish language")
expected := "feat(lang): add Polish language"
if result != expected {
t.Errorf("Expected %q, got %q", expected, result)
}
})
}

// TestContainsHelper tests the contains helper function
Expand Down Expand Up @@ -349,7 +359,7 @@ func TestGetRecentCommits_EdgeCases(t *testing.T) {
// Test parsing logic with malformed input
malformedOutput := "sha1\nsubject\n---COMMIT-END---\nsha2\n---COMMIT-END---"
rawCommits := strings.Split(strings.TrimSpace(malformedOutput), "---COMMIT-END---")

var commits []GitCommit
for _, rawCommit := range rawCommits {
if strings.TrimSpace(rawCommit) == "" {
Expand Down Expand Up @@ -379,7 +389,7 @@ func TestGetRecentCommits_EdgeCases(t *testing.T) {
t.Run("empty git log output", func(t *testing.T) {
emptyOutput := ""
rawCommits := strings.Split(strings.TrimSpace(emptyOutput), "---COMMIT-END---")

var commits []GitCommit
for _, rawCommit := range rawCommits {
if strings.TrimSpace(rawCommit) == "" {
Expand All @@ -406,7 +416,7 @@ func TestGetRecentCommits_EdgeCases(t *testing.T) {
t.Run("commit with body", func(t *testing.T) {
outputWithBody := "sha123\nsubject line\nbody content\n---COMMIT-END---"
rawCommits := strings.Split(strings.TrimSpace(outputWithBody), "---COMMIT-END---")

var commits []GitCommit
for _, rawCommit := range rawCommits {
if strings.TrimSpace(rawCommit) == "" {
Expand Down Expand Up @@ -436,7 +446,7 @@ func TestGetRecentCommits_EdgeCases(t *testing.T) {
t.Run("commit without body", func(t *testing.T) {
outputWithoutBody := "sha123\nsubject line\n---COMMIT-END---"
rawCommits := strings.Split(strings.TrimSpace(outputWithoutBody), "---COMMIT-END---")

var commits []GitCommit
for _, rawCommit := range rawCommits {
if strings.TrimSpace(rawCommit) == "" {
Expand Down Expand Up @@ -657,7 +667,7 @@ func TestGetRecentCommits_ErrorPaths(t *testing.T) {
// Test that commits with body are parsed correctly
output := "sha123\nsubject line\nbody content\n---COMMIT-END---"
rawCommits := strings.Split(strings.TrimSpace(output), "---COMMIT-END---")

var commits []GitCommit
for _, rawCommit := range rawCommits {
if strings.TrimSpace(rawCommit) == "" {
Expand Down Expand Up @@ -688,7 +698,7 @@ func TestGetRecentCommits_ErrorPaths(t *testing.T) {
// Test that commits without body are parsed correctly
output := "sha123\nsubject line\n---COMMIT-END---"
rawCommits := strings.Split(strings.TrimSpace(output), "---COMMIT-END---")

var commits []GitCommit
for _, rawCommit := range rawCommits {
if strings.TrimSpace(rawCommit) == "" {
Expand Down Expand Up @@ -719,7 +729,7 @@ func TestGetRecentCommits_ErrorPaths(t *testing.T) {
// Test that malformed commits are skipped
malformedOutput := "sha1\n---COMMIT-END---\nsha2\nsubject\n---COMMIT-END---"
rawCommits := strings.Split(strings.TrimSpace(malformedOutput), "---COMMIT-END---")

var commits []GitCommit
for _, rawCommit := range rawCommits {
if strings.TrimSpace(rawCommit) == "" {
Expand Down
9 changes: 7 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,12 @@ func init() {

func constructCommitMessage(cfg *config.Config, typeFlag, scopeFlag, messageFlag string) string {
typeMatch := typeFlag
emoji := ""
for _, t := range cfg.Types {
if typeFlag == t.Name {
if !cfg.NoEmoji {
typeMatch = fmt.Sprintf("%s %s", t.Name, t.Emoji)
typeMatch = t.Name
emoji = t.Emoji
break
} else {
typeMatch = t.Name
Expand All @@ -117,8 +119,11 @@ func constructCommitMessage(cfg *config.Config, typeFlag, scopeFlag, messageFlag
}

commitHeader := typeMatch
if emoji != "" {
commitHeader += " " + emoji
}
if scopeFlag != "" {
if !cfg.NoEmoji {
if emoji != "" {
commitHeader += fmt.Sprintf(" (%s)", scopeFlag)
} else {
commitHeader += fmt.Sprintf("(%s)", scopeFlag)
Expand Down
24 changes: 16 additions & 8 deletions pkg/utils/askQuestions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ func AskQuestions(config *config.Config, presetType, presetMessage string) ([]st

// Validate that Types is not empty
if len(config.Types) == 0 {
return nil, fmt.Errorf("no commit types configured. Please run 'goji init --global' or 'goji init --repo' to initialize your configuration")
return nil, fmt.Errorf(
"no commit types configured. Please run 'goji init --global' or 'goji init --repo' to initialize your configuration",
)
}

nameStyle := lipgloss.NewStyle().Width(15).Align(lipgloss.Left)
Expand All @@ -28,12 +30,13 @@ func AskQuestions(config *config.Config, presetType, presetMessage string) ([]st
nameStyle.Render(ct.Name),
emojiStyle.Render(ct.Emoji),
descStyle.Render(ct.Description))
commitTypeOptions[i] = huh.NewOption[string](row, fmt.Sprintf("%s %s", ct.Name, func() string {
if !config.NoEmoji {
return ct.Emoji
}
return ""
}()))

// Build value: "type emoji" or just "type" if NoEmoji
value := ct.Name
if !config.NoEmoji && ct.Emoji != "" {
value = fmt.Sprintf("%s %s", ct.Name, ct.Emoji)
}
commitTypeOptions[i] = huh.NewOption[string](row, value)
}

if presetType != "" {
Expand Down Expand Up @@ -99,7 +102,12 @@ func AskQuestions(config *config.Config, presetType, presetMessage string) ([]st

commitMessage := commitType
if commitScope != "" {
commitMessage += fmt.Sprintf(" (%s)", strings.TrimSpace(commitScope))
// Add space before scope if emoji is present (commitType would be "type emoji")
if !config.NoEmoji && strings.Contains(commitType, " ") {
commitMessage += fmt.Sprintf(" (%s)", strings.TrimSpace(commitScope))
} else {
commitMessage += fmt.Sprintf("(%s)", strings.TrimSpace(commitScope))
}
}
commitMessage += fmt.Sprintf(": %s", strings.TrimSpace(commitSubject))

Expand Down
Loading