Blame · Line-by-line history
web.tsx
Each line is annotated with the commit that last touched it. Click any SHA to jump to that commit and see the surrounding change.
| fc1817a | 1 | /** |
| 2 | * Web UI routes — browse repositories, code, commits, diffs. | |
| 06d5ffe | 3 | * Now auth-aware with user profiles, repo creation, stars, and syntax highlighting. |
| fc1817a | 4 | */ |
| 5 | ||
| 6 | import { Hono } from "hono"; | |
| 79136bb | 7 | import { html } from "hono/html"; |
| 06d5ffe | 8 | import { eq, and, desc } from "drizzle-orm"; |
| 9 | import { db } from "../db"; | |
| 10 | import { users, repositories, stars } from "../db/schema"; | |
| fc1817a | 11 | import { Layout } from "../views/layout"; |
| 12 | import { | |
| 13 | RepoHeader, | |
| 14 | RepoNav, | |
| 15 | Breadcrumb, | |
| 16 | FileTable, | |
| 17 | CommitList, | |
| 18 | DiffView, | |
| 06d5ffe | 19 | RepoCard, |
| 20 | BranchSwitcher, | |
| 21 | HighlightedCode, | |
| 22 | PlainCode, | |
| fc1817a | 23 | } from "../views/components"; |
| 24 | import { | |
| 25 | getTree, | |
| 26 | getBlob, | |
| 27 | listCommits, | |
| 28 | getCommit, | |
| 29 | getCommitFullMessage, | |
| 30 | getDiff, | |
| 31 | getReadme, | |
| 32 | getDefaultBranch, | |
| 33 | listBranches, | |
| 34 | repoExists, | |
| 06d5ffe | 35 | initBareRepo, |
| 79136bb | 36 | getBlame, |
| 37 | getRawBlob, | |
| 38 | searchCode, | |
| fc1817a | 39 | } from "../git/repository"; |
| 79136bb | 40 | import { renderMarkdown, markdownCss } from "../lib/markdown"; |
| 06d5ffe | 41 | import { highlightCode } from "../lib/highlight"; |
| 42 | import { softAuth, requireAuth } from "../middleware/auth"; | |
| 43 | import type { AuthEnv } from "../middleware/auth"; | |
| fc1817a | 44 | |
| 06d5ffe | 45 | const web = new Hono<AuthEnv>(); |
| 46 | ||
| 47 | // Soft auth on all web routes — c.get("user") available but may be null | |
| 48 | web.use("*", softAuth); | |
| fc1817a | 49 | |
| 50 | // Home page | |
| 06d5ffe | 51 | web.get("/", async (c) => { |
| 52 | const user = c.get("user"); | |
| 53 | ||
| 54 | if (user) { | |
| 55 | // Show user's repos | |
| 56 | const repos = await db | |
| 57 | .select() | |
| 58 | .from(repositories) | |
| 59 | .where(eq(repositories.ownerId, user.id)) | |
| 60 | .orderBy(desc(repositories.updatedAt)); | |
| 61 | ||
| 62 | return c.html( | |
| 63 | <Layout title="Dashboard" user={user}> | |
| 64 | <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px"> | |
| 65 | <h2>Your repositories</h2> | |
| 66 | <a href="/new" class="btn btn-primary"> | |
| 67 | + New repository | |
| 68 | </a> | |
| 69 | </div> | |
| 70 | {repos.length === 0 ? ( | |
| 71 | <div class="empty-state"> | |
| 72 | <h2>No repositories yet</h2> | |
| 73 | <p>Create your first repository to get started.</p> | |
| 74 | </div> | |
| 75 | ) : ( | |
| 76 | <div class="card-grid"> | |
| 77 | {repos.map((repo) => ( | |
| 78 | <RepoCard repo={repo} ownerName={user.username} /> | |
| 79 | ))} | |
| 80 | </div> | |
| 81 | )} | |
| 82 | </Layout> | |
| 83 | ); | |
| 84 | } | |
| 85 | ||
| fc1817a | 86 | return c.html( |
| 06d5ffe | 87 | <Layout user={null}> |
| fc1817a | 88 | <div class="empty-state"> |
| 89 | <h2>gluecron</h2> | |
| 90 | <p>AI-native code intelligence platform</p> | |
| 06d5ffe | 91 | <div style="margin-top: 24px; display: flex; gap: 12px; justify-content: center"> |
| 92 | <a href="/register" class="btn btn-primary"> | |
| 93 | Get started | |
| 94 | </a> | |
| 95 | <a href="/login" class="btn"> | |
| 96 | Sign in | |
| 97 | </a> | |
| 98 | </div> | |
| 99 | <pre style="margin-top: 32px">{`# Quick start | |
| fc1817a | 100 | curl -X POST http://localhost:3000/api/setup \\ |
| 101 | -H 'Content-Type: application/json' \\ | |
| 102 | -d '{"username":"you","email":"you@dev.com","repoName":"hello"}' | |
| 103 | ||
| 104 | git remote add gluecron http://localhost:3000/you/hello.git | |
| 105 | git push gluecron main`}</pre> | |
| 106 | </div> | |
| 107 | </Layout> | |
| 108 | ); | |
| 109 | }); | |
| 110 | ||
| 06d5ffe | 111 | // New repository form |
| 112 | web.get("/new", requireAuth, (c) => { | |
| 113 | const user = c.get("user")!; | |
| 114 | const error = c.req.query("error"); | |
| 115 | ||
| 116 | return c.html( | |
| 117 | <Layout title="New repository" user={user}> | |
| 118 | <div class="new-repo-form"> | |
| 119 | <h2>Create a new repository</h2> | |
| 120 | {error && <div class="auth-error">{decodeURIComponent(error)}</div>} | |
| 121 | <form method="POST" action="/new"> | |
| 122 | <div class="form-group"> | |
| 123 | <label>Owner</label> | |
| 124 | <input type="text" value={user.username} disabled class="input-disabled" /> | |
| 125 | </div> | |
| 126 | <div class="form-group"> | |
| 127 | <label for="name">Repository name</label> | |
| 128 | <input | |
| 129 | type="text" | |
| 130 | id="name" | |
| 131 | name="name" | |
| 132 | required | |
| 133 | pattern="^[a-zA-Z0-9._-]+$" | |
| 134 | placeholder="my-project" | |
| 135 | autocomplete="off" | |
| 136 | /> | |
| 137 | </div> | |
| 138 | <div class="form-group"> | |
| 139 | <label for="description">Description (optional)</label> | |
| 140 | <input | |
| 141 | type="text" | |
| 142 | id="description" | |
| 143 | name="description" | |
| 144 | placeholder="A short description of your repository" | |
| 145 | /> | |
| 146 | </div> | |
| 147 | <div class="visibility-options"> | |
| 148 | <label class="visibility-option"> | |
| 149 | <input type="radio" name="visibility" value="public" checked /> | |
| 150 | <div class="vis-label">Public</div> | |
| 151 | <div class="vis-desc">Anyone can see this repository</div> | |
| 152 | </label> | |
| 153 | <label class="visibility-option"> | |
| 154 | <input type="radio" name="visibility" value="private" /> | |
| 155 | <div class="vis-label">Private</div> | |
| 156 | <div class="vis-desc">Only you can see this repository</div> | |
| 157 | </label> | |
| 158 | </div> | |
| 159 | <button type="submit" class="btn btn-primary"> | |
| 160 | Create repository | |
| 161 | </button> | |
| 162 | </form> | |
| 163 | </div> | |
| 164 | </Layout> | |
| 165 | ); | |
| 166 | }); | |
| 167 | ||
| 168 | web.post("/new", requireAuth, async (c) => { | |
| 169 | const user = c.get("user")!; | |
| 170 | const body = await c.req.parseBody(); | |
| 171 | const name = String(body.name || "").trim(); | |
| 172 | const description = String(body.description || "").trim(); | |
| 173 | const isPrivate = body.visibility === "private"; | |
| 174 | ||
| 175 | if (!name) { | |
| 176 | return c.redirect("/new?error=Repository+name+is+required"); | |
| 177 | } | |
| 178 | ||
| 179 | if (!/^[a-zA-Z0-9._-]+$/.test(name)) { | |
| 180 | return c.redirect("/new?error=Invalid+repository+name"); | |
| 181 | } | |
| 182 | ||
| 183 | if (await repoExists(user.username, name)) { | |
| 184 | return c.redirect("/new?error=Repository+already+exists"); | |
| 185 | } | |
| 186 | ||
| 187 | const diskPath = await initBareRepo(user.username, name); | |
| 188 | ||
| 189 | await db.insert(repositories).values({ | |
| 190 | name, | |
| 191 | ownerId: user.id, | |
| 192 | description: description || null, | |
| 193 | isPrivate, | |
| 194 | diskPath, | |
| 195 | }); | |
| 196 | ||
| 197 | return c.redirect(`/${user.username}/${name}`); | |
| 198 | }); | |
| 199 | ||
| 200 | // User profile | |
| fc1817a | 201 | web.get("/:owner", async (c) => { |
| 06d5ffe | 202 | const { owner: ownerName } = c.req.param(); |
| 203 | const user = c.get("user"); | |
| 204 | ||
| 205 | // Avoid clashing with fixed routes | |
| 206 | if ( | |
| 207 | ["login", "register", "logout", "new", "settings", "api"].includes( | |
| 208 | ownerName | |
| 209 | ) | |
| 210 | ) { | |
| 211 | return c.notFound(); | |
| 212 | } | |
| 213 | ||
| 214 | let ownerUser; | |
| 215 | try { | |
| 216 | const [found] = await db | |
| 217 | .select() | |
| 218 | .from(users) | |
| 219 | .where(eq(users.username, ownerName)) | |
| 220 | .limit(1); | |
| 221 | ownerUser = found; | |
| 222 | } catch { | |
| 223 | // DB not available — check if repos exist on disk | |
| 224 | ownerUser = null; | |
| 225 | } | |
| 226 | ||
| 227 | // Even without DB, show repos if they exist on disk | |
| 228 | let repos: any[] = []; | |
| 229 | if (ownerUser) { | |
| 230 | const allRepos = await db | |
| 231 | .select() | |
| 232 | .from(repositories) | |
| 233 | .where(eq(repositories.ownerId, ownerUser.id)) | |
| 234 | .orderBy(desc(repositories.updatedAt)); | |
| 235 | ||
| 236 | // Show public repos to everyone, private only to owner | |
| 237 | repos = | |
| 238 | user?.id === ownerUser.id | |
| 239 | ? allRepos | |
| 240 | : allRepos.filter((r) => !r.isPrivate); | |
| 241 | } | |
| 242 | ||
| fc1817a | 243 | return c.html( |
| 06d5ffe | 244 | <Layout title={ownerName} user={user}> |
| 245 | <div class="user-profile"> | |
| 246 | <div class="user-avatar"> | |
| 247 | {(ownerUser?.displayName || ownerName)[0].toUpperCase()} | |
| 248 | </div> | |
| 249 | <div class="user-info"> | |
| 250 | <h2>{ownerUser?.displayName || ownerName}</h2> | |
| 251 | <div class="username">@{ownerName}</div> | |
| 252 | {ownerUser?.bio && <div class="bio">{ownerUser.bio}</div>} | |
| 253 | </div> | |
| 254 | </div> | |
| 255 | <h3 style="margin-bottom: 16px">Repositories</h3> | |
| 256 | {repos.length === 0 ? ( | |
| 257 | <p style="color: var(--text-muted)">No repositories yet.</p> | |
| 258 | ) : ( | |
| 259 | <div class="card-grid"> | |
| 260 | {repos.map((repo) => ( | |
| 261 | <RepoCard repo={repo} ownerName={ownerName} /> | |
| 262 | ))} | |
| 263 | </div> | |
| 264 | )} | |
| fc1817a | 265 | </Layout> |
| 266 | ); | |
| 267 | }); | |
| 268 | ||
| 06d5ffe | 269 | // Star/unstar a repo |
| 270 | web.post("/:owner/:repo/star", requireAuth, async (c) => { | |
| 271 | const { owner: ownerName, repo: repoName } = c.req.param(); | |
| 272 | const user = c.get("user")!; | |
| 273 | ||
| 274 | try { | |
| 275 | const [ownerUser] = await db | |
| 276 | .select() | |
| 277 | .from(users) | |
| 278 | .where(eq(users.username, ownerName)) | |
| 279 | .limit(1); | |
| 280 | if (!ownerUser) return c.redirect(`/${ownerName}/${repoName}`); | |
| 281 | ||
| 282 | const [repo] = await db | |
| 283 | .select() | |
| 284 | .from(repositories) | |
| 285 | .where( | |
| 286 | and( | |
| 287 | eq(repositories.ownerId, ownerUser.id), | |
| 288 | eq(repositories.name, repoName) | |
| 289 | ) | |
| 290 | ) | |
| 291 | .limit(1); | |
| 292 | if (!repo) return c.redirect(`/${ownerName}/${repoName}`); | |
| 293 | ||
| 294 | // Toggle star | |
| 295 | const [existing] = await db | |
| 296 | .select() | |
| 297 | .from(stars) | |
| 298 | .where( | |
| 299 | and(eq(stars.userId, user.id), eq(stars.repositoryId, repo.id)) | |
| 300 | ) | |
| 301 | .limit(1); | |
| 302 | ||
| 303 | if (existing) { | |
| 304 | await db.delete(stars).where(eq(stars.id, existing.id)); | |
| 305 | await db | |
| 306 | .update(repositories) | |
| 307 | .set({ starCount: Math.max(0, repo.starCount - 1) }) | |
| 308 | .where(eq(repositories.id, repo.id)); | |
| 309 | } else { | |
| 310 | await db.insert(stars).values({ | |
| 311 | userId: user.id, | |
| 312 | repositoryId: repo.id, | |
| 313 | }); | |
| 314 | await db | |
| 315 | .update(repositories) | |
| 316 | .set({ starCount: repo.starCount + 1 }) | |
| 317 | .where(eq(repositories.id, repo.id)); | |
| 318 | } | |
| 319 | } catch { | |
| 320 | // DB error — ignore | |
| 321 | } | |
| 322 | ||
| 323 | return c.redirect(`/${ownerName}/${repoName}`); | |
| 324 | }); | |
| 325 | ||
| fc1817a | 326 | // Repository overview — file tree at HEAD |
| 327 | web.get("/:owner/:repo", async (c) => { | |
| 328 | const { owner, repo } = c.req.param(); | |
| 06d5ffe | 329 | const user = c.get("user"); |
| fc1817a | 330 | |
| 331 | if (!(await repoExists(owner, repo))) { | |
| 332 | return c.html( | |
| 06d5ffe | 333 | <Layout title="Not Found" user={user}> |
| fc1817a | 334 | <div class="empty-state"> |
| 335 | <h2>Repository not found</h2> | |
| 336 | <p> | |
| 337 | {owner}/{repo} does not exist. | |
| 338 | </p> | |
| 339 | </div> | |
| 340 | </Layout>, | |
| 341 | 404 | |
| 342 | ); | |
| 343 | } | |
| 344 | ||
| 345 | const defaultBranch = (await getDefaultBranch(owner, repo)) || "main"; | |
| 06d5ffe | 346 | const branches = await listBranches(owner, repo); |
| fc1817a | 347 | const tree = await getTree(owner, repo, defaultBranch); |
| 348 | ||
| 06d5ffe | 349 | // Get star info if user logged in |
| 350 | let starCount = 0; | |
| 351 | let starred = false; | |
| 352 | try { | |
| 353 | const [ownerUser] = await db | |
| 354 | .select() | |
| 355 | .from(users) | |
| 356 | .where(eq(users.username, owner)) | |
| 357 | .limit(1); | |
| 358 | if (ownerUser) { | |
| 359 | const [repoRow] = await db | |
| 360 | .select() | |
| 361 | .from(repositories) | |
| 362 | .where( | |
| 363 | and( | |
| 364 | eq(repositories.ownerId, ownerUser.id), | |
| 365 | eq(repositories.name, repo) | |
| 366 | ) | |
| 367 | ) | |
| 368 | .limit(1); | |
| 369 | if (repoRow) { | |
| 370 | starCount = repoRow.starCount; | |
| 371 | if (user) { | |
| 372 | const [star] = await db | |
| 373 | .select() | |
| 374 | .from(stars) | |
| 375 | .where( | |
| 376 | and( | |
| 377 | eq(stars.userId, user.id), | |
| 378 | eq(stars.repositoryId, repoRow.id) | |
| 379 | ) | |
| 380 | ) | |
| 381 | .limit(1); | |
| 382 | starred = !!star; | |
| 383 | } | |
| 384 | } | |
| 385 | } | |
| 386 | } catch { | |
| 387 | // DB not available | |
| 388 | } | |
| 389 | ||
| fc1817a | 390 | if (tree.length === 0) { |
| 391 | return c.html( | |
| 06d5ffe | 392 | <Layout title={`${owner}/${repo}`} user={user}> |
| 393 | <RepoHeader | |
| 394 | owner={owner} | |
| 395 | repo={repo} | |
| 396 | starCount={starCount} | |
| 397 | starred={starred} | |
| 398 | currentUser={user?.username} | |
| 399 | /> | |
| fc1817a | 400 | <RepoNav owner={owner} repo={repo} active="code" /> |
| 401 | <div class="empty-state"> | |
| 402 | <h2>Empty repository</h2> | |
| 403 | <p>Get started by pushing code:</p> | |
| 404 | <pre>{`git remote add gluecron http://localhost:3000/${owner}/${repo}.git | |
| 405 | git push -u gluecron main`}</pre> | |
| 406 | </div> | |
| 407 | </Layout> | |
| 408 | ); | |
| 409 | } | |
| 410 | ||
| 411 | const readme = await getReadme(owner, repo, defaultBranch); | |
| 412 | ||
| 413 | return c.html( | |
| 06d5ffe | 414 | <Layout title={`${owner}/${repo}`} user={user}> |
| 415 | <RepoHeader | |
| 416 | owner={owner} | |
| 417 | repo={repo} | |
| 418 | starCount={starCount} | |
| 419 | starred={starred} | |
| 420 | currentUser={user?.username} | |
| 421 | /> | |
| fc1817a | 422 | <RepoNav owner={owner} repo={repo} active="code" /> |
| 06d5ffe | 423 | <BranchSwitcher |
| 424 | owner={owner} | |
| 425 | repo={repo} | |
| 426 | currentRef={defaultBranch} | |
| 427 | branches={branches} | |
| 428 | pathType="tree" | |
| 429 | /> | |
| fc1817a | 430 | <FileTable |
| 431 | entries={tree} | |
| 432 | owner={owner} | |
| 433 | repo={repo} | |
| 434 | ref={defaultBranch} | |
| 435 | path="" | |
| 436 | /> | |
| 79136bb | 437 | {readme && (() => { |
| 438 | const readmeHtml = renderMarkdown(readme); | |
| 439 | return ( | |
| 440 | <div class="blob-view" style="margin-top: 20px"> | |
| 441 | <div class="blob-header">README.md</div> | |
| 442 | <style>{markdownCss}</style> | |
| 443 | <div class="markdown-body"> | |
| 444 | {html([readmeHtml] as unknown as TemplateStringsArray)} | |
| 445 | </div> | |
| fc1817a | 446 | </div> |
| 79136bb | 447 | ); |
| 448 | })()} | |
| fc1817a | 449 | </Layout> |
| 450 | ); | |
| 451 | }); | |
| 452 | ||
| 453 | // Browse tree at ref/path | |
| 454 | web.get("/:owner/:repo/tree/:ref{.+$}", async (c) => { | |
| 455 | const { owner, repo } = c.req.param(); | |
| 06d5ffe | 456 | const user = c.get("user"); |
| fc1817a | 457 | const refAndPath = c.req.param("ref"); |
| 458 | ||
| 459 | const branches = await listBranches(owner, repo); | |
| 460 | let ref = ""; | |
| 461 | let treePath = ""; | |
| 462 | ||
| 463 | for (const branch of branches) { | |
| 464 | if (refAndPath === branch || refAndPath.startsWith(branch + "/")) { | |
| 465 | ref = branch; | |
| 466 | treePath = refAndPath.slice(branch.length + 1); | |
| 467 | break; | |
| 468 | } | |
| 469 | } | |
| 470 | ||
| 471 | if (!ref) { | |
| 472 | const slashIdx = refAndPath.indexOf("/"); | |
| 473 | if (slashIdx === -1) { | |
| 474 | ref = refAndPath; | |
| 475 | } else { | |
| 476 | ref = refAndPath.slice(0, slashIdx); | |
| 477 | treePath = refAndPath.slice(slashIdx + 1); | |
| 478 | } | |
| 479 | } | |
| 480 | ||
| 481 | const tree = await getTree(owner, repo, ref, treePath); | |
| 482 | ||
| 483 | return c.html( | |
| 06d5ffe | 484 | <Layout title={`${treePath || "/"} — ${owner}/${repo}`} user={user}> |
| fc1817a | 485 | <RepoHeader owner={owner} repo={repo} /> |
| 486 | <RepoNav owner={owner} repo={repo} active="code" /> | |
| 06d5ffe | 487 | <BranchSwitcher |
| 488 | owner={owner} | |
| 489 | repo={repo} | |
| 490 | currentRef={ref} | |
| 491 | branches={branches} | |
| 492 | pathType="tree" | |
| 493 | subPath={treePath} | |
| 494 | /> | |
| fc1817a | 495 | <Breadcrumb owner={owner} repo={repo} ref={ref} path={treePath} /> |
| 496 | <FileTable | |
| 497 | entries={tree} | |
| 498 | owner={owner} | |
| 499 | repo={repo} | |
| 500 | ref={ref} | |
| 501 | path={treePath} | |
| 502 | /> | |
| 503 | </Layout> | |
| 504 | ); | |
| 505 | }); | |
| 506 | ||
| 06d5ffe | 507 | // View file blob with syntax highlighting |
| fc1817a | 508 | web.get("/:owner/:repo/blob/:ref{.+$}", async (c) => { |
| 509 | const { owner, repo } = c.req.param(); | |
| 06d5ffe | 510 | const user = c.get("user"); |
| fc1817a | 511 | const refAndPath = c.req.param("ref"); |
| 512 | ||
| 513 | const branches = await listBranches(owner, repo); | |
| 514 | let ref = ""; | |
| 515 | let filePath = ""; | |
| 516 | ||
| 517 | for (const branch of branches) { | |
| 518 | if (refAndPath.startsWith(branch + "/")) { | |
| 519 | ref = branch; | |
| 520 | filePath = refAndPath.slice(branch.length + 1); | |
| 521 | break; | |
| 522 | } | |
| 523 | } | |
| 524 | ||
| 525 | if (!ref) { | |
| 526 | const slashIdx = refAndPath.indexOf("/"); | |
| 527 | if (slashIdx === -1) return c.text("Not found", 404); | |
| 528 | ref = refAndPath.slice(0, slashIdx); | |
| 529 | filePath = refAndPath.slice(slashIdx + 1); | |
| 530 | } | |
| 531 | ||
| 532 | const blob = await getBlob(owner, repo, ref, filePath); | |
| 533 | if (!blob) { | |
| 534 | return c.html( | |
| 06d5ffe | 535 | <Layout title="Not Found" user={user}> |
| fc1817a | 536 | <div class="empty-state"> |
| 537 | <h2>File not found</h2> | |
| 538 | </div> | |
| 539 | </Layout>, | |
| 540 | 404 | |
| 541 | ); | |
| 542 | } | |
| 543 | ||
| 06d5ffe | 544 | const fileName = filePath.split("/").pop() || filePath; |
| fc1817a | 545 | |
| 546 | return c.html( | |
| 06d5ffe | 547 | <Layout title={`${filePath} — ${owner}/${repo}`} user={user}> |
| fc1817a | 548 | <RepoHeader owner={owner} repo={repo} /> |
| 549 | <RepoNav owner={owner} repo={repo} active="code" /> | |
| 06d5ffe | 550 | <BranchSwitcher |
| 551 | owner={owner} | |
| 552 | repo={repo} | |
| 553 | currentRef={ref} | |
| 554 | branches={branches} | |
| 555 | pathType="blob" | |
| 556 | subPath={filePath} | |
| 557 | /> | |
| fc1817a | 558 | <Breadcrumb owner={owner} repo={repo} ref={ref} path={filePath} /> |
| 559 | <div class="blob-view"> | |
| 560 | <div class="blob-header"> | |
| 06d5ffe | 561 | <span>{fileName} — {blob.size} bytes</span> |
| 79136bb | 562 | <span style="display: flex; gap: 12px"> |
| 563 | <a href={`/${owner}/${repo}/raw/${ref}/${filePath}`} style="font-size: 12px"> | |
| 564 | Raw | |
| 565 | </a> | |
| 566 | <a href={`/${owner}/${repo}/blame/${ref}/${filePath}`} style="font-size: 12px"> | |
| 567 | Blame | |
| 568 | </a> | |
| 16b325c | 569 | <a href={`/${owner}/${repo}/timeline/${ref}/${filePath}`} style="font-size: 12px"> |
| 570 | History | |
| 571 | </a> | |
| 0074234 | 572 | {user && ( |
| 573 | <a href={`/${owner}/${repo}/edit/${ref}/${filePath}`} style="font-size: 12px"> | |
| 574 | Edit | |
| 575 | </a> | |
| 576 | )} | |
| 79136bb | 577 | </span> |
| fc1817a | 578 | </div> |
| 579 | {blob.isBinary ? ( | |
| 580 | <div style="padding: 16px; color: var(--text-muted)"> | |
| 581 | Binary file not shown. | |
| 582 | </div> | |
| 06d5ffe | 583 | ) : (() => { |
| 584 | const { html: highlighted, language } = highlightCode( | |
| 585 | blob.content, | |
| 586 | fileName | |
| 587 | ); | |
| 588 | const lineCount = blob.content.split("\n").length; | |
| 589 | // Trim trailing newline from count | |
| 590 | const adjustedCount = | |
| 591 | blob.content.endsWith("\n") ? lineCount - 1 : lineCount; | |
| 592 | ||
| 593 | if (language) { | |
| 594 | return ( | |
| 595 | <HighlightedCode | |
| 596 | highlightedHtml={highlighted} | |
| 597 | lineCount={adjustedCount} | |
| 598 | /> | |
| 599 | ); | |
| 600 | } | |
| 601 | const lines = blob.content.split("\n"); | |
| 602 | if (lines[lines.length - 1] === "") lines.pop(); | |
| 603 | return <PlainCode lines={lines} />; | |
| 604 | })()} | |
| fc1817a | 605 | </div> |
| 606 | </Layout> | |
| 607 | ); | |
| 608 | }); | |
| 609 | ||
| 610 | // Commit log | |
| 611 | web.get("/:owner/:repo/commits/:ref?", async (c) => { | |
| 612 | const { owner, repo } = c.req.param(); | |
| 06d5ffe | 613 | const user = c.get("user"); |
| fc1817a | 614 | const ref = |
| 615 | c.req.param("ref") || (await getDefaultBranch(owner, repo)) || "main"; | |
| 06d5ffe | 616 | const branches = await listBranches(owner, repo); |
| fc1817a | 617 | |
| 618 | const commits = await listCommits(owner, repo, ref, 50); | |
| 619 | ||
| 620 | return c.html( | |
| 06d5ffe | 621 | <Layout title={`Commits — ${owner}/${repo}`} user={user}> |
| fc1817a | 622 | <RepoHeader owner={owner} repo={repo} /> |
| 623 | <RepoNav owner={owner} repo={repo} active="commits" /> | |
| 06d5ffe | 624 | <BranchSwitcher |
| 625 | owner={owner} | |
| 626 | repo={repo} | |
| 627 | currentRef={ref} | |
| 628 | branches={branches} | |
| 629 | pathType="commits" | |
| 630 | /> | |
| fc1817a | 631 | {commits.length === 0 ? ( |
| 632 | <div class="empty-state"> | |
| 633 | <p>No commits yet.</p> | |
| 634 | </div> | |
| 635 | ) : ( | |
| 636 | <CommitList commits={commits} owner={owner} repo={repo} /> | |
| 637 | )} | |
| 638 | </Layout> | |
| 639 | ); | |
| 640 | }); | |
| 641 | ||
| 642 | // Single commit with diff | |
| 643 | web.get("/:owner/:repo/commit/:sha", async (c) => { | |
| 644 | const { owner, repo, sha } = c.req.param(); | |
| 06d5ffe | 645 | const user = c.get("user"); |
| fc1817a | 646 | |
| 647 | const commit = await getCommit(owner, repo, sha); | |
| 648 | if (!commit) { | |
| 649 | return c.html( | |
| 06d5ffe | 650 | <Layout title="Not Found" user={user}> |
| fc1817a | 651 | <div class="empty-state"> |
| 652 | <h2>Commit not found</h2> | |
| 653 | </div> | |
| 654 | </Layout>, | |
| 655 | 404 | |
| 656 | ); | |
| 657 | } | |
| 658 | ||
| 659 | const fullMessage = await getCommitFullMessage(owner, repo, sha); | |
| 660 | const { files, raw } = await getDiff(owner, repo, sha); | |
| 661 | ||
| 662 | return c.html( | |
| 06d5ffe | 663 | <Layout title={`${commit.message} — ${owner}/${repo}`} user={user}> |
| fc1817a | 664 | <RepoHeader owner={owner} repo={repo} /> |
| 665 | <div | |
| 666 | style="background: var(--bg-secondary); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin-bottom: 20px" | |
| 667 | > | |
| 668 | <div style="font-size: 18px; font-weight: 600; margin-bottom: 8px"> | |
| 669 | {commit.message} | |
| 670 | </div> | |
| 671 | {fullMessage !== commit.message && ( | |
| 672 | <div style="white-space: pre-wrap; color: var(--text-muted); font-size: 14px; margin-bottom: 12px"> | |
| 673 | {fullMessage} | |
| 674 | </div> | |
| 675 | )} | |
| 676 | <div style="font-size: 13px; color: var(--text-muted)"> | |
| 677 | <strong style="color: var(--text)">{commit.author}</strong>{" "} | |
| 678 | committed on{" "} | |
| 679 | {new Date(commit.date).toLocaleDateString("en-US", { | |
| 680 | month: "long", | |
| 681 | day: "numeric", | |
| 682 | year: "numeric", | |
| 683 | })} | |
| 684 | </div> | |
| 685 | <div style="margin-top: 8px"> | |
| 686 | <span class="commit-sha">{commit.sha}</span> | |
| 687 | {commit.parentShas.length > 0 && ( | |
| 688 | <span style="margin-left: 12px; font-size: 13px; color: var(--text-muted)"> | |
| 689 | Parent:{" "} | |
| 690 | {commit.parentShas.map((p) => ( | |
| 691 | <a | |
| 692 | href={`/${owner}/${repo}/commit/${p}`} | |
| 693 | class="commit-sha" | |
| 694 | style="margin-left: 4px" | |
| 695 | > | |
| 696 | {p.slice(0, 7)} | |
| 697 | </a> | |
| 698 | ))} | |
| 699 | </span> | |
| 700 | )} | |
| 701 | </div> | |
| 702 | </div> | |
| 703 | <DiffView raw={raw} files={files} /> | |
| 704 | </Layout> | |
| 705 | ); | |
| 706 | }); | |
| 707 | ||
| 79136bb | 708 | // Raw file download |
| 709 | web.get("/:owner/:repo/raw/:ref{.+$}", async (c) => { | |
| 710 | const { owner, repo } = c.req.param(); | |
| 711 | const refAndPath = c.req.param("ref"); | |
| 712 | ||
| 713 | const branches = await listBranches(owner, repo); | |
| 714 | let ref = ""; | |
| 715 | let filePath = ""; | |
| 716 | ||
| 717 | for (const branch of branches) { | |
| 718 | if (refAndPath.startsWith(branch + "/")) { | |
| 719 | ref = branch; | |
| 720 | filePath = refAndPath.slice(branch.length + 1); | |
| 721 | break; | |
| 722 | } | |
| 723 | } | |
| 724 | ||
| 725 | if (!ref) { | |
| 726 | const slashIdx = refAndPath.indexOf("/"); | |
| 727 | if (slashIdx === -1) return c.text("Not found", 404); | |
| 728 | ref = refAndPath.slice(0, slashIdx); | |
| 729 | filePath = refAndPath.slice(slashIdx + 1); | |
| 730 | } | |
| 731 | ||
| 732 | const data = await getRawBlob(owner, repo, ref, filePath); | |
| 733 | if (!data) return c.text("Not found", 404); | |
| 734 | ||
| 735 | const fileName = filePath.split("/").pop() || "file"; | |
| 736 | return new Response(data, { | |
| 737 | headers: { | |
| 738 | "Content-Type": "application/octet-stream", | |
| 739 | "Content-Disposition": `attachment; filename="${fileName}"`, | |
| 740 | "Cache-Control": "no-cache", | |
| 741 | }, | |
| 742 | }); | |
| 743 | }); | |
| 744 | ||
| 745 | // Blame view | |
| 746 | web.get("/:owner/:repo/blame/:ref{.+$}", async (c) => { | |
| 747 | const { owner, repo } = c.req.param(); | |
| 748 | const user = c.get("user"); | |
| 749 | const refAndPath = c.req.param("ref"); | |
| 750 | ||
| 751 | const branches = await listBranches(owner, repo); | |
| 752 | let ref = ""; | |
| 753 | let filePath = ""; | |
| 754 | ||
| 755 | for (const branch of branches) { | |
| 756 | if (refAndPath.startsWith(branch + "/")) { | |
| 757 | ref = branch; | |
| 758 | filePath = refAndPath.slice(branch.length + 1); | |
| 759 | break; | |
| 760 | } | |
| 761 | } | |
| 762 | ||
| 763 | if (!ref) { | |
| 764 | const slashIdx = refAndPath.indexOf("/"); | |
| 765 | if (slashIdx === -1) return c.text("Not found", 404); | |
| 766 | ref = refAndPath.slice(0, slashIdx); | |
| 767 | filePath = refAndPath.slice(slashIdx + 1); | |
| 768 | } | |
| 769 | ||
| 770 | const blameLines = await getBlame(owner, repo, ref, filePath); | |
| 771 | if (blameLines.length === 0) { | |
| 772 | return c.html( | |
| 773 | <Layout title="Not Found" user={user}> | |
| 774 | <div class="empty-state"> | |
| 775 | <h2>File not found</h2> | |
| 776 | </div> | |
| 777 | </Layout>, | |
| 778 | 404 | |
| 779 | ); | |
| 780 | } | |
| 781 | ||
| 782 | const fileName = filePath.split("/").pop() || filePath; | |
| 783 | ||
| 784 | return c.html( | |
| 785 | <Layout title={`Blame: ${filePath} — ${owner}/${repo}`} user={user}> | |
| 786 | <RepoHeader owner={owner} repo={repo} /> | |
| 787 | <RepoNav owner={owner} repo={repo} active="code" /> | |
| 788 | <Breadcrumb owner={owner} repo={repo} ref={ref} path={filePath} /> | |
| 789 | <div class="blob-view"> | |
| 790 | <div class="blob-header"> | |
| 791 | <span>{fileName} — blame</span> | |
| 792 | <a href={`/${owner}/${repo}/blob/${ref}/${filePath}`} style="font-size: 12px"> | |
| 793 | Normal view | |
| 794 | </a> | |
| 795 | </div> | |
| 796 | <div class="blob-code" style="overflow-x: auto"> | |
| 797 | <table style="width: 100%; border-collapse: collapse; font-size: 13px; font-family: var(--font-mono)"> | |
| 798 | <tbody> | |
| 799 | {blameLines.map((line, i) => { | |
| 800 | const showInfo = | |
| 801 | i === 0 || blameLines[i - 1].sha !== line.sha; | |
| 802 | return ( | |
| 803 | <tr style="border-bottom: 1px solid var(--border)"> | |
| 804 | <td | |
| 805 | style={`width: 200px; padding: 0 8px; font-size: 11px; color: var(--text-muted); white-space: nowrap; vertical-align: top; ${showInfo ? "border-top: 1px solid var(--border)" : ""}`} | |
| 806 | > | |
| 807 | {showInfo && ( | |
| 808 | <> | |
| 809 | <a | |
| 810 | href={`/${owner}/${repo}/commit/${line.sha}`} | |
| 811 | style="color: var(--text-link); font-family: var(--font-mono)" | |
| 812 | > | |
| 813 | {line.sha.slice(0, 7)} | |
| 814 | </a>{" "} | |
| 815 | <span>{line.author}</span> | |
| 816 | </> | |
| 817 | )} | |
| 818 | </td> | |
| 819 | <td class="line-num">{line.lineNum}</td> | |
| 820 | <td class="line-content">{line.content}</td> | |
| 821 | </tr> | |
| 822 | ); | |
| 823 | })} | |
| 824 | </tbody> | |
| 825 | </table> | |
| 826 | </div> | |
| 827 | </div> | |
| 828 | </Layout> | |
| 829 | ); | |
| 830 | }); | |
| 831 | ||
| 832 | // Search | |
| 833 | web.get("/:owner/:repo/search", async (c) => { | |
| 834 | const { owner, repo } = c.req.param(); | |
| 835 | const user = c.get("user"); | |
| 836 | const q = c.req.query("q") || ""; | |
| 837 | ||
| 838 | if (!(await repoExists(owner, repo))) return c.notFound(); | |
| 839 | ||
| 840 | const defaultBranch = (await getDefaultBranch(owner, repo)) || "main"; | |
| 841 | let results: Array<{ file: string; lineNum: number; line: string }> = []; | |
| 842 | ||
| 843 | if (q.trim()) { | |
| 844 | results = await searchCode(owner, repo, defaultBranch, q.trim()); | |
| 845 | } | |
| 846 | ||
| 847 | return c.html( | |
| 848 | <Layout title={`Search — ${owner}/${repo}`} user={user}> | |
| 849 | <RepoHeader owner={owner} repo={repo} /> | |
| 850 | <RepoNav owner={owner} repo={repo} active="code" /> | |
| 851 | <form | |
| 852 | method="GET" | |
| 853 | action={`/${owner}/${repo}/search`} | |
| 854 | style="margin-bottom: 20px" | |
| 855 | > | |
| 856 | <div style="display: flex; gap: 8px"> | |
| 857 | <input | |
| 858 | type="text" | |
| 859 | name="q" | |
| 860 | value={q} | |
| 861 | placeholder="Search code..." | |
| 862 | style="flex: 1; padding: 8px 12px; background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); color: var(--text); font-size: 14px" | |
| 863 | /> | |
| 864 | <button type="submit" class="btn btn-primary"> | |
| 865 | Search | |
| 866 | </button> | |
| 867 | </div> | |
| 868 | </form> | |
| 869 | {q && ( | |
| 870 | <p style="font-size: 14px; color: var(--text-muted); margin-bottom: 16px"> | |
| 871 | {results.length} result{results.length !== 1 ? "s" : ""} for{" "} | |
| 872 | <strong style="color: var(--text)">"{q}"</strong> | |
| 873 | </p> | |
| 874 | )} | |
| 875 | {results.length > 0 && ( | |
| 876 | <div class="search-results"> | |
| 877 | {(() => { | |
| 878 | // Group by file | |
| 879 | const grouped: Record< | |
| 880 | string, | |
| 881 | Array<{ lineNum: number; line: string }> | |
| 882 | > = {}; | |
| 883 | for (const r of results) { | |
| 884 | if (!grouped[r.file]) grouped[r.file] = []; | |
| 885 | grouped[r.file].push({ lineNum: r.lineNum, line: r.line }); | |
| 886 | } | |
| 887 | return Object.entries(grouped).map(([file, matches]) => ( | |
| 888 | <div class="diff-file" style="margin-bottom: 12px"> | |
| 889 | <div class="diff-file-header"> | |
| 890 | <a | |
| 891 | href={`/${owner}/${repo}/blob/${defaultBranch}/${file}`} | |
| 892 | > | |
| 893 | {file} | |
| 894 | </a> | |
| 895 | </div> | |
| 896 | <div class="blob-code"> | |
| 897 | <table> | |
| 898 | <tbody> | |
| 899 | {matches.map((m) => ( | |
| 900 | <tr> | |
| 901 | <td class="line-num">{m.lineNum}</td> | |
| 902 | <td class="line-content">{m.line}</td> | |
| 903 | </tr> | |
| 904 | ))} | |
| 905 | </tbody> | |
| 906 | </table> | |
| 907 | </div> | |
| 908 | </div> | |
| 909 | )); | |
| 910 | })()} | |
| 911 | </div> | |
| 912 | )} | |
| 913 | </Layout> | |
| 914 | ); | |
| 915 | }); | |
| 916 | ||
| fc1817a | 917 | export default web; |