-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
145 lines (127 loc) · 5.88 KB
/
main.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2019 George Aristy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"math"
"os"
"strconv"
"strings"
"github.com/llorllale/go-gitlint/internal/commits"
"github.com/llorllale/go-gitlint/internal/issues"
"github.com/llorllale/go-gitlint/internal/repo"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
// @todo #9 Global variables are a code smell (especially those in filterse.go).
// They promote coupling across different components inside the same package.
// Figure out a way to remove these global variables. Whatever command line
// parser we choose should be able to auto-generate usage.
var (
path = kingpin.Flag("path", `Path to the git repo (default: ".").`).Default(".").String() //nolint:lll,gochecknoglobals // https://github.com/llorllale/go-gitlint/issues/23
subjectRegex = kingpin.Flag("subject-regex", `Commit subject line must conform to this regular expression (default: ".*").`).Default(".*").String() //nolint:lll,gochecknoglobals // https://github.com/llorllale/go-gitlint/issues/23
subjectMaxLength = kingpin.Flag("subject-maxlen", "Max length for commit subject line (default: math.MaxInt32 - 1).").Default(strconv.Itoa(math.MaxInt32 - 1)).Int() //nolint:lll,gochecknoglobals // https://github.com/llorllale/go-gitlint/issues/23
subjectMinLength = kingpin.Flag("subject-minlen", "Min length for commit subject line (default: 0).").Default("0").Int() //nolint:lll,gochecknoglobals // https://github.com/llorllale/go-gitlint/issues/23
bodyRegex = kingpin.Flag("body-regex", `Commit message body must conform to this regular expression (default: ".*").`).Default(".*").String() //nolint:lll,gochecknoglobals // https://github.com/llorllale/go-gitlint/issues/23
bodyMaxLength = kingpin.Flag("body-maxlen", `Max length for commit body (default: math.MaxInt32 - 1)`).Default(strconv.Itoa(math.MaxInt32 - 1)).Int() //nolint:lll,gochecknoglobals // https://github.com/llorllale/go-gitlint/issues/23
since = kingpin.Flag("since", `A date in "yyyy-MM-dd" format starting from which commits will be analyzed (default: "1970-01-01").`).Default("1970-01-01").String() //nolint:lll,gochecknoglobals // https://github.com/llorllale/go-gitlint/issues/23
msgFile = kingpin.Flag("msg-file", `Only analyze the commit message found in this file (default: "").`).Default("").String() //nolint:lll,gochecknoglobals // https://github.com/llorllale/go-gitlint/issues/23
maxParents = kingpin.Flag("max-parents", `Max number of parents a commit can have in order to be analyzed (default: 1). Useful for excluding merge commits.`).Default("1").Int() //nolint:lll,gochecknoglobals // https://github.com/llorllale/go-gitlint/issues/23
authorNames = kingpin.Flag("excl-author-names", "Don't lint commits with authors whose names match these comma-separated regular expressions (default: '$a').").Default("$a").String() //nolint:lll,gochecknoglobals // https://github.com/llorllale/go-gitlint/issues/23
authorEmails = kingpin.Flag("excl-author-emails", "Don't lint commits with authors whose emails match these comma-separated regular expressions (default: '$a').").Default("$a").String() //nolint:lll,gochecknoglobals // https://github.com/llorllale/go-gitlint/issues/23
)
func main() {
configure()
os.Exit(
len(
issues.Printed(
os.Stdout, "\n",
issues.Collected(
[]issues.Filter{
issues.OfSubjectRegex(*subjectRegex),
issues.OfSubjectMaxLength(*subjectMaxLength),
issues.OfSubjectMinLength(*subjectMinLength),
issues.OfBodyRegex(*bodyRegex),
issues.OfBodyMaxLength(*bodyMaxLength),
},
try(
len(*msgFile) > 0,
func() commits.Commits {
file, err := os.Open(*msgFile)
if err != nil {
panic(err)
}
return commits.MsgIn(file)
},
func() commits.Commits {
return commits.NotAuthoredByNames(
strings.Split(*authorNames, ","),
commits.NotAuthoredByEmails(
strings.Split(*authorEmails, ","),
commits.WithMaxParents(
*maxParents,
commits.Since(
*since,
commits.In(
repo.Filesystem(*path),
),
),
),
),
)
},
),
),
)(),
),
)
}
func configure() {
const file = ".gitlint"
args := os.Args[1:]
if _, err := os.Stat(file); err == nil {
config, err := kingpin.ExpandArgsFromFile(file)
if err != nil {
panic(err)
}
args = append(args, config...)
}
if _, err := kingpin.CommandLine.Parse(unique(args)); err != nil {
panic(err)
}
}
func unique(args []string) []string {
u := make([]string, 0)
flags := make([]string, 0)
for _, a := range args {
name := strings.Split(a, "=")[0]
if !contains(name, flags) {
u = append(u, a)
flags = append(flags, name)
}
}
return u
}
func contains(s string, strs []string) bool {
for _, str := range strs {
if s == str {
return true
}
}
return false
}
func try(cond bool, actual, dflt func() commits.Commits) commits.Commits {
if cond {
return actual()
}
return dflt()
}