Skip to content

Commit

Permalink
feat: walk with node (#142)
Browse files Browse the repository at this point in the history
* feat: walk with node
  • Loading branch information
hunjixin authored Mar 16, 2024
1 parent fdff8c8 commit 899193b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 35 deletions.
4 changes: 2 additions & 2 deletions controller/object_ctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,11 @@ func (oct ObjectController) GetFiles(ctx context.Context, w *api.JiaozifsRespons
return
}

files, err := workTree.GetFiles(ctx, utils.StringValue(params.Pattern))
treeManifest, err := workTree.GetTreeManifest(ctx, utils.StringValue(params.Pattern))
if err != nil {
w.Error(err)
return
}

w.JSON(files)
w.JSON(treeManifest.FileList)
}
10 changes: 8 additions & 2 deletions versionmgr/files_walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type nodeWithPath struct {
path string
}

func (wk FileWalk) Walk(ctx context.Context, fn func(path string) error) error {
func (wk FileWalk) Walk(ctx context.Context, fn func(blob *models.Blob, path string) error) error {
cache := list.New()
cache.PushFront(nodeWithPath{wk.curNode, ""})
for {
Expand All @@ -47,7 +47,13 @@ func (wk FileWalk) Walk(ctx context.Context, fn func(path string) error) error {
if subNodes[i].IsDir {
continue
}
err := fn(path.Join(curNode.path, subNodes[i].Name))

blob, err := wk.object.Blob(ctx, subNodes[i].Hash)
if err != nil {
return err
}

err = fn(blob, path.Join(curNode.path, subNodes[i].Name))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion versionmgr/files_walk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestFileWalk_Walk(t *testing.T) {
curNode: workTree.root,
}
var paths []string
err = wk.Walk(ctx, func(path string) error {
err = wk.Walk(ctx, func(_ *models.Blob, path string) error {
fmt.Println(path)
paths = append(paths, path)
return nil
Expand Down
20 changes: 15 additions & 5 deletions versionmgr/worktree.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,23 +443,33 @@ func (workTree *WorkTree) Ls(ctx context.Context, pattern string) ([]FullTreeEnt
return workTree.getFullEntry(ctx, lastNode.Node().SubObjects)
}

func (workTree *WorkTree) GetFiles(ctx context.Context, pattern string) ([]string, error) {
type TreeManifest struct {
Size int64 `json:"size"`
FileList []string `json:"file_list"`
}

func (workTree *WorkTree) GetTreeManifest(ctx context.Context, pattern string) (TreeManifest, error) {
//todo match all files, it maybe slow maybe need a new algo like filepath.Glob
wk := FileWalk{curNode: workTree.root, object: workTree.object}
files := make([]string, 0)
g, err := glob.Compile(pattern)
if err != nil {
return files, err
return TreeManifest{}, err
}

err = wk.Walk(ctx, func(path string) error {
files := make([]string, 0)
var size int64
err = wk.Walk(ctx, func(blob *models.Blob, path string) error {
fmt.Println(path)
if g.Match(path) {
size += blob.Size
files = append(files, path)
}
return nil
})
return files, err
return TreeManifest{
Size: size,
FileList: files,
}, err
}

func (workTree *WorkTree) getFullEntry(ctx context.Context, treeEntries []models.TreeEntry) ([]FullTreeEntry, error) {
Expand Down
50 changes: 25 additions & 25 deletions versionmgr/worktree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,52 +177,52 @@ func TestWorkTreeGetFiles(t *testing.T) {
}

t.Run("all", func(t *testing.T) {
newPaths, err := workTree.GetFiles(ctx, "*")
manifest, err := workTree.GetTreeManifest(ctx, "*")
require.NoError(t, err)
require.Equal(t, 7, len(newPaths))
require.Equal(t, "a.txt", newPaths[0])
require.Equal(t, "a/b/c.txt", newPaths[1])
require.Equal(t, "a/b/d.txt", newPaths[2])
require.Equal(t, "ff/b/c.txt", newPaths[3])
require.Equal(t, "ff/b/d.txt", newPaths[4])
require.Equal(t, "ff/b/e.jpg", newPaths[5])
require.Equal(t, "ff/b/f.jpg", newPaths[6])
require.Equal(t, 7, len(manifest.FileList))
require.Equal(t, "a.txt", manifest.FileList[0])
require.Equal(t, "a/b/c.txt", manifest.FileList[1])
require.Equal(t, "a/b/d.txt", manifest.FileList[2])
require.Equal(t, "ff/b/c.txt", manifest.FileList[3])
require.Equal(t, "ff/b/d.txt", manifest.FileList[4])
require.Equal(t, "ff/b/e.jpg", manifest.FileList[5])
require.Equal(t, "ff/b/f.jpg", manifest.FileList[6])
})

t.Run("single file", func(t *testing.T) {
newPaths, err := workTree.GetFiles(ctx, "a/b/d.txt")
manifest, err := workTree.GetTreeManifest(ctx, "a/b/d.txt")
require.NoError(t, err)
require.Equal(t, 1, len(newPaths))
require.Equal(t, "a/b/d.txt", newPaths[0])
require.Equal(t, 1, len(manifest.FileList))
require.Equal(t, "a/b/d.txt", manifest.FileList[0])
})

t.Run("single path", func(t *testing.T) {
newPaths, err := workTree.GetFiles(ctx, "a")
manifest, err := workTree.GetTreeManifest(ctx, "a")
require.NoError(t, err)
require.Equal(t, 0, len(newPaths))
require.Equal(t, 0, len(manifest.FileList))
})

t.Run("ext match", func(t *testing.T) {
newPaths, err := workTree.GetFiles(ctx, "*.jpg")
manifest, err := workTree.GetTreeManifest(ctx, "*.jpg")
require.NoError(t, err)
require.Equal(t, 2, len(newPaths))
require.Equal(t, "ff/b/e.jpg", newPaths[0])
require.Equal(t, "ff/b/f.jpg", newPaths[1])
require.Equal(t, 2, len(manifest.FileList))
require.Equal(t, "ff/b/e.jpg", manifest.FileList[0])
require.Equal(t, "ff/b/f.jpg", manifest.FileList[1])
})

t.Run("filename match", func(t *testing.T) {
newPaths, err := workTree.GetFiles(ctx, "*/e.jpg")
manifest, err := workTree.GetTreeManifest(ctx, "*/e.jpg")
require.NoError(t, err)
require.Equal(t, 1, len(newPaths))
require.Equal(t, "ff/b/e.jpg", newPaths[0])
require.Equal(t, 1, len(manifest.FileList))
require.Equal(t, "ff/b/e.jpg", manifest.FileList[0])
})

t.Run("sub", func(t *testing.T) {
newPaths, err := workTree.GetFiles(ctx, "a/*")
manifest, err := workTree.GetTreeManifest(ctx, "a/*")
require.NoError(t, err)
require.Equal(t, 2, len(newPaths))
require.Equal(t, "a/b/c.txt", newPaths[0])
require.Equal(t, "a/b/d.txt", newPaths[1])
require.Equal(t, 2, len(manifest.FileList))
require.Equal(t, "a/b/c.txt", manifest.FileList[0])
require.Equal(t, "a/b/d.txt", manifest.FileList[1])
})

}

0 comments on commit 899193b

Please sign in to comment.