Pre-launch — Gluecron is in final validation. Public signups and git hosting for non-owner users open after launch review.
CodeIssuesDiscussionsWikiPull RequestsProjectsCommitsActionsReleasesContributorsPulse● GatesSecuritySettingsDeploymentsPipelineInsightsAgents✨ Explain✨ Ask AI✨ Workspace✨ Spec✨ Tests▓ Debt Map✨ NL Search🏛 Archaeology
claude/adoring-hopper-5x74bqclaude/affectionate-feynman-ykrf1hclaude/architecture-audit-design-wxprenclaude/build-status-update-3MXsfclaude/charming-meitner-mllb5rclaude/compare-gate-gluecron-s4mFQclaude/confident-faraday-tikcwbclaude/continue-work-XMTlIclaude/crontech-gluecron-deploy-7MIECclaude/crontech-platform-setup-SeKfwclaude/design-2026claude/ecstatic-ptolemy-jMdigclaude/enhance-github-integration-QNHdGclaude/fix-aa-loop-issue-PonMQclaude/fix-actions-and-processclaude/fix-desktop-errors-XqoW8claude/fix-red-workflowsclaude/fix-website-access-6FKJNclaude/gatetest-integration-hardeningclaude/github-audit-improvements-bDFr9claude/gluecron-launch-status-FoMRlclaude/hopeful-lamport-olfCTclaude/issue-to-pr-and-protectionsclaude/jolly-heisenberg-2sg1Qclaude/launch-preparation-QmTb6claude/new-session-xk1l7claude/plan-platform-architecture-kkN4yclaude/platform-analysis-roadmap-1nUGLclaude/platform-launch-assessment-8dWV8claude/polish-platform-release-AeDrUclaude/resume-previous-work-KzyLwclaude/review-crontech-handoff-qYEVqclaude/review-project-completeness-lHhS2claude/review-readme-docs-ulqPKclaude/serene-edison-rj87weclaude/setup-multi-repo-dev-BCwNQclaude/ship-fixes-and-tests-Jvz1cclaude/site-audit-competitive-pctlwgclaude/site-migration-vercel-XstpKclaude/standalone-product-repos-XHFTDcopilot/feat-smart-empty-states-keyboard-first-enhancementcopilot/feat-smart-morning-digest-review-context-restorecopilot/fix-and-process-workflowscopilot/update-ai-powered-code-reviewfeat/debt-mapfeat/push-policy-codeowners-hardeningfeat/smart-digest-contextfeat/stage-impactfeat/t1-secret-migrationfeat/u-polishfeat/w-self-hostfeat/w2-claude-configfix/agent-journey-orphan-sweepgatetest/auto-fix-1776586424172gatetest/auto-fix-1776586534814gatetest/auto-fix-1776590685143gatetest/auto-fix-1776590808199mainops/redeploy-retriggerstyle/dxt-cta-themeworktree-agent-a3377aad30d55da26worktree-agent-a7ef607b7ee1d6c74
fork.tsx19.6 KB · 665 lines
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/**
 * Fork route — copy a repository into your account.
 *
 * GET  /:owner/:repo/fork  — polished confirmation page (source repo
 *                            card + destination owner picker + gradient
 *                            "Create my fork" submit). Mirrors the
 *                            existing inline POST button on RepoHeader;
 *                            the form here POSTs to the same endpoint
 *                            below, so the POST handler logic is the
 *                            single source of truth.
 * POST /:owner/:repo/fork  — perform the fork.
 *
 * All CSS is scoped under `.fork-*` so it can't bleed into other
 * surfaces. Visual recipe follows `src/routes/import.tsx` and
 * `src/routes/connect-claude.tsx`.
 */

import { Hono } from "hono";
import { eq, and } from "drizzle-orm";
import { db } from "../db";
import { repositories, users, activityFeed } from "../db/schema";
import { softAuth, requireAuth } from "../middleware/auth";
import type { AuthEnv } from "../middleware/auth";
import { getRepoPath, repoExists } from "../git/repository";
import { config } from "../lib/config";
import { join } from "path";
import { Layout } from "../views/layout";

const fork = new Hono<AuthEnv>();

fork.use("*", softAuth);

// ─── Scoped CSS — `.fork-*` ────────────────────────────────────────────────
const forkStyles = `
  .fork-wrap { max-width: 900px; margin: 0 auto; padding: var(--space-6, 32px) var(--space-4, 24px); }

  /* ─── Hero ─── */
  .fork-hero {
    position: relative;
    margin-bottom: var(--space-5);
    padding: clamp(28px, 4vw, 44px) clamp(24px, 4vw, 36px);
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 18px;
    overflow: hidden;
  }
  .fork-hero::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0;
    height: 2px;
    background: linear-gradient(90deg, transparent 0%, #5b6ee8 30%, #5f8fa0 70%, transparent 100%);
    opacity: 0.75;
    pointer-events: none;
  }
  .fork-hero-orb {
    position: absolute;
    inset: -30% -10% auto auto;
    width: 420px; height: 420px;
    background: radial-gradient(circle, rgba(91,110,232,0.22), rgba(95,143,160,0.10) 45%, transparent 70%);
    filter: blur(80px);
    opacity: 0.7;
    pointer-events: none;
    animation: forkHeroOrb 14s ease-in-out infinite;
    z-index: 0;
  }
  @keyframes forkHeroOrb {
    0%, 100% { transform: scale(1) translate(0, 0); opacity: 0.55; }
    50%      { transform: scale(1.08) translate(-12px, 8px); opacity: 0.85; }
  }
  @media (prefers-reduced-motion: reduce) {
    .fork-hero-orb { animation: none; }
  }
  .fork-hero-inner { position: relative; z-index: 1; max-width: 640px; }
  .fork-eyebrow {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-family: var(--font-mono);
    font-size: 11px;
    letter-spacing: 0.16em;
    text-transform: uppercase;
    color: var(--text-muted);
    font-weight: 600;
    margin-bottom: 12px;
  }
  .fork-eyebrow-dot {
    width: 8px; height: 8px;
    border-radius: 9999px;
    background: linear-gradient(135deg, #5b6ee8, #5f8fa0);
    box-shadow: 0 0 0 3px rgba(91,110,232,0.18);
  }
  .fork-eyebrow strong { color: var(--accent); font-weight: 700; }
  .fork-title {
    font-family: var(--font-display);
    font-size: clamp(28px, 4vw, 40px);
    font-weight: 800;
    letter-spacing: -0.028em;
    line-height: 1.06;
    margin: 0 0 var(--space-2);
    color: var(--text-strong);
  }
  .fork-title-grad {
    background-image: linear-gradient(135deg, #5b6ee8 0%, #5b6ee8 50%, #5f8fa0 100%);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    color: transparent;
  }
  .fork-sub {
    font-size: 15px;
    color: var(--text-muted);
    line-height: 1.55;
    margin: 0;
    max-width: 560px;
  }

  /* ─── Source repo card ─── */
  .fork-source-card {
    position: relative;
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 16px 18px;
    margin-bottom: var(--space-4);
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 14px;
    overflow: hidden;
  }
  .fork-source-card::before {
    content: '';
    position: absolute;
    left: 0; top: 0; bottom: 0;
    width: 3px;
    background: linear-gradient(180deg, #5b6ee8 0%, #5f8fa0 100%);
  }
  .fork-source-icon {
    flex-shrink: 0;
    width: 44px; height: 44px;
    border-radius: 12px;
    background: linear-gradient(135deg, rgba(91,110,232,0.20), rgba(95,143,160,0.14));
    border: 1px solid rgba(91,110,232,0.35);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: #c5b3ff;
    font-family: var(--font-display);
    font-weight: 800;
    font-size: 20px;
  }
  .fork-source-body { flex: 1; min-width: 0; }
  .fork-source-eyebrow {
    font-family: var(--font-mono);
    font-size: 11px;
    letter-spacing: 0.14em;
    text-transform: uppercase;
    color: var(--text-muted);
    font-weight: 600;
    margin-bottom: 3px;
  }
  .fork-source-name {
    font-family: var(--font-display);
    font-size: 16px;
    font-weight: 700;
    color: var(--text-strong);
    text-decoration: none;
    letter-spacing: -0.012em;
    display: block;
  }
  .fork-source-name:hover { color: var(--accent); }
  .fork-source-desc {
    font-size: 13px;
    color: var(--text-muted);
    margin: 3px 0 0;
    line-height: 1.5;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  /* ─── Form card ─── */
  .fork-form-card {
    position: relative;
    padding: clamp(20px, 3vw, 28px);
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 14px;
    overflow: hidden;
  }
  .fork-form-head {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 8px;
  }
  .fork-form-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 26px; height: 26px;
    border-radius: 50%;
    background: linear-gradient(135deg, rgba(91,110,232,0.20), rgba(95,143,160,0.14));
    color: #c5b3ff;
    border: 1px solid rgba(91,110,232,0.40);
    font-family: var(--font-display);
    font-weight: 700;
    font-size: 13px;
  }
  .fork-form-title {
    font-family: var(--font-display);
    font-size: 16px;
    font-weight: 700;
    letter-spacing: -0.012em;
    color: var(--text-strong);
    margin: 0;
  }
  .fork-form-sub {
    font-size: 13px;
    color: var(--text-muted);
    margin: 0 0 var(--space-3);
    line-height: 1.55;
  }
  .fork-field { display: flex; flex-direction: column; gap: 6px; margin-bottom: var(--space-3); }
  .fork-field-label {
    font-size: 13px;
    color: var(--text-strong);
    font-weight: 600;
  }
  .fork-dest-row {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 10px;
  }
  .fork-dest-avatar {
    width: 28px; height: 28px;
    border-radius: 50%;
    background: linear-gradient(135deg, #5b6ee8 0%, #5f8fa0 100%);
    color: #fff;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    font-family: var(--font-display);
    font-weight: 700;
    font-size: 13px;
    flex-shrink: 0;
  }
  .fork-dest-name {
    font-family: var(--font-display);
    font-weight: 700;
    font-size: 14px;
    color: var(--text-strong);
  }
  .fork-dest-slash {
    color: var(--text-muted);
    font-family: var(--font-mono);
    margin: 0 2px;
  }
  .fork-dest-repo {
    font-family: var(--font-mono);
    font-size: 13.5px;
    color: var(--text);
  }
  .fork-dest-hint {
    font-size: 12px;
    color: var(--text-muted);
    margin-top: 6px;
  }

  /* ─── Banners ─── */
  .fork-banner {
    position: relative;
    padding: 12px 16px 12px 40px;
    margin-bottom: var(--space-4);
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--bg-elevated);
    font-size: 14px;
    line-height: 1.5;
  }
  .fork-banner::before {
    content: '';
    position: absolute;
    left: 14px; top: 16px;
    width: 12px; height: 12px;
    border-radius: 50%;
  }
  .fork-banner-warn {
    border-color: rgba(251, 191, 36, 0.32);
    background: linear-gradient(180deg, rgba(251,191,36,0.06) 0%, var(--bg-elevated) 100%);
  }
  .fork-banner-warn::before {
    background: radial-gradient(circle, #fbbf24 30%, transparent 70%);
    box-shadow: 0 0 10px rgba(251,191,36,0.5);
  }
  .fork-banner-error {
    border-color: rgba(248, 81, 73, 0.32);
    background: linear-gradient(180deg, rgba(248,81,73,0.06) 0%, var(--bg-elevated) 100%);
  }
  .fork-banner-error::before {
    background: radial-gradient(circle, #f85149 30%, transparent 70%);
    box-shadow: 0 0 10px rgba(248,81,73,0.5);
  }
  .fork-banner a { color: var(--accent); text-decoration: none; }
  .fork-banner a:hover { text-decoration: underline; }

  /* ─── Actions ─── */
  .fork-actions {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    margin-top: var(--space-4);
    flex-wrap: wrap;
  }
  .fork-submit {
    appearance: none;
    border: 1px solid rgba(91,110,232,0.45);
    background: linear-gradient(135deg, #5b6ee8 0%, #5f8fa0 100%);
    color: #fff;
    padding: 12px 22px;
    border-radius: 10px;
    font-family: var(--font-display);
    font-weight: 700;
    font-size: 14px;
    cursor: pointer;
    box-shadow: 0 10px 24px -10px rgba(91,110,232,0.55);
    transition: transform 140ms ease, box-shadow 140ms ease, filter 140ms ease;
  }
  .fork-submit:hover {
    transform: translateY(-1px);
    box-shadow: 0 14px 28px -10px rgba(91,110,232,0.7);
    filter: brightness(1.06);
  }
  .fork-submit:focus-visible {
    outline: 3px solid rgba(91,110,232,0.45);
    outline-offset: 2px;
  }
  .fork-submit:disabled {
    cursor: not-allowed;
    filter: grayscale(0.6) brightness(0.8);
    box-shadow: none;
  }
  .fork-cancel {
    font-size: 13.5px;
    color: var(--text-muted);
    text-decoration: none;
  }
  .fork-cancel:hover { color: var(--text-strong); }

  /* ─── Empty state (zero state when invalid source) ─── */
  .fork-empty {
    position: relative;
    padding: 48px 24px;
    text-align: center;
    background: var(--bg-elevated);
    border: 1px solid var(--border);
    border-radius: 16px;
    overflow: hidden;
  }
  .fork-empty-orb {
    position: absolute;
    inset: -40% 25% auto 25%;
    width: 50%; height: 240px;
    background: radial-gradient(circle, rgba(91,110,232,0.18), transparent 65%);
    filter: blur(60px);
    opacity: 0.7;
    pointer-events: none;
    z-index: 0;
  }
  .fork-empty-inner { position: relative; z-index: 1; }
  .fork-empty-title {
    font-family: var(--font-display);
    font-size: 18px;
    font-weight: 700;
    letter-spacing: -0.014em;
    color: var(--text-strong);
    margin: 0 0 8px;
  }
  .fork-empty-desc {
    font-size: 14px;
    color: var(--text-muted);
    line-height: 1.55;
    margin: 0 0 var(--space-3);
  }
`;

// ─── GET — confirmation form ───────────────────────────────────────────────
// Preserves the existing POST contract; this just gives users a polished
// confirmation surface before firing. The existing inline button on the
// repo header is untouched and still posts straight through.
fork.get("/:owner/:repo/fork", requireAuth, async (c) => {
  const { owner: ownerName, repo: repoName } = c.req.param();
  const user = c.get("user")!;

  // Resolve source repo for the card
  const [sourceOwner] = await db
    .select()
    .from(users)
    .where(eq(users.username, ownerName))
    .limit(1);

  const sourceRepo = sourceOwner
    ? (
        await db
          .select()
          .from(repositories)
          .where(
            and(
              eq(repositories.ownerId, sourceOwner.id),
              eq(repositories.name, repoName)
            )
          )
          .limit(1)
      )[0]
    : null;

  // Zero state — invalid source
  if (!sourceOwner || !sourceRepo || !(await repoExists(ownerName, repoName))) {
    return c.html(
      <Layout title="Fork repository" user={user}>
        <style dangerouslySetInnerHTML={{ __html: forkStyles }} />
        <div class="fork-wrap">
          <div class="fork-empty">
            <div class="fork-empty-orb" aria-hidden="true" />
            <div class="fork-empty-inner">
              <h1 class="fork-empty-title">
                {ownerName}/{repoName} can't be forked
              </h1>
              <p class="fork-empty-desc">
                Either the repository doesn't exist or it's no longer
                available. Try browsing repositories you can fork.
              </p>
              <a href="/explore" class="fork-submit">Explore repositories</a>
            </div>
          </div>
        </div>
      </Layout>,
      404
    );
  }

  // Owner forking own repo
  const isSelf = ownerName === user.username;
  // Already forked
  const alreadyForked = !isSelf && (await repoExists(user.username, repoName));

  const avatarLetter = (user.username || "?").charAt(0).toUpperCase();

  return c.html(
    <Layout
      title={`Fork ${ownerName}/${repoName}`}
      user={user}
    >
      <style dangerouslySetInnerHTML={{ __html: forkStyles }} />
      <div class="fork-wrap">
        {/* ─── Hero ─── */}
        <div class="fork-hero">
          <div class="fork-hero-orb" aria-hidden="true" />
          <div class="fork-hero-inner">
            <div class="fork-eyebrow">
              <span class="fork-eyebrow-dot" aria-hidden="true" />
              <strong>Fork</strong> · copy to your account
            </div>
            <h1 class="fork-title">
              Fork{" "}
              <span class="fork-title-grad">{ownerName}/{repoName}</span>.
            </h1>
            <p class="fork-sub">
              We'll make a private copy under <strong>{user.username}</strong>.
              All branches and history carry over; gates, AI review, and
              auto-merge are wired up the moment the fork is ready.
            </p>
          </div>
        </div>

        {/* ─── Source repo card ─── */}
        <div class="fork-source-card">
          <div class="fork-source-icon" aria-hidden="true">
            {(sourceRepo.name || "?").charAt(0).toUpperCase()}
          </div>
          <div class="fork-source-body">
            <div class="fork-source-eyebrow">Source</div>
            <a
              class="fork-source-name"
              href={`/${ownerName}/${repoName}`}
            >
              {ownerName}/{repoName}
            </a>
            {sourceRepo.description && (
              <p class="fork-source-desc" title={sourceRepo.description}>
                {sourceRepo.description}
              </p>
            )}
          </div>
        </div>

        {/* ─── Conflict banners ─── */}
        {isSelf && (
          <div class="fork-banner fork-banner-warn" role="alert">
            You can't fork your own repository. Browse{" "}
            <a href="/explore">other repositories</a> instead.
          </div>
        )}
        {alreadyForked && (
          <div class="fork-banner fork-banner-warn" role="alert">
            You already have a fork at{" "}
            <a href={`/${user.username}/${repoName}`}>
              {user.username}/{repoName}
            </a>
            .
          </div>
        )}

        {/* ─── Destination + submit ─── */}
        <div class="fork-form-card">
          <div class="fork-form-head">
            <span class="fork-form-badge" aria-hidden="true">1</span>
            <h2 class="fork-form-title">Destination</h2>
          </div>
          <p class="fork-form-sub">
            v1 forks always land under your personal account. Org
            destinations coming soon.
          </p>
          <div class="fork-field">
            <span class="fork-field-label">Owner</span>
            <div class="fork-dest-row">
              <span class="fork-dest-avatar" aria-hidden="true">
                {avatarLetter}
              </span>
              <span class="fork-dest-name">{user.username}</span>
              <span class="fork-dest-slash" aria-hidden="true">/</span>
              <span class="fork-dest-repo">{repoName}</span>
            </div>
            <p class="fork-dest-hint">
              The fork will live at{" "}
              <strong>{user.username}/{repoName}</strong>.
            </p>
          </div>

          <form
            method="post"
            action={`/${ownerName}/${repoName}/fork`}
          >
            <div class="fork-actions">
              <button
                type="submit"
                class="fork-submit"
                disabled={isSelf || alreadyForked}
              >
                {alreadyForked ? "Already forked" : "Create my fork"}
              </button>
              <a href={`/${ownerName}/${repoName}`} class="fork-cancel">
                Cancel
              </a>
            </div>
          </form>
        </div>
      </div>
    </Layout>
  );
});

// Fork a repository
fork.post("/:owner/:repo/fork", requireAuth, async (c) => {
  const { owner: ownerName, repo: repoName } = c.req.param();
  const user = c.get("user")!;

  // Can't fork your own repo
  if (ownerName === user.username) {
    return c.redirect(`/${ownerName}/${repoName}`);
  }

  // Check source exists
  if (!(await repoExists(ownerName, repoName))) {
    return c.redirect(`/${ownerName}/${repoName}`);
  }

  // Check if already forked
  if (await repoExists(user.username, repoName)) {
    return c.redirect(`/${user.username}/${repoName}`);
  }

  // Get source repo from DB
  const [sourceOwner] = await db
    .select()
    .from(users)
    .where(eq(users.username, ownerName))
    .limit(1);
  if (!sourceOwner) return c.redirect(`/${ownerName}/${repoName}`);

  const [sourceRepo] = await db
    .select()
    .from(repositories)
    .where(
      and(
        eq(repositories.ownerId, sourceOwner.id),
        eq(repositories.name, repoName)
      )
    )
    .limit(1);
  if (!sourceRepo) return c.redirect(`/${ownerName}/${repoName}`);

  // Clone the bare repo
  const sourcePath = getRepoPath(ownerName, repoName);
  const destPath = join(config.gitReposPath, user.username, `${repoName}.git`);

  const proc = Bun.spawn(["git", "clone", "--bare", sourcePath, destPath], {
    stdout: "pipe",
    stderr: "pipe",
  });
  await proc.exited;

  // Insert into DB
  const [newRepo] = await db
    .insert(repositories)
    .values({
      name: repoName,
      ownerId: user.id,
      description: sourceRepo.description
        ? `Fork of ${ownerName}/${repoName}${sourceRepo.description}`
        : `Fork of ${ownerName}/${repoName}`,
      isPrivate: false,
      defaultBranch: sourceRepo.defaultBranch,
      diskPath: destPath,
      forkedFromId: sourceRepo.id,
    })
    .returning();

  // Bootstrap the fork with green-by-default settings, protection, labels
  if (newRepo) {
    const { bootstrapRepository } = await import("../lib/repo-bootstrap");
    await bootstrapRepository({
      repositoryId: newRepo.id,
      ownerUserId: user.id,
      defaultBranch: sourceRepo.defaultBranch,
      skipWelcomeIssue: true, // forks don't need a welcome issue
    });
  }

  // Update fork count
  await db
    .update(repositories)
    .set({ forkCount: sourceRepo.forkCount + 1 })
    .where(eq(repositories.id, sourceRepo.id));

  // Log activity
  try {
    await db.insert(activityFeed).values({
      repositoryId: sourceRepo.id,
      userId: user.id,
      action: "fork",
      metadata: JSON.stringify({ forkOwner: user.username }),
    });
  } catch {
    // best effort
  }

  return c.redirect(`/${user.username}/${repoName}`);
});

export default fork;