CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | /**
* Block J30 — Repository language breakdown.
*
* GET /:owner/:repo/languages[?vendored=1&fold=N&ref=<branch>]
*
* Renders a GitHub-parity language breakdown: a stacked percentage bar +
* a per-language table (bytes, files, share). Uses `listTreeRecursive`
* against the repo's default branch (or an explicit `ref`) to get file
* sizes, then runs the pure `buildLanguageReport` helper.
*
* Defaults: vendored/generated files (node_modules, dist, lockfiles, etc.)
* are excluded. Pass `?vendored=1` to include them.
*
* softAuth + try/catch-wrapped resolveRepo — never 500s.
*/
import { Hono } from "hono";
import { and, eq } from "drizzle-orm";
import { db } from "../db";
import { repositories, users } from "../db/schema";
import { Layout } from "../views/layout";
import { RepoHeader } from "../views/components";
import { softAuth } from "../middleware/auth";
import type { AuthEnv } from "../middleware/auth";
import { getDefaultBranch, listTreeRecursive } from "../git/repository";
import {
buildLanguageReport,
formatBytes,
formatPercent,
type LanguageFileEntry,
type LanguageReport,
} from "../lib/language-stats";
const languageRoutes = new Hono<AuthEnv>();
languageRoutes.use("*", softAuth);
async function resolveRepo(ownerName: string, repoName: string) {
try {
const [owner] = await db
.select()
.from(users)
.where(eq(users.username, ownerName))
.limit(1);
if (!owner) return null;
const [repo] = await db
.select()
.from(repositories)
.where(
and(
eq(repositories.ownerId, owner.id),
eq(repositories.name, repoName)
)
)
.limit(1);
if (!repo) return null;
return { owner, repo };
} catch {
return null;
}
}
function parseFold(raw: string | undefined): number {
if (!raw) return 0;
const n = Number(raw);
if (!Number.isFinite(n) || n <= 0 || n >= 100) return 0;
return n;
}
/** Minimal ref sanity check — branches/tags shouldn't contain whitespace or `..`. */
function sanitiseRef(raw: string | undefined): string | null {
if (!raw) return null;
const trimmed = raw.trim();
if (!trimmed || trimmed.length > 200) return null;
if (/\.\./.test(trimmed)) return null;
if (/[\s~^:?*[\\]/.test(trimmed)) return null;
return trimmed;
}
languageRoutes.get("/:owner/:repo/languages", async (c) => {
const { owner: ownerName, repo: repoName } = c.req.param();
const user = c.get("user");
const includeVendored = c.req.query("vendored") === "1";
const foldUnderPercent = parseFold(c.req.query("fold"));
const refParam = sanitiseRef(c.req.query("ref"));
const resolved = await resolveRepo(ownerName, repoName);
if (!resolved) {
return c.html(
<Layout title="Not Found" user={user}>
<div class="empty-state">
<h2>Repository not found</h2>
</div>
</Layout>,
404
);
}
if (resolved.repo.isPrivate && (!user || user.id !== resolved.owner.id)) {
return c.html(
<Layout title="Not Found" user={user}>
<div class="empty-state">
<h2>Repository not found</h2>
</div>
</Layout>,
404
);
}
let ref: string | null = refParam;
if (!ref) {
try {
ref = await getDefaultBranch(ownerName, repoName);
} catch {
ref = null;
}
}
let entries: LanguageFileEntry[] = [];
if (ref) {
try {
entries = await listTreeRecursive(ownerName, repoName, ref);
} catch {
entries = [];
}
}
const report: LanguageReport = buildLanguageReport({
entries,
ignoreVendored: !includeVendored,
foldUnderPercent,
});
const empty = report.buckets.length === 0;
return c.html(
<Layout title={`Languages — ${ownerName}/${repoName}`} user={user}>
<RepoHeader owner={ownerName} repo={repoName} />
<div style="max-width: 920px">
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px">
<h2 style="margin: 0">Languages</h2>
<form
method="GET"
action={`/${ownerName}/${repoName}/languages`}
style="display: flex; gap: 10px; align-items: center; font-size: 12px"
>
<label style="display: flex; align-items: center; gap: 4px">
<input
type="checkbox"
name="vendored"
value="1"
checked={includeVendored}
onchange="this.form.submit()"
/>
Include vendored
</label>
<label style="display: flex; align-items: center; gap: 4px">
Fold <
<select
name="fold"
onchange="this.form.submit()"
style="padding: 2px 6px; font-size: 12px"
>
{[0, 0.5, 1, 2, 5].map((n) => (
<option
value={String(n)}
selected={Math.abs(n - foldUnderPercent) < 0.001}
>
{n === 0 ? "Off" : `${n}%`}
</option>
))}
</select>
</label>
{refParam ? (
<input type="hidden" name="ref" value={refParam} />
) : null}
</form>
</div>
<p style="color: var(--text-muted); font-size: 13px; margin-bottom: 16px">
{ref ? (
<>
Analyzed <code>{ref}</code> —{" "}
<strong>{report.countedFiles.toLocaleString()}</strong> of{" "}
{report.totalFiles.toLocaleString()} files (
{formatBytes(report.totalBytes)} total).{" "}
{includeVendored
? "Including vendored + lock files."
: "Vendored directories and lock files excluded."}
</>
) : (
<>No default branch detected — repository may be empty.</>
)}
</p>
{empty ? (
<div class="empty-state">
<h3>No classifiable files</h3>
<p>
{ref
? "Either the repo is empty, only contains vendored files, or the file types aren't recognised."
: "Push some code to see the language breakdown."}
</p>
</div>
) : (
<>
<div
style="display: flex; width: 100%; height: 12px; border-radius: 6px; overflow: hidden; margin-bottom: 6px; background: var(--bg-secondary)"
aria-label="Language breakdown"
role="img"
>
{report.buckets.map((b) => (
<div
title={`${b.language} — ${formatPercent(b.percent)}`}
style={`width: ${b.percent.toFixed(4)}%; background: ${b.color}; height: 100%`}
/>
))}
</div>
<div style="display: flex; flex-wrap: wrap; gap: 10px 16px; margin-bottom: 20px; font-size: 12px">
{report.buckets.map((b) => (
<span style="display: inline-flex; align-items: center; gap: 6px">
<span
style={`display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: ${b.color}`}
/>
<strong>{b.language}</strong>
<span style="color: var(--text-muted)">
{formatPercent(b.percent)}
</span>
</span>
))}
</div>
<table style="width: 100%; border-collapse: collapse">
<thead>
<tr>
<th style="text-align: left; padding: 8px; border-bottom: 1px solid var(--border); font-size: 12px; color: var(--text-muted)">
Language
</th>
<th style="text-align: right; padding: 8px; border-bottom: 1px solid var(--border); font-size: 12px; color: var(--text-muted); width: 120px">
Files
</th>
<th style="text-align: right; padding: 8px; border-bottom: 1px solid var(--border); font-size: 12px; color: var(--text-muted); width: 120px">
Size
</th>
<th style="text-align: right; padding: 8px; border-bottom: 1px solid var(--border); font-size: 12px; color: var(--text-muted); width: 100px">
Share
</th>
</tr>
</thead>
<tbody>
{report.buckets.map((b) => (
<tr>
<td style="padding: 8px; border-bottom: 1px solid var(--border)">
<span
style={`display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: ${b.color}; margin-right: 8px; vertical-align: middle`}
/>
{b.language}
</td>
<td style="padding: 8px; border-bottom: 1px solid var(--border); text-align: right; font-family: var(--font-mono); font-size: 12px">
{b.fileCount.toLocaleString()}
</td>
<td style="padding: 8px; border-bottom: 1px solid var(--border); text-align: right; font-family: var(--font-mono); font-size: 12px">
{formatBytes(b.bytes)}
</td>
<td style="padding: 8px; border-bottom: 1px solid var(--border); text-align: right; font-family: var(--font-mono); font-size: 12px">
{formatPercent(b.percent)}
</td>
</tr>
))}
</tbody>
</table>
</>
)}
</div>
</Layout>
);
});
export default languageRoutes;
|