/
check_range_commit.go
75 lines (54 loc) · 1.37 KB
/
check_range_commit.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package cmd
import (
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/antham/gommit/gommit"
)
// checkRangeCommitCmd represents the check command
var checkRangeCommitCmd = &cobra.Command{
Use: "range [commitFrom] [commitTo] [&path]",
Short: "Check messages in commit range",
Run: func(cmd *cobra.Command, args []string) {
err := validateFileConfig()
if err != nil {
failure(err)
exitError()
}
from, to, path, err := extractCheckRangeCommitArgs(args)
if err != nil {
failure(err)
exitError()
}
q := gommit.RangeCommitQuery{
Path: path,
From: from,
To: to,
Matchers: viper.GetStringMapString("matchers"),
Options: buildOptions(),
}
matchings, err := gommit.MatchRangeCommitQuery(q)
processMatchResult(matchings, err, viper.GetStringMapString("examples"))
},
}
func extractCheckRangeCommitArgs(args []string) (string, string, string, error) {
if len(args) < 2 {
return "", "", "", fmt.Errorf("Two arguments required : origin commit and end commit")
}
if len(args) > 3 {
return "", "", "", fmt.Errorf("3 arguments must be provided at most")
}
var path string
var err error
if len(args) == 3 {
path = args[2]
}
path, err = parseDirectory(path)
if err != nil {
return "", "", "", err
}
return args[0], args[1], path, nil
}
func init() {
checkCmd.AddCommand(checkRangeCommitCmd)
}