Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

Commit b6d4a0d

Browse files
authored
Svelte: allow picking by commit hash (#62160)
This updates the revision picker to support searching for commits by hash. This has been a long-standing product gap in the React webapp, and the same problem exists in the Svelte webapp. I did not fix this in the React webapp as well because our pagination logic is very tied to GraphQL pagination, and I couldn't come up with an easy way to add an item to the list that is outside of a paginated list of nodes. Fixes #18792 in the Svelte webapp Fixes #44881 in the Svelte webapp
1 parent d82c7fd commit b6d4a0d

File tree

4 files changed

+41
-34
lines changed

4 files changed

+41
-34
lines changed

client/web-sveltekit/src/routes/[...repo=reporev]/(validrev)/(code)/+layout.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,21 @@ export const load: LayoutLoad = async ({ parent, params }) => {
127127
revision: resolvedRevision.commitID,
128128
})
129129
.then(
130-
mapOrThrow(({ data, error }) => {
131-
if (!data?.repository?.commit) {
132-
throw new Error(error?.message)
133-
}
130+
mapOrThrow(({ data }) => {
131+
let nodes = data?.repository?.ancestorCommits?.ancestors.nodes ?? []
134132

135-
return data.repository.commit.ancestors
133+
// If we got a match for the OID, add it to the list if it doesn't already exist.
134+
// We double check that the OID contains the search term because we cannot search
135+
// specifically by OID, and an empty string resolves to HEAD.
136+
const commitByHash = data?.repository?.commitByHash
137+
if (
138+
commitByHash &&
139+
commitByHash.oid.includes(searchTerm) &&
140+
!nodes.some(node => node.oid === commitByHash.oid)
141+
) {
142+
nodes = [commitByHash, ...nodes]
143+
}
144+
return { nodes }
136145
})
137146
)
138147
),

client/web-sveltekit/src/routes/[...repo=reporev]/(validrev)/(code)/RepositoryRevPicker.gql

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,13 @@ fragment RepositoryGitRefs on Repository {
2222
}
2323
}
2424

25-
# Used to query data for repository git commits
26-
# NOTE: the query which is going to use this fragment is supposed
27-
# to have query and anchor revision variables
28-
fragment RepositoryGitCommits on Repository {
25+
fragment RevPickerGitCommit on GitCommit {
2926
__typename
30-
commit(rev: $revision) {
31-
__typename
32-
ancestors(first: 15, query: $query) {
33-
__typename
34-
nodes {
35-
__typename
36-
id
37-
oid
38-
subject
39-
abbreviatedOID
40-
...RepositoryGitRevAuthor
41-
}
42-
}
43-
}
27+
id
28+
oid
29+
subject
30+
abbreviatedOID
31+
...RepositoryGitRevAuthor
4432
}
4533

4634
fragment RepositoryGitRevAuthor on GitCommit {

client/web-sveltekit/src/routes/[...repo=reporev]/(validrev)/(code)/RepositoryRevPicker.svelte

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
11
<script context="module" lang="ts">
2-
import type { RepositoryGitRefs, RepositoryGitCommits } from './RepositoryRevPicker.gql'
2+
import type { RepositoryGitRefs, RevPickerGitCommit } from './RepositoryRevPicker.gql'
33
44
export type RepositoryBranches = RepositoryGitRefs['gitRefs']
55
export type RepositoryBranch = RepositoryBranches['nodes'][number]
66
77
export type RepositoryTags = RepositoryGitRefs['gitRefs']
88
export type RepositoryTag = RepositoryTags['nodes'][number]
99
10-
export type RepositoryCommits = NonNullable<RepositoryGitCommits['commit']>['ancestors']
11-
export type RepositoryGitCommit = RepositoryCommits['nodes'][number]
10+
export type RepositoryCommits = { nodes: RevPickerGitCommit[] }
11+
export type RepositoryGitCommit = RevPickerGitCommit
1212
</script>
1313
1414
<script lang="ts">
1515
import { mdiClose, mdiSourceBranch, mdiTagOutline, mdiSourceCommit } from '@mdi/js'
16-
import { Button, Badge } from '$lib/wildcard'
17-
import Popover from '$lib/Popover.svelte'
16+
17+
import { replaceRevisionInURL } from '@sourcegraph/shared/src/util/url'
18+
19+
import { goto } from '$app/navigation'
1820
import Icon from '$lib/Icon.svelte'
19-
import Tabs from '$lib/Tabs.svelte'
21+
import Popover from '$lib/Popover.svelte'
2022
import TabPanel from '$lib/TabPanel.svelte'
23+
import Tabs from '$lib/Tabs.svelte'
2124
import Tooltip from '$lib/Tooltip.svelte'
22-
23-
import { goto } from '$app/navigation'
24-
import { replaceRevisionInURL } from '@sourcegraph/shared/src/util/url'
25+
import { Button, Badge } from '$lib/wildcard'
2526
2627
import type { ResolvedRevision } from '../../+layout'
2728

client/web-sveltekit/src/routes/[...repo=reporev]/(validrev)/(code)/layout.gql

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,18 @@ query RepositoryGitRefs($repoName: String!, $query: String, $type: GitRefType!)
2929
}
3030
}
3131

32-
query RepositoryGitCommits($repoName: String!, $query: String, $revision: String!) {
32+
query RepositoryGitCommits($repoName: String!, $query: String!, $revision: String!) {
3333
repository(name: $repoName) {
34-
...RepositoryGitCommits
34+
commitByHash: commit(rev: $query) {
35+
...RevPickerGitCommit
36+
}
37+
ancestorCommits: commit(rev: $revision) {
38+
ancestors(first: 15, query: $query) {
39+
nodes {
40+
...RevPickerGitCommit
41+
}
42+
}
43+
}
3544
}
3645
}
3746

0 commit comments

Comments
 (0)