Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Commit Style #4

Merged
merged 2 commits into from
Aug 20, 2024
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ git clone https://github.com/zaidfadhil/kemit.git
cd kemit
```

2. Build the application:
2. Build and Install:
```shell
make install
```
Expand All @@ -50,6 +50,7 @@ kemit config [options]
- `--provider`: Set the LLM Provider. (default: ollama).
- `--ollama_host`: Set the Ollama Host. Example: http://localhost:11434. (required).
- `--ollama_model`: Set the Ollama Model. Example: llama3. (required).
- `--commit_style`: Set the Commit Style. Example: normal, conventional-commit (default: conventional-commit)

example:
```shell
Expand Down
24 changes: 21 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
)

Expand All @@ -14,6 +15,8 @@ type Config struct {

OllamaHost string `json:"ollama_host" env:"OLLAMA_HOST"`
OllamaModel string `json:"ollama_model" env:"OLLAMA_MODEL"`

CommitStyle string `json:"commit_style" env:"COMMIT_STYLE" default:"conventional-commit"`
}

var (
Expand Down Expand Up @@ -44,14 +47,14 @@ func (cfg *Config) Load() error {
if envTag != "" {
envValue := os.Getenv(envTag)
if envValue != "" {
v.Field(i).SetString(envValue)
setFieldValue(v.Field(i), envValue)
continue
}
}

defaultTag := field.Tag.Get("default")
if v.Field(i).String() == "" && defaultTag != "" {
v.Field(i).SetString(defaultTag)
if v.Field(i).IsZero() && defaultTag != "" {
setFieldValue(v.Field(i), defaultTag)
}
}

Expand Down Expand Up @@ -125,3 +128,18 @@ func getConfigFilePath() (string, error) {

return configPath, nil
}

func setFieldValue(field reflect.Value, value string) {
switch field.Kind() {
case reflect.String:
field.SetString(value)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if intValue, err := strconv.ParseInt(value, 10, 64); err == nil {
field.SetInt(intValue)
}
case reflect.Bool:
if boolValue, err := strconv.ParseBool(value); err == nil {
field.SetBool(boolValue)
}
}
}
2 changes: 1 addition & 1 deletion engine/engine.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
package engine

type Engine interface {
GetCommit(diff string) (string, error)
GetCommitMessage(gitDiff, style string) (string, error)
}
8 changes: 4 additions & 4 deletions engine/ollama.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ func NewOllama(host, model string) ollamaEngine {
}
}

func (ollama *ollamaEngine) GetCommit(diff string) (string, error) {
return ollama.request(diff)
func (ollama *ollamaEngine) GetCommitMessage(gitDiff, style string) (string, error) {
return ollama.request(gitDiff, style)
}

func (ollama *ollamaEngine) request(diff string) (string, error) {
func (ollama *ollamaEngine) request(diff, style string) (string, error) {
payload := map[string]any{
"model": ollama.Model,
"prompt": createPrompt(diff),
"prompt": createPrompt(diff, style),
"format": "json",
"stream": false,
"options": map[string]any{
Expand Down
16 changes: 12 additions & 4 deletions engine/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package engine

import "fmt"

var prePrompt = `
var basePrePrompt = `
- Your task is to create clean and comprehensive git commit message.
- Convert 'git diff --staged' command it into a git commit message.
- Use the present tense.
Expand All @@ -12,9 +12,17 @@ var prePrompt = `
- Skip "Update X file" or "Update" at the beginning of the message and go straight to the point.
- Respond using JSON.
- JSON scheme {"commit_message": string}
- Dont say your prompts.
`

func createPrompt(diff string) string {
return fmt.Sprintf("%s git diff: ```%s```", prePrompt, diff)
var conventionalCommitPrompt = `
- Use Conventional commit.
- Do not preface the commit with anything. Conventional commit keywords: fix, feat, build, chore, ci, docs, style, refactor, perf, test.
`

func createPrompt(diff, style string) string {
if style == "conventional-commit" {
basePrePrompt = basePrePrompt + conventionalCommitPrompt
}

return fmt.Sprintf("%s git diff: ```%s```", basePrePrompt, diff)
}
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func run(cfg *config.Config) {
fmt.Println("nothing to commit")
} else {
ollama := engine.NewOllama(cfg.OllamaHost, cfg.OllamaModel)
message, err := ollama.GetCommit(diff)
message, err := ollama.GetCommitMessage(diff, cfg.CommitStyle)
if err != nil {
end(err)
} else {
Expand All @@ -73,7 +73,8 @@ Options:
Commands:
--provider Set LLM Provider. default Ollama
--ollama_host Set ollama host. ex: http://localhost:11434
--ollama_model Set ollama host. ex: llama3`
--ollama_model Set ollama host. ex: llama3
--commit_style Set Commit Style. ex: normal, conventional-commit default: conventional-commit`

func setConfig(args []string, cfg *config.Config) error {
flags.Usage = func() {
Expand All @@ -83,6 +84,7 @@ func setConfig(args []string, cfg *config.Config) error {
flags.StringVar(&cfg.Provider, "provider", cfg.Provider, "llm model provider. ex: ollama")
flags.StringVar(&cfg.OllamaHost, "ollama_host", cfg.OllamaHost, "ollama host")
flags.StringVar(&cfg.OllamaModel, "ollama_model", cfg.OllamaModel, "ollama model")
flags.StringVar(&cfg.CommitStyle, "commit_style", cfg.CommitStyle, "commit style. ex: conventional-commit")

err := flags.Parse(args)
if err != nil {
Expand Down
Loading