Skip to content

Commit 5e35338

Browse files
authored
Generate stringer files automatically (#841)
Added `stringer` annotations to `git.go` for `ErrorClass` and `ErrorCode`. Added `generate` rule for `Makefile` to generate string representations for these types (first building cgo files in `_obj` dir to get C constants). Finally, updated `ci` actions workflow to check that generated files are up to date. Fixes: #543
1 parent 533c82f commit 5e35338

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

.github/workflows/ci.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,26 @@ jobs:
105105
sudo ./script/build-libgit2.sh --static --system
106106
- name: Test
107107
run: go test --count=1 --tags "static,system_libgit2" ./...
108+
109+
check-generate:
110+
name: Check generated files were not modified
111+
runs-on: ubuntu-20.04
112+
steps:
113+
- name: Set up Go
114+
uses: actions/setup-go@v1
115+
with:
116+
go-version: '1.17'
117+
id: go
118+
- name: Check out code into the Go module directory
119+
uses: actions/checkout@v2
120+
- name: Install libgit2 build dependencies
121+
run: |
122+
git submodule update --init
123+
sudo apt-get install -y --no-install-recommends libssh2-1-dev
124+
go install golang.org/x/tools/cmd/stringer@latest
125+
- name: Generate files
126+
run: |
127+
export PATH=$(go env GOPATH)/bin:$PATH
128+
make generate
129+
- name: Check nothing changed
130+
run: git diff --quiet --exit-code || (echo "detected changes after generate" ; git status ; exit 1)

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ TEST_ARGS ?= --count=1
22

33
default: test
44

5+
6+
generate: static-build/install/lib/libgit2.a
7+
go generate --tags "static" ./...
8+
59
# System library
610
# ==============
711
# This uses whatever version of libgit2 can be found in the system.

git.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"unsafe"
1515
)
1616

17+
//go:generate stringer -type ErrorClass -trimprefix ErrorClass -tags static
1718
type ErrorClass int
1819

1920
const (
@@ -48,6 +49,7 @@ const (
4849
ErrorClassPatch ErrorClass = C.GIT_ERROR_PATCH
4950
)
5051

52+
//go:generate stringer -type ErrorCode -trimprefix ErrorCode -tags static
5153
type ErrorCode int
5254

5355
const (

0 commit comments

Comments
 (0)