Skip to content

Commit

Permalink
use new build version feature (#397)
Browse files Browse the repository at this point in the history
* use new build version feature

* add a version test

* fix path issue

* there is no path issue, output go version

* show me the output!

* fix detached head state?

* more debugging

* more debugging

* make it work in CI

* make it work in CI
  • Loading branch information
jakecoffman authored Feb 20, 2025
1 parent c6d4783 commit c149fb5
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
go-version-file: go.mod

- name: Build
run: go build -v ./...
run: go version && go build -v ./...

- name: Test
# -count=2 ensures that test fixtures cleanup after themselves
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@ Use any of the following for a pain-free installation:
```
The benefit of this method is that re-running the command will always update to the latest version.
* You can download a pre-built binary from the [releases] page.
* If you have the [`gh`][gh] command available, you can install the latest release
of `dependabot` using the following command ([gist source](https://gist.github.com/mattt/e09e1ecd76d5573e0517a7622009f06f)):
```shell
gh gist view --raw e09e1ecd76d5573e0517a7622009f06f | bash
```
* On Mac, you can run `brew install dependabot`

## Requirements

Expand Down
4 changes: 2 additions & 2 deletions cmd/dependabot/dependabot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

func TestDependabot(t *testing.T) {
err := exec.Command("go", "build", "dependabot.go").Run()
err := exec.Command("go", "build", ".").Run()
if err != nil {
t.Fatal("failed to build dependabot")
}
Expand All @@ -24,7 +24,7 @@ func TestDependabot(t *testing.T) {
engine := &script.Engine{
Conds: scripttest.DefaultConds(),
Cmds: Commands(),
Quiet: !testing.Verbose(),
Quiet: false,
}
env := []string{
"PATH=" + os.Getenv("PATH"),
Expand Down
32 changes: 7 additions & 25 deletions cmd/dependabot/internal/cmd/version.go
Original file line number Diff line number Diff line change
@@ -1,42 +1,24 @@
package cmd

import (
"fmt"
"regexp"
"log"
"runtime/debug"
)

// ldflags inserts the version here on release
var version string

var timestampRegex = regexp.MustCompile("[^a-zA-Z0-9]+")

func Version() string {
if version == "" {
version = "0.0.0-dev"
commit := ""
timestamp := ""
modified := false

info, _ := debug.ReadBuildInfo()
for _, entry := range info.Settings {
if entry.Key == "vcs.revision" && len(entry.Value) >= 7 {
commit = entry.Value[:7] // short ref
}

if entry.Key == "vcs.modified" {
modified = entry.Value == "true"
}

if entry.Key == "vcs.time" {
timestamp = timestampRegex.ReplaceAllString(entry.Value, "")
}
info, ok := debug.ReadBuildInfo()
if !ok {
log.Println("debug.ReadBuildInfo failed")
return version
}

if modified && timestamp != "" {
return fmt.Sprintf("%s+%s", version, timestamp)
} else if commit != "" {
return fmt.Sprintf("%s+%s", version, commit)
if info.Main.Version != "" {
version = info.Main.Version
}
}

Expand Down
5 changes: 5 additions & 0 deletions testdata/scripts/version.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Run the dependabot command
dependabot --version

# example: dependabot version v1.57.1-0.20250218140044-2f3fdc69ebfd
stdout 'dependabot version v\d+.\d+.\d+-(\d+\.)?\d{14}-[a-f0-9]{12}'

0 comments on commit c149fb5

Please sign in to comment.