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

Function walkTree

internal/skills/discovery/discovery.go:910–941  ·  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

908// avoiding the truncation limit of the recursive tree API. Recursion
909// depth is bounded by maxTreeDepth to prevent unbounded API calls.
910func walkTree(client *api.Client, host, owner, repo, sha, prefix string, depth int) ([]SkillFile, error) {
911 if depth > maxTreeDepth {
912 return nil, fmt.Errorf("tree depth exceeds %d levels at %s", maxTreeDepth, prefix)
913 }
914 apiPath, err := safeurl.JoinPath("repos", owner, repo, "git", "trees", sha)
915 if err != nil {
916 return nil, err
917 }
918 var tree treeResponse
919 if err := client.REST(host, "GET", apiPath.String(), nil, &tree); err != nil {
920 return nil, fmt.Errorf("could not fetch tree %s: %w", prefix, err)
921 }
922
923 var files []SkillFile
924 for _, entry := range tree.Tree {
925 entryPath := entry.Path
926 if prefix != "" {
927 entryPath = prefix + "/" + entry.Path
928 }
929 switch entry.Type {
930 case "blob":
931 files = append(files, SkillFile{Path: entryPath, SHA: entry.SHA, Size: entry.Size})
932 case "tree":
933 sub, err := walkTree(client, host, owner, repo, entry.SHA, entryPath, depth+1)
934 if err != nil {
935 return nil, err
936 }
937 files = append(files, sub...)
938 }
939 }
940 return files, nil
941}
942
943// FetchBlob retrieves the content of a blob by SHA. The blob is base64-encoded
944// 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