From bc476540118d4807db0af3f42d6d2db5f377337a Mon Sep 17 00:00:00 2001 From: Tom van der Woerdt Date: Wed, 28 Aug 2024 16:34:03 +0200 Subject: [PATCH] Some cleanups on how directories are created from git git doesn't really have a concept of a directory, only files, so we have to figure them out ourselves. My previous code for that was a bit silly, this one still is. --- vcs/git.go | 10 +++++----- vcs/git_fs.go | 36 ++++++++++++++++-------------------- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/vcs/git.go b/vcs/git.go index 05604cd8..cc7eb1e8 100644 --- a/vcs/git.go +++ b/vcs/git.go @@ -38,7 +38,7 @@ func newGit(b []byte) (Driver, error) { func (g *GitDriver) Pull(dir string) (string, error) { repo, err := gogit.PlainOpen(dir) if err != nil { - return "", err + return "", fmt.Errorf("failed to open repository: %w", err) } fetchTarget := "HEAD" @@ -54,18 +54,18 @@ func (g *GitDriver) Pull(dir string) (string, error) { config.RefSpec(fmt.Sprintf("+%s:refs/heads/%s", fetchTarget, indexRef)), }, }); err != nil { - return "", err + return "", fmt.Errorf("failed to pull: %w", err) } newHead, err := repo.ResolveRevision(indexRef) if err != nil { - return "", err + return "", fmt.Errorf("failed to resolve revision: %w", err) } if !g.Bare { worktree, err := repo.Worktree() if err != nil { - return "", err + return "", fmt.Errorf("failed to reset worktree: %w", err) } worktree.Reset(&gogit.ResetOptions{ Mode: gogit.HardReset, @@ -82,7 +82,7 @@ func (g *GitDriver) Clone(dir, url string) (string, error) { URL: url, }) if err != nil { - return "", err + return "", fmt.Errorf("failed to clone %s: %w", url, err) } return g.Pull(dir) diff --git a/vcs/git_fs.go b/vcs/git_fs.go index 4808757d..9ad691aa 100644 --- a/vcs/git_fs.go +++ b/vcs/git_fs.go @@ -61,35 +61,31 @@ func (fs *gitFilesystem) Open(name string) (io.ReadCloser, error) { } func (fs *gitFilesystem) Walk(fn FileSystemWalkFunc) error { - seenDirs := make(map[string]interface{}) + seenDirs := make(map[string]bool) return fs.root.Files().ForEach(func(f *object.File) error { n := f.Name var createDirs []string - for n != "" { + if f.Mode != filemode.Dir { n = path.Dir(n) - if _, ok := seenDirs[n]; ok { - break - } + } + for n != "" && !seenDirs[n] { seenDirs[n] = true createDirs = append(createDirs, n) + n = path.Dir(n) } - if len(createDirs) > 0 { - slices.Reverse(createDirs) - for _, createDir := range createDirs { - err := fn(createDir, &gitDirinfo{createDir}, nil) - if err != nil { - return err - } + slices.Reverse(createDirs) + for _, createDir := range createDirs { + if err := fn(createDir, &gitDirinfo{n}, nil); err != nil { + return err } } - fi := &gitFileinfo{f} - if fi.IsDir() { - seenDirs[f.Name] = true + if f.Mode != filemode.Dir { + return fn(f.Name, &gitFileinfo{f}, nil) + } else { + return nil } - - return fn(f.Name, fi, nil) }) } @@ -98,7 +94,7 @@ func (fi *gitFileinfo) Name() string { } func (fi *gitFileinfo) IsDir() bool { - return fi.raw.Mode == filemode.Dir + return fi.Mode().IsDir() } func (fi *gitFileinfo) Mode() fs.FileMode { @@ -110,8 +106,8 @@ func (di *gitDirinfo) Name() string { return path.Base(di.name) } func (di *gitDirinfo) IsDir() bool { - return true + return di.Mode().IsDir() } func (di *gitDirinfo) Mode() fs.FileMode { - return fs.FileMode(0o755) + return fs.FileMode(0o755 | fs.ModeDir) }