Skip to content
29 changes: 25 additions & 4 deletions cmd/commit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ func main() {
"Path to the config json file",
)

var showConfig bool
flag.BoolVar(
&showConfig,
"show-config",
false,
"Prints out the current config",
)

var dryRun bool
flag.BoolVar(
&dryRun,
Expand All @@ -36,7 +44,6 @@ func main() {

flag.Parse()

commitMessage := getCommitMessage()
repo := openRepo()
worktree := openWorktree(repo)

Expand All @@ -47,6 +54,21 @@ func main() {
)
}

fileReader := config.FileReader{}

if showConfig {
configJSON, err := config.EncodeConfigAtPath(fileReader, configFilePath)
if err == nil {
Comment thread
artem-y marked this conversation as resolved.
fmt.Fprintf(os.Stdout, "%s\n", configJSON)
os.Exit(0)
} else {
fmt.Fprintf(os.Stderr, helpers.Red("Failed to parse config: %v\n"), err)
os.Exit(1)
}
}

commitMessage := getCommitMessage()

head, err := repo.Reference(plumbing.HEAD, false)
if err != nil {
fmt.Fprintf(os.Stderr, helpers.Red("Failed to read HEAD: %v\n"), err)
Expand All @@ -57,8 +79,8 @@ func main() {
if head.Type() == plumbing.SymbolicReference && head.Target().IsBranch() {
branchName := head.Target().Short()

fileReader := config.FileReader{}
cfg, err := config.ReadCommitConfig(fileReader, configFilePath)
isValidating := true
cfg, err := config.ReadCommitConfig(fileReader, configFilePath, isValidating)
if err != nil {
fmt.Fprintf(os.Stderr, helpers.Red("Failed to read config: %v\n"), err)
os.Exit(1)
Expand Down Expand Up @@ -131,7 +153,6 @@ func makeCommitOptions(usr user.User) git.CommitOptions {

// Commits changes with provided message
func commitChanges(repo *git.Repository, worktree *git.Worktree, commitMessage string) {

checkStagedChanges(worktree)

usr := user.GetUser(*repo)
Expand Down
5 changes: 5 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ If you don't want to include the `.commit.json` file at the root of your reposit
```shell
commit -config-path=${HOME}/.config/.commit.json "Finally fix everything"
```
### Show Current Config
To see the config that the tool will apply, use `--show-config`:
```shell
commit --show-config
```
### Multiple Issue Numbers
If the branch has multiple issues in its name, the tool will include them all, comma-separated.
For example, the branch named `add-tests-for-CR-127-and-CR-131-features`, the issue regex set to `[A-Z]{2}-[0-9]+`, and the "outputIssuePrefix" and "outputIssueSuffix" settings for the output set to `[` and `]:`, the generated commit message would start with the following:
Expand Down
41 changes: 41 additions & 0 deletions e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,46 @@ test_commit_from_another_worktree() {
pass_test $TESTNAME
}

test_show_config() {
TESTNAME="test_show_config"
start_test $TESTNAME

setup_test_repository &&\
git checkout -b feature/H22-handle-edge-cases && \

# Write a config file
echo '
{
"issueRegex": "H[0-9]+",
"outputIssuePrefix": "[",
"outputIssueSuffix": "]",
"outputStringPrefix": "",
"outputStringSuffix": " "
}
' > .commit.json && \

# Show the config using the CLI
CONFIG_OUTPUT=$(../bin/commit --show-config)

EXPECTED_CONFIG='{
"issueRegex": "H[0-9]+",
"outputIssuePrefix": "[",
"outputIssueSuffix": "]",
"outputStringPrefix": "",
"outputStringSuffix": " "
}'

if [ "$CONFIG_OUTPUT" != "$EXPECTED_CONFIG" ]; then
echo "Expected config output:"
echo "$EXPECTED_CONFIG"
echo "Actual config output:"
echo "$CONFIG_OUTPUT"
fail_test $TESTNAME
fi

pass_test $TESTNAME
}

# MARK: - Run Tests

build_if_needed
Expand All @@ -356,3 +396,4 @@ test_set_correct_author
test_use_config_with_empty_regex
test_commit_with_detached_head
test_commit_from_another_worktree
test_show_config
51 changes: 45 additions & 6 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"bytes"
"encoding/json"
"errors"
"regexp"
Expand All @@ -27,7 +28,7 @@ type commitConfigDTO struct {
}

// Reads config at the file path and unmarshals it into commitConfig struct
func ReadCommitConfig(fileReader FileReading, configFilePath string) (CommitConfig, error) {
func ReadCommitConfig(fileReader FileReading, configFilePath string, isValidating bool) (CommitConfig, error) {
var cfgDto commitConfigDTO

_, err := fileReader.Stat(configFilePath)
Expand All @@ -38,21 +39,59 @@ func ReadCommitConfig(fileReader FileReading, configFilePath string) (CommitConf
return CommitConfig{}, err
}

err = json.Unmarshal(file, &cfgDto)
if err != nil {
return CommitConfig{}, err
if len(bytes.TrimSpace(file)) > 0 {
err = json.Unmarshal(file, &cfgDto)
if err != nil {
return CommitConfig{}, err
}
} else if isValidating {
return CommitConfig{}, validateRegex("")
} else {
return CommitConfig{}, nil
}
}

cfg := makeConfig(cfgDto)

if err := validateRegex(cfg.IssueRegex); err != nil {
return CommitConfig{}, err
if isValidating {
if err := validateRegex(cfg.IssueRegex); err != nil {
return CommitConfig{}, err
}
}

return cfg, nil
}

// Encodes config at the given file path into JSON
func EncodeConfigAtPath(fileReader FileReading, configFilePath string) ([]byte, error) {
isValidating := false
cfg, err := ReadCommitConfig(fileReader, configFilePath, isValidating)
if err != nil {
return nil, err
}

cfgDto := commitConfigDTO{
IssueRegex: &cfg.IssueRegex,
OutputIssuePrefix: &cfg.OutputIssuePrefix,
OutputIssueSuffix: &cfg.OutputIssueSuffix,
OutputStringPrefix: &cfg.OutputStringPrefix,
OutputStringSuffix: &cfg.OutputStringSuffix,
}

var cfgBuffer bytes.Buffer
encoder := json.NewEncoder(&cfgBuffer)
encoder.SetIndent("", " ")
encoder.SetEscapeHTML(false)

if err := encoder.Encode(cfgDto); err != nil {
return nil, err
}

configJson := bytes.TrimSuffix(cfgBuffer.Bytes(), []byte("\n"))

return configJson, nil
}

// Helper function to create a default config
func MakeDefaultConfig() CommitConfig {
return CommitConfig{
Expand Down
Loading
Loading