Skip to content

Commit f35cf24

Browse files
authored
Merge pull request #43 from github/vdye/version-cmd
Add `version` command
2 parents 88fe9de + e55fd2d commit f35cf24

File tree

9 files changed

+147
-16
lines changed

9 files changed

+147
-16
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,11 @@ jobs:
1616
with:
1717
go-version-file: 'src/git-bundle-server/go.mod'
1818

19-
# TODO: when the '-C' option is available, remove the 'cd ...'
20-
# See https://github.com/golang/go/issues/50332 for more details
2119
- name: Build
22-
run: |
23-
cd $GITHUB_WORKSPACE/src/git-bundle-server
24-
go build -v ./...
20+
run: make -C src/git-bundle-server build
2521

2622
- name: Check style
27-
run: |
28-
cd $GITHUB_WORKSPACE/src/git-bundle-server
29-
go vet ./...
23+
run: make -C src/git-bundle-server vet
3024

3125
- name: Run unit tests
32-
run: |
33-
cd $GITHUB_WORKSPACE/src/git-bundle-server
34-
go test -v ./...
26+
run: make -C src/git-bundle-server test

.github/workflows/release.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ name: Release
33
on:
44
push:
55
tags:
6-
- 'v[0-9]*.[0-9]*.[0-9]*' # matches "v<number...>.<number...>.<number>..."
6+
- 'v[0-9]+.[0-9]+.[0-9]+' # matches "v<number>.<number>.<number>"
7+
- 'v[0-9]+.[0-9]+.[0-9]+-*' # matches "v<number>.<number>.<number>-<string>"
78

89
jobs:
910
# Check prerequisites for the workflow

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/git-bundle-server
22
/git-bundle-web-server
3+
/VERSION-FILE
34
/bin/
45
/_dist/
56
/_docs/

Makefile

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
# Default target
22
build:
33

4-
# Project metadata (note: to package, VERSION *must* be set by the caller)
4+
# Project metadata
5+
# By default, the project version is automatically determined using
6+
# 'git describe'. If you would like to set the version manually, set the
7+
# 'VERSION' variable to the desired version string.
58
NAME := git-bundle-server
6-
VERSION :=
7-
PACKAGE_REVISION := 1
89

910
# Installation information
1011
INSTALL_ROOT := /
@@ -22,6 +23,7 @@ GOARCH := $(shell go env GOARCH)
2223
# Packaging information
2324
SUPPORTED_PACKAGE_GOARCHES := amd64 arm64
2425
PACKAGE_ARCH := $(GOARCH)
26+
PACKAGE_REVISION := 1
2527

2628
# Guard against environment variables
2729
APPLE_APP_IDENTITY =
@@ -30,18 +32,39 @@ APPLE_KEYCHAIN_PROFILE =
3032
E2E_FLAGS=
3133
INTEGRATION_FLAGS=
3234

35+
# General targets
36+
.PHONY: FORCE
37+
38+
ifdef VERSION
39+
# If the version is set by the user, don't bother with regenerating the version
40+
# file.
41+
.PHONY: VERSION-FILE
42+
else
43+
# If the version is not set by the user, we need to generate the version file
44+
# and load it.
45+
VERSION-FILE: FORCE
46+
@scripts/generate-version.sh --version-file="$@"
47+
-include VERSION-FILE
48+
endif
49+
3350
# Build targets
51+
LDFLAGS += -X '$(shell go list -m)/cmd/utils.Version=$(VERSION)'
52+
3453
.PHONY: build
3554
build:
3655
$(RM) -r $(BINDIR)
3756
@mkdir -p $(BINDIR)
38-
GOOS="$(GOOS)" GOARCH="$(GOARCH)" go build -o $(BINDIR) ./...
57+
GOOS="$(GOOS)" GOARCH="$(GOARCH)" go build -o $(BINDIR) -ldflags "$(LDFLAGS)" ./...
3958

4059
.PHONY: doc
4160
doc:
4261
@scripts/make-docs.sh --docs="$(CURDIR)/docs/man" \
4362
--output="$(DOCDIR)"
4463

64+
.PHONY: vet
65+
vet:
66+
GOOS="$(GOOS)" GOARCH="$(GOARCH)" go vet ./...
67+
4568
# Testing targets
4669
.PHONY: test
4770
test: build
@@ -193,6 +216,7 @@ endif
193216
.PHONY: clean
194217
clean:
195218
go clean ./...
219+
$(RM) -r VERSION-FILE
196220
$(RM) -r $(BINDIR)
197221
$(RM) -r $(DISTDIR)
198222
$(RM) -r $(DOCDIR)

cmd/git-bundle-server/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func all(logger log.TraceLogger) []argparse.Subcommand {
2121
NewUpdateCommand(logger, container),
2222
NewUpdateAllCommand(logger, container),
2323
NewListCommand(logger, container),
24+
NewVersionCommand(logger, container),
2425
NewWebServerCommand(logger, container),
2526
}
2627
}

cmd/git-bundle-server/version.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/github/git-bundle-server/cmd/utils"
8+
"github.com/github/git-bundle-server/internal/argparse"
9+
"github.com/github/git-bundle-server/internal/log"
10+
)
11+
12+
type versionCmd struct {
13+
logger log.TraceLogger
14+
container *utils.DependencyContainer
15+
}
16+
17+
func NewVersionCommand(logger log.TraceLogger, container *utils.DependencyContainer) argparse.Subcommand {
18+
return &versionCmd{
19+
logger: logger,
20+
container: container,
21+
}
22+
}
23+
24+
func (versionCmd) Name() string {
25+
return "version"
26+
}
27+
28+
func (versionCmd) Description() string {
29+
return `
30+
Display the version information for the bundle server CLI.`
31+
}
32+
33+
func (v *versionCmd) Run(ctx context.Context, args []string) error {
34+
parser := argparse.NewArgParser(v.logger, "git-bundle-server version")
35+
parser.Parse(ctx, args)
36+
37+
versionStr := utils.Version
38+
if versionStr == "" {
39+
versionStr = "<no version>"
40+
}
41+
42+
fmt.Printf("git-bundle-server version %s\n", versionStr)
43+
44+
return nil
45+
}

cmd/utils/version.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package utils
2+
3+
// The purpose of this file is to contain globally-accessible variables that
4+
// specify version information for the bundle server. The values of these
5+
// variables are set during the build process when using 'make'.
6+
7+
// The executable's version string with no leading 'v' (e.g. "1.0.0" or
8+
// "1.0.1-g1a2b3c4d").
9+
var Version string

docs/man/git-bundle-server.adoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ the server.
5353

5454
== COMMANDS
5555

56+
*version*::
57+
Display the version information for the bundle server CLI
58+
5659
*init* _url_ [_route_]::
5760
Initialize a repository for which bundles should be served. The repository is
5861
cloned into a bare repo from _url_. A base bundle is created for the

scripts/generate-version.sh

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/bin/bash
2+
die () {
3+
echo "$*" >&2
4+
exit 1
5+
}
6+
7+
write_version () {
8+
if [ -n "$1" ]; then
9+
echo "Setting VERSION to $1"
10+
else
11+
echo "No version found!"
12+
fi
13+
echo "VERSION := $1" >"$2"
14+
}
15+
16+
# Parse script arguments
17+
for i in "$@"
18+
do
19+
case "$i" in
20+
--version-file=*)
21+
VERSION_FILE="${i#*=}"
22+
shift # past argument=value
23+
;;
24+
*)
25+
die "unknown option '$i'"
26+
;;
27+
esac
28+
done
29+
30+
if [ -z "$VERSION_FILE" ]; then
31+
die "error: missing version file path"
32+
fi
33+
34+
set -e
35+
36+
# The pattern match used below is *less* restrictive than it should be (i.e.
37+
# "v<number>.<number>.<number>[-<optional string>]"), but we're limited by the
38+
# glob patterns used in 'git describe'.
39+
#
40+
# That said, we're explicitly specifying 'VERSION' for formal releases(based on
41+
# the more precise pattern match in 'release.yml', so it's not super important
42+
# to be strict here.
43+
VERSION_RAW="$(git describe --tags --match "v[0-9]*.[0-9]*.[0-9]*" 2>/dev/null || echo "")"
44+
VERSION="${VERSION_RAW:1}"
45+
46+
if [ -r "$VERSION_FILE" ]
47+
then
48+
CACHE_VERSION=$(sed -e 's/^VERSION := //' <"$VERSION_FILE")
49+
if [ "$VERSION" != "$CACHE_VERSION" ]; then
50+
write_version "$VERSION" "$VERSION_FILE"
51+
fi
52+
else
53+
# If the file doesn't exist, write unconditionally
54+
write_version "$VERSION" "$VERSION_FILE"
55+
fi

0 commit comments

Comments
 (0)