Directly fetch directory contents (#4842)

This commit is contained in:
qwerty287
2025-02-15 21:40:35 +02:00
committed by GitHub
parent 9e1ea45418
commit 4ec9d4e285
2 changed files with 8 additions and 24 deletions

View File

@@ -20,8 +20,6 @@ import (
"errors"
"fmt"
"net/http"
"path"
"path/filepath"
"strconv"
"strings"
"time"
@@ -287,22 +285,16 @@ func (c *Forgejo) Dir(ctx context.Context, u *model.User, r *model.Repo, b *mode
return nil, err
}
// List files in repository. Path from root
tree, _, err := client.GetTrees(r.Owner, r.Name, b.Commit, true)
// List files in repository
contents, _, err := client.ListContents(r.Owner, r.Name, b.Commit, f)
if err != nil {
return nil, err
}
f = path.Clean(f) // We clean path and remove trailing slash
f += "/" + "*" // construct pattern for match i.e. file in subdir
for _, e := range tree.Entries {
// Filter path matching pattern and type file (blob)
if m, _ := filepath.Match(f, e.Path); m && e.Type == "blob" {
for _, e := range contents {
if e.Type == "file" {
data, err := c.File(ctx, u, r, b, e.Path)
if err != nil {
if errors.Is(err, &forge_types.ErrConfigNotFound{}) {
return nil, fmt.Errorf("git tree reported existence of file but we got: %s", err.Error())
}
return nil, fmt.Errorf("multi-pipeline cannot get %s: %w", e.Path, err)
}

View File

@@ -22,8 +22,6 @@ import (
"errors"
"fmt"
"net/http"
"path"
"path/filepath"
"strconv"
"strings"
"time"
@@ -289,22 +287,16 @@ func (c *Gitea) Dir(ctx context.Context, u *model.User, r *model.Repo, b *model.
return nil, err
}
// List files in repository. Path from root
tree, _, err := client.GetTrees(r.Owner, r.Name, b.Commit, true)
// List files in repository
contents, _, err := client.ListContents(r.Owner, r.Name, b.Commit, f)
if err != nil {
return nil, err
}
f = path.Clean(f) // We clean path and remove trailing slash
f += "/" + "*" // construct pattern for match i.e. file in subdir
for _, e := range tree.Entries {
// Filter path matching pattern and type file (blob)
if m, _ := filepath.Match(f, e.Path); m && e.Type == "blob" {
for _, e := range contents {
if e.Type == "file" {
data, err := c.File(ctx, u, r, b, e.Path)
if err != nil {
if errors.Is(err, &forge_types.ErrConfigNotFound{}) {
return nil, fmt.Errorf("git tree reported existence of file but we got: %s", err.Error())
}
return nil, fmt.Errorf("multi-pipeline cannot get %s: %w", e.Path, err)
}