Skip to content

Commit 696d06b

Browse files
branch handling
1 parent 898c909 commit 696d06b

File tree

7 files changed

+33
-20
lines changed

7 files changed

+33
-20
lines changed

packages/web/src/app/[domain]/components/pathHeader.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ export const PathHeader = ({
233233
}}
234234
>
235235
<span className="mr-0.5">@</span>
236-
{`${branchDisplayName}`}
236+
{`${branchDisplayName.replace(/^refs\/(heads|tags)\//, '')}`}
237237
</p>
238238
)}
239239
<span>·</span>

packages/web/src/app/[domain]/search/components/searchResultsPage.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
import { Separator } from "@/components/ui/separator";
1313
import { Tooltip, TooltipContent, TooltipTrigger } from "@/components/ui/tooltip";
1414
import { RepositoryInfo, SearchResultFile, SearchStats } from "@/features/search/types";
15+
import useCaptureEvent from "@/hooks/useCaptureEvent";
1516
import { useDomain } from "@/hooks/useDomain";
1617
import { useNonEmptyQueryParam } from "@/hooks/useNonEmptyQueryParam";
1718
import { useSearchHistory } from "@/hooks/useSearchHistory";
@@ -32,7 +33,6 @@ import { CodePreviewPanel } from "./codePreviewPanel";
3233
import { FilterPanel } from "./filterPanel";
3334
import { useFilteredMatches } from "./filterPanel/useFilterMatches";
3435
import { SearchResultsPanel, SearchResultsPanelHandle } from "./searchResultsPanel";
35-
import useCaptureEvent from "@/hooks/useCaptureEvent";
3636

3737
interface SearchResultsPageProps {
3838
searchQuery: string;
@@ -156,6 +156,13 @@ export const SearchResultsPage = ({
156156
router.push(url);
157157
}, [maxMatchCount, router, searchQuery, domain]);
158158

159+
// Look for any files that are not on the default branch.
160+
const isBranchFilteringEnabled = useMemo(() => {
161+
return files.some((file) => {
162+
return file.branches?.some((branch) => branch !== 'HEAD') ?? false;
163+
});
164+
}, [files]);
165+
159166
return (
160167
<div className="flex flex-col h-screen overflow-clip">
161168
{/* TopBar */}
@@ -189,8 +196,7 @@ export const SearchResultsPage = ({
189196
isStreaming={isStreaming}
190197
searchStats={stats}
191198
isMoreResultsButtonVisible={!isExhaustive}
192-
// @todo: handle branch filtering
193-
isBranchFilteringEnabled={false}
199+
isBranchFilteringEnabled={isBranchFilteringEnabled}
194200
/>
195201
)}
196202
</div>

packages/web/src/app/[domain]/search/components/searchResultsPanel/fileMatchContainer.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export const FileMatchContainer = ({
7575
}
7676

7777
return `${branches[0]}${branches.length > 1 ? ` +${branches.length - 1}` : ''}`;
78-
}, [isBranchFilteringEnabled, branches]);
78+
}, [branches, isBranchFilteringEnabled]);
7979

8080
const repo = useMemo(() => {
8181
return repoInfo[file.repositoryId];

packages/web/src/app/[domain]/search/components/searchResultsPanel/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ export const SearchResultsPanel = forwardRef<SearchResultsPanelHandle, SearchRes
8888

8989
const resetScroll = useCallback(() => {
9090
virtualizer.scrollToIndex(0);
91-
}, [fileMatches.length, virtualizer]);
91+
}, [virtualizer]);
9292

9393
// Expose the resetScroll function to parent components
9494
useImperativeHandle(ref, () => ({
@@ -121,7 +121,7 @@ export const SearchResultsPanel = forwardRef<SearchResultsPanelHandle, SearchRes
121121
align: 'start'
122122
});
123123
}
124-
}, [showAllMatchesMap, virtualizer]);
124+
}, [showAllMatchesActions, showAllMatchesMap, virtualizer]);
125125

126126

127127
return (

packages/web/src/app/[domain]/search/useStreamedSearch.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ const isCacheValid = (entry: CacheEntry): boolean => {
3333
};
3434

3535
export const useStreamedSearch = ({ query, matches, contextLines, whole, isRegexEnabled, isCaseSensitivityEnabled }: SearchRequest) => {
36-
3736
const [state, setState] = useState<{
3837
isStreaming: boolean,
3938
isExhaustive: boolean,

packages/web/src/features/search/query.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ export const transformLezerTreeToZoektGrpcQuery = async ({
6161
isRegexEnabled: boolean;
6262
onExpandSearchContext: (contextName: string) => Promise<string[]>;
6363
}): Promise<ZoektGrpcQuery> => {
64-
6564
const transformNode = async (node: SyntaxNode): Promise<ZoektGrpcQuery> => {
6665
switch (node.type.id) {
6766
case Program: {
@@ -200,7 +199,7 @@ export const transformLezerTreeToZoektGrpcQuery = async ({
200199
query: "substring"
201200
};
202201

203-
202+
204203
case LangExpr:
205204
return {
206205
language: {
@@ -277,11 +276,11 @@ export const transformLezerTreeToZoektGrpcQuery = async ({
277276
}
278277
case ForkExpr: {
279278
const rawValue = value.toLowerCase();
280-
279+
281280
if (!isForkValue(rawValue)) {
282281
throw new Error(`Invalid fork value: ${rawValue}. Expected 'yes', 'no', or 'only'`);
283282
}
284-
283+
285284
const flags: ('FLAG_ONLY_FORKS' | 'FLAG_NO_FORKS')[] = [];
286285

287286
if (rawValue === 'yes') {
@@ -336,12 +335,8 @@ const getChildren = (node: SyntaxNode): SyntaxNode[] => {
336335
const children: SyntaxNode[] = [];
337336
let child = node.firstChild;
338337
while (child) {
339-
// Skip certain node types that are just structural
340-
if (!["(", ")", "or"].includes(child.type.name)) {
341-
children.push(child);
342-
}
338+
children.push(child);
343339
child = child.nextSibling;
344340
}
345341
return children;
346342
}
347-

packages/web/src/features/search/searchApi.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import path from 'path';
1818
import { parseQueryIntoLezerTree, transformLezerTreeToZoektGrpcQuery } from './query';
1919
import { RepositoryInfo, SearchRequest, SearchResponse, SearchResultFile, SearchStats, SourceRange, StreamedSearchResponse } from "./types";
2020
import { FlushReason as ZoektFlushReason } from "@/proto/zoekt/webserver/v1/FlushReason";
21+
import { RevisionExpr } from "@sourcebot/query-language";
2122

2223
const logger = createLogger("searchApi");
2324

@@ -454,18 +455,30 @@ const createZoektSearchRequest = async ({
454455
},
455456
});
456457

458+
// Find if there are any `rev:` filters in the query.
459+
let containsRevExpression = false;
460+
tree.iterate({
461+
enter: (node) => {
462+
if (node.type.id === RevisionExpr) {
463+
containsRevExpression = true;
464+
// false to stop the iteration.
465+
return false;
466+
}
467+
}
468+
});
469+
457470
const zoektSearchRequest: ZoektGrpcSearchRequest = {
458471
query: {
459472
and: {
460473
children: [
461474
zoektQuery,
462-
// @todo: handle branch filtering.
463-
{
475+
// If the query does not contain a `rev:` filter, we default to searching `HEAD`.
476+
...(!containsRevExpression ? [{
464477
branch: {
465478
pattern: 'HEAD',
466479
exact: true,
467480
}
468-
}
481+
}] : []),
469482
]
470483
}
471484
},

0 commit comments

Comments
 (0)