-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsuggest.go
More file actions
112 lines (94 loc) · 2.59 KB
/
Copy pathsuggest.go
File metadata and controls
112 lines (94 loc) · 2.59 KB
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
package main
import (
"fmt"
"strings"
)
// levenshtein returns the edit distance between a and b.
func levenshtein(a, b string) int {
if len(a) == 0 {
return len(b)
}
if len(b) == 0 {
return len(a)
}
prev := make([]int, len(b)+1)
curr := make([]int, len(b)+1)
for j := 0; j <= len(b); j++ {
prev[j] = j
}
for i := 1; i <= len(a); i++ {
curr[0] = i
for j := 1; j <= len(b); j++ {
cost := 1
if a[i-1] == b[j-1] {
cost = 0
}
curr[j] = min(prev[j]+1, curr[j-1]+1, prev[j-1]+cost)
}
prev, curr = curr, prev
}
return prev[len(b)]
}
// closestMatch returns the candidate nearest to word, or "" when none is
// within a small edit distance (1 for short words, 2 otherwise).
func closestMatch(word string, candidates []string) string {
maxDist := 1
if len(word) > 4 {
maxDist = 2
}
best := ""
bestDist := maxDist + 1
for _, candidate := range candidates {
if d := levenshtein(word, candidate); d < bestDist {
best = candidate
bestDist = d
}
}
return best
}
// suggestTag proposes a corrected "TAG" or "TAG/SEVERITY" prefix for an
// unrecognized commit tag, or "" when nothing is close enough.
func (c CommitPolicyConfig) suggestTag(tag, severity string, patchTypes []string) string {
tags := []string{}
severities := []string{}
for _, pType := range patchTypes {
tags = append(tags, c.PatchTypes[pType].Values...)
if scope := c.PatchTypes[pType].Scope; scope != "" {
severities = append(severities, c.PatchScopes[scope]...)
}
}
suggestion := closestMatch(strings.ToUpper(tag), tags)
if suggestion == "" {
return ""
}
original := tag
if severity != "" {
bestSeverity := closestMatch(strings.ToUpper(severity), severities)
if bestSeverity == "" {
return ""
}
suggestion += "/" + bestSeverity
original += "/" + severity
}
if suggestion == original { // input already looks like the suggestion, nothing helpful to say
return ""
}
return suggestion
}
// suggestFromSubject handles subjects where no tag pattern matched at all,
// e.g. lowercase tags; it inspects the text before the first colon.
func (c CommitPolicyConfig) suggestFromSubject(rawSubject []byte, patchTypes []string) string {
prefix, _, found := strings.Cut(string(rawSubject), ":")
if !found || prefix == "" || strings.ContainsAny(prefix, " \t") {
return ""
}
tag, severity, _ := strings.Cut(prefix, "/")
return c.suggestTag(tag, severity, patchTypes)
}
// suggestionHint formats a suggestion for inclusion in error messages.
func suggestionHint(suggestion string) string {
if suggestion == "" {
return ""
}
return fmt.Sprintf(" (did you mean '%s'?)", suggestion)
}