Skip to content

Commit 65986f4

Browse files
authored
Refactor embedded assets and drop unnecessary dependencies (#34692)
Benefits: 1. smaller binary size (reduces more than 1MB) 2. better control of the assets details 3. fewer unmaintained dependencies 4. faster startup if the assets are not needed 5. won't hang up editors when open "bindata.go" by accident
1 parent 18bafcc commit 65986f4

24 files changed

+579
-368
lines changed

.gitignore

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,10 @@ _testmain.go
4242
coverage.all
4343
cpu.out
4444

45-
/modules/migration/bindata.go
46-
/modules/migration/bindata.go.hash
47-
/modules/options/bindata.go
48-
/modules/options/bindata.go.hash
49-
/modules/public/bindata.go
50-
/modules/public/bindata.go.hash
51-
/modules/templates/bindata.go
52-
/modules/templates/bindata.go.hash
45+
/modules/migration/bindata.*
46+
/modules/options/bindata.*
47+
/modules/public/bindata.*
48+
/modules/templates/bindata.*
5349

5450
*.db
5551
*.log

Makefile

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ WEBPACK_CONFIGS := webpack.config.js tailwind.config.js
120120
WEBPACK_DEST := public/assets/js/index.js public/assets/css/index.css
121121
WEBPACK_DEST_ENTRIES := public/assets/js public/assets/css public/assets/fonts
122122

123-
BINDATA_DEST := modules/public/bindata.go modules/options/bindata.go modules/templates/bindata.go
124-
BINDATA_HASH := $(addsuffix .hash,$(BINDATA_DEST))
123+
BINDATA_DEST := modules/public/bindata.dat modules/options/bindata.dat modules/templates/bindata.dat
125124

126125
GENERATED_GO_DEST := modules/charset/invisible_gen.go modules/charset/ambiguous_gen.go
127126

@@ -149,14 +148,8 @@ SPELLCHECK_FILES := $(GO_DIRS) $(WEB_DIRS) templates options/locale/locale_en-US
149148
EDITORCONFIG_FILES := templates .github/workflows options/locale/locale_en-US.ini
150149

151150
GO_SOURCES := $(wildcard *.go)
152-
GO_SOURCES += $(shell find $(GO_DIRS) -type f -name "*.go" ! -path modules/options/bindata.go ! -path modules/public/bindata.go ! -path modules/templates/bindata.go)
151+
GO_SOURCES += $(shell find $(GO_DIRS) -type f -name "*.go")
153152
GO_SOURCES += $(GENERATED_GO_DEST)
154-
GO_SOURCES_NO_BINDATA := $(GO_SOURCES)
155-
156-
ifeq ($(filter $(TAGS_SPLIT),bindata),bindata)
157-
GO_SOURCES += $(BINDATA_DEST)
158-
GENERATED_GO_DEST += $(BINDATA_DEST)
159-
endif
160153

161154
# Force installation of playwright dependencies by setting this flag
162155
ifdef DEPS_PLAYWRIGHT
@@ -226,7 +219,7 @@ clean-all: clean ## delete backend, frontend and integration files
226219

227220
.PHONY: clean
228221
clean: ## delete backend and integration files
229-
rm -rf $(EXECUTABLE) $(DIST) $(BINDATA_DEST) $(BINDATA_HASH) \
222+
rm -rf $(EXECUTABLE) $(DIST) $(BINDATA_DEST) \
230223
integrations*.test \
231224
e2e*.test \
232225
tests/integration/gitea-integration-* \
@@ -268,7 +261,7 @@ endif
268261
.PHONY: generate-swagger
269262
generate-swagger: $(SWAGGER_SPEC) ## generate the swagger spec from code comments
270263

271-
$(SWAGGER_SPEC): $(GO_SOURCES_NO_BINDATA) $(SWAGGER_SPEC_INPUT)
264+
$(SWAGGER_SPEC): $(GO_SOURCES) $(SWAGGER_SPEC_INPUT)
272265
$(GO) run $(SWAGGER_PACKAGE) generate spec --exclude "$(SWAGGER_EXCLUDE)" --input "$(SWAGGER_SPEC_INPUT)" --output './$(SWAGGER_SPEC)'
273266

274267
.PHONY: swagger-check
@@ -373,7 +366,7 @@ lint-go-gitea-vet: ## lint go files with gitea-vet
373366
.PHONY: lint-go-gopls
374367
lint-go-gopls: ## lint go files with gopls
375368
@echo "Running gopls check..."
376-
@GO=$(GO) GOPLS_PACKAGE=$(GOPLS_PACKAGE) tools/lint-go-gopls.sh $(GO_SOURCES_NO_BINDATA)
369+
@GO=$(GO) GOPLS_PACKAGE=$(GOPLS_PACKAGE) tools/lint-go-gopls.sh $(GO_SOURCES)
377370

378371
.PHONY: lint-editorconfig
379372
lint-editorconfig:

build.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,10 @@
55

66
package main
77

8-
// Libraries that are included to vendor utilities used during build.
8+
// Libraries that are included to vendor utilities used during Makefile build.
99
// These libraries will not be included in a normal compilation.
1010

1111
import (
12-
// for embed
13-
_ "github.com/shurcooL/vfsgen"
14-
15-
// for cover merge
16-
_ "golang.org/x/tools/cover"
17-
1812
// for vet
1913
_ "code.gitea.io/gitea-vet"
20-
21-
// for swagger
22-
_ "github.com/go-swagger/go-swagger/cmd/swagger"
2314
)

build/generate-bindata.go

Lines changed: 9 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -6,87 +6,22 @@
66
package main
77

88
import (
9-
"bytes"
10-
"crypto/sha1"
119
"fmt"
12-
"log"
13-
"net/http"
1410
"os"
15-
"path/filepath"
16-
"strconv"
1711

18-
"github.com/shurcooL/vfsgen"
12+
"code.gitea.io/gitea/modules/assetfs"
1913
)
2014

21-
func needsUpdate(dir, filename string) (bool, []byte) {
22-
needRegen := false
23-
_, err := os.Stat(filename)
24-
if err != nil {
25-
needRegen = true
26-
}
27-
28-
oldHash, err := os.ReadFile(filename + ".hash")
29-
if err != nil {
30-
oldHash = []byte{}
31-
}
32-
33-
hasher := sha1.New()
34-
35-
err = filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
36-
if err != nil {
37-
return err
38-
}
39-
info, err := d.Info()
40-
if err != nil {
41-
return err
42-
}
43-
_, _ = hasher.Write([]byte(d.Name()))
44-
_, _ = hasher.Write([]byte(info.ModTime().String()))
45-
_, _ = hasher.Write([]byte(strconv.FormatInt(info.Size(), 16)))
46-
return nil
47-
})
48-
if err != nil {
49-
return true, oldHash
50-
}
51-
52-
newHash := hasher.Sum([]byte{})
53-
54-
if bytes.Compare(oldHash, newHash) != 0 {
55-
return true, newHash
56-
}
57-
58-
return needRegen, newHash
59-
}
60-
6115
func main() {
62-
if len(os.Args) < 4 {
63-
log.Fatal("Insufficient number of arguments. Need: directory packageName filename")
64-
}
65-
66-
dir, packageName, filename := os.Args[1], os.Args[2], os.Args[3]
67-
var useGlobalModTime bool
68-
if len(os.Args) == 5 {
69-
useGlobalModTime, _ = strconv.ParseBool(os.Args[4])
70-
}
71-
72-
update, newHash := needsUpdate(dir, filename)
73-
74-
if !update {
75-
fmt.Printf("bindata for %s already up-to-date\n", packageName)
76-
return
16+
if len(os.Args) != 3 {
17+
fmt.Println("usage: ./generate-bindata {local-directory} {bindata-filename}")
18+
os.Exit(1)
7719
}
7820

79-
fmt.Printf("generating bindata for %s\n", packageName)
80-
var fsTemplates http.FileSystem = http.Dir(dir)
81-
err := vfsgen.Generate(fsTemplates, vfsgen.Options{
82-
PackageName: packageName,
83-
BuildTags: "bindata",
84-
VariableName: "Assets",
85-
Filename: filename,
86-
UseGlobalModTime: useGlobalModTime,
87-
})
88-
if err != nil {
89-
log.Fatalf("%v\n", err)
21+
dir, filename := os.Args[1], os.Args[2]
22+
fmt.Printf("generating bindata for %s to %s\n", dir, filename)
23+
if err := assetfs.GenerateEmbedBindata(dir, filename); err != nil {
24+
fmt.Printf("failed: %s\n", err.Error())
25+
os.Exit(1)
9026
}
91-
_ = os.WriteFile(filename+".hash", newHash, 0o666)
9227
}

cmd/embedded.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,23 @@ func initEmbeddedExtractor(c *cli.Command) error {
118118

119119
func runList(_ context.Context, c *cli.Command) error {
120120
if err := runListDo(c); err != nil {
121-
fmt.Fprintf(os.Stderr, "%v\n", err)
121+
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
122122
return err
123123
}
124124
return nil
125125
}
126126

127127
func runView(_ context.Context, c *cli.Command) error {
128128
if err := runViewDo(c); err != nil {
129-
fmt.Fprintf(os.Stderr, "%v\n", err)
129+
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
130130
return err
131131
}
132132
return nil
133133
}
134134

135135
func runExtract(_ context.Context, c *cli.Command) error {
136136
if err := runExtractDo(c); err != nil {
137-
fmt.Fprintf(os.Stderr, "%v\n", err)
137+
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
138138
return err
139139
}
140140
return nil
@@ -217,7 +217,7 @@ func runExtractDo(c *cli.Command) error {
217217
for _, a := range matchedAssetFiles {
218218
if err := extractAsset(destdir, a, overwrite, rename); err != nil {
219219
// Non-fatal error
220-
fmt.Fprintf(os.Stderr, "%s: %v", a.path, err)
220+
_, _ = fmt.Fprintf(os.Stderr, "%s: %v\n", a.path, err)
221221
}
222222
}
223223

go.mod

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ require (
6060
github.com/go-ldap/ldap/v3 v3.4.11
6161
github.com/go-redsync/redsync/v4 v4.13.0
6262
github.com/go-sql-driver/mysql v1.9.2
63-
github.com/go-swagger/go-swagger v0.31.0
6463
github.com/go-webauthn/webauthn v0.12.3
6564
github.com/gobwas/glob v0.2.3
6665
github.com/gogs/chardet v0.0.0-20211120154057-b7413eaefb8f
@@ -105,7 +104,6 @@ require (
105104
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1
106105
github.com/sassoftware/go-rpmutils v0.4.0
107106
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3
108-
github.com/shurcooL/vfsgen v0.0.0-20230704071429-0000e147ea92
109107
github.com/stretchr/testify v1.10.0
110108
github.com/syndtr/goleveldb v1.0.0
111109
github.com/tstranex/u2f v1.0.0
@@ -126,7 +124,6 @@ require (
126124
golang.org/x/sync v0.15.0
127125
golang.org/x/sys v0.33.0
128126
golang.org/x/text v0.26.0
129-
golang.org/x/tools v0.33.0
130127
google.golang.org/grpc v1.72.0
131128
google.golang.org/protobuf v1.36.6
132129
gopkg.in/ini.v1 v1.67.0
@@ -144,15 +141,11 @@ require (
144141
git.sr.ht/~mariusor/go-xsd-duration v0.0.0-20220703122237-02e73435a078 // indirect
145142
github.com/Azure/azure-sdk-for-go/sdk/internal v1.11.1 // indirect
146143
github.com/DataDog/zstd v1.5.7 // indirect
147-
github.com/Masterminds/goutils v1.1.1 // indirect
148-
github.com/Masterminds/semver/v3 v3.3.1 // indirect
149-
github.com/Masterminds/sprig/v3 v3.3.0 // indirect
150144
github.com/Microsoft/go-winio v0.6.2 // indirect
151145
github.com/RoaringBitmap/roaring/v2 v2.4.5 // indirect
152146
github.com/andybalholm/brotli v1.1.1 // indirect
153147
github.com/andybalholm/cascadia v1.3.3 // indirect
154148
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be // indirect
155-
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
156149
github.com/aws/aws-sdk-go-v2 v1.36.3 // indirect
157150
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.34 // indirect
158151
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.34 // indirect
@@ -195,7 +188,6 @@ require (
195188
github.com/dlclark/regexp2 v1.11.5 // indirect
196189
github.com/emersion/go-sasl v0.0.0-20241020182733-b788ff22d5a6 // indirect
197190
github.com/fatih/color v1.18.0 // indirect
198-
github.com/felixge/httpsnoop v1.0.4 // indirect
199191
github.com/fxamacker/cbor/v2 v2.8.0 // indirect
200192
github.com/git-lfs/pktline v0.0.0-20230103162542-ca444d533ef1 // indirect
201193
github.com/go-ap/errors v0.0.0-20250409143711-5686c11ae650 // indirect
@@ -204,18 +196,6 @@ require (
204196
github.com/go-fed/httpsig v1.1.1-0.20201223112313-55836744818e // indirect
205197
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
206198
github.com/go-ini/ini v1.67.0 // indirect
207-
github.com/go-openapi/analysis v0.23.0 // indirect
208-
github.com/go-openapi/errors v0.22.1 // indirect
209-
github.com/go-openapi/inflect v0.21.2 // indirect
210-
github.com/go-openapi/jsonpointer v0.21.1 // indirect
211-
github.com/go-openapi/jsonreference v0.21.0 // indirect
212-
github.com/go-openapi/loads v0.22.0 // indirect
213-
github.com/go-openapi/runtime v0.28.0 // indirect
214-
github.com/go-openapi/spec v0.21.0 // indirect
215-
github.com/go-openapi/strfmt v0.23.0 // indirect
216-
github.com/go-openapi/swag v0.23.1 // indirect
217-
github.com/go-openapi/validate v0.24.0 // indirect
218-
github.com/go-viper/mapstructure/v2 v2.2.1 // indirect
219199
github.com/go-webauthn/x v0.1.20 // indirect
220200
github.com/goccy/go-json v0.10.5 // indirect
221201
github.com/golang-jwt/jwt/v4 v4.5.2 // indirect
@@ -229,20 +209,16 @@ require (
229209
github.com/google/go-querystring v1.1.0 // indirect
230210
github.com/google/go-tpm v0.9.3 // indirect
231211
github.com/gorilla/css v1.0.1 // indirect
232-
github.com/gorilla/handlers v1.5.2 // indirect
233212
github.com/gorilla/mux v1.8.1 // indirect
234213
github.com/gorilla/securecookie v1.1.2 // indirect
235214
github.com/hashicorp/errwrap v1.1.0 // indirect
236215
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
237216
github.com/hashicorp/go-multierror v1.1.1 // indirect
238217
github.com/hashicorp/go-retryablehttp v0.7.7 // indirect
239218
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
240-
github.com/jessevdk/go-flags v1.6.1 // indirect
241219
github.com/josharian/intern v1.0.0 // indirect
242220
github.com/kevinburke/ssh_config v1.2.0 // indirect
243221
github.com/klauspost/pgzip v1.2.6 // indirect
244-
github.com/kr/pretty v0.3.1 // indirect
245-
github.com/kr/text v0.2.0 // indirect
246222
github.com/libdns/libdns v1.0.0-beta.1 // indirect
247223
github.com/mailru/easyjson v0.9.0 // indirect
248224
github.com/markbates/going v1.0.3 // indirect
@@ -253,19 +229,15 @@ require (
253229
github.com/miekg/dns v1.1.65 // indirect
254230
github.com/minio/crc64nvme v1.0.1 // indirect
255231
github.com/minio/md5-simd v1.1.2 // indirect
256-
github.com/mitchellh/copystructure v1.2.0 // indirect
257232
github.com/mitchellh/mapstructure v1.5.0 // indirect
258-
github.com/mitchellh/reflectwalk v1.0.2 // indirect
259233
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
260234
github.com/modern-go/reflect2 v1.0.2 // indirect
261235
github.com/mrjones/oauth v0.0.0-20190623134757-126b35219450 // indirect
262236
github.com/mschoch/smat v0.2.0 // indirect
263237
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
264238
github.com/nwaples/rardecode v1.1.3 // indirect
265-
github.com/oklog/ulid v1.3.1 // indirect
266239
github.com/olekukonko/tablewriter v0.0.5 // indirect
267240
github.com/onsi/ginkgo v1.16.5 // indirect
268-
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
269241
github.com/pierrec/lz4/v4 v4.1.22 // indirect
270242
github.com/pjbgf/sha1cd v0.3.2 // indirect
271243
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
@@ -274,22 +246,11 @@ require (
274246
github.com/prometheus/procfs v0.16.1 // indirect
275247
github.com/rhysd/actionlint v1.7.7 // indirect
276248
github.com/rivo/uniseg v0.4.7 // indirect
277-
github.com/rogpeppe/go-internal v1.14.1 // indirect
278249
github.com/rs/xid v1.6.0 // indirect
279250
github.com/russross/blackfriday/v2 v2.1.0 // indirect
280-
github.com/sagikazarmark/locafero v0.9.0 // indirect
281-
github.com/shopspring/decimal v1.4.0 // indirect
282-
github.com/shurcooL/httpfs v0.0.0-20230704072500-f1e31cf0ba5c // indirect
283251
github.com/sirupsen/logrus v1.9.3 // indirect
284252
github.com/skeema/knownhosts v1.3.1 // indirect
285-
github.com/sourcegraph/conc v0.3.0 // indirect
286-
github.com/spf13/afero v1.14.0 // indirect
287-
github.com/spf13/cast v1.7.1 // indirect
288-
github.com/spf13/pflag v1.0.6 // indirect
289-
github.com/spf13/viper v1.20.1 // indirect
290253
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
291-
github.com/subosito/gotenv v1.6.0 // indirect
292-
github.com/toqueteos/webbrowser v1.2.0 // indirect
293254
github.com/unknwon/com v1.0.1 // indirect
294255
github.com/valyala/fastjson v1.6.4 // indirect
295256
github.com/x448/float16 v0.8.4 // indirect
@@ -300,23 +261,21 @@ require (
300261
github.com/zeebo/assert v1.3.0 // indirect
301262
github.com/zeebo/blake3 v0.2.4 // indirect
302263
go.etcd.io/bbolt v1.4.0 // indirect
303-
go.mongodb.org/mongo-driver v1.17.3 // indirect
304264
go.uber.org/atomic v1.11.0 // indirect
305265
go.uber.org/multierr v1.11.0 // indirect
306266
go.uber.org/zap v1.27.0 // indirect
307267
go.uber.org/zap/exp v0.3.0 // indirect
308268
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect
309269
golang.org/x/mod v0.25.0 // indirect
310270
golang.org/x/time v0.11.0 // indirect
271+
golang.org/x/tools v0.33.0 // indirect
311272
google.golang.org/genproto/googleapis/rpc v0.0.0-20250422160041-2d3770c4ea7f // indirect
312273
gopkg.in/warnings.v0 v0.1.2 // indirect
313274
gopkg.in/yaml.v2 v2.4.0 // indirect
314275
)
315276

316277
replace github.com/hashicorp/go-version => github.com/6543/go-version v1.3.1
317278

318-
replace github.com/shurcooL/vfsgen => github.com/lunny/vfsgen v0.0.0-20220105142115-2c99e1ffdfa0
319-
320279
replace github.com/nektos/act => gitea.com/gitea/act v0.261.6
321280

322281
// TODO: the only difference is in `PutObject`: the fork doesn't use `NewVerifyingReader(r, sha256.New(), oid, expectedSize)`, need to figure out why

0 commit comments

Comments
 (0)