Don't store assets modified time into generated files (#18193)

This commit is contained in:
Lunny Xiao 2022-01-07 10:33:17 +08:00 committed by GitHub
parent 21ed4fd8da
commit a1c12fb0b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 110 additions and 22 deletions

View file

@ -58,11 +58,15 @@ func needsUpdate(dir string, filename string) (bool, []byte) {
}
func main() {
if len(os.Args) != 4 {
if len(os.Args) < 4 {
log.Fatal("Insufficient number of arguments. Need: directory packageName filename")
}
dir, packageName, filename := os.Args[1], os.Args[2], os.Args[3]
var useGlobalModTime bool
if len(os.Args) == 5 {
useGlobalModTime, _ = strconv.ParseBool(os.Args[4])
}
update, newHash := needsUpdate(dir, filename)
@ -74,10 +78,11 @@ func main() {
fmt.Printf("generating bindata for %s\n", packageName)
var fsTemplates http.FileSystem = http.Dir(dir)
err := vfsgen.Generate(fsTemplates, vfsgen.Options{
PackageName: packageName,
BuildTags: "bindata",
VariableName: "Assets",
Filename: filename,
PackageName: packageName,
BuildTags: "bindata",
VariableName: "Assets",
Filename: filename,
UseGlobalModTime: useGlobalModTime,
})
if err != nil {
log.Fatalf("%v\n", err)

2
go.mod
View file

@ -141,3 +141,5 @@ require (
replace github.com/hashicorp/go-version => github.com/6543/go-version v1.3.1
replace github.com/golang-jwt/jwt v3.2.1+incompatible => github.com/golang-jwt/jwt v3.2.2+incompatible
replace github.com/shurcooL/vfsgen => github.com/lunny/vfsgen v0.0.0-20220105142115-2c99e1ffdfa0

4
go.sum
View file

@ -802,6 +802,8 @@ github.com/lunny/dingtalk_webhook v0.0.0-20171025031554-e3534c89ef96 h1:uNwtsDp7
github.com/lunny/dingtalk_webhook v0.0.0-20171025031554-e3534c89ef96/go.mod h1:mmIfjCSQlGYXmJ95jFN84AkQFnVABtKuJL8IrzwvUKQ=
github.com/lunny/log v0.0.0-20160921050905-7887c61bf0de/go.mod h1:3q8WtuPQsoRbatJuy3nvq/hRSvuBJrHHr+ybPPiNvHQ=
github.com/lunny/nodb v0.0.0-20160621015157-fc1ef06ad4af/go.mod h1:Cqz6pqow14VObJ7peltM+2n3PWOz7yTrfUuGbVFkzN0=
github.com/lunny/vfsgen v0.0.0-20220105142115-2c99e1ffdfa0 h1:F/3FfGmKdiKFa8kL3YrpZ7pe9H4l4AzA1pbaOUnRvPI=
github.com/lunny/vfsgen v0.0.0-20220105142115-2c99e1ffdfa0/go.mod h1:JEfTc3+2DF9Z4PXhLLvXL42zexJyh8rIq3OzUj/0rAk=
github.com/lyft/protoc-gen-validate v0.0.13/go.mod h1:XbGvPuh87YZc5TdIa2/I4pLk0QoUACkjt2znoq26NVQ=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
@ -1044,8 +1046,6 @@ github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFR
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749 h1:bUGsEnyNbVPw06Bs80sCeARAlK8lhwqGyi6UT8ymuGk=
github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749/go.mod h1:ZY1cvUeJuFPAdZ/B6v7RHavJWZn2YPVFQ1OSXhCGOkg=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 h1:pXY9qYc/MP5zdvqWEUH6SjNiu7VhSjuVFTFiTcphaLU=
github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546/go.mod h1:TrYk7fJVaAttu97ZZKrO9UbRa8izdowaMIZcxYMbVaw=
github.com/siddontang/go v0.0.0-20180604090527-bdc77568d726/go.mod h1:3yhqj7WBBfRhbBlzyOC3gUxftwsU0u8gqevxwIHQpMw=
github.com/siddontang/go-snappy v0.0.0-20140704025258-d8f7bb82a96d/go.mod h1:vq0tzqLRu6TS7Id0wMo2N5QzJoKedVeovOpHjnykSzY=
github.com/siddontang/ledisdb v0.0.0-20190202134119-8ceb77e66a92/go.mod h1:mF1DpOSOUiJRMR+FDqaqu3EBqrybQtrDDszLUZ6oxPg=

View file

@ -7,4 +7,4 @@
package public
//go:generate go run -mod=vendor ../../build/generate-bindata.go ../../public public bindata.go
//go:generate go run -mod=vendor ../../build/generate-bindata.go ../../public public bindata.go true

View file

@ -18,8 +18,14 @@ import (
"time"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil"
)
// GlobalModTime provide a gloabl mod time for embedded asset files
func GlobalModTime(filename string) time.Time {
return timeutil.GetExecutableModTime()
}
func fileSystem(dir string) http.FileSystem {
return Assets
}

View file

@ -15,9 +15,11 @@ import (
"path/filepath"
"strings"
texttmpl "text/template"
"time"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
)
@ -26,6 +28,11 @@ var (
bodyTemplates = template.New("")
)
// GlobalModTime provide a gloabl mod time for embedded asset files
func GlobalModTime(filename string) time.Time {
return timeutil.GetExecutableModTime()
}
// GetAsset get a special asset, only for chi
func GetAsset(name string) ([]byte, error) {
bs, err := os.ReadFile(filepath.Join(setting.CustomPath, name))

View file

@ -7,4 +7,4 @@
package templates
//go:generate go run -mod=vendor ../../build/generate-bindata.go ../../templates templates bindata.go
//go:generate go run -mod=vendor ../../build/generate-bindata.go ../../templates templates bindata.go true

View file

@ -0,0 +1,49 @@
// Copyright 2022 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package timeutil
import (
"os"
"path/filepath"
"sync"
"time"
"code.gitea.io/gitea/modules/log"
)
var executablModTime = time.Now()
var executablModTimeOnce sync.Once
// GetExecutableModTime get executable file modified time of current process.
func GetExecutableModTime() time.Time {
executablModTimeOnce.Do(func() {
exePath, err := os.Executable()
if err != nil {
log.Error("os.Executable: %v", err)
return
}
exePath, err = filepath.Abs(exePath)
if err != nil {
log.Error("filepath.Abs: %v", err)
return
}
exePath, err = filepath.EvalSymlinks(exePath)
if err != nil {
log.Error("filepath.EvalSymlinks: %v", err)
return
}
st, err := os.Stat(exePath)
if err != nil {
log.Error("os.Stat: %v", err)
return
}
executablModTime = st.ModTime()
})
return executablModTime
}

View file

@ -30,7 +30,9 @@ func Generate(input http.FileSystem, opt Options) error {
return err
}
var toc toc
var toc = toc{
UseGlobalModTime: opt.UseGlobalModTime,
}
err = findAndWriteFiles(buf, input, &toc)
if err != nil {
return err
@ -56,6 +58,8 @@ type toc struct {
HasCompressedFile bool // There's at least one compressedFile.
HasFile bool // There's at least one uncompressed file.
UseGlobalModTime bool // copy from opt
}
// fileInfo is a definition of a file.
@ -64,14 +68,16 @@ type fileInfo struct {
Name string
ModTime time.Time
UncompressedSize int64
UseGlobalModTime bool
}
// dirInfo is a definition of a directory.
type dirInfo struct {
Path string
Name string
ModTime time.Time
Entries []string
Path string
Name string
ModTime time.Time
Entries []string
UseGlobalModTime bool
}
// findAndWriteFiles recursively finds all the file paths in the given directory tree.
@ -91,6 +97,7 @@ func findAndWriteFiles(buf *bytes.Buffer, fs http.FileSystem, toc *toc) error {
Name: pathpkg.Base(path),
ModTime: fi.ModTime().UTC(),
UncompressedSize: fi.Size(),
UseGlobalModTime: toc.UseGlobalModTime,
}
marker := buf.Len()
@ -125,10 +132,11 @@ func findAndWriteFiles(buf *bytes.Buffer, fs http.FileSystem, toc *toc) error {
}
dir := &dirInfo{
Path: path,
Name: pathpkg.Base(path),
ModTime: fi.ModTime().UTC(),
Entries: entries,
Path: path,
Name: pathpkg.Base(path),
ModTime: fi.ModTime().UTC(),
Entries: entries,
UseGlobalModTime: toc.UseGlobalModTime,
}
toc.dirs = append(toc.dirs, dir)
@ -242,7 +250,9 @@ var {{.VariableName}} = func() http.FileSystem {
{{define "CompressedFileInfo-Before"}} {{quote .Path}}: &vfsgen۰CompressedFileInfo{
name: {{quote .Name}},
{{if not .UseGlobalModTime}}
modTime: {{template "Time" .ModTime}},
{{end}}
uncompressedSize: {{.UncompressedSize}},
{{/* This blank line separating compressedContent is neccessary to prevent potential gofmt issues. See issue #19. */}}
compressedContent: []byte("{{end}}{{define "CompressedFileInfo-After"}}"),
@ -253,7 +263,9 @@ var {{.VariableName}} = func() http.FileSystem {
{{define "FileInfo-Before"}} {{quote .Path}}: &vfsgen۰FileInfo{
name: {{quote .Name}},
{{if not .UseGlobalModTime}}
modTime: {{template "Time" .ModTime}},
{{end}}
content: []byte("{{end}}{{define "FileInfo-After"}}"),
},
{{end}}
@ -262,7 +274,9 @@ var {{.VariableName}} = func() http.FileSystem {
{{define "DirInfo"}} {{quote .Path}}: &vfsgen۰DirInfo{
name: {{quote .Name}},
{{if not .UseGlobalModTime}}
modTime: {{template "Time" .ModTime}},
{{end}}
},
{{end}}
@ -335,7 +349,7 @@ func (f *vfsgen۰CompressedFileInfo) GzipBytes() []byte {
func (f *vfsgen۰CompressedFileInfo) Name() string { return f.name }
func (f *vfsgen۰CompressedFileInfo) Size() int64 { return f.uncompressedSize }
func (f *vfsgen۰CompressedFileInfo) Mode() os.FileMode { return 0444 }
func (f *vfsgen۰CompressedFileInfo) ModTime() time.Time { return f.modTime }
func (f *vfsgen۰CompressedFileInfo) ModTime() time.Time { return {{if .UseGlobalModTime}}GlobalModTime(f.name){{else}}f.modTime{{end}} }
func (f *vfsgen۰CompressedFileInfo) IsDir() bool { return false }
func (f *vfsgen۰CompressedFileInfo) Sys() interface{} { return nil }
@ -407,7 +421,7 @@ func (f *vfsgen۰FileInfo) NotWorthGzipCompressing() {}
func (f *vfsgen۰FileInfo) Name() string { return f.name }
func (f *vfsgen۰FileInfo) Size() int64 { return int64(len(f.content)) }
func (f *vfsgen۰FileInfo) Mode() os.FileMode { return 0444 }
func (f *vfsgen۰FileInfo) ModTime() time.Time { return f.modTime }
func (f *vfsgen۰FileInfo) ModTime() time.Time { return {{if .UseGlobalModTime}}GlobalModTime(f.name){{else}}f.modTime{{end}} }
func (f *vfsgen۰FileInfo) IsDir() bool { return false }
func (f *vfsgen۰FileInfo) Sys() interface{} { return nil }
@ -440,7 +454,7 @@ func (d *vfsgen۰DirInfo) Stat() (os.FileInfo, error) { return d, nil }
func (d *vfsgen۰DirInfo) Name() string { return d.name }
func (d *vfsgen۰DirInfo) Size() int64 { return 0 }
func (d *vfsgen۰DirInfo) Mode() os.FileMode { return 0755 | os.ModeDir }
func (d *vfsgen۰DirInfo) ModTime() time.Time { return d.modTime }
func (d *vfsgen۰DirInfo) ModTime() time.Time { return {{if .UseGlobalModTime}}GlobalModTime(d.name){{else}}d.modTime{{end}} }
func (d *vfsgen۰DirInfo) IsDir() bool { return true }
func (d *vfsgen۰DirInfo) Sys() interface{} { return nil }

View file

@ -26,6 +26,10 @@ type Options struct {
// VariableComment is the comment of the http.FileSystem variable in the generated code.
// If left empty, it defaults to "{{.VariableName}} statically implements the virtual filesystem provided to vfsgen.".
VariableComment string
// UseGlobalModTime indicates that not retrieve files' modified time if it's true. Once this
// is true, you have to define a function GlobalModTime(filename string) time.Time in the same package of generated files
UseGlobalModTime bool
}
// fillMissing sets default values for mandatory options that are left empty.

3
vendor/modules.txt vendored
View file

@ -727,7 +727,7 @@ github.com/sergi/go-diff/diffmatchpatch
# github.com/shurcooL/httpfs v0.0.0-20190707220628-8d4bc4ba7749
## explicit
github.com/shurcooL/httpfs/vfsutil
# github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546
# github.com/shurcooL/vfsgen v0.0.0-20200824052919-0d455de96546 => github.com/lunny/vfsgen v0.0.0-20220105142115-2c99e1ffdfa0
## explicit
github.com/shurcooL/vfsgen
# github.com/sirupsen/logrus v1.8.1
@ -1061,3 +1061,4 @@ xorm.io/xorm/schemas
xorm.io/xorm/tags
# github.com/hashicorp/go-version => github.com/6543/go-version v1.3.1
# github.com/golang-jwt/jwt v3.2.1+incompatible => github.com/golang-jwt/jwt v3.2.2+incompatible
# github.com/shurcooL/vfsgen => github.com/lunny/vfsgen v0.0.0-20220105142115-2c99e1ffdfa0