Skip to content

Commit f69da39

Browse files
authored
fix: use commit hash when version is v0.0.0 (#5479)
1 parent c01a04b commit f69da39

File tree

2 files changed

+66
-17
lines changed

2 files changed

+66
-17
lines changed

pkg/commands/config_verify.go

+41-16
Original file line numberDiff line numberDiff line change
@@ -70,33 +70,58 @@ func createSchemaURL(flags *pflag.FlagSet, buildInfo BuildInfo) (string, error)
7070
return "", fmt.Errorf("parse version: %w", err)
7171
}
7272

73-
schemaURL = fmt.Sprintf("https://golangci-lint.run/jsonschema/golangci.v%d.%d.jsonschema.json",
74-
version.Segments()[0], version.Segments()[1])
73+
if version.Core().Equal(hcversion.Must(hcversion.NewVersion("v0.0.0"))) {
74+
commit, err := extractCommitHash(buildInfo)
75+
if err != nil {
76+
return "", err
77+
}
7578

76-
case buildInfo.Commit != "" && buildInfo.Commit != "?":
77-
if buildInfo.Commit == "unknown" {
78-
return "", errors.New("unknown commit information")
79+
return fmt.Sprintf("https://raw.githubusercontent.com/golangci/golangci-lint/%s/jsonschema/golangci.next.jsonschema.json",
80+
commit), nil
7981
}
8082

81-
commit := buildInfo.Commit
83+
return fmt.Sprintf("https://golangci-lint.run/jsonschema/golangci.v%d.%d.jsonschema.json",
84+
version.Segments()[0], version.Segments()[1]), nil
8285

83-
if strings.HasPrefix(commit, "(") {
84-
c, _, ok := strings.Cut(strings.TrimPrefix(commit, "("), ",")
85-
if !ok {
86-
return "", errors.New("commit information not found")
87-
}
88-
89-
commit = c
86+
case buildInfo.Commit != "" && buildInfo.Commit != "?":
87+
commit, err := extractCommitHash(buildInfo)
88+
if err != nil {
89+
return "", err
9090
}
9191

92-
schemaURL = fmt.Sprintf("https://raw.githubusercontent.com/golangci/golangci-lint/%s/jsonschema/golangci.next.jsonschema.json",
93-
commit)
92+
return fmt.Sprintf("https://raw.githubusercontent.com/golangci/golangci-lint/%s/jsonschema/golangci.next.jsonschema.json",
93+
commit), nil
9494

9595
default:
9696
return "", errors.New("version not found")
9797
}
98+
}
99+
100+
func extractCommitHash(buildInfo BuildInfo) (string, error) {
101+
if buildInfo.Commit == "" || buildInfo.Commit == "?" {
102+
return "", errors.New("empty commit information")
103+
}
104+
105+
if buildInfo.Commit == "unknown" {
106+
return "", errors.New("unknown commit information")
107+
}
108+
109+
commit := buildInfo.Commit
110+
111+
if strings.HasPrefix(commit, "(") {
112+
c, _, ok := strings.Cut(strings.TrimPrefix(commit, "("), ",")
113+
if !ok {
114+
return "", errors.New("commit information not found")
115+
}
116+
117+
commit = c
118+
}
119+
120+
if commit == "unknown" {
121+
return "", errors.New("unknown commit information")
122+
}
98123

99-
return schemaURL, nil
124+
return commit, nil
100125
}
101126

102127
func validateConfiguration(schemaPath, targetFile string) error {

pkg/commands/config_verify_test.go

+25-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,22 @@ func Test_createSchemaURL(t *testing.T) {
6060
},
6161
expected: "https://raw.githubusercontent.com/golangci/golangci-lint/cd8b11773c6c1f595e8eb98c0d4310af20ae20df/jsonschema/golangci.next.jsonschema.json",
6262
},
63+
{
64+
desc: "v0 version",
65+
info: BuildInfo{
66+
Version: "v0.0.0-20250213211019-0a603e49e5e9",
67+
Commit: `(0a603e49e5e9870f5f9f2035bcbe42cd9620a9d5, modified: "false", mod sum: "123")`,
68+
},
69+
expected: "https://raw.githubusercontent.com/golangci/golangci-lint/0a603e49e5e9870f5f9f2035bcbe42cd9620a9d5/jsonschema/golangci.next.jsonschema.json",
70+
},
71+
{
72+
desc: "dirty",
73+
info: BuildInfo{
74+
Version: "v1.64.6-0.20250225205237-3eecab1ebde9+dirty",
75+
Commit: `(3eecab1ebde9, modified: "false", mod sum: "123")`,
76+
},
77+
expected: "https://golangci-lint.run/jsonschema/golangci.v1.64.jsonschema.json",
78+
},
6379
}
6480

6581
for _, test := range testCases {
@@ -87,12 +103,20 @@ func Test_createSchemaURL_error(t *testing.T) {
87103
expected string
88104
}{
89105
{
90-
desc: "commit unknown",
106+
desc: "unknown commit",
91107
info: BuildInfo{
92108
Commit: "unknown",
93109
},
94110
expected: "unknown commit information",
95111
},
112+
{
113+
desc: "detailed unknown commit",
114+
info: BuildInfo{
115+
Version: "",
116+
Commit: `(unknown, modified: ?, mod sum: "")`,
117+
},
118+
expected: "unknown commit information",
119+
},
96120
{
97121
desc: "commit ?",
98122
info: BuildInfo{

0 commit comments

Comments
 (0)