Skip to content

Commit 0c7782a

Browse files
lhchavezg4s8
andauthored
Generate stringer files automatically (#841) (#866)
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 (cherry picked from commit 5e35338) Co-authored-by: Kirill <[email protected]>
1 parent 1828c9d commit 0c7782a

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
@@ -108,3 +108,26 @@ jobs:
108108
sudo ./script/build-libgit2.sh --static --system
109109
- name: Test
110110
run: go test --count=1 --tags "static,system_libgit2" ./...
111+
112+
check-generate:
113+
name: Check generated files were not modified
114+
runs-on: ubuntu-20.04
115+
steps:
116+
- name: Set up Go
117+
uses: actions/setup-go@v1
118+
with:
119+
go-version: '1.17'
120+
id: go
121+
- name: Check out code into the Go module directory
122+
uses: actions/checkout@v2
123+
- name: Install libgit2 build dependencies
124+
run: |
125+
git submodule update --init
126+
sudo apt-get install -y --no-install-recommends libssh2-1-dev
127+
go install golang.org/x/tools/cmd/stringer@latest
128+
- name: Generate files
129+
run: |
130+
export PATH=$(go env GOPATH)/bin:$PATH
131+
make generate
132+
- name: Check nothing changed
133+
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)