MCPcopy Create free account
hub / github.com/cli/cli / walkTree

Function walkTree

internal/skills/discovery/discovery.go:882–913  ·  view source on GitHub ↗

walkTree enumerates files by fetching each tree level individually, avoiding the truncation limit of the recursive tree API. Recursion depth is bounded by maxTreeDepth to prevent unbounded API calls.

(client *api.Client, host, owner, repo, sha, prefix string, depth int)

Source from the content-addressed store, hash-verified

880// avoiding the truncation limit of the recursive tree API. Recursion
881// depth is bounded by maxTreeDepth to prevent unbounded API calls.
882func walkTree(client *api.Client, host, owner, repo, sha, prefix string, depth int) ([]SkillFile, error) {
883 if depth > maxTreeDepth {
884 return nil, fmt.Errorf("tree depth exceeds %d levels at %s", maxTreeDepth, prefix)
885 }
886 apiPath, err := safeurl.JoinPath("repos", owner, repo, "git", "trees", sha)
887 if err != nil {
888 return nil, err
889 }
890 var tree treeResponse
891 if err := client.REST(host, "GET", apiPath.String(), nil, &tree); err != nil {
892 return nil, fmt.Errorf("could not fetch tree %s: %w", prefix, err)
893 }
894
895 var files []SkillFile
896 for _, entry := range tree.Tree {
897 entryPath := entry.Path
898 if prefix != "" {
899 entryPath = prefix + "/" + entry.Path
900 }
901 switch entry.Type {
902 case "blob":
903 files = append(files, SkillFile{Path: entryPath, SHA: entry.SHA, Size: entry.Size})
904 case "tree":
905 sub, err := walkTree(client, host, owner, repo, entry.SHA, entryPath, depth+1)
906 if err != nil {
907 return nil, err
908 }
909 files = append(files, sub...)
910 }
911 }
912 return files, nil
913}
914
915// FetchBlob retrieves the content of a blob by SHA. The blob is base64-encoded
916// inside the JSON response and decoded here, so it is returned as

Callers 2

DiscoverSkillFilesFunction · 0.85
ListSkillFilesFunction · 0.85

Calls 4

JoinPathFunction · 0.92
ErrorfMethod · 0.65
RESTMethod · 0.65
StringMethod · 0.65

Tested by

no test coverage detected