Skip to content

Commit

Permalink
Some cleanups on how directories are created from git
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TvdW committed Aug 28, 2024
1 parent 8172ac9 commit bc47654
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
10 changes: 5 additions & 5 deletions vcs/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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,
Expand All @@ -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)
Expand Down
36 changes: 16 additions & 20 deletions vcs/git_fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

Expand All @@ -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 {
Expand All @@ -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)
}

0 comments on commit bc47654

Please sign in to comment.