Skip to content

Commit 8248fce

Browse files
authored
Merge pull request #14 from artem-y/feat/wrap-each-issue-with-prefix-and-suffix
Wrap each issue with prefix and suffix
2 parents 627fbf5 + 885448d commit 8248fce

File tree

6 files changed

+100
-41
lines changed

6 files changed

+100
-41
lines changed

.commit.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"issueRegex": "[0-9]+",
33
"outputIssuePrefix": "#",
4-
"outputIssueSuffix": ": "
4+
"outputIssueSuffix": "",
5+
"outputStringPrefix": "[",
6+
"outputStringSuffix": "]: "
57
}

Makefile

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Build the current machine
22
.PHONY: build
33
build:
4-
@go build -o bin/commit ./cmd/commit/
4+
@go build -o bin/ ./cmd/commit/
55

66
# Build for all platforms
77
.PHONY: all
@@ -10,24 +10,31 @@ all: windows linux macos
1010
# Build for MacOS
1111
.PHONY: macos
1212
macos:
13-
@GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w" -o bin/macos-amd64/commit ./cmd/commit/
14-
@GOOS=darwin GOARCH=arm64 go build -ldflags "-s -w" -o bin/macos-arm64/commit ./cmd/commit/
13+
@GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w" -o bin/macos-amd64/ ./cmd/commit/
14+
@GOOS=darwin GOARCH=arm64 go build -ldflags "-s -w" -o bin/macos-arm64/ ./cmd/commit/
1515

1616
# Build for Linux
1717
.PHONY: linux
1818
linux:
19-
@GOOS=linux GOARCH=386 go build -ldflags "-s -w" -o bin/linux-386/commit ./cmd/commit/
20-
@GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o bin/linux-amd64/commit ./cmd/commit/
21-
@GOOS=linux GOARCH=arm go build -ldflags "-s -w" -o bin/linux-arm/commit ./cmd/commit/
22-
@GOOS=linux GOARCH=arm64 go build -ldflags "-s -w" -o bin/linux-arm64/commit ./cmd/commit/
19+
@GOOS=linux GOARCH=386 go build -ldflags "-s -w" -o bin/linux-386/ ./cmd/commit/
20+
@GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -o bin/linux-amd64/ ./cmd/commit/
21+
@GOOS=linux GOARCH=arm go build -ldflags "-s -w" -o bin/linux-arm/ ./cmd/commit/
22+
@GOOS=linux GOARCH=arm64 go build -ldflags "-s -w" -o bin/linux-arm64/ ./cmd/commit/
2323

2424
# Build for Windows
2525
.PHONY: windows
2626
windows:
27-
@GOOS=windows GOARCH=386 go build -ldflags "-s -w" -o bin/windows-386/commit ./cmd/commit/
28-
@GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -o bin/windows-amd64/commit ./cmd/commit/
29-
@GOOS=windows GOARCH=arm go build -ldflags "-s -w" -o bin/windows-arm/commit ./cmd/commit/
30-
@GOOS=windows GOARCH=arm64 go build -ldflags "-s -w" -o bin/windows-arm64/commit ./cmd/commit/
27+
@GOOS=windows GOARCH=386 go build -ldflags "-s -w" -o bin/windows-386/ ./cmd/commit/
28+
@GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -o bin/windows-amd64/ ./cmd/commit/
29+
@GOOS=windows GOARCH=arm go build -ldflags "-s -w" -o bin/windows-arm/ ./cmd/commit/
30+
@GOOS=windows GOARCH=arm64 go build -ldflags "-s -w" -o bin/windows-arm64/ ./cmd/commit/
31+
32+
# Build and prepare a release archive for all platforms
33+
.PHONY: release
34+
release:
35+
@make clean
36+
@make all
37+
@zip -r bin/bin.zip bin/
3138

3239
# Clean the build artifacts
3340
.PHONY: clean

cmd/commit/main.go

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,21 @@ import (
1919

2020
func main() {
2121
var configFilePath string
22-
2322
flag.StringVar(
2423
&configFilePath,
2524
"config-path",
2625
helpers.DEFAULT_CONFIG_FILE_PATH,
2726
"Path to the config json file",
2827
)
28+
29+
var dryRun bool
30+
flag.BoolVar(
31+
&dryRun,
32+
"dry-run",
33+
false,
34+
"Prints the commit message without making the actual commit",
35+
)
36+
2937
flag.Parse()
3038

3139
commitMessage := getCommitMessage()
@@ -40,17 +48,12 @@ func main() {
4048
matches := findIssueMatchesInBranch(cfg.IssueRegex, branchName)
4149

4250
if len(matches) > 0 {
43-
joinedIssues := strings.Join(matches, ", ")
44-
commitMessage = fmt.Sprintf(
45-
"%s%s%s%s",
46-
*cfg.OutputIssuePrefix,
47-
joinedIssues,
48-
*cfg.OutputIssueSuffix,
49-
commitMessage,
50-
)
51+
commitMessage = generateCommitMessageWithMatches(matches, cfg, commitMessage)
5152
}
5253

53-
commitChanges(repo, commitMessage)
54+
if !dryRun {
55+
commitChanges(repo, commitMessage)
56+
}
5457

5558
fmt.Println(commitMessage)
5659

@@ -117,6 +120,30 @@ func findIssueMatchesInBranch(rgxRaw string, branchName string) []string {
117120
return matches
118121
}
119122

123+
// Generates a commit message with the issue number matches and config settings
124+
func generateCommitMessageWithMatches(matches []string, cfg config.CommitConfig, commitMessage string) string {
125+
mappedMatches := make([]string, len(matches))
126+
127+
for index, match := range matches {
128+
wrappedIssueNumber := fmt.Sprintf(
129+
"%s%s%s",
130+
*cfg.OutputIssuePrefix,
131+
match,
132+
*cfg.OutputIssueSuffix,
133+
)
134+
mappedMatches[index] = wrappedIssueNumber
135+
}
136+
137+
joinedIssues := strings.Join(mappedMatches, ", ")
138+
return fmt.Sprintf(
139+
"%s%s%s%s",
140+
*cfg.OutputStringPrefix,
141+
joinedIssues,
142+
*cfg.OutputStringSuffix,
143+
commitMessage,
144+
)
145+
}
146+
120147
// Creates commit options with the author information
121148
func makeCommitOptions(usr user.User) git.CommitOptions {
122149
return git.CommitOptions{
@@ -155,7 +182,7 @@ func checkStagedChanges(worktree *git.Worktree) {
155182
}
156183

157184
for _, status := range fileStatuses {
158-
if status.Staging != git.Unmodified {
185+
if status.Staging != git.Unmodified && status.Staging != git.Untracked {
159186
return
160187
}
161188
}

docs/README.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,21 @@ But this can be changed by setting different values in a `.commit.json` file at
4747
{
4848
"issueRegex": "ABC-[0-9]+",
4949
"outputIssuePrefix": "#",
50-
"outputIssueSuffix": ": "
50+
"outputIssueSuffix": "",
51+
"outputStringPrefix": "",
52+
"outputStringSuffix": ": ",
5153
}
5254
```
5355
What each setting does:
5456
- **issueRegex**: This is how the tool determines what is the pattern to look for.
55-
- **outputIssuePrefix**: Precedes the generated part of the commit message.
56-
- **outputIssueSuffix**: Follows at the end of the generated part of the commit message.
57+
- **outputIssuePrefix**: Added before each issue.
58+
- **outputIssueSuffix**: Added after each issue.
59+
- **outputStringPrefix**: Precedes the generated part of the commit message.
60+
- **outputStringSuffix**: Follows at the end of the generated part of the commit message.
5761

5862
The structure of the resulting commit message is as follows:
5963
```
60-
<outputIssuePrefix><issueRegex><outputIssueSuffix> <commit message>
64+
<outputStringPrefix><outputIssuePrefix><issueRegex><outputIssueSuffix>, <outputIssuePrefix><issueRegex><outputIssueSuffix><outputStringSuffix> <commit message>
6165
```
6266
If the `.commit.json` file is not included, the tool will just fall back to its default settings (GitHub style issues).
6367
Same will happen for any of the settings that is not included in the config json. See the default values in [constants.go](/internal/helpers/constants.go) file.
@@ -73,6 +77,11 @@ For example, the branch named `add-tests-for-CR-127-and-CR-131-features`, the is
7377
## Testing
7478
So far I haven't added a lot of unit tests for this project, but I will be doing it in the future.
7579
To run the tests, use the `make test` or `go test -v ./tests` commands.
80+
### Debug
81+
To check what commit message will be generated without making the actual commit, there is a `-dry-run` flag that can be passed to the command:
82+
```shell
83+
commit -dry-run "Not a real commit"
84+
```
7685
## Contributing
7786
Originally I wrote this tool for myself in shell and Swift and used a lot on MacOS. This repo is an attempt to make it crossplatform and an opportunity to excercise in writing Go.
7887
If you find it useful and see that something's wrong or missing, feel free to raise issues and contribute to the project.

internal/config/config.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,18 @@ import (
88
"github.com/artem-y/commit/internal/helpers"
99
)
1010

11-
type commitConfig struct {
12-
IssueRegex string `json:"issueRegex"`
13-
OutputIssuePrefix *string `json:"outputIssuePrefix"`
14-
OutputIssueSuffix *string `json:"outputIssueSuffix"`
11+
type CommitConfig struct {
12+
IssueRegex string `json:"issueRegex"`
13+
OutputIssuePrefix *string `json:"outputIssuePrefix"`
14+
OutputIssueSuffix *string `json:"outputIssueSuffix"`
15+
OutputStringPrefix *string `json:"outputStringPrefix"`
16+
OutputStringSuffix *string `json:"outputStringSuffix"`
1517
}
1618

1719
// Reads config at the file path and unmarshals it into commitConfig struct
18-
func ReadCommitConfig(configFilePath string) commitConfig {
20+
func ReadCommitConfig(configFilePath string) CommitConfig {
1921

20-
var cfg commitConfig
22+
var cfg CommitConfig
2123

2224
_, err := os.Stat(configFilePath)
2325
if err == nil {
@@ -40,13 +42,23 @@ func ReadCommitConfig(configFilePath string) commitConfig {
4042
}
4143

4244
if cfg.OutputIssuePrefix == nil {
43-
defaultPrefix := helpers.DEFAULT_OUTPUT_ISSUE_PREFIX
44-
cfg.OutputIssuePrefix = &defaultPrefix
45+
defaultIssuePrefix := helpers.DEFAULT_OUTPUT_ISSUE_PREFIX
46+
cfg.OutputIssuePrefix = &defaultIssuePrefix
4547
}
4648

4749
if cfg.OutputIssueSuffix == nil {
48-
defaultSuffix := helpers.DEFAULT_OUTPUT_ISSUE_SUFFIX
49-
cfg.OutputIssueSuffix = &defaultSuffix
50+
defaultIssueSuffix := helpers.DEFAULT_OUTPUT_ISSUE_SUFFIX
51+
cfg.OutputIssueSuffix = &defaultIssueSuffix
52+
}
53+
54+
if cfg.OutputStringPrefix == nil {
55+
defaultStringPrefix := helpers.DEFAULT_OUTPUT_STRING_PREFIX
56+
cfg.OutputStringPrefix = &defaultStringPrefix
57+
}
58+
59+
if cfg.OutputStringSuffix == nil {
60+
defaultStringSuffix := helpers.DEFAULT_OUTPUT_STRING_SUFFIX
61+
cfg.OutputStringSuffix = &defaultStringSuffix
5062
}
5163

5264
return cfg

internal/helpers/constants.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package helpers
22

33
const (
4-
DEFAULT_CONFIG_FILE_PATH = ".commit.json"
5-
DEFAULT_ISSUE_REGEX = "[0-9]+"
6-
DEFAULT_OUTPUT_ISSUE_PREFIX = "[#"
7-
DEFAULT_OUTPUT_ISSUE_SUFFIX = "]: "
4+
DEFAULT_CONFIG_FILE_PATH = ".commit.json"
5+
DEFAULT_ISSUE_REGEX = "[0-9]+"
6+
DEFAULT_OUTPUT_ISSUE_PREFIX = "#"
7+
DEFAULT_OUTPUT_ISSUE_SUFFIX = ""
8+
DEFAULT_OUTPUT_STRING_PREFIX = ""
9+
DEFAULT_OUTPUT_STRING_SUFFIX = ": "
810
)

0 commit comments

Comments
 (0)