Skip to content

Commit

Permalink
feat (msg): add nice commit msg format 🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasAugustin committed Aug 10, 2023
1 parent cb0c367 commit f059a8f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 27 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
There is already a nice [gitmoji-cli][gitmoji-cli] command line interface available.
Because I was searching for a nice project to get more into golang this project was created.

It is possible to use different commit message formats.
Per default the format is [conventional-commits[conventional-commits] with emoji
`<type>[optional scope]: :smile: <description>`


## Configuration

It is possible to configure the cli either with a `.go-gitmoji-cli.json` file within the repo directory
Expand Down Expand Up @@ -59,6 +64,7 @@ make help

[gitmoji]: https://gitmoji.dev/
[gitmoji-cli]: https://github.com/carloscuesta/gitmoji-cli
[conventional-commits]: https://www.conventionalcommits.org/en/v1.0.0/

## Contributors ✨

Expand Down
54 changes: 27 additions & 27 deletions cmd/commit.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package cmd

import (
"fmt"
"github.com/AndreasAugustin/go-gitmoji-cli/pkg"
"github.com/AndreasAugustin/go-gitmoji-cli/pkg/ui"
log "github.com/sirupsen/logrus"
"strings"

"github.com/spf13/cobra"
)

var commitMsg []string
var isDryRun bool
var scope string
var desc string
var isBreaking bool
var _type string

// CommitCmd represents the commit command
var CommitCmd = &cobra.Command{
Expand Down Expand Up @@ -45,18 +46,10 @@ to quickly create a Cobra application.`,
log.Debugf("selected gitmoji %s", selectedGitmoji)
textInputsData := buildTextInputsData()
inputsRes := ui.TextInputsRun("please add", textInputsData)
scopeAndTitle := buildTitleEventualWithScope(inputsRes)
log.Debugf("scope and title: %s ", scopeAndTitle)
var selectedEmoji string
if pkg.ConfigInstance.EmojiFormat == pkg.CODE {
selectedEmoji = selectedGitmoji.Code
} else {
selectedEmoji = selectedGitmoji.Emoji
}
completeMessage := fmt.Sprintf("%s %s", selectedEmoji, scopeAndTitle)
log.Debugf("complete message: %s", completeMessage)
msg := pkg.BuildCommitTitle(extractType(inputsRes), extractScope(inputsRes), isBreaking, extractDesc(inputsRes), selectedGitmoji, pkg.ConfigInstance)
log.Debugf("complete message: %s", msg)
if isDryRun {
log.Infof("The commit message: %s", completeMessage)
log.Infof("The commit message: %s", msg)
}
//longMessage := ui.TextAreaRun()
// TODO(anau) check if message prompt enabled
Expand All @@ -70,31 +63,38 @@ func init() {
var a []string
CommitCmd.PersistentFlags().StringSliceVarP(&commitMsg, "message", "m", a, "The commit message. Can be repeated")
CommitCmd.PersistentFlags().BoolVarP(&isDryRun, "dry-run", "n", false, "dry run: just output the commit message without doing a commit")
CommitCmd.PersistentFlags().StringVar(&_type, "type", "", "add the type")
CommitCmd.PersistentFlags().StringVar(&scope, "scope", "", "add a scope")
CommitCmd.PersistentFlags().StringVar(&desc, "desc", "", "add a description")
CommitCmd.PersistentFlags().BoolVar(&isBreaking, "is-breaking", false, "set if the commit is a breaking change")
}

func buildTitleEventualWithScope(inputsRes []string) string {
if len(inputsRes) == 0 {
return ""
} else if len(inputsRes) == 1 {
return eventualCapitalizeTitle(inputsRes[0])
func extractType(inputsRes []string) string {
return inputsRes[0]
}

func extractScope(inputsRes []string) string {
if len(inputsRes) > 2 {
return inputsRes[1]
} else {
return fmt.Sprintf("(%s) %s", inputsRes[0], eventualCapitalizeTitle(inputsRes[1]))
return scope
}
}

func eventualCapitalizeTitle(title string) string {
if pkg.ConfigInstance.CapitalizeTitle {
return strings.ToUpper(title)
func extractDesc(inputsRes []string) string {
if len(inputsRes) > 2 {
return inputsRes[2]
} else {
return inputsRes[1]
}
return title
}

func buildTextInputsData() []ui.TextInputData {
var textInputsData []ui.TextInputData
var textInputsData = []ui.TextInputData{{Placeholder: "type", Charlimit: 64, Label: "type", InitialValue: _type}}

if pkg.ConfigInstance.ScopePrompt {
textInputsData = append(textInputsData, ui.TextInputData{Placeholder: "scope", Charlimit: 64, Label: "scope"})
textInputsData = append(textInputsData, ui.TextInputData{Placeholder: "scope", Charlimit: 64, Label: "scope", InitialValue: scope})
}

return append(textInputsData, ui.TextInputData{Placeholder: "title", Charlimit: 64, Label: "title"})
return append(textInputsData, ui.TextInputData{Placeholder: "description", Charlimit: 64, Label: "description", InitialValue: desc})
}
41 changes: 41 additions & 0 deletions pkg/commit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package pkg

import (
"fmt"
"strings"
)

func BuildCommitTitle(_type string, scope string, isBreaking bool, desc string, gitmoji Gitmoji, config Config) string {

var s strings.Builder

s.WriteString(_type)

if scope != "" {
s.WriteString(fmt.Sprintf("(%s)", scope))
}

if isBreaking {
s.WriteString("!")
}

s.WriteString(fmt.Sprintf(": %s ", gitmojiToString(gitmoji, config)))
s.WriteString(eventualCapitalizeTitle(desc, config))

return s.String()
}

func gitmojiToString(gitmoji Gitmoji, config Config) string {
if config.EmojiFormat == CODE {
return gitmoji.Code
} else {
return gitmoji.Emoji
}
}

func eventualCapitalizeTitle(title string, config Config) string {
if config.CapitalizeTitle {
return strings.ToUpper(title)
}
return title
}
1 change: 1 addition & 0 deletions pkg/commit_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package pkg_test

0 comments on commit f059a8f

Please sign in to comment.