Skip to content

Commit 1cc6f7c

Browse files
committed
filterPaths - trailing slashes
1 parent 588866f commit 1cc6f7c

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

pkg/github/repositories.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1295,20 +1295,29 @@ func GetTag(getClient GetClientFn, t translations.TranslationHelperFunc) (tool m
12951295
// It returns a slice of strings containing the matching paths.
12961296
// Directories are returned with a trailing slash.
12971297
func filterPaths(entries []*github.TreeEntry, path string, maxResults int) []string {
1298-
path = strings.TrimSuffix(path, "/") // Normalize path to avoid double slashes
1298+
// Remove trailing slash for matching purposes, but flag whether we
1299+
// only want directories.
1300+
dirOnly := false
1301+
if strings.HasSuffix(path, "/") {
1302+
dirOnly = true
1303+
path = strings.TrimSuffix(path, "/")
1304+
}
12991305

13001306
matchedPaths := []string{}
13011307
for _, entry := range entries {
13021308
if len(matchedPaths) == maxResults {
13031309
break // Limit the number of results to maxResults
13041310
}
1311+
if dirOnly && entry.GetType() != "tree" {
1312+
continue // Skip non-directory entries if dirOnly is true
1313+
}
13051314
entryPath := entry.GetPath()
13061315
if entryPath == "" {
13071316
continue // Skip empty paths
13081317
}
13091318
if strings.HasSuffix(entryPath, path) {
13101319
if entry.GetType() == "tree" {
1311-
entryPath += "/" // show directories with a trailing slash
1320+
entryPath += "/" // Return directories with a trailing slash
13121321
}
13131322
matchedPaths = append(matchedPaths, entryPath)
13141323
}

pkg/github/repositories_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2117,6 +2117,26 @@ func Test_filterPaths(t *testing.T) {
21172117
maxResults: -1,
21182118
expected: []string{"folder/", "nested/folder/"},
21192119
},
2120+
{
2121+
name: "dir and file match",
2122+
tree: []*github.TreeEntry{
2123+
{Path: github.Ptr("name"), Type: github.Ptr("tree")},
2124+
{Path: github.Ptr("name"), Type: github.Ptr("blob")},
2125+
},
2126+
path: "name", // No trailing slash can match both files and directories
2127+
maxResults: -1,
2128+
expected: []string{"name/", "name"},
2129+
},
2130+
{
2131+
name: "dir only match",
2132+
tree: []*github.TreeEntry{
2133+
{Path: github.Ptr("name"), Type: github.Ptr("tree")},
2134+
{Path: github.Ptr("name"), Type: github.Ptr("blob")},
2135+
},
2136+
path: "name/", // Trialing slash ensures only directories are matched
2137+
maxResults: -1,
2138+
expected: []string{"name/"},
2139+
},
21202140
}
21212141

21222142
for _, tc := range tests {

0 commit comments

Comments
 (0)