Skip to content

Commit 3269af4

Browse files
committed
add contingency to match path in git tree
1 parent 3730b84 commit 3269af4

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

pkg/github/repositories.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,31 @@ func GetFileContents(getClient GetClientFn, getRawClient raw.GetRawClientFn, t t
610610
}
611611
return mcp.NewToolResultText(string(r)), nil
612612
}
613+
614+
// The path does not point to a file or directory.
615+
// Instead let's try to find it in the Git Tree by matching the end of the path.
616+
617+
// Step 1: Get Git Tree recursively
618+
tree, resp, err := client.Git.GetTree(ctx, owner, repo, ref, true)
619+
if err != nil {
620+
return ghErrors.NewGitHubAPIErrorResponse(ctx,
621+
"failed to get git tree",
622+
resp,
623+
err,
624+
), nil
625+
}
626+
627+
// Step 2: Filter tree for matching paths
628+
const maxMatchingFiles = 3
629+
matchingFiles := filterPaths(tree.Entries, path, maxMatchingFiles)
630+
if len(matchingFiles) > 0 {
631+
matchingFilesJSON, err := json.Marshal(matchingFiles)
632+
if err != nil {
633+
return mcp.NewToolResultError(fmt.Sprintf("failed to marshal matching files: %s", err)), nil
634+
}
635+
return mcp.NewToolResultText(fmt.Sprintf("Provided path did not match a file or directory, but possible matches are: %s", matchingFilesJSON)), nil
636+
}
637+
613638
return mcp.NewToolResultError("Failed to get file contents. The path does not point to a file or directory, or the file does not exist in the repository."), nil
614639
}
615640
}
@@ -1293,3 +1318,22 @@ func GetTag(getClient GetClientFn, t translations.TranslationHelperFunc) (tool m
12931318
return mcp.NewToolResultText(string(r)), nil
12941319
}
12951320
}
1321+
1322+
// filterPaths filters the entries in a GitHub tree to find paths that
1323+
// match the given suffix.
1324+
func filterPaths(entries []*github.TreeEntry, path string, maxResults int) []string {
1325+
matchedPaths := []string{}
1326+
for _, entry := range entries {
1327+
if len(matchedPaths) == maxResults {
1328+
break // Limit the number of results to maxResults
1329+
}
1330+
entryPath := entry.GetPath()
1331+
if entryPath == "" {
1332+
continue // Skip empty paths
1333+
}
1334+
if strings.HasSuffix(entryPath, path) {
1335+
matchedPaths = append(matchedPaths, entryPath)
1336+
}
1337+
}
1338+
return matchedPaths
1339+
}

0 commit comments

Comments
 (0)