Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 16 additions & 17 deletions client/web/src/enterprise/codeintel/searchBased.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,11 @@ function makeRepositoryPattern(repo: string): string {
/** The time in ms to delay between unindexed search request and the fallback indexed search request. */
const DEFAULT_UNINDEXED_SEARCH_TIMEOUT_MS = 5000

export type RepoFilter = 'current-repo' | 'all-other-repos'

/**
* Invoke the given search function by modifying the query with a term that will
* only look in the current git tree by appending a repo filter with the repo name
* and the current commit or, if `negateRepoFilter` is set, outside of current git
* tree.
* look in certain git trees (controlled by `repoFilter`).
*
* This is likely to timeout on large repos or organizations with monorepos if the
* current commit is not an indexed commit. Instead of waiting for a timeout, we
Expand All @@ -125,7 +125,6 @@ const DEFAULT_UNINDEXED_SEARCH_TIMEOUT_MS = 5000
*
* @param search The search function.
* @param args The arguments to the search function.
* @param negateRepoFilter Whether to look only inside or outside the given repo.
*/
export function searchWithFallback<
P extends {
Expand All @@ -136,14 +135,14 @@ export function searchWithFallback<
queryTerms: string[]
},
R
>(search: (args: P) => Promise<R>, args: P, negateRepoFilter = false, getSetting: SettingsGetter): Promise<R> {
>(search: (args: P) => Promise<R>, args: P, repoFilter: RepoFilter, getSetting: SettingsGetter): Promise<R> {
if (getSetting<boolean>('basicCodeIntel.indexOnly', false)) {
return searchIndexed(search, args, negateRepoFilter, getSetting)
return searchIndexed(search, args, repoFilter, getSetting)
}

return raceWithDelayOffset(
searchUnindexed(search, args, negateRepoFilter, getSetting),
() => searchIndexed(search, args, negateRepoFilter, getSetting),
searchUnindexed(search, args, repoFilter, getSetting),
() => searchIndexed(search, args, repoFilter, getSetting),
getSetting<number>('basicCodeIntel.unindexedSearchTimeout', DEFAULT_UNINDEXED_SEARCH_TIMEOUT_MS)
)
}
Expand All @@ -153,18 +152,16 @@ export function searchWithFallback<
*
* @param search The search function.
* @param args The arguments to the search function.
* @param negateRepoFilter Whether to look only inside or outside the given repo.
*/
function searchIndexed<
P extends {
repo: string
isFork: boolean
isArchived: boolean
commit: string
queryTerms: string[]
},
R
>(search: (args: P) => Promise<R>, args: P, negateRepoFilter = false, getSetting: SettingsGetter): Promise<R> {
>(search: (args: P) => Promise<R>, args: P, repoFilter: RepoFilter, getSetting: SettingsGetter): Promise<R> {
const { repo, isFork, isArchived, queryTerms } = args

// Create a copy of the args so that concurrent calls to other
Expand All @@ -175,14 +172,16 @@ function searchIndexed<
// Unlike unindexed search, we can't supply a commit as that particular
// commit may not be indexed. We force index and look inside/outside
// the repo at _whatever_ commit happens to be indexed at the time.
queryTermsCopy.push((negateRepoFilter ? '-' : '') + `repo:${makeRepositoryPattern(repo)}`)
const isCurrentRepoSearch = repoFilter === 'current-repo'
const prefix = isCurrentRepoSearch ? '' : '-'
queryTermsCopy.push(prefix + `repo:${makeRepositoryPattern(repo)}`)
queryTermsCopy.push('index:only')

// If we're a fork, search in forks _for the same repo_. Otherwise,
// search in forks only if it's set in the settings. This is also
// symmetric for archived repositories.
queryTermsCopy.push(
...repositoryKindTerms(isFork && !negateRepoFilter, isArchived && !negateRepoFilter, getSetting)
...repositoryKindTerms(isFork && isCurrentRepoSearch, isArchived && isCurrentRepoSearch, getSetting)
)

return search({ ...args, queryTerms: queryTermsCopy })
Expand All @@ -193,7 +192,6 @@ function searchIndexed<
*
* @param search The search function.
* @param args The arguments to the search function.
* @param negateRepoFilter Whether to look only inside or outside the given repo.
*/
function searchUnindexed<
P extends {
Expand All @@ -204,15 +202,16 @@ function searchUnindexed<
queryTerms: string[]
},
R
>(search: (args: P) => Promise<R>, args: P, negateRepoFilter = false, getSetting: SettingsGetter): Promise<R> {
>(search: (args: P) => Promise<R>, args: P, repoFilter: RepoFilter, getSetting: SettingsGetter): Promise<R> {
const { repo, isFork, isArchived, commit, queryTerms } = args

// Create a copy of the args so that concurrent calls to other
// search methods do not have their query terms unintentionally
// modified.
const queryTermsCopy = [...queryTerms]

if (!negateRepoFilter) {
const isCurrentRepoSearch = repoFilter === 'current-repo'
if (isCurrentRepoSearch) {
// Look in this commit only
queryTermsCopy.push(`repo:${makeRepositoryPattern(repo)}@${commit}`)
} else {
Expand All @@ -224,7 +223,7 @@ function searchUnindexed<
// search in forks only if it's set in the settings. This is also
// symmetric for archived repositories.
queryTermsCopy.push(
...repositoryKindTerms(isFork && !negateRepoFilter, isArchived && !negateRepoFilter, getSetting)
...repositoryKindTerms(isFork && isCurrentRepoSearch, isArchived && isCurrentRepoSearch, getSetting)
)

return search({ ...args, queryTerms: queryTermsCopy })
Expand Down
Loading