swadhinbiswas feat: finalize platform polish - discussions, gists, webhook queue, realtime, push-mirror, auth/metrics hardening · 8/28/2026
f02976d
1984 lines · 52.3 KB
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
/**
 * Git Engine - Repository operations
 * Handles git repository initialization, cloning, and operations
 */

import {
  deleteRepoFromStorage,
  isCloudStorage,
  parseStoragePath,
} from "@/lib/git-storage";
import { logger } from "@/lib/logger";
import { spawn } from "child_process";
import { existsSync, mkdirSync, mkdtempSync, readdirSync, rmSync } from "fs";
import { tmpdir } from "os";
import { basename, dirname, extname, join, resolve } from "path";
import { simpleGit, SimpleGit, SimpleGitOptions } from "simple-git";

// Memory-bounded operation limits (read once at module load, mirroring the
// GIT_PROCESS_TIMEOUT pattern in src/lib/git-server.ts)
const BLAME_MAX_LINES = Math.max(
  1,
  parseInt(process.env.BLAME_MAX_LINES || "20000", 10),
);
const DIFF_MAX_BYTES = Math.max(
  1024,
  parseInt(process.env.DIFF_MAX_BYTES || "2000000", 10),
);
const SEARCH_MAX_RESULTS = Math.max(
  1,
  parseInt(process.env.SEARCH_MAX_RESULTS || "500", 10),
);
const ACTIVITY_MAX_COMMITS = Math.max(
  1,
  parseInt(process.env.ACTIVITY_MAX_COMMITS || "50000", 10),
);
const FILE_CONTENT_MAX_BYTES = Math.max(
  1024,
  parseInt(process.env.FILE_CONTENT_MAX_BYTES || "1000000", 10),
);

// Truncate a string to at most maxBytes UTF-8 bytes without splitting a
// multi-byte character (walks back over continuation bytes)
function utf8SafeSlice(input: string, maxBytes: number): string {
  if (Buffer.byteLength(input, "utf8") <= maxBytes) {
    return input;
  }
  const buf = Buffer.from(input, "utf8");
  let end = maxBytes;
  while (end > 0 && (buf[end] & 0xc0) === 0x80) {
    end--;
  }
  return buf.subarray(0, end).toString("utf8");
}

export interface RepoInitOptions {
  defaultBranch?: string;
  readme?: boolean;
  gitignoreTemplate?: string;
  licenseType?: string;
  repoName?: string;
  ownerName?: string;
  skipHooks?: boolean;
}

export type MergeResult = {
  success: boolean;
  message: string;
  sha?: string; // SHA of the merge commit if successful
};

export interface FileEntry {
  name: string;
  path: string;
  type: "file" | "directory" | "submodule";
  size?: number;
  mode?: string;
  sha?: string;
}

export interface CommitInfo {
  sha: string;
  message: string;
  body?: string;
  authorName: string;
  authorEmail: string;
  authorDate: Date;
  committerName: string;
  committerEmail: string;
  committerDate: Date;
  parentShas: string[];
  verification?: {
    status: "G" | "B" | "U" | "E" | "N" | "X" | "Y" | "R"; // G=Good, B=Bad, U=Untrusted, E=Error, N=None
    signerKeyId?: string;
    signerName?: string;
  };
}

export interface BranchInfo {
  name: string;
  sha: string;
  isDefault: boolean;
  isProtected?: boolean;
  ahead?: number;
  behind?: number;
}

export interface TagInfo {
  name: string;
  sha: string;
  message?: string;
  taggerName?: string;
  taggerEmail?: string;
  taggedAt?: Date;
}

export interface DiffInfo {
  file: string;
  additions: number;
  deletions: number;
  changes: number;
  status: "added" | "deleted" | "modified" | "renamed" | "copied";
  oldPath?: string;
  patch?: string;
}

export interface BlameInfo {
  sha: string;
  line: number;
  content: string;
  author: string;
  email: string;
  date: Date;
  truncated?: boolean;
}

/**
 * Initialize a new bare git repository
 * The caller is responsible for providing the correct local path.
 * For cloud storage, the caller should use initRepoInStorage before calling this,
 * and finalizeRepoInit after this completes.
 */
export async function initRepository(
  repoPath: string,
  options: RepoInitOptions = {},
): Promise<void> {
  const {
    defaultBranch = "main",
    readme = true,
    gitignoreTemplate,
    licenseType,
    repoName = "New Repository",
    ownerName = "Owner",
  } = options;

  logger.info(`[initRepository] Starting initialization at: ${repoPath}`);

  // repoPath should already be a valid local path (provided by caller)
  const localRepoPath = repoPath;

  // Ensure parent directory exists
  const dir = dirname(localRepoPath);
  if (!existsSync(dir)) {
    logger.info(`[initRepository] Creating parent directory: ${dir}`);
    mkdirSync(dir, { recursive: true });
  }

  // Ensure the repo directory itself exists
  if (!existsSync(localRepoPath)) {
    logger.info(`[initRepository] Creating repo directory: ${localRepoPath}`);
    mkdirSync(localRepoPath, { recursive: true });
  }

  // Initialize bare repository
  try {
    logger.info(
      `[initRepository] Running git init --bare at: ${localRepoPath}`,
    );
    const git = createSimpleGit({ baseDir: localRepoPath });
    await git.init(true);
    logger.info(`[initRepository] git init completed`);
  } catch (err) {
    logger.error(`[initRepository] git init failed:`, err);
    throw err;
  }

  // If we need initial content, create a temporary working copy
  if (readme || gitignoreTemplate || licenseType) {
    const tempPath = `${localRepoPath}.tmp`;
    logger.info(`[initRepository] Creating temp working copy at: ${tempPath}`);
    mkdirSync(tempPath, { recursive: true });

    try {
      const workingGit = createSimpleGit({ baseDir: tempPath });
      await workingGit.init();
      await workingGit.addConfig("user.name", ownerName);
      await workingGit.addConfig("user.email", "[email protected]");

      // Create README.md
      if (readme) {
        const readmeContent = `# ${repoName}\n\nA new repository created with OpenCodeHub.\n`;
        const readmePath = join(tempPath, "README.md");
        const fs = await import("fs/promises");
        await fs.writeFile(readmePath, readmeContent);
        await workingGit.add("README.md");
        logger.info(`[initRepository] Created README.md`);
      }

      // Create .gitignore
      if (gitignoreTemplate) {
        const gitignorePath = join(tempPath, ".gitignore");
        const gitignoreContent = getGitignoreTemplate(gitignoreTemplate);
        const fs = await import("fs/promises");
        await fs.writeFile(gitignorePath, gitignoreContent);
        await workingGit.add(".gitignore");
        logger.info(`[initRepository] Created .gitignore`);
      }

      // Create LICENSE
      if (licenseType) {
        const licensePath = join(tempPath, "LICENSE");
        const licenseContent = getLicenseTemplate(licenseType, ownerName);
        if (licenseContent) {
          const fs = await import("fs/promises");
          await fs.writeFile(licensePath, licenseContent);
          await workingGit.add("LICENSE");
          logger.info(`[initRepository] Created LICENSE`);
        }
      }

      // Create initial commit
      await workingGit.commit("Initial commit");
      logger.info(`[initRepository] Created initial commit`);

      // Rename branch to default
      if (defaultBranch !== "master") {
        await workingGit.branch(["-M", defaultBranch]);
      }

      // Push to bare repository
      // Use --no-verify to skip hooks: either they aren't installed yet (normal case)
      // or they're leftover from a previous failed init (repo dir already existed)
      await workingGit.addRemote("origin", localRepoPath);
      await workingGit.push(["origin", defaultBranch, "--no-verify"]);
      logger.info(`[initRepository] Pushed to bare repo`);

      // Clean up temp directory
      rmSync(tempPath, { recursive: true, force: true });
      logger.info(`[initRepository] Cleaned up temp directory`);
    } catch (err) {
      logger.error(`[initRepository] Working copy operations failed:`, err);
      // Clean up temp if it exists
      if (existsSync(tempPath)) {
        rmSync(tempPath, { recursive: true, force: true });
      }
      throw err;
    }
  }

  // Install hooks AFTER the initial push to avoid pre-receive hook callback
  // during initialization (which would fail if server isn't reachable yet)
  if (!options.skipHooks) {
    try {
      logger.info(`[initRepository] Installing hooks`);
      await installHooks(localRepoPath);
      logger.info(`[initRepository] Hooks installed`);
    } catch (err) {
      logger.error(`[initRepository] Hook installation failed:`, err);
      throw err;
    }
  }

  // Set default branch in bare repo
  try {
    const bareGit = createSimpleGit({ baseDir: localRepoPath });
    await bareGit.raw(["symbolic-ref", "HEAD", `refs/heads/${defaultBranch}`]);
    logger.info(`[initRepository] Set default branch to ${defaultBranch}`);
  } catch (err) {
    logger.error(`[initRepository] Failed to set default branch:`, err);
    throw err;
  }

  // Verify files were created
  const files = readdirSync(localRepoPath);
  logger.info(`[initRepository] Completed. Files in repo: ${files.join(", ")}`);
}

/**
 * Delete a repository from disk and cloud storage
 */
export async function deleteRepository(repoPath: string): Promise<void> {
  // Delete from local filesystem if exists
  if (existsSync(repoPath)) {
    rmSync(repoPath, { recursive: true, force: true });
  }

  // If cloud storage, also delete from cloud
  if (await isCloudStorage()) {
    // repoPath might be a logical path (repos/owner/name.git)
    // or a physical path - try to delete from storage either way
    try {
      await deleteRepoFromStorage(repoPath);
    } catch (err) {
      // Also try parsing it as a storage path
      const parsed = parseStoragePath(repoPath);
      if (parsed) {
        await deleteRepoFromStorage(
          `repos/${parsed.owner}/${parsed.repoName}.git`,
        );
      }
    }
  }
}

/**
 * Fork a repository by cloning it as a bare repo
 */
export async function forkRepository(
  sourcePath: string,
  targetPath: string,
): Promise<void> {
  // Handle paths - if already absolute, use as-is
  const fullSourcePath = sourcePath.startsWith("/")
    ? sourcePath
    : join(process.cwd(), sourcePath);
  const fullTargetPath = targetPath.startsWith("/")
    ? targetPath
    : join(process.cwd(), targetPath);

  // Create parent directory if needed
  const parentDir = dirname(fullTargetPath);
  if (!existsSync(parentDir)) {
    mkdirSync(parentDir, { recursive: true });
  }

  // Check if source exists
  if (!existsSync(fullSourcePath)) {
    throw new Error(`Source repository not found: ${fullSourcePath}`);
  }

  // Clone as bare repository
  const git = createSimpleGit();
  await git.clone(fullSourcePath, fullTargetPath, ["--bare"]);

  logger.info(`Repository forked from ${sourcePath} to ${targetPath}`);
}

/**
 * Centralised factory for `simple-git` instances.
 *
 * OpenCodeHub is a git server; some workflows (force-deleting a branch,
 * `git checkout`/merge/rebase which may spawn `$EDITOR`, custom SSH
 * command for the SSH transport, `--upload-pack` / `--receive-pack` for
 * the smart-HTTP server, and credential helpers for HTTPS remotes)
 * legitimately use the operations that simple-git's safety plugin
 * blocks by default.  We opt-in to the *minimum* set required and
 * document each flag.  Everything else stays blocked.
 *
 * Use this helper everywhere instead of calling `simpleGit()` directly.
 */
export const SIMPLE_GIT_UNSAFE_OPTS = {
  allowUnsafeEditor: true,         // git checkout/merge/rebase may spawn EDITOR
  allowUnsafePack: true,           // git server uses --upload-pack / --receive-pack
  allowUnsafeSshCommand: true,     // SSH transport uses -c ssh.command=
  allowUnsafeCredentialHelper: true, // for HTTPS remotes that use a credential helper
} as const;

export function getSanitizedGitEnv(ceilingDir?: string): NodeJS.ProcessEnv {
  const env: NodeJS.ProcessEnv = { ...process.env };
  delete env.PAGER;
  delete env.GIT_PAGER;
  
  // Inject internal secrets needed by git hooks if they are missing from process.env
  const metaEnv = (import.meta as any).env;
  const hookSecret = process.env.INTERNAL_HOOK_SECRET || metaEnv?.INTERNAL_HOOK_SECRET;
  if (hookSecret) {
    env.INTERNAL_HOOK_SECRET = hookSecret;
  }
  
  // Pass system user for internal pushes to satisfy hook checks if REMOTE_USER is missing
  if (!env.REMOTE_USER) {
    env.REMOTE_USER = "system";
  }
  
  if (ceilingDir) {
    env.GIT_CEILING_DIRECTORIES = ceilingDir;
  }
  
  return env;
}

export function createSimpleGit(options: Partial<SimpleGitOptions> = {}): SimpleGit {
  const git = simpleGit({
    binary: "git",
    maxConcurrentProcesses: 6,
    unsafe: { ...SIMPLE_GIT_UNSAFE_OPTS },
    ...options,
  });
  
  git.env(getSanitizedGitEnv());
  
  return git;
}

export function getGit(repoPath: string): SimpleGit {
  // logger.info(`[getGit] Initializing for path: ${repoPath} (Exists: ${require('fs').existsSync(repoPath)})`);
  if (!repoPath || repoPath === "." || repoPath === "./") {
    logger.warn(
      `[getGit] WARNING: repoPath is empty or current directory! This might read project git history. Path: '${repoPath}'`,
    );
  }
  // Prevent git from looking in parent directories if the repoPath is not a git repo
  // This fixes the issue where empty temp dirs cause git to read the project's own history
  const ceilingDir = dirname(resolve(repoPath));

  const options: Partial<SimpleGitOptions> = {
    baseDir: repoPath,
    binary: "git",
    maxConcurrentProcesses: 6,
    unsafe: { ...SIMPLE_GIT_UNSAFE_OPTS },
  };

  const git = simpleGit(options);

  git.env(getSanitizedGitEnv(ceilingDir));

  return git;
}

/**
 * Check if a repository is empty (has no commits)
 */
export async function isRepoEmpty(repoPath: string): Promise<boolean> {
  const git = getGit(repoPath);
  try {
    // Try to get HEAD ref - if it fails, repo is empty
    await git.raw(["rev-parse", "HEAD"]);
    return false;
  } catch {
    return true;
  }
}

/**
 * Get the actual default branch of a repository (from git, not database)
 * Returns null if repository is empty
 */
export async function getActualDefaultBranch(
  repoPath: string,
): Promise<string | null> {
  const git = getGit(repoPath);
  try {
    const headRef = await git.raw(["symbolic-ref", "--short", "HEAD"]);
    return headRef.trim();
  } catch {
    // Repository might be empty or HEAD detached
    return null;
  }
}

// Helper to check for expected errors
function isExpectedGitError(error: unknown): boolean {
  const msg = error instanceof Error ? error.message : String(error);
  return (
    msg.includes("outside repository") ||
    msg.includes("not a git repository") ||
    msg.includes("does not exist") ||
    msg.includes("Not a valid object name") ||
    msg.includes("unknown revision") ||
    msg.includes("ambiguous argument")
  );
}

/**
 * List files in a directory at a specific ref
 */
export async function listFiles(
  repoPath: string,
  ref: string = "HEAD",
  path: string = "",
): Promise<FileEntry[]> {
  const git = getGit(repoPath);

  try {
    // Check if repository is empty first
    if (await isRepoEmpty(repoPath)) {
      return [];
    }

    // Use ls-tree to get files
    // Append / to path to list contents of directory, otherwise it lists the directory entry itself
    const targetPath = path ? (path.endsWith("/") ? path : `${path}/`) : ".";

    const treeOutput = await git.raw([
      "ls-tree",
      "-l", // Include size
      ref,
      targetPath,
    ]);

    const entries: FileEntry[] = [];
    const lines = treeOutput.trim().split("\n").filter(Boolean);

    for (const line of lines) {
      // Format: mode type sha size path
      const match = line.match(/^(\d+)\s+(\w+)\s+(\w+)\s+(-|\d+)\s+(.+)$/);
      if (match) {
        const [, mode, type, sha, size, filePath] = match;
        entries.push({
          name: basename(filePath),
          path: filePath,
          type:
            type === "tree"
              ? "directory"
              : type === "commit"
                ? "submodule"
                : "file",
          size: size === "-" ? undefined : parseInt(size, 10),
          mode,
          sha,
        });
      }
    }

    // Sort: directories first, then files
    entries.sort((a, b) => {
      if (a.type === "directory" && b.type !== "directory") return -1;
      if (a.type !== "directory" && b.type === "directory") return 1;
      return a.name.localeCompare(b.name);
    });

    return entries;
  } catch (error) {
    if (!isExpectedGitError(error)) {
      logger.error({ err: error }, "Error listing files");
    }
    return [];
  }
}

/**
 * Get file content at a specific ref
 */
export async function getFileContent(
  repoPath: string,
  filePath: string,
  ref: string = "HEAD",
): Promise<{
  content: string;
  isBinary: boolean;
  size: number;
  truncated?: boolean;
} | null> {
  const git = getGit(repoPath);

  try {
    // Get file blob
    const content = await git.show([`${ref}:${filePath}`]);

    // Check if binary
    const isBinary = await isFileBinary(repoPath, filePath, ref);

    const size = Buffer.byteLength(content, "utf8");

    if (!isBinary && size > FILE_CONTENT_MAX_BYTES) {
      logger.warn(
        { repoPath, filePath, ref, size, max: FILE_CONTENT_MAX_BYTES },
        "File content exceeds FILE_CONTENT_MAX_BYTES, truncating",
      );
      return {
        content: utf8SafeSlice(content, FILE_CONTENT_MAX_BYTES),
        isBinary,
        size,
        truncated: true,
      };
    }

    return {
      content: isBinary ? "" : content,
      isBinary,
      size,
    };
  } catch (error) {
    return null;
  }
}

/**
 * Check if a file is binary
 */
async function isFileBinary(
  repoPath: string,
  filePath: string,
  ref: string,
): Promise<boolean> {
  const git = getGit(repoPath);

  try {
    // Use git diff to check if file is binary
    const result = await git.raw([
      "diff",
      "--numstat",
      "4b825dc642cb6eb9a060e54bf8d69288fbee4904", // Empty tree SHA
      ref,
      "--",
      filePath,
    ]);

    // Binary files show as "-\t-\t" in numstat
    return result.startsWith("-\t-\t");
  } catch {
    // Check by extension as fallback
    const binaryExtensions = [
      ".png",
      ".jpg",
      ".jpeg",
      ".gif",
      ".bmp",
      ".ico",
      ".webp",
      ".pdf",
      ".doc",
      ".docx",
      ".xls",
      ".xlsx",
      ".ppt",
      ".pptx",
      ".zip",
      ".tar",
      ".gz",
      ".rar",
      ".7z",
      ".exe",
      ".dll",
      ".so",
      ".dylib",
      ".mp3",
      ".mp4",
      ".wav",
      ".avi",
      ".mov",
      ".ttf",
      ".woff",
      ".woff2",
      ".eot",
    ];
    const ext = extname(filePath).toLowerCase();
    return binaryExtensions.includes(ext);
  }
}

/**
 * Get commit history
 */
export async function getCommits(
  repoPath: string,
  options: {
    ref?: string;
    path?: string;
    limit?: number;
    skip?: number;
  } = {},
): Promise<CommitInfo[]> {
  const git = getGit(repoPath);
  const { ref = "HEAD", path, limit = 30, skip = 0 } = options;

  try {
    // Check if repository is empty first
    if (await isRepoEmpty(repoPath)) {
      return [];
    }

    const args = [
      "log",
      // Use ASCII record separator (0x1e) as delimiter to avoid issues with | in commit messages
      "--format=%H%x1e%s%x1e%b%x1e%an%x1e%ae%x1e%ai%x1e%cn%x1e%ce%x1e%ci%x1e%P%x1e%G?%x1e%GK%x1e%GS",
      `-n${limit}`,
      `--skip=${skip}`,
      ref,
    ];

    if (path) {
      args.push("--", path);
    }

    const output = await git.raw(args);
    const commits: CommitInfo[] = [];
    const DELIMITER = "\x1e"; // ASCII record separator

    for (const line of output.trim().split("\n").filter(Boolean)) {
      const parts = line.split(DELIMITER);
      // Ensure we have at least the required fields
      if (parts.length < 9) continue;

      const [
        sha,
        subject,
        body,
        authorName,
        authorEmail,
        authorDate,
        committerName,
        committerEmail,
        committerDate,
        parents,
        gpgStatus,
        gpgKeyId,
        gpgSigner,
      ] = parts;

      commits.push({
        sha,
        message: subject,
        body: body || undefined,
        authorName: authorName || "Unknown",
        authorEmail: authorEmail || "unknown@unknown",
        authorDate: new Date(authorDate),
        committerName: committerName || "Unknown",
        committerEmail: committerEmail || "unknown@unknown",
        committerDate: new Date(committerDate),
        parentShas: parents ? parents.split(" ").filter(Boolean) : [],
        verification: {
          status: gpgStatus as any,
          signerKeyId: gpgKeyId || undefined,
          signerName: gpgSigner || undefined,
        },
      });
    }

    return commits;
  } catch (error) {
    if (!isExpectedGitError(error)) {
      logger.error({ err: error }, "Error getting commits");
    }
    return [];
  }
}

/**
 * Get commit activity (daily commit counts)
 */
export async function getCommitActivity(
  repoPath: string,
  since: string = "1 year ago",
): Promise<{ date: string; count: number }[]> {
  const git = getGit(repoPath);

  try {
    if (await isRepoEmpty(repoPath)) {
      return [];
    }

    // Get just the dates of commits
    // -n caps the scan so huge histories cannot produce unbounded output
    const output = await git.raw([
      "log",
      `--since=${since}`,
      "--date=short",
      "--format=%ad",
      `-n${ACTIVITY_MAX_COMMITS}`,
    ]);

    const dates = output.trim().split("\n").filter(Boolean);
    const counts = new Map<string, number>();

    for (const date of dates) {
      counts.set(date, (counts.get(date) || 0) + 1);
    }

    return Array.from(counts.entries())
      .map(([date, count]) => ({ date, count }))
      .sort((a, b) => a.date.localeCompare(b.date));
  } catch (error) {
    if (!isExpectedGitError(error)) {
      logger.error({ err: error }, "Error getting commit activity");
    }
    return [];
  }
}

export interface SearchResult {
  file: string;
  line: number;
  content: string;
  truncated?: boolean;
}

/**
 * Search code in repository using git grep
 */
export async function searchCode(
  repoPath: string,
  query: string,
  ref: string = "HEAD",
): Promise<SearchResult[]> {
  const git = getGit(repoPath);

  try {
    if (await isRepoEmpty(repoPath)) {
      return [];
    }

    // git grep -n -I <query> <ref>
    const output = await git.raw([
      "grep",
      "-n", // Line numbers
      "-I", // Ignore binary files
      query,
      ref,
    ]);

    const results: SearchResult[] = [];

    for (const line of output.trim().split("\n").filter(Boolean)) {
      // Format: <ref>:<file>:<line>:<content>
      const parts = line.split(":");
      if (parts.length < 4) continue;

      const filePath = parts[1];
      const lineNumber = parseInt(parts[2], 10);
      const content = parts.slice(3).join(":");

      results.push({
        file: filePath,
        line: lineNumber,
        content: content.trim(),
      });
    }

    if (results.length > SEARCH_MAX_RESULTS) {
      const truncatedResults = results.slice(0, SEARCH_MAX_RESULTS);
      for (const result of truncatedResults) {
        result.truncated = true;
      }
      logger.warn(
        { repoPath, query, ref, total: results.length, max: SEARCH_MAX_RESULTS },
        "Search results exceed SEARCH_MAX_RESULTS, truncating",
      );
      return truncatedResults;
    }

    return results;
  } catch (error) {
    const msg = error instanceof Error ? error.message : String(error);
    if (msg.includes("exit code 1")) {
      return []; // No matches
    }

    if (!isExpectedGitError(error)) {
      logger.error({ err: error }, "Error searching code");
    }
    return [];
  }
}

/**
 * Get total commit count
 */
export async function getCommitCount(
  repoPath: string,
  ref: string = "HEAD",
): Promise<number> {
  const git = getGit(repoPath);

  try {
    // Check if repository is empty first
    if (await isRepoEmpty(repoPath)) {
      return 0;
    }

    const count = await git.raw(["rev-list", "--count", ref]);
    return parseInt(count.trim(), 10);
  } catch (error) {
    return 0;
  }
}

/**
 * Get a single commit
 */
export async function getCommit(
  repoPath: string,
  sha: string,
): Promise<CommitInfo | null> {
  const commits = await getCommits(repoPath, { ref: sha, limit: 1 });
  return commits[0] || null;
}

/**
 * Get branches
 */
export async function getBranches(repoPath: string): Promise<BranchInfo[]> {
  const git = getGit(repoPath);

  try {
    const output = await git.raw([
      "for-each-ref",
      "--format=%(refname:short)|%(objectname)",
      "refs/heads/",
    ]);

    // Get default branch
    let defaultBranch = "main";
    try {
      const headRef = await git.raw(["symbolic-ref", "--short", "HEAD"]);
      defaultBranch = headRef.trim();
    } catch {
      // Ignore if HEAD doesn't point to a branch
    }

    const branches: BranchInfo[] = [];

    for (const line of output.trim().split("\n").filter(Boolean)) {
      const [name, sha] = line.split("|");

      // Filter out HEAD and other invalid branch names
      if (!name || name === "HEAD" || name.includes("HEAD")) {
        continue;
      }

      branches.push({
        name,
        sha,
        isDefault: name === defaultBranch,
      });
    }

    // Sort: default branch first, then alphabetically
    branches.sort((a, b) => {
      if (a.isDefault) return -1;
      if (b.isDefault) return 1;
      return a.name.localeCompare(b.name);
    });

    return branches;
  } catch (error) {
    if (!isExpectedGitError(error)) {
      logger.error({ err: error }, "Error getting branches");
    }
    return [];
  }
}

/**
 * Get list of changed files between two refs
 */
export async function getChangedFiles(
  repoPath: string,
  baseRef: string,
  headRef: string,
): Promise<string[]> {
  const git = getGit(repoPath);

  try {
    // git diff --name-only base...head
    // using 3 dots for merge-base check, or 2 dots?
    // usually for PRs we want 3 dots (changes from common ancestor)
    const output = await git.raw([
      "diff",
      "--name-only",
      `${baseRef}...${headRef}`,
    ]);

    return output.trim().split("\n").filter(Boolean);
  } catch (error) {
    if (!isExpectedGitError(error)) {
      logger.error({ err: error }, "Error getting changed files");
    }
    return [];
  }
}

/**
 * Get tags
 */
export async function getTags(repoPath: string): Promise<TagInfo[]> {
  const git = getGit(repoPath);

  try {
    const output = await git.raw([
      "for-each-ref",
      "--format=%(refname:short)|%(*objectname)|%(objectname)|%(taggername)|%(taggeremail)|%(taggerdate:iso)|%(contents:subject)",
      "--sort=-version:refname",
      "refs/tags/",
    ]);

    const tags: TagInfo[] = [];

    for (const line of output.trim().split("\n").filter(Boolean)) {
      const [name, deref, sha, taggerName, taggerEmail, taggedAt, message] =
        line.split("|");
      tags.push({
        name,
        sha: deref || sha, // Use dereferenced sha for annotated tags
        message: message || undefined,
        taggerName: taggerName || undefined,
        taggerEmail: taggerEmail?.replace(/[<>]/g, "") || undefined,
        taggedAt: taggedAt ? new Date(taggedAt) : undefined,
      });
    }

    return tags;
  } catch (error) {
    if (!isExpectedGitError(error)) {
      logger.error({ err: error }, "Error getting tags");
    }
    return [];
  }
}

/**
 * Get diff for a commit
 */
export async function getCommitDiff(
  repoPath: string,
  sha: string,
): Promise<DiffInfo[]> {
  const git = getGit(repoPath);

  try {
    const output = await git.raw([
      "diff-tree",
      "--no-commit-id",
      "--numstat",
      "-r",
      "-M", // Detect renames
      sha,
    ]);

    const diffs: DiffInfo[] = [];

    for (const line of output.trim().split("\n").filter(Boolean)) {
      const parts = line.split("\t");
      if (parts.length >= 3) {
        const [additions, deletions, file] = parts;
        const add = additions === "-" ? 0 : parseInt(additions, 10);
        const del = deletions === "-" ? 0 : parseInt(deletions, 10);

        // Check for rename
        let filePath = file;
        let oldPath: string | undefined;
        let status: DiffInfo["status"] = "modified";

        if (file.includes(" => ")) {
          const [old, newPath] = file.split(" => ");
          oldPath = old;
          filePath = newPath;
          status = "renamed";
        }

        diffs.push({
          file: filePath,
          additions: add,
          deletions: del,
          changes: add + del,
          status,
          oldPath,
        });
      }
    }

    return diffs;
  } catch (error) {
    if (!isExpectedGitError(error)) {
      logger.error({ err: error }, "Error getting commit diff");
    }
    return [];
  }
}

/**
 * Get the raw unified diff (patch) for a single commit
 */
export async function getCommitPatchDiff(
  repoPath: string,
  sha: string,
): Promise<string> {
  const git = getGit(repoPath);
  try {
    const output = await git.raw([
      "diff-tree",
      "-p",
      "--no-commit-id",
      "-r",
      "-M",
      sha,
    ]);
    if (Buffer.byteLength(output, "utf8") > DIFF_MAX_BYTES) {
      logger.warn(
        { repoPath, sha, max: DIFF_MAX_BYTES },
        "Commit patch diff exceeds DIFF_MAX_BYTES, truncating",
      );
      return utf8SafeSlice(output, DIFF_MAX_BYTES);
    }
    return output;
  } catch (error) {
    if (!isExpectedGitError(error)) {
      logger.error({ err: error }, "Error getting commit patch diff");
    }
    return "";
  }
}
/**
 * Get blame for a file
 */
export async function getBlame(
  repoPath: string,
  filePath: string,
  ref: string = "HEAD",
): Promise<BlameInfo[]> {
  const git = getGit(repoPath);

  try {
    // -L 1,<cap> caps blame output upstream; git clamps the end line to EOF
    // so this is safe for files smaller than the cap
    const output = await git.raw([
      "blame",
      "--line-porcelain",
      "-L",
      `1,${BLAME_MAX_LINES}`,
      ref,
      "--",
      filePath,
    ]);

    const blameLines: BlameInfo[] = [];
    const lines = output.split("\n");
    let currentBlame: Partial<BlameInfo> = {};
    let lineContent = "";

    for (const line of lines) {
      if (line.match(/^[a-f0-9]{40}/)) {
        if (currentBlame.sha && currentBlame.line !== undefined) {
          blameLines.push({
            ...currentBlame,
            content: lineContent,
          } as BlameInfo);
        }
        const [sha, , , lineNum] = line.split(" ");
        currentBlame = { sha, line: parseInt(lineNum, 10) };
      } else if (line.startsWith("author ")) {
        currentBlame.author = line.slice(7);
      } else if (line.startsWith("author-mail ")) {
        currentBlame.email = line.slice(12).replace(/[<>]/g, "");
      } else if (line.startsWith("author-time ")) {
        currentBlame.date = new Date(parseInt(line.slice(12), 10) * 1000);
      } else if (line.startsWith("\t")) {
        lineContent = line.slice(1);
      }
    }

    // Add last blame
    if (currentBlame.sha && currentBlame.line !== undefined) {
      blameLines.push({
        ...currentBlame,
        content: lineContent,
      } as BlameInfo);
    }

    // If we filled the cap, probe one line past it to detect truncation
    if (blameLines.length >= BLAME_MAX_LINES) {
      try {
        await git.raw([
          "blame",
          "--line-porcelain",
          "-L",
          `${BLAME_MAX_LINES + 1},${BLAME_MAX_LINES + 1}`,
          ref,
          "--",
          filePath,
        ]);
        for (const blameLine of blameLines) {
          blameLine.truncated = true;
        }
      } catch {
        // Probe failed: file ends at/below the cap, nothing truncated
      }
    }

    return blameLines;
  } catch (error) {
    if (!isExpectedGitError(error)) {
      logger.error({ err: error }, "Error getting blame");
    }
    return [];
  }
}

/**
 * Get repository size
 */
export async function getRepoSize(repoPath: string): Promise<number> {
  const git = getGit(repoPath);

  try {
    const output = await git.raw(["count-objects", "-vH"]);
    const match = output.match(/size-pack:\s*([\d.]+)\s*(\w+)/);
    if (match) {
      const size = parseFloat(match[1]);
      const unit = match[2].toLowerCase();
      const multipliers: Record<string, number> = {
        bytes: 1,
        kib: 1024,
        mib: 1024 * 1024,
        gib: 1024 * 1024 * 1024,
      };
      return Math.round(size * (multipliers[unit] || 1));
    }
    return 0;
  } catch (error) {
    return 0;
  }
}

/**
 * Create a new branch
 */
export async function createBranch(
  repoPath: string,
  branchName: string,
  startPoint: string = "HEAD",
): Promise<void> {
  const git = getGit(repoPath);
  try {
    await git.branch([branchName, startPoint]);
  } catch (error) {
    logger.error({ err: error }, "Error creating branch");
    throw new Error(`Failed to create branch: ${error}`);
  }
}

/**
 * Delete a branch
 */
export async function deleteBranch(
  repoPath: string,
  branchName: string,
): Promise<void> {
  const git = getGit(repoPath);
  try {
    await git.branch(["-D", branchName]);
  } catch (error) {
    logger.error({ err: error }, "Error deleting branch");
    throw new Error(`Failed to delete branch: ${error}`);
  }
}

/**
 * Get merge base of two commits
 */
export async function getMergeBase(
  repoPath: string,
  base: string,
  head: string,
): Promise<string> {
  const git = getGit(repoPath);
  try {
    const output = await git.raw(["merge-base", base, head]);
    return output.trim();
  } catch (error) {
    logger.error({ err: error }, "Error getting merge base");
    return "";
  }
}

/**
 * Compare two branches to get commits and diff stats
 */
export async function compareBranches(
  repoPath: string,
  base: string,
  head: string,
): Promise<{ commits: CommitInfo[]; diffs: DiffInfo[]; truncated?: boolean }> {
  // Verify branches exist first to avoid "ambiguous argument" errors
  const fullBase = base.startsWith("refs/") ? base : `refs/heads/${base}`;
  const fullHead = head.startsWith("refs/") ? head : `refs/heads/${head}`;

  const git = getGit(repoPath);

  try {
    await git.raw(["rev-parse", "--verify", fullBase]);
  } catch {
    logger.warn({ base, fullBase }, "Base branch not found in compareBranches");
    return { commits: [], diffs: [] };
  }

  try {
    await git.raw(["rev-parse", "--verify", fullHead]);
  } catch {
    // Only warn if head is missing but base exists (uncommon but possible)
    logger.warn({ head, fullHead }, "Head branch not found in compareBranches");
    return { commits: [], diffs: [] };
  }

  try {
    // Get commits between base and head
    // git log base..head
    const commits = await getCommits(repoPath, {
      ref: `${fullBase}..${fullHead}`,
      limit: 100, // Limit to prevent overload
    });

    // Get diff stats
    // git diff --numstat base...head (triple dot for merge base comparison)
    const output = await git.raw([
      "diff",
      "--numstat",
      `${fullBase}...${fullHead}`, // Triple dot finds merge base automatically
    ]);

    const numstatTruncated = Buffer.byteLength(output, "utf8") > DIFF_MAX_BYTES;
    const numstat = numstatTruncated
      ? utf8SafeSlice(output, DIFF_MAX_BYTES)
      : output;

    const diffs: DiffInfo[] = [];
    for (const line of numstat.trim().split("\n").filter(Boolean)) {
      const parts = line.split("\t");
      if (parts.length >= 3) {
        const [additions, deletions, file] = parts;
        const add = additions === "-" ? 0 : parseInt(additions, 10);
        const del = deletions === "-" ? 0 : parseInt(deletions, 10);

        diffs.push({
          file,
          additions: add,
          deletions: del,
          changes: add + del,
          status: "modified", // Simplified status
        });
      }
    }

    if (numstatTruncated) {
      logger.warn(
        { repoPath, base, head, max: DIFF_MAX_BYTES },
        "Branch comparison numstat exceeds DIFF_MAX_BYTES, truncating",
      );
    }

    return { commits, diffs, truncated: numstatTruncated || undefined };
  } catch (error: any) {
    // Fallback for any other errors
    logger.error({ err: error }, "Error comparing branches");
    return { commits: [], diffs: [] };
  }
}

/**
 * Get unified patch diff between two refs (for compare view)
 * Returns raw unified diff output suitable for rendering in a diff viewer
 */
export async function getComparePatchDiff(
  repoPath: string,
  base: string,
  head: string,
): Promise<string> {
  const fullBase = base.startsWith("refs/") ? base : `refs/heads/${base}`;
  const fullHead = head.startsWith("refs/") ? head : `refs/heads/${head}`;
  const git = getGit(repoPath);

  try {
    await git.raw(["rev-parse", "--verify", fullBase]);
    await git.raw(["rev-parse", "--verify", fullHead]);
  } catch {
    return "";
  }

  try {
    const output = await git.raw([
      "diff",
      `${fullBase}...${fullHead}`,
      "--unified=5",
      "-M", // Detect renames
    ]);
    if (Buffer.byteLength(output, "utf8") > DIFF_MAX_BYTES) {
      logger.warn(
        { repoPath, base, head, max: DIFF_MAX_BYTES },
        "Compare patch diff exceeds DIFF_MAX_BYTES, truncating",
      );
      return utf8SafeSlice(output, DIFF_MAX_BYTES);
    }
    return output;
  } catch (error: any) {
    logger.error({ err: error }, "Error getting compare patch diff");
    return "";
  }
}

/**
 * Merge a branch into another
 * Uses --shared + --no-checkout for performance (avoids full clone)
 */
export async function mergeBranch(
  repoPath: string,
  base: string,
  head: string,
  message?: string,
  method: "merge" | "squash" | "rebase" = "merge",
): Promise<MergeResult> {
  const tempDir = mkdtempSync(join(tmpdir(), "och-merge-"));
  try {
    const tempGit = createSimpleGit();
    // Use --shared to avoid duplicating objects (local repos only) + shallow depth
    await tempGit.clone(repoPath, tempDir, [
      "--shared",
      "--no-checkout",
      "--single-branch",
      "--branch",
      base,
    ]);

    const workGit = createSimpleGit({ baseDir: tempDir });
    await workGit.checkout(base);
    // --single-branch clones restrict the fetch refspec to the base branch, so
    // a bare `fetch origin <head>` only writes FETCH_HEAD and never creates
    // `refs/remotes/origin/<head>`; use an explicit refspec instead. The clone
    // is `--shared`, so objects are already local and no --depth is needed
    // (shallow fetches would make the head look like unrelated history).
    await workGit.fetch("origin", `refs/heads/${head}:refs/remotes/origin/${head}`);

    const commitMsg = message || `Merge pull request from ${head} into ${base}`;

    if (method === "squash") {
      // Squash merge: merge --squash stages the combined diff, then commit
      await workGit.merge(["--squash", `origin/${head}`]);
      await workGit.commit(commitMsg);
    } else if (method === "rebase") {
      // Rebase merge: replay head commits onto base, then fast-forward base
      await workGit.checkout(["-b", "och-rebase", `origin/${head}`]);
      await workGit.rebase([base]);
      await workGit.checkout(base);
      await workGit.merge(["--ff-only", "och-rebase"]);
    } else {
      await workGit.merge(["--no-ff", "-m", commitMsg, `origin/${head}`]);
    }

    const newCommitSha = (await workGit.revparse(["HEAD"])).trim();
    await workGit.push("origin", `${base}:${base}`);

    return { success: true, message: "Merged successfully", sha: newCommitSha };
  } catch (error: any) {
    logger.error("Error merging branches:", error);
    return { success: false, message: error.message || "Merge failed" };
  } finally {
    rmSync(tempDir, { recursive: true, force: true });
  }
}

/**
 * Get gitignore templates
 */
function getGitignoreTemplate(template: string): string {
  const templates: Record<string, string> = {
    node: `# Dependencies
node_modules/
.pnp/
.pnp.js

# Build
dist/
build/
.next/
out/

# Environment
.env
.env.local
.env.*.local

# Logs
logs/
*.log
npm-debug.log*

# IDE
.vscode/
.idea/
*.swp
*.swo

# OS
.DS_Store
Thumbs.db

# Testing
coverage/
.nyc_output/
`,
    python: `# Byte-compiled
__pycache__/
*.py[cod]
*$py.class

# Virtual environments
venv/
.env/
.venv/

# Distribution
dist/
build/
*.egg-info/

# IDE
.vscode/
.idea/
*.swp

# Testing
.pytest_cache/
.coverage
htmlcov/

# Environment
.env
.env.local
`,
    rust: `# Build
target/

# IDE
.vscode/
.idea/

# Environment
.env

# Cargo.lock for libraries
# Cargo.lock
`,
    go: `# Binaries
*.exe
*.dll
*.so
*.dylib

# Test
*.test
coverage.txt

# Dependency
vendor/

# IDE
.vscode/
.idea/
`,
  };

  return templates[template] || templates.node;
}

/**
 * Get license templates
 */
function getLicenseTemplate(license: string, owner: string): string | null {
  const year = new Date().getFullYear();

  const templates: Record<string, string> = {
    mit: `MIT License

Copyright (c) ${year} ${owner}

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
`,
    apache2: `Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/

Copyright ${year} ${owner}

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
`,
    gpl3: `GNU GENERAL PUBLIC LICENSE
Version 3, 29 June 2007

Copyright (C) ${year} ${owner}

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
`,
  };

  return templates[license.toLowerCase()] || null;
}

export async function getLastCommit(
  repoPath: string,
  ref: string,
  filePath: string,
): Promise<CommitInfo | null> {
  const git = getGit(repoPath);
  try {
    // Format: hash|author|date|message
    const output = await git.raw([
      "log",
      "-1",
      "--format=%H|%an|%aI|%s",
      ref,
      "--",
      filePath,
    ]);

    if (!output.trim()) return null;

    const [sha, authorName, dateStr, message] = output.trim().split("|");
    const date = new Date(dateStr);

    return {
      sha,
      message,
      authorName,
      authorEmail: "", // Not fetching email to save bandwidth/parsing
      authorDate: date,
      committerName: authorName,
      committerEmail: "",
      committerDate: date,
      parentShas: [],
    };
  } catch (e) {
    return null;
  }
}

export async function getFileRawContent(
  repoPath: string,
  filePath: string,
  ref: string = "HEAD",
): Promise<Buffer | null> {
  return new Promise((resolve, reject) => {
    const git = spawn("git", ["show", `${ref}:${filePath}`], { cwd: repoPath });
    const chunks: Buffer[] = [];

    git.stdout.on("data", (chunk) => {
      chunks.push(Buffer.from(chunk));
    });

    git.on("close", (code) => {
      if (code !== 0) {
        resolve(null);
      } else {
        resolve(Buffer.concat(chunks));
      }
    });

    git.on("error", (err) => {
      logger.error({ err }, "Git show error");
      resolve(null);
    });
  });
}

/**
 * Get contributors list with commit counts
 */
export async function getContributors(
  repoPath: string,
  ref: string = "HEAD",
): Promise<{ name: string; email: string; commits: number }[]> {
  const git = getGit(repoPath);

  try {
    // Check if repository is empty first
    if (await isRepoEmpty(repoPath)) {
      return [];
    }

    // git shortlog -sne (summary, numbered, email)
    const output = await git.raw(["shortlog", "-sne", ref]);

    const contributors: { name: string; email: string; commits: number }[] = [];

    output
      .trim()
      .split("\n")
      .filter(Boolean)
      .forEach((line) => {
        const match = line.match(/^\s*(\d+)\s+(.+)\s+<(.+)>$/);
        if (match) {
          contributors.push({
            commits: parseInt(match[1], 10),
            name: match[2].trim(),
            email: match[3].trim(),
          });
        }
      });

    return contributors;
  } catch (error) {
    // Catch "outside repository" error and others
    const errorMessage = error instanceof Error ? error.message : String(error);
    if (
      errorMessage.includes("outside repository") ||
      errorMessage.includes("not a git repository") ||
      errorMessage.includes("does not exist")
    ) {
      // This is expected for empty/missing repos
      return [];
    }

    logger.error({ err: error }, "Error getting contributors");
    return [];
  }
}

export async function installHooks(repoPath: string) {
  const hooksDir = join(repoPath, "hooks");
  const fs = await import("fs/promises");

  // Ensure hooks dir exists
  if (!existsSync(hooksDir)) {
    await fs.mkdir(hooksDir, { recursive: true });
  }

  // Internal hooks should always call the local server instance, regardless of SITE_URL
  // This prevents connection timeouts if SITE_URL is an external IP that the host cannot route to itself.
  const metaEnv = (import.meta as ImportMeta & { env?: Record<string, string | undefined> }).env;
  const port = process.env.PORT || metaEnv?.PORT || "4321";
  const internalApiUrl = `http://127.0.0.1:${port}`;
  const hookSecret = process.env.INTERNAL_HOOK_SECRET || metaEnv?.INTERNAL_HOOK_SECRET;
  if (!hookSecret) {
    throw new Error(
      "INTERNAL_HOOK_SECRET environment variable is required to install git hooks",
    );
  }

  const postReceiveScript = `#!/bin/bash
# Read hook secret from environment (set by the server process, not stored in this file)
HOOK_SECRET="\${INTERNAL_HOOK_SECRET}"
if [ -z "$HOOK_SECRET" ]; then
    echo "Warning: INTERNAL_HOOK_SECRET not set, hook call may fail"
fi
while read oldrev newrev refname
do
    curl -sS -X POST \\
      -H "Content-Type: application/json" \\
      -H "X-Hook-Secret: $HOOK_SECRET" \\
      -d "{\\"oldrev\\":\\"$oldrev\\",\\"newrev\\":\\"$newrev\\",\\"refname\\":\\"$refname\\",\\"pusher\\":\\"$REMOTE_USER\\"}" \\
      ${internalApiUrl}/api/internal/hooks/post-receive?repo=${encodeURIComponent(repoPath)}
done
`;

  const preReceiveScript = `#!/bin/bash
HOOK_SECRET="\${INTERNAL_HOOK_SECRET}"
if [ -z "$HOOK_SECRET" ]; then
    echo "Warning: INTERNAL_HOOK_SECRET not set, hook call may fail"
fi
while read oldrev newrev refname
do
    RESPONSE=$(curl -sS -w "%{http_code}" -X POST \\
      -H "Content-Type: application/json" \\
      -H "X-Hook-Secret: $HOOK_SECRET" \\
      -d "{\\"oldrev\\":\\"$oldrev\\",\\"newrev\\":\\"$newrev\\",\\"refname\\":\\"$refname\\",\\"pusher\\":\\"$REMOTE_USER\\"}" \\
      ${internalApiUrl}/api/internal/hooks/pre-receive?repo=${encodeURIComponent(repoPath)})

    HTTP_CODE=\${RESPONSE: -3}
    BODY=\${RESPONSE:0:\${#RESPONSE}-3}

    if [ "$HTTP_CODE" -ne 200 ]; then
        echo "Error: $BODY"
        exit 1
    fi
done
exit 0
`;

  await fs.writeFile(join(hooksDir, "post-receive"), postReceiveScript, {
    mode: 0o755,
  });

  await fs.writeFile(join(hooksDir, "pre-receive"), preReceiveScript, {
    mode: 0o755,
  });
}

/**
 * Commit a file modification to the repository
 */
export async function commitFile(
  repoPath: string,
  branch: string,
  filePath: string,
  content: string,
  message: string,
  author: { name: string; email: string },
): Promise<string> {
  const git = getGit(repoPath);

  // We need to use plumbing commands because it's a bare repo
  const resolvedRepoPath = resolve(repoPath);
  const tempIndexFile = join(
    resolvedRepoPath,
    `temp_index_${Date.now()}_${Math.random().toString(36).substring(7)}`,
  );

  try {
    // 1. Read the tree of the branch into a temporary index
    // We set GIT_INDEX_FILE environment variable for these commands
    const env = {
      ...getSanitizedGitEnv(dirname(repoPath)),
      GIT_INDEX_FILE: tempIndexFile,
    };

    // Get the tree of the current branch/commit
    // If branch doesn't exist (empty repo), start with an empty index
    let branchExists = true;
    try {
      await git.env(env).raw(["read-tree", branch]);
    } catch (e) {
      branchExists = false;
      await git.env(env).raw(["read-tree", "--empty"]);
    }

    // 2. Hash the new object using spawn to pipe content
    const hashObjectOutput = await new Promise<string>((resolve, reject) => {
      const gitProcess = spawn("git", ["hash-object", "-w", "--stdin"], {
        cwd: repoPath,
      });
      let output = "";
      gitProcess.stdout.on("data", (data) => {
        output += data.toString();
      });
      gitProcess.on("close", (code) => {
        if (code === 0) resolve(output.trim());
        else reject(new Error(`git hash-object failed with code ${code}`));
      });
      gitProcess.stdin.write(content);
      gitProcess.stdin.end();
    });
    const blobSha = hashObjectOutput;

    // 3. Update the index with the new blob
    // git update-index --add --cacheinfo 100644 <blob_sha> <path>
    await git
      .env(env)
      .raw([
        "update-index",
        "--add",
        "--cacheinfo",
        "100644",
        blobSha,
        filePath,
      ]);

    // 4. Write the new tree
    const writeTreeOutput = await git.env(env).raw(["write-tree"]);
    const treeSha = writeTreeOutput.trim();

    // 5. Create new commit
    // We need the parent commit (if the branch has commits yet)
    let parentSha = "";
    try {
      parentSha = (await git.revparse([branch])).trim();
    } catch (e) {
      // Branch has no commits yet (empty repo) — this will be the root commit
      parentSha = "";
    }

    const commitTreeArgs = parentSha
      ? ["commit-tree", treeSha, "-p", parentSha, "-m", message]
      : ["commit-tree", treeSha, "-m", message];

    // Set author/committer
    const commitEnv = {
      ...env,
      GIT_AUTHOR_NAME: author.name,
      GIT_AUTHOR_EMAIL: author.email,
      GIT_COMMITTER_NAME: author.name,
      GIT_COMMITTER_EMAIL: author.email,
    };

    const commitTreeOutput = await git.env(commitEnv).raw(commitTreeArgs);
    const newCommitSha = commitTreeOutput.trim();

    // 6. Update the branch ref
    await git.raw(["update-ref", `refs/heads/${branch}`, newCommitSha]);

    return newCommitSha;
  } finally {
    // Cleanup temporary index
    const fs = await import("fs/promises");
    try {
      await fs.unlink(tempIndexFile);
    } catch (e) {
      // Ignore cleanup error
    }
  }
}

/**
 * Get the physical path for a repository
 */
export function getRepoPath(owner: string, name: string): string {
  return join(process.cwd(), "repos", owner, `${name}.git`);
}

/**
 * Create a speculative branch combining multiple PRs
 */
export async function createSpeculativeBranch(
  repoPath: string,
  baseBranch: string,
  prs: { headBranch: string; number: number }[],
): Promise<{ branchName: string; success: boolean; message?: string }> {
  const git = getGit(repoPath);

  // Generate unique temp branch name
  const timestamp = Date.now();
  const prNumbers = prs.map((p) => p.number).join("-");
  const branchName = `mq-spec-${timestamp}-${prNumbers}`;

  try {
    // 1. Create temp branch from base
    await git.checkout(baseBranch);
    await git.pull();
    await git.checkoutLocalBranch(branchName);

    // 2. Merge each PR in order
    for (const pr of prs) {
      try {
        // Must fetch the PR branch first
        await git.fetch("origin", pr.headBranch);
        await git.merge([`origin/${pr.headBranch}`]);
      } catch (e: any) {
        // Conflict detected
        await git.checkout(baseBranch);
        // Attempt to delete the failed branch
        try {
          await git.deleteLocalBranch(branchName, true);
        } catch {}

        return {
          branchName,
          success: false,
          message: `Conflict merging PR #${pr.number} during speculative build: ${e.message}`,
        };
      }
    }

    // 3. Push to trigger CI
    await git.push("origin", branchName, ["--force"]);

    return { branchName, success: true };
  } catch (error: any) {
    return { branchName, success: false, message: error.message };
  }
}