-
Notifications
You must be signed in to change notification settings - Fork 321
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
go118: Get VCS info from debug.BuildInfo #374
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2022 The Prometheus Authors | ||
// 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. | ||
|
||
//go:build !go1.18 | ||
// +build !go1.18 | ||
|
||
package version | ||
|
||
func getRevision() string { | ||
return Revision | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright 2022 The Prometheus Authors | ||
// 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. | ||
|
||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
package version | ||
|
||
import "runtime/debug" | ||
|
||
var computedRevision string | ||
|
||
func getRevision() string { | ||
if Revision != "" { | ||
return Revision | ||
} | ||
return computedRevision | ||
SuperQ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
func init() { | ||
computedRevision = computeRevision() | ||
} | ||
|
||
func computeRevision() string { | ||
var ( | ||
rev = "unknown" | ||
modified bool | ||
) | ||
|
||
buildInfo, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
return rev | ||
} | ||
for _, v := range buildInfo.Settings { | ||
if v.Key == "vcs.revision" { | ||
rev = v.Value | ||
} | ||
if v.Key == "vcs.modified" { | ||
if v.Value == "true" { | ||
modified = true | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to add |
||
} | ||
if modified { | ||
return rev + "-modified" | ||
} | ||
Comment on lines
+54
to
+56
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've seen this returned as a separate parameter, this is basically checking if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. changing the API is out of scope here I'd say, as we see revision as one. it should still be pretty easy to parse as modified is not a valid git hash |
||
return rev | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this future proof for >= 1.19?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like it is. I tested this by adding a
test_go118.go
to an existing project:Running this with Go 1.19 printed the line. Flipping it to
//go:build !go1.18
made it go away when compiled/run with Go 1.19.