From f633706986ffe58471b3453531f81a08ecdabfd4 Mon Sep 17 00:00:00 2001 From: Kirill Date: Thu, 14 Oct 2021 18:02:04 +0300 Subject: [PATCH 1/6] Generate stringer files automatically 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. Ticket: #543 --- .github/workflows/ci.yml | 16 ++++++++++++++++ Makefile | 14 ++++++++++++++ git.go | 2 ++ 3 files changed, 32 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 50e5e14c5..0edadb20a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -105,3 +105,19 @@ jobs: sudo ./script/build-libgit2.sh --static --system - name: Test run: go test --count=1 --tags "static,system_libgit2" ./... + + check-generate: + name: Check generated files were not modified + runs-on: ubuntu-20.04 + steps: + - name: Set up Go + uses: actions/setup-go@v1 + with: + go-version: '1.17' + id: go + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + - name: Generate files + run: make generate + - name: Check nothing changed + run: git diff --quiet --exit-code || (echo "detected changes after generate" && exit 1) diff --git a/Makefile b/Makefile index 84262f4da..f887648f4 100644 --- a/Makefile +++ b/Makefile @@ -53,3 +53,17 @@ test-static: static-build/install/lib/libgit2.a install-static: static-build/install/lib/libgit2.a go install --tags "static" ./... + +define _gen_file + rm -fr _obj + go tool cgo $(1) + find ./_obj -type f ! -name '*.go' -exec rm {} \; + mv ./_obj/_cgo_gotypes.go ./_obj/cgo_gotypes.go + cd ./_obj && go generate ./... + find ./_obj -type f -name '*_string.go' -exec mv -v {} . \; + rm -fr ./_obj +endef + +generate: + $(call _gen_file,diff.go) + $(call _gen_file,git.go) diff --git a/git.go b/git.go index 383c49261..b7c8b3c7d 100644 --- a/git.go +++ b/git.go @@ -14,6 +14,7 @@ import ( "unsafe" ) +//go:generate stringer -type ErrorClass -trimprefix ErrorClass -tags static type ErrorClass int const ( @@ -48,6 +49,7 @@ const ( ErrorClassPatch ErrorClass = C.GIT_ERROR_PATCH ) +//go:generate stringer -type ErrorCode -trimprefix ErrorCode -tags static type ErrorCode int const ( From 67bd1b00f668198e1527331c3c3a8bfc3e9de350 Mon Sep 17 00:00:00 2001 From: Kirill Date: Mon, 8 Nov 2021 11:19:55 +0300 Subject: [PATCH 2/6] Show status on stringer check Apply review comments about stringer workflow steps Co-authored-by: lhchavez --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0edadb20a..8a8adf3a7 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -120,4 +120,4 @@ jobs: - name: Generate files run: make generate - name: Check nothing changed - run: git diff --quiet --exit-code || (echo "detected changes after generate" && exit 1) + run: git diff --quiet --exit-code || (echo "detected changes after generate" ; git status ; exit 1) From 0cbbc1dc99237cde8931b33eb5be85213eb4a235 Mon Sep 17 00:00:00 2001 From: Kirill Date: Mon, 8 Nov 2021 17:08:10 +0300 Subject: [PATCH 3/6] Update .github/workflows/ci.yml Review fixes for workflow Co-authored-by: lhchavez --- .github/workflows/ci.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8a8adf3a7..a929f93a9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -117,6 +117,10 @@ jobs: id: go - name: Check out code into the Go module directory uses: actions/checkout@v2 + - name: Install libgit2 build dependencies + run: | + git submodule update --init + sudo apt-get install -y --no-install-recommends libssh2-1-dev - name: Generate files run: make generate - name: Check nothing changed From 0e14c5c3cde537620ee5b340bab205cfa5b9edfb Mon Sep 17 00:00:00 2001 From: Kirill Date: Mon, 8 Nov 2021 17:09:24 +0300 Subject: [PATCH 4/6] Fixed generate makefile target --- Makefile | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index f887648f4..71c5ee069 100644 --- a/Makefile +++ b/Makefile @@ -2,6 +2,10 @@ TEST_ARGS ?= --count=1 default: test + +generate: static-build/install/lib/libgit2.a + go generate --tags "static" ./... + # System library # ============== # This uses whatever version of libgit2 can be found in the system. @@ -53,17 +57,3 @@ test-static: static-build/install/lib/libgit2.a install-static: static-build/install/lib/libgit2.a go install --tags "static" ./... - -define _gen_file - rm -fr _obj - go tool cgo $(1) - find ./_obj -type f ! -name '*.go' -exec rm {} \; - mv ./_obj/_cgo_gotypes.go ./_obj/cgo_gotypes.go - cd ./_obj && go generate ./... - find ./_obj -type f -name '*_string.go' -exec mv -v {} . \; - rm -fr ./_obj -endef - -generate: - $(call _gen_file,diff.go) - $(call _gen_file,git.go) From a64e4a1a75ab043f0578ea14922cdc4fd1ba6196 Mon Sep 17 00:00:00 2001 From: Kirill Date: Mon, 8 Nov 2021 17:29:12 +0300 Subject: [PATCH 5/6] Update .github/workflows/ci.yml Co-authored-by: lhchavez --- .github/workflows/ci.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index a929f93a9..c613e3c72 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -121,6 +121,7 @@ jobs: run: | git submodule update --init sudo apt-get install -y --no-install-recommends libssh2-1-dev + go install golang.org/x/tools/cmd/stringer@latest - name: Generate files run: make generate - name: Check nothing changed From 9c737a6d50a84f16b4387ddd109ef0fbf4a49b5c Mon Sep 17 00:00:00 2001 From: Kirill Date: Mon, 8 Nov 2021 17:34:55 +0300 Subject: [PATCH 6/6] ci: export GOPATH --- .github/workflows/ci.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c613e3c72..53f070562 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -123,6 +123,8 @@ jobs: sudo apt-get install -y --no-install-recommends libssh2-1-dev go install golang.org/x/tools/cmd/stringer@latest - name: Generate files - run: make generate + run: | + export PATH=$(go env GOPATH)/bin:$PATH + make generate - name: Check nothing changed run: git diff --quiet --exit-code || (echo "detected changes after generate" ; git status ; exit 1)