Skip to content

Commit 4886087

Browse files
TIL-28 Properly parse the commit message
Closes #28 Signed-off-by: Chris Cummer <[email protected]>
1 parent 2e4c002 commit 4886087

File tree

2 files changed

+74
-9
lines changed

2 files changed

+74
-9
lines changed

til.go

+28-9
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const (
2525
tilConfigDir = "~/.config/til/"
2626
tilConfigFile = "config.yml"
2727

28+
defaultCommitMsg = "build, save, push"
29+
2830
defaultConfig = `---
2931
commitMessage: "build, save, push"
3032
committerEmail: [email protected]
@@ -124,7 +126,7 @@ func main() {
124126
}
125127

126128
if saveFlag {
127-
commitMsg := strings.Join(os.Args[2:], " ")
129+
commitMsg := determineCommitMessage(globalConfig, os.Args)
128130

129131
buildContent()
130132
save(commitMsg)
@@ -528,6 +530,24 @@ func createNewPage(title string) string {
528530
return filePath
529531
}
530532

533+
// determineCommitMessage figures out which commit message to save the repo with
534+
// The order of precedence is:
535+
// * message passed in via the -s flag
536+
// * message defined in config.yml for the commitMessage key
537+
// * message as a hard-coded constant, at top, in defaultCommitMsg
538+
// Example:
539+
// > til -t b -s this is message
540+
func determineCommitMessage(cfg *config.Config, args []string) string {
541+
if flag.NArg() == 0 {
542+
return cfg.UString("commitMessage", defaultCommitMsg)
543+
}
544+
545+
msgOffset := len(args) - flag.NArg()
546+
msg := strings.Join(args[msgOffset:], " ")
547+
548+
return msg
549+
}
550+
531551
// listTargetDirectories writes the list of target directories in the configuration
532552
// out to the terminal
533553
func listTargetDirectories(cfg *config.Config) {
@@ -664,19 +684,18 @@ func save(commitMsg string) {
664684
Defeat(err)
665685
}
666686

667-
defaultCommitMsg, err1 := globalConfig.String("commitMessage")
668687
defaultCommitEmail, err2 := globalConfig.String("committerEmail")
669688
defaultCommitName, err3 := globalConfig.String("committerName")
670-
if err1 != nil || err2 != nil || err3 != nil {
689+
if err2 != nil || err3 != nil {
671690
Defeat(errors.New(errConfigValueRead))
672691
}
673692

674-
if commitMsg == "" {
675-
// The incoming commitMsg is optional (if it is set, it probably came in
676-
// via command line args on -save). If it isn't set, we use the default
677-
// from the config file instead
678-
commitMsg = defaultCommitMsg
679-
}
693+
// if commitMsg == "" {
694+
// // The incoming commitMsg is optional (if it is set, it probably came in
695+
// // via command line args on -save). If it isn't set, we use the default
696+
// // from the config file instead
697+
// commitMsg = defaultCommitMsg
698+
// }
680699

681700
commit, err := w.Commit(commitMsg, &git.CommitOptions{
682701
Author: &object.Signature{

til_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,57 @@
11
package main
22

33
import (
4+
"flag"
5+
"fmt"
6+
"os"
47
"testing"
58

9+
"github.com/olebedev/config"
610
"github.com/stretchr/testify/assert"
711
)
812

13+
func Test_determineCommitMessage(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
cfgMessage string
17+
args []string
18+
expected string
19+
}{
20+
{
21+
name: "passed in via -s",
22+
cfgMessage: "from the config",
23+
args: []string{"test", "-t", "b", "-s", "this", "is", "test"},
24+
expected: "this is test",
25+
},
26+
{
27+
name: "from config file",
28+
cfgMessage: "from the config",
29+
args: []string{"test", "-t", "b", "-s"},
30+
expected: "from the config",
31+
},
32+
{
33+
name: "from default const",
34+
cfgMessage: "",
35+
args: []string{"test", "-t", "b", "-s"},
36+
expected: "build, save, push",
37+
},
38+
}
39+
40+
for _, tt := range tests {
41+
t.Run(tt.name, func(t *testing.T) {
42+
os.Args = tt.args
43+
flag.Parse()
44+
45+
msg := fmt.Sprintf("commitMessage: %s", tt.cfgMessage)
46+
cfg, _ := config.ParseYamlBytes([]byte(msg))
47+
48+
actual := determineCommitMessage(cfg, os.Args)
49+
50+
assert.Equal(t, tt.expected, actual)
51+
})
52+
}
53+
}
54+
955
/* -------------------- Configuration -------------------- */
1056

1157
func Test_getConfigPath(t *testing.T) {

0 commit comments

Comments
 (0)