Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions forge-web/components/repo/clone-box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ import type { RepoAddress } from '@/hooks/use-query-param'
import { ACTIVE_NETWORK } from '@/lib/constants'
import { errorMessage } from '@/lib/utils'
import { saveBytes } from '@/lib/view/release-download'
import { formatBytes, plural, selectedTip, type RepoHome, type SelectedRef } from '@/lib/view'
import { formatBytes, plural, selectedTip, tipOidOf, type RepoHome, type SelectedRef } from '@/lib/view'
import { repoCommands } from '@/lib/view/repo-commands'
import { resolveTip } from '@/lib/view/tip'
import { repoKey } from '@/lib/repo'
import {
compressInWorker,
listFiles,
planArchive,
readZipFiles,
substituteFiles,
type ArchiveRefs,
storedSize,
ZIP_MAX_BYTES,
zipFileName,
Expand Down Expand Up @@ -78,6 +80,16 @@ export function CloneBox({ home, addr, selected }: { home: RepoHome; addr: RepoA
)
}

/** The repo's tags and branches, as `%(describe)` and `%d` in an export-subst file read them. */
function archiveRefs(home: RepoHome): ArchiveRefs {
const named = (refs: RepoHome['tags'], prefix: string) =>
refs.flatMap((r) => {
const oid = tipOidOf(r)
return oid === null ? [] : [{ name: r.refName.replace(prefix, ''), oid }]
})
return { tags: named(home.tags, 'refs/tags/'), heads: named(home.branches, 'refs/heads/') }
}

function ZipDownload({ home, addr, selected }: { home: RepoHome; addr: RepoAddress; selected: SelectedRef }): JSX.Element | null {
const state = useBrowseReader(home.repo)
const [progress, setProgress] = useState<ZipProgress | null>(null)
Expand All @@ -97,14 +109,20 @@ function ZipDownload({ home, addr, selected }: { home: RepoHome; addr: RepoAddre
setProgress({ phase: 'listing', files: 0, filesTotal: 0, bytes: 0 })
try {
// A short pinned id resolved to its commit, and a tag to what it names (L-01, L-32).
const files = await listFiles(reader, (await resolveTip(reader, tip, { repoKey: repoKey(home.repo), pinned: selected.pinned !== undefined })).oid)
// As `git archive` (QW-026): the tree's export-ignore and export-subst, modes, the commit's time.
const plan = await planArchive(reader, (await resolveTip(reader, tip, { repoKey: repoKey(home.repo), pinned: selected.pinned !== undefined })).oid)
const { files } = plan
const stored = storedSize(files)
if (stored > ZIP_MAX_BYTES) throw new ZipTooLargeError(stored)
const entries = await readZipFiles(reader, files, setProgress, cancel.current.signal)
await substituteFiles(reader, plan, entries, archiveRefs(home), { signal: cancel.current.signal, onProgress: setProgress })
const name = zipFileName(addr.name, selected.name)
const prefix = `${name.replace(/\.zip$/, '')}/`
const rooted: Record<string, Uint8Array> = {}
for (const [path, bytes] of Object.entries(entries)) rooted[`${name.replace(/\.zip$/, '')}/${path}`] = bytes
const zip = await compressInWorker(rooted, setProgress, cancel.current.signal)
const modes: Record<string, number> = {}
for (const f of files) modes[prefix + f.path] = f.mode
for (const [path, bytes] of Object.entries(entries)) rooted[prefix + path] = bytes
const zip = await compressInWorker(rooted, setProgress, cancel.current.signal, { modes, mtime: plan.mtime, comment: plan.commit?.oid ?? null })
saveBytes(zip, name, 'application/zip')
setMessage(`Saved ${name} (${formatBytes(zip.length)}, ${plural(files.length, 'file')}, each hash-checked).`)
} catch (e) {
Expand All @@ -123,7 +141,9 @@ function ZipDownload({ home, addr, selected }: { home: RepoHome; addr: RepoAddre
? 'Listing files…'
: progress.phase === 'reading'
? `Reading ${progress.files} of ${plural(progress.filesTotal, 'file')} (${formatBytes(progress.bytes)})`
: `Compressing ${formatBytes(progress.bytes)}…`
: progress.phase === 'describing'
? 'Describing the commit for export-subst (git describe)…'
: `Compressing ${formatBytes(progress.bytes)}…`

return (
<div className="mt-2">
Expand Down
5 changes: 5 additions & 0 deletions forge-web/lib/browse/reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,11 @@ export class BrowseReader {
return Object.assign(walker, { flush: () => verdicts?.flush() })
}

/** How many objects the locator indexes (git's automatic abbreviation length grows with it). */
get objectCount(): number {
return this.locator.count
}

/** OIDs starting with a hex `prefix` ({@link ObjectLocator.findByPrefix}). */
findByPrefix(prefix: string, limit?: number): string[] {
return this.locator.findByPrefix(prefix, limit)
Expand Down
Loading
Loading