diff --git a/src/lib/router.ts b/src/lib/router.ts index 41f91629d..e666ca8cb 100644 --- a/src/lib/router.ts +++ b/src/lib/router.ts @@ -109,6 +109,41 @@ export async function replace(newRoute: Route): Promise { await navigate("replace", newRoute); } +// We need a SHA1 commit in some places, so we return early if the revision is +// a SHA and else we look into branches. +export function getOid( + revision: string, + branches?: Record, +): string | undefined { + if (isOid(revision)) return revision; + + if (branches) { + const oid = branches[revision]; + if (oid) return oid; + } + return undefined; +} + +// Check whether the input is a SHA1 commit. +export function isOid(input: string): boolean { + return /^[a-fA-F0-9]{40}$/.test(input); +} + +export function parseRevisionToOid( + revision: string | undefined, + defaultBranch: string, + branches: Record, +): string { + if (revision) { + const oid = getOid(revision, branches); + if (!oid) { + throw new Error(`Revision ${revision} not found`); + } + return oid; + } + return branches[defaultBranch]; +} + function pathToRoute(path: string): Route | null { // This matches e.g. an empty string if (!path) { @@ -266,4 +301,4 @@ export function routeToPath(route: Route) { } } -export const testExports = { pathToRoute }; +export const testExports = { pathToRoute, isOid, routeToPath }; diff --git a/src/lib/utils.ts b/src/lib/utils.ts index e6601cbdb..f4566a928 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -19,24 +19,6 @@ export function getRawBasePath( return `${baseUrl.scheme}://${baseUrl.hostname}:${baseUrl.port}/raw/${id}/${commit}`; } -// We need a SHA1 commit in some places, so we return early if the revision is -// a SHA and else we look into branches. -export function getOid( - revision: string, - branches?: Record, -): string | null { - if (isOid(revision)) return revision; - - if (branches) { - const oid = branches[revision]; - - if (oid) { - return oid; - } - } - return null; -} - export function formatLocationHash(hash: string | null): number | null { if (hash && hash.match(/^#L[0-9]+$/)) return parseInt(hash.slice(2)); return null; @@ -180,11 +162,6 @@ export const formatTimestamp = ( return new Date(timestamp).toUTCString(); }; -// Check whether the input is a SHA1 commit. -export function isOid(input: string): boolean { - return /^[a-fA-F0-9]{40}$/.test(input); -} - // Check whether the input is a URL. export function isUrl(input: string): boolean { return /^https?:\/\//.test(input); diff --git a/src/views/projects/BranchSelector.svelte b/src/views/projects/BranchSelector.svelte index 74c9dc11d..96736853f 100644 --- a/src/views/projects/BranchSelector.svelte +++ b/src/views/projects/BranchSelector.svelte @@ -1,5 +1,6 @@