Skip to content

Commit

Permalink
Properly serve results from repositories that have no VCS metadata
Browse files Browse the repository at this point in the history
e.g. "plain directory" repositories of the kind produced by
`zoekt-index` (as opposed to `zoekt-git-index`).

I simply had never tested support for these, and they of course lack
properties on the returned JSON objects that are otherwise present.
Tolerate certain properties being missing or null.

Fixes #30.
  • Loading branch information
isker committed Oct 31, 2024
1 parent e0060d4 commit 5974e0f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/happy-goats-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"neogrok": patch
---

Properly serve results from repositories that have no VCS metadata
14 changes: 8 additions & 6 deletions src/lib/server/search-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,13 @@ const searchResultSchema = v.object({
.object({
Repository: v.string(),
FileName: v.string(),
Branches: v.array(v.string()),
Branches: v.array(v.string()).optional(),
// Will be the empty string if zoekt couldn't figure it out, we
// call it Text for both display purposes and for parameterizing
// syntax highlighting; Text is the name zoekt will pick for
// plain text files it can identify.
Language: v.string().map((lang) => lang || "Text"),
Version: v.string(),
Version: v.string().optional(),
ChunkMatches: v.array(
v
.object({
Expand Down Expand Up @@ -166,7 +166,7 @@ const searchResultSchema = v.object({
({
Repository: repository,
FileName: fileName,
Branches: branches,
Branches: branches = [],
Language: language,
Version: version,
ChunkMatches: chunkMatches,
Expand Down Expand Up @@ -241,9 +241,11 @@ const searchResultSchema = v.object({
),
fileName,
chunks,
fileUrl: repoUrls[repository]
?.replaceAll("{{.Version}}", version)
.replaceAll("{{.Path}}", fileName.text),
fileUrl:
version &&
repoUrls[repository]
?.replaceAll("{{.Version}}", version)
.replaceAll("{{.Path}}", fileName.text),
// The 'template' is such that the line number can be `join`ed
// into it. JSON serializable!
lineNumberTemplate:
Expand Down
25 changes: 16 additions & 9 deletions src/lib/server/zoekt-list-repositories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,21 @@ const listResultSchema = v.object({
LatestCommitDate: dateSchema,
FileURLTemplate: v.string(),
CommitURLTemplate: v.string(),
Branches: v.array(
v
.object({ Name: v.string(), Version: v.string() })
.map(({ Name, Version }) => ({
name: Name,
version: Version,
})),
),
Branches: v
.array(
v
.object({ Name: v.string(), Version: v.string() })
.map(({ Name, Version }) => ({
name: Name,
version: Version,
})),
)
// Zoekt returns `null` when there are no branches (e.g.
// the repo is just a directory, no VCS info). To me that
// seems wrong and I think it should be `omitempty`, so
// tolerate both options in case that ever gets fixed.
.nullable()
.optional(),
})
.map(
({
Expand All @@ -116,7 +123,7 @@ const listResultSchema = v.object({
lastCommit: toISOStringWithoutMs(LatestCommitDate),
fileUrlTemplate: FileURLTemplate,
commitUrlTemplate: CommitURLTemplate,
branches: Branches,
branches: Branches ?? [],
}),
),
IndexMetadata: v
Expand Down

0 comments on commit 5974e0f

Please sign in to comment.