Skip to content

Commit

Permalink
OpenGrok /diff compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
isker committed Jul 10, 2023
1 parent 1c5528b commit c883c62
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
5 changes: 0 additions & 5 deletions src/routes/(opengrok-compat)/diff/+page.svelte

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { escapeRegExp } from "$lib/regexp";
import { projectToRepo } from "$lib/server/opengrok-compat";
import { listRepositories } from "$lib/server/zoekt-list-repositories";
import { error, redirect } from "@sveltejs/kit";

export const load: import("./$types").PageServerLoad = async ({
url,
params: { file, project },
parent,
setHeaders,
fetch,
}) => {
if (!file) {
throw error(404);
}
const revision = url.searchParams.get("r");
const convertedRepo = projectToRepo.get(project);

const result = await listRepositories(
{
query: `repo:^${escapeRegExp(convertedRepo ?? project)}$`,
},
fetch
);
if (result.kind === "error") {
throw new Error(`Failed to list repositories: ${result.error}`);
}

const repo = result.results.repositories[0];
const fileUrl = repo?.fileUrlTemplate
.replaceAll("{{.Version}}", revision ?? repo.branches[0].name)
.replaceAll("{{.Path}}", file);

setHeaders({
"cache-control": "no-store,must-revalidate",
});

if (fileUrl && (await parent()).preferences.openGrokInstantRedirect) {
throw redirect(301, fileUrl);
}

return { file, fileUrl };
};
27 changes: 27 additions & 0 deletions src/routes/(opengrok-compat)/diff/[project]/[...file]/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<script lang="ts">
import ExampleQuery from "$lib/example-query.svelte";
import Link from "$lib/link.svelte";
import { escapeRegExp } from "$lib/regexp";
export let data: import("./$types").PageData;
</script>

<section class="space-y-2 pt-2">
<h2 class="text-2xl text-center">Diff viewer</h2>
<p>
OpenGrok's diff viewer that was previously available at this URL is
unfortunately not supported in neogrok.
</p>
{#if data.fileUrl}
<p>
That being said, we've been able to produce a URL to view the file on its
home site. Head on over: <Link to={data.fileUrl}>{data.fileUrl}</Link>.
</p>
{:else}
<p>
Even more unfortunately, we've not been able to produce a URL to view the
file somewhere else. You can try searching for it and going from there:
<ExampleQuery query={`file:^${escapeRegExp(data.file)}$`} />.
</p>
{/if}
</section>

0 comments on commit c883c62

Please sign in to comment.