mirror of
https://gitea.com/gitea/act_runner
synced 2026-05-01 01:27:56 +02:00
Use `golangci-lint fmt` to format code, upgrading `.golangci.yml` to v2 and mirroring the linter configuration used by https://github.com/go-gitea/gitea. `gci` now handles import ordering into standard, project-local, blank, and default groups. Mirrors https://github.com/go-gitea/gitea/pull/37194. Changes: - Upgrade `.golangci.yml` to v2 format with the same linter set as gitea (minus `prealloc`, `unparam`, `testifylint`, `nilnil` which produced too many pre-existing issues) - Add path-based exclusions (`bodyclose`, `gosec` in tests; `gosec:G115`/`G117` globally) - Run lint via `make lint-go` in CI instead of `golangci/golangci-lint-action`, matching the pattern used by other Gitea repos - Apply safe auto-fixes (`modernize`, `perfsprint`, `usetesting`, etc.) - Add explanations to existing `//nolint` directives - Remove dead code (unused `newRemoteReusableWorkflow` and `networkName`), duplicate imports, and shadowed `max` builtins - Replace deprecated `docker/distribution/reference` with `distribution/reference` - Fix `Deprecated:` comment casing and simplify nil/len checks --- This PR was written with the help of Claude Opus 4.7 Reviewed-on: https://gitea.com/gitea/act/pulls/163 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Co-authored-by: silverwind <me@silverwind.io> Co-committed-by: silverwind <me@silverwind.io>
173 lines
4.6 KiB
Go
173 lines
4.6 KiB
Go
package filecollector
|
|
|
|
import (
|
|
"archive/tar"
|
|
"context"
|
|
"io"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/go-git/go-billy/v5"
|
|
"github.com/go-git/go-billy/v5/memfs"
|
|
git "github.com/go-git/go-git/v5"
|
|
"github.com/go-git/go-git/v5/plumbing/cache"
|
|
"github.com/go-git/go-git/v5/plumbing/format/gitignore"
|
|
"github.com/go-git/go-git/v5/plumbing/format/index"
|
|
"github.com/go-git/go-git/v5/storage/filesystem"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
type memoryFs struct {
|
|
billy.Filesystem
|
|
}
|
|
|
|
func (mfs *memoryFs) walk(root string, fn filepath.WalkFunc) error {
|
|
dir, err := mfs.ReadDir(root)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for i := range dir {
|
|
filename := filepath.Join(root, dir[i].Name())
|
|
err = fn(filename, dir[i], nil)
|
|
if dir[i].IsDir() {
|
|
if err == filepath.SkipDir {
|
|
err = nil
|
|
} else if err := mfs.walk(filename, fn); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (mfs *memoryFs) Walk(root string, fn filepath.WalkFunc) error {
|
|
stat, err := mfs.Lstat(root)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = fn(strings.Join([]string{root, "."}, string(filepath.Separator)), stat, nil)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return mfs.walk(root, fn)
|
|
}
|
|
|
|
func (mfs *memoryFs) OpenGitIndex(path string) (*index.Index, error) {
|
|
f, _ := mfs.Filesystem.Chroot(filepath.Join(path, ".git"))
|
|
storage := filesystem.NewStorage(f, cache.NewObjectLRUDefault())
|
|
i, err := storage.Index()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return i, nil
|
|
}
|
|
|
|
func (mfs *memoryFs) Open(path string) (io.ReadCloser, error) {
|
|
return mfs.Filesystem.Open(path)
|
|
}
|
|
|
|
func (mfs *memoryFs) Readlink(path string) (string, error) {
|
|
return mfs.Filesystem.Readlink(path)
|
|
}
|
|
|
|
func TestIgnoredTrackedfile(t *testing.T) {
|
|
fs := memfs.New()
|
|
_ = fs.MkdirAll("mygitrepo/.git", 0o777)
|
|
dotgit, _ := fs.Chroot("mygitrepo/.git")
|
|
worktree, _ := fs.Chroot("mygitrepo")
|
|
repo, _ := git.Init(filesystem.NewStorage(dotgit, cache.NewObjectLRUDefault()), worktree)
|
|
f, _ := worktree.Create(".gitignore")
|
|
_, _ = f.Write([]byte(".*\n"))
|
|
f.Close()
|
|
// This file shouldn't be in the tar
|
|
f, _ = worktree.Create(".env")
|
|
_, _ = f.Write([]byte("test=val1\n"))
|
|
f.Close()
|
|
w, _ := repo.Worktree()
|
|
// .gitignore is in the tar after adding it to the index
|
|
_, _ = w.Add(".gitignore")
|
|
|
|
tmpTar, _ := fs.Create("temp.tar")
|
|
tw := tar.NewWriter(tmpTar)
|
|
ps, _ := gitignore.ReadPatterns(worktree, []string{})
|
|
ignorer := gitignore.NewMatcher(ps)
|
|
fc := &FileCollector{
|
|
Fs: &memoryFs{Filesystem: fs},
|
|
Ignorer: ignorer,
|
|
SrcPath: "mygitrepo",
|
|
SrcPrefix: "mygitrepo" + string(filepath.Separator),
|
|
Handler: &TarCollector{
|
|
TarWriter: tw,
|
|
},
|
|
}
|
|
err := fc.Fs.Walk("mygitrepo", fc.CollectFiles(context.Background(), []string{}))
|
|
assert.NoError(t, err, "successfully collect files")
|
|
tw.Close()
|
|
_, _ = tmpTar.Seek(0, io.SeekStart)
|
|
tr := tar.NewReader(tmpTar)
|
|
h, err := tr.Next()
|
|
assert.NoError(t, err, "tar must not be empty")
|
|
assert.Equal(t, ".gitignore", h.Name)
|
|
_, err = tr.Next()
|
|
assert.ErrorIs(t, err, io.EOF, "tar must only contain one element")
|
|
}
|
|
|
|
func TestSymlinks(t *testing.T) {
|
|
fs := memfs.New()
|
|
_ = fs.MkdirAll("mygitrepo/.git", 0o777)
|
|
dotgit, _ := fs.Chroot("mygitrepo/.git")
|
|
worktree, _ := fs.Chroot("mygitrepo")
|
|
repo, _ := git.Init(filesystem.NewStorage(dotgit, cache.NewObjectLRUDefault()), worktree)
|
|
// This file shouldn't be in the tar
|
|
f, err := worktree.Create(".env")
|
|
assert.NoError(t, err)
|
|
_, err = f.Write([]byte("test=val1\n"))
|
|
assert.NoError(t, err)
|
|
f.Close()
|
|
err = worktree.Symlink(".env", "test.env")
|
|
assert.NoError(t, err)
|
|
|
|
w, err := repo.Worktree()
|
|
assert.NoError(t, err)
|
|
|
|
// .gitignore is in the tar after adding it to the index
|
|
_, err = w.Add(".env")
|
|
assert.NoError(t, err)
|
|
_, err = w.Add("test.env")
|
|
assert.NoError(t, err)
|
|
|
|
tmpTar, _ := fs.Create("temp.tar")
|
|
tw := tar.NewWriter(tmpTar)
|
|
ps, _ := gitignore.ReadPatterns(worktree, []string{})
|
|
ignorer := gitignore.NewMatcher(ps)
|
|
fc := &FileCollector{
|
|
Fs: &memoryFs{Filesystem: fs},
|
|
Ignorer: ignorer,
|
|
SrcPath: "mygitrepo",
|
|
SrcPrefix: "mygitrepo" + string(filepath.Separator),
|
|
Handler: &TarCollector{
|
|
TarWriter: tw,
|
|
},
|
|
}
|
|
err = fc.Fs.Walk("mygitrepo", fc.CollectFiles(context.Background(), []string{}))
|
|
assert.NoError(t, err, "successfully collect files")
|
|
tw.Close()
|
|
_, _ = tmpTar.Seek(0, io.SeekStart)
|
|
tr := tar.NewReader(tmpTar)
|
|
h, err := tr.Next()
|
|
files := map[string]tar.Header{}
|
|
for err == nil {
|
|
files[h.Name] = *h
|
|
h, err = tr.Next()
|
|
}
|
|
|
|
assert.Equal(t, ".env", files[".env"].Name)
|
|
assert.Equal(t, "test.env", files["test.env"].Name)
|
|
assert.Equal(t, ".env", files["test.env"].Linkname)
|
|
assert.ErrorIs(t, err, io.EOF, "tar must be read cleanly to EOF")
|
|
}
|