gitx.do 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (356) hide show
  1. package/README.md +40 -353
  2. package/dist/do/logger.d.ts +50 -0
  3. package/dist/do/logger.d.ts.map +1 -0
  4. package/dist/do/logger.js +122 -0
  5. package/dist/do/logger.js.map +1 -0
  6. package/dist/{durable-object → do}/schema.d.ts +3 -3
  7. package/dist/do/schema.d.ts.map +1 -0
  8. package/dist/{durable-object → do}/schema.js +4 -3
  9. package/dist/do/schema.js.map +1 -0
  10. package/dist/do/types.d.ts +267 -0
  11. package/dist/do/types.d.ts.map +1 -0
  12. package/dist/do/types.js +62 -0
  13. package/dist/do/types.js.map +1 -0
  14. package/dist/index.d.ts +14 -469
  15. package/dist/index.d.ts.map +1 -1
  16. package/dist/index.js +31 -483
  17. package/dist/index.js.map +1 -1
  18. package/package.json +13 -21
  19. package/dist/cli/commands/add.d.ts +0 -176
  20. package/dist/cli/commands/add.d.ts.map +0 -1
  21. package/dist/cli/commands/add.js +0 -979
  22. package/dist/cli/commands/add.js.map +0 -1
  23. package/dist/cli/commands/blame.d.ts +0 -259
  24. package/dist/cli/commands/blame.d.ts.map +0 -1
  25. package/dist/cli/commands/blame.js +0 -609
  26. package/dist/cli/commands/blame.js.map +0 -1
  27. package/dist/cli/commands/branch.d.ts +0 -249
  28. package/dist/cli/commands/branch.d.ts.map +0 -1
  29. package/dist/cli/commands/branch.js +0 -693
  30. package/dist/cli/commands/branch.js.map +0 -1
  31. package/dist/cli/commands/checkout.d.ts +0 -73
  32. package/dist/cli/commands/checkout.d.ts.map +0 -1
  33. package/dist/cli/commands/checkout.js +0 -725
  34. package/dist/cli/commands/checkout.js.map +0 -1
  35. package/dist/cli/commands/commit.d.ts +0 -182
  36. package/dist/cli/commands/commit.d.ts.map +0 -1
  37. package/dist/cli/commands/commit.js +0 -457
  38. package/dist/cli/commands/commit.js.map +0 -1
  39. package/dist/cli/commands/diff.d.ts +0 -464
  40. package/dist/cli/commands/diff.d.ts.map +0 -1
  41. package/dist/cli/commands/diff.js +0 -959
  42. package/dist/cli/commands/diff.js.map +0 -1
  43. package/dist/cli/commands/log.d.ts +0 -239
  44. package/dist/cli/commands/log.d.ts.map +0 -1
  45. package/dist/cli/commands/log.js +0 -535
  46. package/dist/cli/commands/log.js.map +0 -1
  47. package/dist/cli/commands/merge.d.ts +0 -106
  48. package/dist/cli/commands/merge.d.ts.map +0 -1
  49. package/dist/cli/commands/merge.js +0 -852
  50. package/dist/cli/commands/merge.js.map +0 -1
  51. package/dist/cli/commands/review.d.ts +0 -457
  52. package/dist/cli/commands/review.d.ts.map +0 -1
  53. package/dist/cli/commands/review.js +0 -558
  54. package/dist/cli/commands/review.js.map +0 -1
  55. package/dist/cli/commands/stash.d.ts +0 -157
  56. package/dist/cli/commands/stash.d.ts.map +0 -1
  57. package/dist/cli/commands/stash.js +0 -655
  58. package/dist/cli/commands/stash.js.map +0 -1
  59. package/dist/cli/commands/status.d.ts +0 -269
  60. package/dist/cli/commands/status.d.ts.map +0 -1
  61. package/dist/cli/commands/status.js +0 -492
  62. package/dist/cli/commands/status.js.map +0 -1
  63. package/dist/cli/commands/web.d.ts +0 -199
  64. package/dist/cli/commands/web.d.ts.map +0 -1
  65. package/dist/cli/commands/web.js +0 -697
  66. package/dist/cli/commands/web.js.map +0 -1
  67. package/dist/cli/fs-adapter.d.ts +0 -656
  68. package/dist/cli/fs-adapter.d.ts.map +0 -1
  69. package/dist/cli/fs-adapter.js +0 -1177
  70. package/dist/cli/fs-adapter.js.map +0 -1
  71. package/dist/cli/fsx-cli-adapter.d.ts +0 -359
  72. package/dist/cli/fsx-cli-adapter.d.ts.map +0 -1
  73. package/dist/cli/fsx-cli-adapter.js +0 -619
  74. package/dist/cli/fsx-cli-adapter.js.map +0 -1
  75. package/dist/cli/index.d.ts +0 -387
  76. package/dist/cli/index.d.ts.map +0 -1
  77. package/dist/cli/index.js +0 -579
  78. package/dist/cli/index.js.map +0 -1
  79. package/dist/cli/ui/components/DiffView.d.ts +0 -12
  80. package/dist/cli/ui/components/DiffView.d.ts.map +0 -1
  81. package/dist/cli/ui/components/DiffView.js +0 -11
  82. package/dist/cli/ui/components/DiffView.js.map +0 -1
  83. package/dist/cli/ui/components/ErrorDisplay.d.ts +0 -10
  84. package/dist/cli/ui/components/ErrorDisplay.d.ts.map +0 -1
  85. package/dist/cli/ui/components/ErrorDisplay.js +0 -11
  86. package/dist/cli/ui/components/ErrorDisplay.js.map +0 -1
  87. package/dist/cli/ui/components/FuzzySearch.d.ts +0 -15
  88. package/dist/cli/ui/components/FuzzySearch.d.ts.map +0 -1
  89. package/dist/cli/ui/components/FuzzySearch.js +0 -12
  90. package/dist/cli/ui/components/FuzzySearch.js.map +0 -1
  91. package/dist/cli/ui/components/LoadingSpinner.d.ts +0 -10
  92. package/dist/cli/ui/components/LoadingSpinner.d.ts.map +0 -1
  93. package/dist/cli/ui/components/LoadingSpinner.js +0 -10
  94. package/dist/cli/ui/components/LoadingSpinner.js.map +0 -1
  95. package/dist/cli/ui/components/NavigationList.d.ts +0 -14
  96. package/dist/cli/ui/components/NavigationList.d.ts.map +0 -1
  97. package/dist/cli/ui/components/NavigationList.js +0 -11
  98. package/dist/cli/ui/components/NavigationList.js.map +0 -1
  99. package/dist/cli/ui/components/ScrollableContent.d.ts +0 -13
  100. package/dist/cli/ui/components/ScrollableContent.d.ts.map +0 -1
  101. package/dist/cli/ui/components/ScrollableContent.js +0 -11
  102. package/dist/cli/ui/components/ScrollableContent.js.map +0 -1
  103. package/dist/cli/ui/components/index.d.ts +0 -7
  104. package/dist/cli/ui/components/index.d.ts.map +0 -1
  105. package/dist/cli/ui/components/index.js +0 -9
  106. package/dist/cli/ui/components/index.js.map +0 -1
  107. package/dist/cli/ui/terminal-ui.d.ts +0 -85
  108. package/dist/cli/ui/terminal-ui.d.ts.map +0 -1
  109. package/dist/cli/ui/terminal-ui.js +0 -121
  110. package/dist/cli/ui/terminal-ui.js.map +0 -1
  111. package/dist/do/BashModule.d.ts +0 -871
  112. package/dist/do/BashModule.d.ts.map +0 -1
  113. package/dist/do/BashModule.js +0 -1143
  114. package/dist/do/BashModule.js.map +0 -1
  115. package/dist/do/FsModule.d.ts +0 -612
  116. package/dist/do/FsModule.d.ts.map +0 -1
  117. package/dist/do/FsModule.js +0 -1120
  118. package/dist/do/FsModule.js.map +0 -1
  119. package/dist/do/GitModule.d.ts +0 -635
  120. package/dist/do/GitModule.d.ts.map +0 -1
  121. package/dist/do/GitModule.js +0 -784
  122. package/dist/do/GitModule.js.map +0 -1
  123. package/dist/do/GitRepoDO.d.ts +0 -281
  124. package/dist/do/GitRepoDO.d.ts.map +0 -1
  125. package/dist/do/GitRepoDO.js +0 -479
  126. package/dist/do/GitRepoDO.js.map +0 -1
  127. package/dist/do/bash-ast.d.ts +0 -246
  128. package/dist/do/bash-ast.d.ts.map +0 -1
  129. package/dist/do/bash-ast.js +0 -888
  130. package/dist/do/bash-ast.js.map +0 -1
  131. package/dist/do/container-executor.d.ts +0 -491
  132. package/dist/do/container-executor.d.ts.map +0 -1
  133. package/dist/do/container-executor.js +0 -731
  134. package/dist/do/container-executor.js.map +0 -1
  135. package/dist/do/index.d.ts +0 -53
  136. package/dist/do/index.d.ts.map +0 -1
  137. package/dist/do/index.js +0 -91
  138. package/dist/do/index.js.map +0 -1
  139. package/dist/do/tiered-storage.d.ts +0 -403
  140. package/dist/do/tiered-storage.d.ts.map +0 -1
  141. package/dist/do/tiered-storage.js +0 -689
  142. package/dist/do/tiered-storage.js.map +0 -1
  143. package/dist/do/withBash.d.ts +0 -231
  144. package/dist/do/withBash.d.ts.map +0 -1
  145. package/dist/do/withBash.js +0 -244
  146. package/dist/do/withBash.js.map +0 -1
  147. package/dist/do/withFs.d.ts +0 -237
  148. package/dist/do/withFs.d.ts.map +0 -1
  149. package/dist/do/withFs.js +0 -387
  150. package/dist/do/withFs.js.map +0 -1
  151. package/dist/do/withGit.d.ts +0 -180
  152. package/dist/do/withGit.d.ts.map +0 -1
  153. package/dist/do/withGit.js +0 -271
  154. package/dist/do/withGit.js.map +0 -1
  155. package/dist/durable-object/object-store.d.ts +0 -633
  156. package/dist/durable-object/object-store.d.ts.map +0 -1
  157. package/dist/durable-object/object-store.js +0 -1164
  158. package/dist/durable-object/object-store.js.map +0 -1
  159. package/dist/durable-object/schema.d.ts.map +0 -1
  160. package/dist/durable-object/schema.js.map +0 -1
  161. package/dist/durable-object/wal.d.ts +0 -416
  162. package/dist/durable-object/wal.d.ts.map +0 -1
  163. package/dist/durable-object/wal.js +0 -445
  164. package/dist/durable-object/wal.js.map +0 -1
  165. package/dist/mcp/adapter.d.ts +0 -772
  166. package/dist/mcp/adapter.d.ts.map +0 -1
  167. package/dist/mcp/adapter.js +0 -895
  168. package/dist/mcp/adapter.js.map +0 -1
  169. package/dist/mcp/sandbox/miniflare-evaluator.d.ts +0 -22
  170. package/dist/mcp/sandbox/miniflare-evaluator.d.ts.map +0 -1
  171. package/dist/mcp/sandbox/miniflare-evaluator.js +0 -140
  172. package/dist/mcp/sandbox/miniflare-evaluator.js.map +0 -1
  173. package/dist/mcp/sandbox/object-store-proxy.d.ts +0 -32
  174. package/dist/mcp/sandbox/object-store-proxy.d.ts.map +0 -1
  175. package/dist/mcp/sandbox/object-store-proxy.js +0 -30
  176. package/dist/mcp/sandbox/object-store-proxy.js.map +0 -1
  177. package/dist/mcp/sandbox/template.d.ts +0 -17
  178. package/dist/mcp/sandbox/template.d.ts.map +0 -1
  179. package/dist/mcp/sandbox/template.js +0 -71
  180. package/dist/mcp/sandbox/template.js.map +0 -1
  181. package/dist/mcp/sandbox.d.ts +0 -764
  182. package/dist/mcp/sandbox.d.ts.map +0 -1
  183. package/dist/mcp/sandbox.js +0 -1362
  184. package/dist/mcp/sandbox.js.map +0 -1
  185. package/dist/mcp/sdk-adapter.d.ts +0 -835
  186. package/dist/mcp/sdk-adapter.d.ts.map +0 -1
  187. package/dist/mcp/sdk-adapter.js +0 -974
  188. package/dist/mcp/sdk-adapter.js.map +0 -1
  189. package/dist/mcp/tools/do.d.ts +0 -32
  190. package/dist/mcp/tools/do.d.ts.map +0 -1
  191. package/dist/mcp/tools/do.js +0 -117
  192. package/dist/mcp/tools/do.js.map +0 -1
  193. package/dist/mcp/tools.d.ts +0 -548
  194. package/dist/mcp/tools.d.ts.map +0 -1
  195. package/dist/mcp/tools.js +0 -3170
  196. package/dist/mcp/tools.js.map +0 -1
  197. package/dist/ops/blame.d.ts +0 -551
  198. package/dist/ops/blame.d.ts.map +0 -1
  199. package/dist/ops/blame.js +0 -1037
  200. package/dist/ops/blame.js.map +0 -1
  201. package/dist/ops/branch.d.ts +0 -766
  202. package/dist/ops/branch.d.ts.map +0 -1
  203. package/dist/ops/branch.js +0 -950
  204. package/dist/ops/branch.js.map +0 -1
  205. package/dist/ops/commit-traversal.d.ts +0 -349
  206. package/dist/ops/commit-traversal.d.ts.map +0 -1
  207. package/dist/ops/commit-traversal.js +0 -821
  208. package/dist/ops/commit-traversal.js.map +0 -1
  209. package/dist/ops/commit.d.ts +0 -555
  210. package/dist/ops/commit.d.ts.map +0 -1
  211. package/dist/ops/commit.js +0 -826
  212. package/dist/ops/commit.js.map +0 -1
  213. package/dist/ops/merge-base.d.ts +0 -397
  214. package/dist/ops/merge-base.d.ts.map +0 -1
  215. package/dist/ops/merge-base.js +0 -691
  216. package/dist/ops/merge-base.js.map +0 -1
  217. package/dist/ops/merge.d.ts +0 -855
  218. package/dist/ops/merge.d.ts.map +0 -1
  219. package/dist/ops/merge.js +0 -1551
  220. package/dist/ops/merge.js.map +0 -1
  221. package/dist/ops/tag.d.ts +0 -247
  222. package/dist/ops/tag.d.ts.map +0 -1
  223. package/dist/ops/tag.js +0 -649
  224. package/dist/ops/tag.js.map +0 -1
  225. package/dist/ops/tree-builder.d.ts +0 -178
  226. package/dist/ops/tree-builder.d.ts.map +0 -1
  227. package/dist/ops/tree-builder.js +0 -271
  228. package/dist/ops/tree-builder.js.map +0 -1
  229. package/dist/ops/tree-diff.d.ts +0 -291
  230. package/dist/ops/tree-diff.d.ts.map +0 -1
  231. package/dist/ops/tree-diff.js +0 -705
  232. package/dist/ops/tree-diff.js.map +0 -1
  233. package/dist/pack/delta.d.ts +0 -248
  234. package/dist/pack/delta.d.ts.map +0 -1
  235. package/dist/pack/delta.js +0 -740
  236. package/dist/pack/delta.js.map +0 -1
  237. package/dist/pack/format.d.ts +0 -446
  238. package/dist/pack/format.d.ts.map +0 -1
  239. package/dist/pack/format.js +0 -572
  240. package/dist/pack/format.js.map +0 -1
  241. package/dist/pack/full-generation.d.ts +0 -612
  242. package/dist/pack/full-generation.d.ts.map +0 -1
  243. package/dist/pack/full-generation.js +0 -1378
  244. package/dist/pack/full-generation.js.map +0 -1
  245. package/dist/pack/generation.d.ts +0 -441
  246. package/dist/pack/generation.d.ts.map +0 -1
  247. package/dist/pack/generation.js +0 -707
  248. package/dist/pack/generation.js.map +0 -1
  249. package/dist/pack/index.d.ts +0 -502
  250. package/dist/pack/index.d.ts.map +0 -1
  251. package/dist/pack/index.js +0 -833
  252. package/dist/pack/index.js.map +0 -1
  253. package/dist/refs/branch.d.ts +0 -683
  254. package/dist/refs/branch.d.ts.map +0 -1
  255. package/dist/refs/branch.js +0 -881
  256. package/dist/refs/branch.js.map +0 -1
  257. package/dist/refs/storage.d.ts +0 -833
  258. package/dist/refs/storage.d.ts.map +0 -1
  259. package/dist/refs/storage.js +0 -1023
  260. package/dist/refs/storage.js.map +0 -1
  261. package/dist/refs/tag.d.ts +0 -860
  262. package/dist/refs/tag.d.ts.map +0 -1
  263. package/dist/refs/tag.js +0 -996
  264. package/dist/refs/tag.js.map +0 -1
  265. package/dist/storage/backend.d.ts +0 -425
  266. package/dist/storage/backend.d.ts.map +0 -1
  267. package/dist/storage/backend.js +0 -41
  268. package/dist/storage/backend.js.map +0 -1
  269. package/dist/storage/fsx-adapter.d.ts +0 -204
  270. package/dist/storage/fsx-adapter.d.ts.map +0 -1
  271. package/dist/storage/fsx-adapter.js +0 -518
  272. package/dist/storage/fsx-adapter.js.map +0 -1
  273. package/dist/storage/lru-cache.d.ts +0 -691
  274. package/dist/storage/lru-cache.d.ts.map +0 -1
  275. package/dist/storage/lru-cache.js +0 -813
  276. package/dist/storage/lru-cache.js.map +0 -1
  277. package/dist/storage/object-index.d.ts +0 -585
  278. package/dist/storage/object-index.d.ts.map +0 -1
  279. package/dist/storage/object-index.js +0 -532
  280. package/dist/storage/object-index.js.map +0 -1
  281. package/dist/storage/r2-pack.d.ts +0 -1257
  282. package/dist/storage/r2-pack.d.ts.map +0 -1
  283. package/dist/storage/r2-pack.js +0 -1773
  284. package/dist/storage/r2-pack.js.map +0 -1
  285. package/dist/tiered/cdc-pipeline.d.ts +0 -1888
  286. package/dist/tiered/cdc-pipeline.d.ts.map +0 -1
  287. package/dist/tiered/cdc-pipeline.js +0 -1880
  288. package/dist/tiered/cdc-pipeline.js.map +0 -1
  289. package/dist/tiered/migration.d.ts +0 -1104
  290. package/dist/tiered/migration.d.ts.map +0 -1
  291. package/dist/tiered/migration.js +0 -1217
  292. package/dist/tiered/migration.js.map +0 -1
  293. package/dist/tiered/parquet-writer.d.ts +0 -1145
  294. package/dist/tiered/parquet-writer.d.ts.map +0 -1
  295. package/dist/tiered/parquet-writer.js +0 -1183
  296. package/dist/tiered/parquet-writer.js.map +0 -1
  297. package/dist/tiered/read-path.d.ts +0 -835
  298. package/dist/tiered/read-path.d.ts.map +0 -1
  299. package/dist/tiered/read-path.js +0 -487
  300. package/dist/tiered/read-path.js.map +0 -1
  301. package/dist/types/capability.d.ts +0 -1385
  302. package/dist/types/capability.d.ts.map +0 -1
  303. package/dist/types/capability.js +0 -36
  304. package/dist/types/capability.js.map +0 -1
  305. package/dist/types/index.d.ts +0 -13
  306. package/dist/types/index.d.ts.map +0 -1
  307. package/dist/types/index.js +0 -18
  308. package/dist/types/index.js.map +0 -1
  309. package/dist/types/interfaces.d.ts +0 -673
  310. package/dist/types/interfaces.d.ts.map +0 -1
  311. package/dist/types/interfaces.js +0 -26
  312. package/dist/types/interfaces.js.map +0 -1
  313. package/dist/types/objects.d.ts +0 -692
  314. package/dist/types/objects.d.ts.map +0 -1
  315. package/dist/types/objects.js +0 -837
  316. package/dist/types/objects.js.map +0 -1
  317. package/dist/types/storage.d.ts +0 -603
  318. package/dist/types/storage.d.ts.map +0 -1
  319. package/dist/types/storage.js +0 -191
  320. package/dist/types/storage.js.map +0 -1
  321. package/dist/types/worker-loader.d.ts +0 -60
  322. package/dist/types/worker-loader.d.ts.map +0 -1
  323. package/dist/types/worker-loader.js +0 -62
  324. package/dist/types/worker-loader.js.map +0 -1
  325. package/dist/utils/hash.d.ts +0 -198
  326. package/dist/utils/hash.d.ts.map +0 -1
  327. package/dist/utils/hash.js +0 -272
  328. package/dist/utils/hash.js.map +0 -1
  329. package/dist/utils/sha1.d.ts +0 -325
  330. package/dist/utils/sha1.d.ts.map +0 -1
  331. package/dist/utils/sha1.js +0 -635
  332. package/dist/utils/sha1.js.map +0 -1
  333. package/dist/wire/capabilities.d.ts +0 -1044
  334. package/dist/wire/capabilities.d.ts.map +0 -1
  335. package/dist/wire/capabilities.js +0 -941
  336. package/dist/wire/capabilities.js.map +0 -1
  337. package/dist/wire/path-security.d.ts +0 -157
  338. package/dist/wire/path-security.d.ts.map +0 -1
  339. package/dist/wire/path-security.js +0 -307
  340. package/dist/wire/path-security.js.map +0 -1
  341. package/dist/wire/pkt-line.d.ts +0 -345
  342. package/dist/wire/pkt-line.d.ts.map +0 -1
  343. package/dist/wire/pkt-line.js +0 -381
  344. package/dist/wire/pkt-line.js.map +0 -1
  345. package/dist/wire/receive-pack.d.ts +0 -1059
  346. package/dist/wire/receive-pack.d.ts.map +0 -1
  347. package/dist/wire/receive-pack.js +0 -1414
  348. package/dist/wire/receive-pack.js.map +0 -1
  349. package/dist/wire/smart-http.d.ts +0 -799
  350. package/dist/wire/smart-http.d.ts.map +0 -1
  351. package/dist/wire/smart-http.js +0 -945
  352. package/dist/wire/smart-http.js.map +0 -1
  353. package/dist/wire/upload-pack.d.ts +0 -727
  354. package/dist/wire/upload-pack.d.ts.map +0 -1
  355. package/dist/wire/upload-pack.js +0 -1141
  356. package/dist/wire/upload-pack.js.map +0 -1
@@ -1,979 +0,0 @@
1
- /**
2
- * @fileoverview gitx add command
3
- *
4
- * This module implements the `gitx add` command which adds files to the
5
- * staging area (index). It supports:
6
- * - Adding single files
7
- * - Adding multiple files
8
- * - Adding directories recursively
9
- * - Glob pattern matching
10
- * - Adding all files (-A or --all)
11
- * - Updating tracked files only (-u or --update)
12
- * - Dry run mode (-n or --dry-run)
13
- * - Verbose output (-v or --verbose)
14
- *
15
- * @module cli/commands/add
16
- *
17
- * @example
18
- * // Add a single file
19
- * await addFiles(cwd, ['file.txt'])
20
- *
21
- * @example
22
- * // Add all files
23
- * await addAll(cwd)
24
- *
25
- * @example
26
- * // Dry run to see what would be added
27
- * await addDryRun(cwd, ['*.ts'])
28
- */
29
- import * as fs from 'fs/promises';
30
- import * as path from 'path';
31
- import pako from 'pako';
32
- import { hashObjectStreamingHex } from '../../utils/sha1';
33
- // ============================================================================
34
- // Helper Functions
35
- // ============================================================================
36
- /**
37
- * Check if a directory exists
38
- */
39
- async function directoryExists(dirPath) {
40
- try {
41
- const stat = await fs.stat(dirPath);
42
- return stat.isDirectory();
43
- }
44
- catch {
45
- return false;
46
- }
47
- }
48
- /**
49
- * Check if a file exists
50
- */
51
- async function fileExists(filePath) {
52
- try {
53
- await fs.access(filePath);
54
- return true;
55
- }
56
- catch {
57
- return false;
58
- }
59
- }
60
- /**
61
- * Find the git directory for a repository
62
- */
63
- async function findGitDir(cwd) {
64
- const gitPath = path.join(cwd, '.git');
65
- try {
66
- const stat = await fs.stat(gitPath);
67
- if (stat.isDirectory()) {
68
- return gitPath;
69
- }
70
- if (stat.isFile()) {
71
- // Worktree - read the actual gitdir
72
- const content = await fs.readFile(gitPath, 'utf8');
73
- const match = content.match(/^gitdir:\s*(.+)$/m);
74
- if (match) {
75
- return path.resolve(cwd, match[1].trim());
76
- }
77
- }
78
- }
79
- catch {
80
- // Check if cwd itself is a bare repo
81
- const hasHead = await fileExists(path.join(cwd, 'HEAD'));
82
- const hasObjects = await directoryExists(path.join(cwd, 'objects'));
83
- const hasRefs = await directoryExists(path.join(cwd, 'refs'));
84
- if (hasHead && hasObjects && hasRefs) {
85
- return cwd;
86
- }
87
- }
88
- return null;
89
- }
90
- /**
91
- * Check if path is inside repository
92
- */
93
- function isPathInRepo(repoRoot, filePath) {
94
- const resolved = path.resolve(repoRoot, filePath);
95
- const relative = path.relative(repoRoot, resolved);
96
- return !relative.startsWith('..') && !path.isAbsolute(relative);
97
- }
98
- /**
99
- * Get tracked files from mock-tracked file or empty set
100
- */
101
- async function getTrackedFiles(gitDir) {
102
- const tracked = new Map();
103
- const mockTrackedPath = path.join(gitDir, 'mock-tracked');
104
- try {
105
- const content = await fs.readFile(mockTrackedPath, 'utf8');
106
- for (const line of content.split('\n')) {
107
- if (!line.trim())
108
- continue;
109
- const match = line.match(/^([0-9a-f]{40})\s+(\d+)\s+(.+)$/);
110
- if (match) {
111
- const [, sha, modeStr, filePath] = match;
112
- tracked.set(filePath, { sha, mode: parseInt(modeStr, 8) });
113
- }
114
- }
115
- }
116
- catch {
117
- // No tracked files
118
- }
119
- return tracked;
120
- }
121
- /**
122
- * Get staged files from mock-staged file or index
123
- */
124
- async function getStagedFiles(gitDir) {
125
- const staged = new Map();
126
- const mockStagedPath = path.join(gitDir, 'mock-staged');
127
- try {
128
- const content = await fs.readFile(mockStagedPath, 'utf8');
129
- for (const line of content.split('\n')) {
130
- if (!line.trim())
131
- continue;
132
- const match = line.match(/^([0-9a-f]{40})\s+(\d+)\s+(.+)$/);
133
- if (match) {
134
- const [, sha, modeStr, filePath] = match;
135
- staged.set(filePath, { sha, mode: parseInt(modeStr, 8) });
136
- }
137
- }
138
- }
139
- catch {
140
- // No staged files
141
- }
142
- return staged;
143
- }
144
- /**
145
- * Load .gitignore rules
146
- */
147
- async function loadGitIgnoreRules(cwd) {
148
- const rules = { patterns: [] };
149
- const gitignorePath = path.join(cwd, '.gitignore');
150
- try {
151
- const content = await fs.readFile(gitignorePath, 'utf8');
152
- for (const line of content.split('\n')) {
153
- const trimmed = line.trim();
154
- if (trimmed && !trimmed.startsWith('#')) {
155
- rules.patterns.push(trimmed);
156
- }
157
- }
158
- }
159
- catch {
160
- // No .gitignore
161
- }
162
- return rules;
163
- }
164
- /**
165
- * Check if a file path matches gitignore patterns
166
- */
167
- function isIgnored(filePath, rules) {
168
- for (const pattern of rules.patterns) {
169
- if (matchGlobPattern(filePath, pattern)) {
170
- return true;
171
- }
172
- // Also check basename for patterns without path separator
173
- if (!pattern.includes('/')) {
174
- const basename = path.basename(filePath);
175
- if (matchGlobPattern(basename, pattern)) {
176
- return true;
177
- }
178
- }
179
- // Check directory patterns
180
- if (pattern.endsWith('/')) {
181
- const dirPattern = pattern.slice(0, -1);
182
- if (filePath.startsWith(dirPattern + '/') || filePath === dirPattern) {
183
- return true;
184
- }
185
- }
186
- }
187
- return false;
188
- }
189
- /**
190
- * Get file mode (regular, executable, or symlink)
191
- */
192
- async function getFileMode(filePath) {
193
- try {
194
- const stat = await fs.lstat(filePath);
195
- if (stat.isSymbolicLink()) {
196
- return 0o120000;
197
- }
198
- // Check if executable
199
- if (stat.mode & 0o111) {
200
- return 0o100755;
201
- }
202
- return 0o100644;
203
- }
204
- catch {
205
- return 0o100644;
206
- }
207
- }
208
- /**
209
- * Compute blob SHA for a file
210
- * @internal Reserved for future use
211
- */
212
- async function _computeBlobSha(filePath) {
213
- const content = await fs.readFile(filePath);
214
- return hashObjectStreamingHex('blob', new Uint8Array(content));
215
- }
216
- void _computeBlobSha; // Preserve for future use
217
- /**
218
- * Store a blob object in the object store
219
- */
220
- async function storeBlob(gitDir, sha, content) {
221
- const objectsDir = path.join(gitDir, 'objects');
222
- const prefix = sha.substring(0, 2);
223
- const suffix = sha.substring(2);
224
- const prefixDir = path.join(objectsDir, prefix);
225
- const objectPath = path.join(prefixDir, suffix);
226
- // Create directory if needed
227
- await fs.mkdir(prefixDir, { recursive: true });
228
- // Check if already exists
229
- if (await fileExists(objectPath)) {
230
- return;
231
- }
232
- // Create blob content with header
233
- const header = `blob ${content.length}\0`;
234
- const headerBytes = new TextEncoder().encode(header);
235
- const combined = new Uint8Array(headerBytes.length + content.length);
236
- combined.set(headerBytes, 0);
237
- combined.set(content, headerBytes.length);
238
- // Compress and write
239
- const compressed = pako.deflate(combined);
240
- await fs.writeFile(objectPath, compressed);
241
- }
242
- /**
243
- * Update the index with staged files
244
- */
245
- async function updateIndex(gitDir, entries) {
246
- // For testing purposes, we'll update a mock-staged file
247
- // In a real implementation, this would write the binary index format
248
- const mockStagedPath = path.join(gitDir, 'mock-staged');
249
- // Load existing staged files
250
- const existing = await getStagedFiles(gitDir);
251
- // Merge new entries
252
- for (const entry of entries) {
253
- existing.set(entry.path, { sha: entry.sha, mode: entry.mode });
254
- }
255
- // Write back
256
- const lines = [];
257
- for (const [filePath, { sha, mode }] of existing) {
258
- lines.push(`${sha} ${mode.toString(8)} ${filePath}`);
259
- }
260
- await fs.writeFile(mockStagedPath, lines.join('\n'));
261
- }
262
- /**
263
- * Remove entries from the index (for deletions)
264
- */
265
- async function removeFromIndex(gitDir, paths) {
266
- const mockStagedPath = path.join(gitDir, 'mock-staged');
267
- // Load existing staged files
268
- const existing = await getStagedFiles(gitDir);
269
- // Remove specified paths
270
- for (const filePath of paths) {
271
- existing.delete(filePath);
272
- }
273
- // Write back
274
- const lines = [];
275
- for (const [filePath, { sha, mode }] of existing) {
276
- lines.push(`${sha} ${mode.toString(8)} ${filePath}`);
277
- }
278
- if (lines.length > 0) {
279
- await fs.writeFile(mockStagedPath, lines.join('\n'));
280
- }
281
- else {
282
- // Remove the file if empty
283
- try {
284
- await fs.unlink(mockStagedPath);
285
- }
286
- catch {
287
- // Ignore if doesn't exist
288
- }
289
- }
290
- }
291
- /**
292
- * Walk directory recursively and collect files
293
- */
294
- async function walkDirectory(basePath, relativePath, gitIgnore, files) {
295
- const currentPath = relativePath ? path.join(basePath, relativePath) : basePath;
296
- try {
297
- const entries = await fs.readdir(currentPath, { withFileTypes: true });
298
- for (const entry of entries) {
299
- // Skip .git directory
300
- if (entry.name === '.git')
301
- continue;
302
- const entryRelative = relativePath ? `${relativePath}/${entry.name}` : entry.name;
303
- if (entry.isDirectory()) {
304
- await walkDirectory(basePath, entryRelative, gitIgnore, files);
305
- }
306
- else if (entry.isFile() || entry.isSymbolicLink()) {
307
- files.push(entryRelative);
308
- }
309
- }
310
- }
311
- catch {
312
- // Directory might not be readable
313
- }
314
- }
315
- // ============================================================================
316
- // Command Handler
317
- // ============================================================================
318
- /**
319
- * Handler for the gitx add command.
320
- *
321
- * @description Processes command-line arguments and adds files to the staging area.
322
- *
323
- * @param ctx - Command context with cwd, args, options
324
- * @throws Error if not in a git repository or files not found
325
- */
326
- export async function addCommand(ctx) {
327
- const { cwd, options, stdout, stderr } = ctx;
328
- let { args } = ctx;
329
- // Handle --help flag
330
- if (options.help || options.h) {
331
- stdout(`gitx add - Add file contents to the index
332
-
333
- Usage: gitx add [options] [--] <pathspec>...
334
-
335
- Options:
336
- -A, --all Add all files (new, modified, deleted)
337
- -u, --update Update tracked files only
338
- -n, --dry-run Show what would be added
339
- -v, --verbose Be verbose
340
- -f, --force Allow adding otherwise ignored files
341
- -N, --intent-to-add Record that the path will be added later
342
- -p, --patch Interactively choose hunks of patch
343
- --refresh Don't add, just refresh the stat() info`);
344
- return;
345
- }
346
- // Find git directory
347
- const gitDir = await findGitDir(cwd);
348
- if (!gitDir) {
349
- throw new Error('fatal: not a git repository (or any of the parent directories): .git');
350
- }
351
- const verbose = options.verbose || options.v;
352
- // For add command, -n/--dry-run means dry-run
353
- // The CLI parser may consume the next argument as a value, so handle that case
354
- let dryRun = false;
355
- // Handle --dry-run (may have consumed file.txt as its value)
356
- if (options.dryRun !== undefined) {
357
- if (typeof options.dryRun === 'string') {
358
- args = [options.dryRun, ...args];
359
- dryRun = true;
360
- }
361
- else if (options.dryRun === true) {
362
- dryRun = true;
363
- }
364
- }
365
- // Handle -n (may have consumed file.txt as its value)
366
- if (options.n !== undefined) {
367
- if (typeof options.n === 'string') {
368
- args = [options.n, ...args];
369
- dryRun = true;
370
- }
371
- else if (options.n === null || options.n === true) {
372
- dryRun = true;
373
- }
374
- }
375
- const force = options.force || options.f;
376
- // Handle -N/--intent-to-add (may have consumed file.txt as its value)
377
- let intentToAdd = false;
378
- let intentArg = null;
379
- if (options.intentToAdd !== undefined) {
380
- if (typeof options.intentToAdd === 'string') {
381
- intentArg = options.intentToAdd;
382
- intentToAdd = true;
383
- }
384
- else if (options.intentToAdd === true) {
385
- intentToAdd = true;
386
- }
387
- }
388
- if (options.N !== undefined) {
389
- if (typeof options.N === 'string') {
390
- intentArg = options.N;
391
- intentToAdd = true;
392
- }
393
- else if (options.N === null || options.N === true) {
394
- intentToAdd = true;
395
- }
396
- }
397
- if (intentArg) {
398
- args = [intentArg, ...args];
399
- }
400
- const update = options.update || options.u;
401
- const all = options.all || options.A;
402
- const refresh = options.refresh;
403
- const patch = options.patch || options.p;
404
- // Handle --refresh flag
405
- if (refresh) {
406
- // Just refresh index stat info - no-op for mock implementation
407
- return;
408
- }
409
- // Handle --patch flag (interactive - just acknowledge for now)
410
- if (patch) {
411
- // Patch mode is interactive - just return for now
412
- return;
413
- }
414
- // No files specified and no -A or -u flag
415
- if (args.length === 0 && !all && !update) {
416
- throw new Error('Nothing specified, nothing added.\nMaybe you wanted to say \'gitx add .\'?');
417
- }
418
- let result;
419
- if (all) {
420
- result = await addAll(cwd, { verbose, dryRun, force });
421
- }
422
- else if (update) {
423
- result = await addUpdate(cwd, { verbose, dryRun });
424
- }
425
- else if (dryRun) {
426
- result = await addDryRun(cwd, args, { verbose, force });
427
- }
428
- else {
429
- result = await addFiles(cwd, args, { verbose, force, intentToAdd });
430
- }
431
- // Output for verbose mode
432
- if (verbose) {
433
- for (const filePath of result.added) {
434
- stdout(`add '${filePath}'`);
435
- }
436
- for (const filePath of result.wouldAdd) {
437
- stdout(`add '${filePath}'`);
438
- }
439
- for (const filePath of result.deleted) {
440
- stdout(`remove '${filePath}'`);
441
- }
442
- }
443
- // Output for dry-run mode
444
- if (dryRun) {
445
- for (const filePath of result.wouldAdd) {
446
- stdout(`add '${filePath}'`);
447
- }
448
- }
449
- // Output warnings to stderr
450
- for (const warning of result.warnings) {
451
- stderr(warning);
452
- }
453
- }
454
- // ============================================================================
455
- // Core Functions
456
- // ============================================================================
457
- /**
458
- * Add specified files to the staging area.
459
- *
460
- * @description Adds the given files or patterns to the git index.
461
- * Supports glob patterns and directories.
462
- *
463
- * @param cwd - Working directory (repository root)
464
- * @param paths - File paths or patterns to add
465
- * @param options - Add options
466
- * @returns AddResult with added files information
467
- * @throws Error if not in a git repository or files not found
468
- */
469
- export async function addFiles(cwd, paths, options = {}) {
470
- // Find git directory
471
- const gitDir = await findGitDir(cwd);
472
- if (!gitDir) {
473
- throw new Error('fatal: not a git repository (or any of the parent directories): .git');
474
- }
475
- const result = {
476
- added: [],
477
- deleted: [],
478
- unchanged: [],
479
- wouldAdd: [],
480
- intentToAdd: [],
481
- files: [],
482
- count: 0,
483
- warnings: []
484
- };
485
- // Load gitignore rules
486
- const gitIgnore = await loadGitIgnoreRules(cwd);
487
- // Get files to add
488
- const filesToAdd = await getFilesToAdd(cwd, paths, options);
489
- // Get current staged files to check for unchanged
490
- const stagedFiles = await getStagedFiles(gitDir);
491
- // Process each file
492
- const indexEntries = [];
493
- const errors = [];
494
- for (const file of filesToAdd) {
495
- const fullPath = path.join(cwd, file.path);
496
- // Check if path is outside repository
497
- if (!isPathInRepo(cwd, file.path)) {
498
- throw new Error(`fatal: '${file.path}' is outside repository`);
499
- }
500
- // Check if file is ignored (unless force)
501
- if (!options.force && isIgnored(file.path, gitIgnore)) {
502
- errors.push(`The following paths are ignored by one of your .gitignore files:\n${file.path}`);
503
- continue;
504
- }
505
- // Get file content and compute SHA
506
- try {
507
- const content = await fs.readFile(fullPath);
508
- const sha = hashObjectStreamingHex('blob', new Uint8Array(content));
509
- const mode = await getFileMode(fullPath);
510
- // Check if unchanged
511
- const existingEntry = stagedFiles.get(file.path);
512
- if (existingEntry && existingEntry.sha === sha) {
513
- result.unchanged.push(file.path);
514
- // Still include in files for return value
515
- result.files.push({ path: file.path, sha, mode });
516
- continue;
517
- }
518
- if (options.intentToAdd) {
519
- result.intentToAdd.push(file.path);
520
- // For intent-to-add, we add with empty content
521
- indexEntries.push({ path: file.path, sha: '0'.repeat(40), mode });
522
- }
523
- else {
524
- // Store the blob
525
- await storeBlob(gitDir, sha, new Uint8Array(content));
526
- result.added.push(file.path);
527
- result.files.push({ path: file.path, sha, mode });
528
- indexEntries.push({ path: file.path, sha, mode });
529
- }
530
- }
531
- catch (err) {
532
- if (err.code === 'ENOENT') {
533
- errors.push(`fatal: pathspec '${file.path}' does not exist (file not found)`);
534
- }
535
- else if (err.code === 'EACCES' || err.code === 'EPERM') {
536
- throw new Error(`error: open("${file.path}"): Permission denied`);
537
- }
538
- else {
539
- throw err;
540
- }
541
- }
542
- }
543
- // Update index with new entries
544
- if (indexEntries.length > 0) {
545
- await updateIndex(gitDir, indexEntries);
546
- }
547
- result.count = result.added.length + result.intentToAdd.length;
548
- // Report errors - but if some files were added, just warn and continue
549
- if (errors.length > 0) {
550
- if (result.added.length === 0) {
551
- throw new Error(errors[0]);
552
- }
553
- // Files were added but some had errors - record warnings
554
- result.warnings = errors;
555
- }
556
- return result;
557
- }
558
- /**
559
- * Add all files to the staging area (like git add -A).
560
- *
561
- * @description Adds all untracked, modified, and deleted files
562
- * to the staging area.
563
- *
564
- * @param cwd - Working directory (repository root)
565
- * @param options - Add options
566
- * @returns AddResult with added files information
567
- * @throws Error if not in a git repository
568
- */
569
- export async function addAll(cwd, options = {}) {
570
- // Find git directory
571
- const gitDir = await findGitDir(cwd);
572
- if (!gitDir) {
573
- throw new Error('fatal: not a git repository (or any of the parent directories): .git');
574
- }
575
- const result = {
576
- added: [],
577
- deleted: [],
578
- unchanged: [],
579
- wouldAdd: [],
580
- intentToAdd: [],
581
- files: [],
582
- count: 0,
583
- warnings: []
584
- };
585
- // Load gitignore rules
586
- const gitIgnore = await loadGitIgnoreRules(cwd);
587
- // Get tracked files
588
- const tracked = await getTrackedFiles(gitDir);
589
- // Walk directory to find all files
590
- const allFiles = [];
591
- await walkDirectory(cwd, '', gitIgnore, allFiles);
592
- // Process all files (add new/modified)
593
- const indexEntries = [];
594
- const stagedFiles = await getStagedFiles(gitDir);
595
- for (const filePath of allFiles) {
596
- // Skip ignored files (unless force)
597
- if (!options.force && isIgnored(filePath, gitIgnore)) {
598
- continue;
599
- }
600
- const fullPath = path.join(cwd, filePath);
601
- try {
602
- const content = await fs.readFile(fullPath);
603
- const sha = hashObjectStreamingHex('blob', new Uint8Array(content));
604
- const mode = await getFileMode(fullPath);
605
- // Check if unchanged from staged
606
- const existingEntry = stagedFiles.get(filePath);
607
- if (existingEntry && existingEntry.sha === sha) {
608
- result.unchanged.push(filePath);
609
- continue;
610
- }
611
- if (options.dryRun) {
612
- result.wouldAdd.push(filePath);
613
- }
614
- else {
615
- // Store the blob
616
- await storeBlob(gitDir, sha, new Uint8Array(content));
617
- result.added.push(filePath);
618
- result.files.push({ path: filePath, sha, mode });
619
- indexEntries.push({ path: filePath, sha, mode });
620
- }
621
- }
622
- catch {
623
- // Skip unreadable files
624
- }
625
- }
626
- // Check for deleted files (tracked but not on disk)
627
- for (const [trackedPath] of tracked) {
628
- const fullPath = path.join(cwd, trackedPath);
629
- if (!await fileExists(fullPath)) {
630
- if (options.dryRun) {
631
- result.wouldAdd.push(trackedPath);
632
- }
633
- else {
634
- result.deleted.push(trackedPath);
635
- }
636
- }
637
- }
638
- // Update index
639
- if (!options.dryRun && indexEntries.length > 0) {
640
- await updateIndex(gitDir, indexEntries);
641
- }
642
- // Remove deleted files from index
643
- if (!options.dryRun && result.deleted.length > 0) {
644
- await removeFromIndex(gitDir, result.deleted);
645
- }
646
- result.count = result.added.length + result.deleted.length;
647
- return result;
648
- }
649
- /**
650
- * Update tracked files only (like git add -u).
651
- *
652
- * @description Stages modifications and deletions of tracked files only.
653
- * Does not add untracked files.
654
- *
655
- * @param cwd - Working directory (repository root)
656
- * @param options - Add options
657
- * @returns AddResult with updated files information
658
- * @throws Error if not in a git repository
659
- */
660
- export async function addUpdate(cwd, options = {}) {
661
- // Find git directory
662
- const gitDir = await findGitDir(cwd);
663
- if (!gitDir) {
664
- throw new Error('fatal: not a git repository (or any of the parent directories): .git');
665
- }
666
- const result = {
667
- added: [],
668
- deleted: [],
669
- unchanged: [],
670
- wouldAdd: [],
671
- intentToAdd: [],
672
- files: [],
673
- count: 0,
674
- warnings: []
675
- };
676
- // Get tracked files
677
- const tracked = await getTrackedFiles(gitDir);
678
- void getStagedFiles(gitDir); // Keep available for potential future use
679
- // Process only tracked files
680
- const indexEntries = [];
681
- for (const [trackedPath, trackedInfo] of tracked) {
682
- const fullPath = path.join(cwd, trackedPath);
683
- try {
684
- // Check if file still exists
685
- if (await fileExists(fullPath)) {
686
- const content = await fs.readFile(fullPath);
687
- const sha = hashObjectStreamingHex('blob', new Uint8Array(content));
688
- const mode = await getFileMode(fullPath);
689
- // Check if modified
690
- if (sha !== trackedInfo.sha) {
691
- if (options.dryRun) {
692
- result.wouldAdd.push(trackedPath);
693
- }
694
- else {
695
- // Store the blob
696
- await storeBlob(gitDir, sha, new Uint8Array(content));
697
- result.added.push(trackedPath);
698
- result.files.push({ path: trackedPath, sha, mode });
699
- indexEntries.push({ path: trackedPath, sha, mode });
700
- }
701
- }
702
- else {
703
- result.unchanged.push(trackedPath);
704
- }
705
- }
706
- else {
707
- // File was deleted
708
- if (options.dryRun) {
709
- result.wouldAdd.push(trackedPath);
710
- }
711
- else {
712
- result.deleted.push(trackedPath);
713
- }
714
- }
715
- }
716
- catch {
717
- // Skip unreadable files
718
- }
719
- }
720
- // Update index
721
- if (!options.dryRun && indexEntries.length > 0) {
722
- await updateIndex(gitDir, indexEntries);
723
- }
724
- // Remove deleted files from index
725
- if (!options.dryRun && result.deleted.length > 0) {
726
- await removeFromIndex(gitDir, result.deleted);
727
- }
728
- result.count = result.added.length + result.deleted.length;
729
- return result;
730
- }
731
- /**
732
- * Dry run to show what would be added.
733
- *
734
- * @description Shows what files would be added without actually
735
- * modifying the index.
736
- *
737
- * @param cwd - Working directory (repository root)
738
- * @param paths - File paths or patterns to check
739
- * @param options - Add options
740
- * @returns AddResult with wouldAdd populated
741
- * @throws Error if not in a git repository
742
- */
743
- export async function addDryRun(cwd, paths, options = {}) {
744
- // Find git directory
745
- const gitDir = await findGitDir(cwd);
746
- if (!gitDir) {
747
- throw new Error('fatal: not a git repository (or any of the parent directories): .git');
748
- }
749
- const result = {
750
- added: [],
751
- deleted: [],
752
- unchanged: [],
753
- wouldAdd: [],
754
- intentToAdd: [],
755
- files: [],
756
- count: 0,
757
- warnings: []
758
- };
759
- // Load gitignore rules
760
- const gitIgnore = await loadGitIgnoreRules(cwd);
761
- // Get files to add
762
- const filesToAdd = await getFilesToAdd(cwd, paths, options);
763
- for (const file of filesToAdd) {
764
- const fullPath = path.join(cwd, file.path);
765
- // Check if path is outside repository
766
- if (!isPathInRepo(cwd, file.path)) {
767
- throw new Error(`fatal: '${file.path}' is outside repository`);
768
- }
769
- // Check if file is ignored (unless force)
770
- if (!options.force && isIgnored(file.path, gitIgnore)) {
771
- continue;
772
- }
773
- // Check if file exists
774
- if (await fileExists(fullPath)) {
775
- result.wouldAdd.push(file.path);
776
- }
777
- }
778
- result.count = result.wouldAdd.length;
779
- return result;
780
- }
781
- /**
782
- * Get list of files that would be added for given paths.
783
- *
784
- * @description Resolves paths and glob patterns to a list of files
785
- * that would be added to the index.
786
- *
787
- * @param cwd - Working directory (repository root)
788
- * @param paths - File paths or patterns
789
- * @param options - Add options including exclude patterns
790
- * @returns Array of FileToAdd objects
791
- * @throws Error if not in a git repository
792
- */
793
- export async function getFilesToAdd(cwd, paths, options = {}) {
794
- // Find git directory
795
- const gitDir = await findGitDir(cwd);
796
- if (!gitDir) {
797
- throw new Error('fatal: not a git repository (or any of the parent directories): .git');
798
- }
799
- const result = [];
800
- const seen = new Set();
801
- // Load gitignore rules for filtering
802
- const gitIgnore = await loadGitIgnoreRules(cwd);
803
- for (const pathSpec of paths) {
804
- // Check if it's a glob pattern
805
- if (pathSpec.includes('*') || pathSpec.includes('?') || pathSpec.includes('{')) {
806
- // Walk directory and match pattern
807
- const allFiles = [];
808
- await walkDirectory(cwd, '', gitIgnore, allFiles);
809
- for (const filePath of allFiles) {
810
- if (matchGlobPattern(filePath, pathSpec)) {
811
- // Check exclude patterns
812
- if (options.exclude) {
813
- let excluded = false;
814
- for (const exclude of options.exclude) {
815
- if (matchGlobPattern(filePath, exclude)) {
816
- excluded = true;
817
- break;
818
- }
819
- }
820
- if (excluded)
821
- continue;
822
- }
823
- if (!seen.has(filePath)) {
824
- seen.add(filePath);
825
- result.push({ path: filePath, sha: '', mode: 0 });
826
- }
827
- }
828
- }
829
- }
830
- else {
831
- // Direct path - could be file or directory
832
- const fullPath = path.join(cwd, pathSpec);
833
- try {
834
- const stat = await fs.stat(fullPath);
835
- if (stat.isDirectory()) {
836
- // Add all files in directory
837
- const dirFiles = [];
838
- // Handle '.' as root directory (empty string)
839
- const relPath = pathSpec === '.' ? '' : pathSpec;
840
- await walkDirectory(cwd, relPath, gitIgnore, dirFiles);
841
- for (const filePath of dirFiles) {
842
- // Check exclude patterns
843
- if (options.exclude) {
844
- let excluded = false;
845
- for (const exclude of options.exclude) {
846
- if (matchGlobPattern(filePath, exclude)) {
847
- excluded = true;
848
- break;
849
- }
850
- }
851
- if (excluded)
852
- continue;
853
- }
854
- if (!seen.has(filePath)) {
855
- seen.add(filePath);
856
- result.push({ path: filePath, sha: '', mode: 0 });
857
- }
858
- }
859
- }
860
- else if (stat.isFile() || stat.isSymbolicLink()) {
861
- // Check exclude patterns
862
- if (options.exclude) {
863
- let excluded = false;
864
- for (const exclude of options.exclude) {
865
- if (matchGlobPattern(pathSpec, exclude)) {
866
- excluded = true;
867
- break;
868
- }
869
- }
870
- if (excluded)
871
- continue;
872
- }
873
- if (!seen.has(pathSpec)) {
874
- seen.add(pathSpec);
875
- result.push({ path: pathSpec, sha: '', mode: 0 });
876
- }
877
- }
878
- }
879
- catch (err) {
880
- if (err.code === 'ENOENT') {
881
- // File doesn't exist - still add to list for error handling later
882
- if (!seen.has(pathSpec)) {
883
- seen.add(pathSpec);
884
- result.push({ path: pathSpec, sha: '', mode: 0 });
885
- }
886
- }
887
- else {
888
- throw err;
889
- }
890
- }
891
- }
892
- }
893
- return result;
894
- }
895
- /**
896
- * Match a file path against a glob pattern.
897
- *
898
- * @description Utility function to test if a path matches a glob pattern.
899
- *
900
- * @param filePath - File path to test
901
- * @param pattern - Glob pattern to match against
902
- * @returns true if the path matches the pattern
903
- */
904
- export function matchGlobPattern(filePath, pattern) {
905
- // Normalize paths
906
- const normalizedPath = filePath.replace(/\\/g, '/');
907
- const normalizedPattern = pattern.replace(/\\/g, '/');
908
- // Handle brace expansion {a,b,c}
909
- if (normalizedPattern.includes('{') && normalizedPattern.includes('}')) {
910
- const braceMatch = normalizedPattern.match(/\{([^}]+)\}/);
911
- if (braceMatch) {
912
- const options = braceMatch[1].split(',');
913
- for (const option of options) {
914
- const expandedPattern = normalizedPattern.replace(braceMatch[0], option);
915
- if (matchGlobPattern(filePath, expandedPattern)) {
916
- return true;
917
- }
918
- }
919
- return false;
920
- }
921
- }
922
- // Handle ** pattern for recursive matching
923
- if (normalizedPattern.includes('**')) {
924
- // **/*.ts should match:
925
- // - root.ts (at root level)
926
- // - src/file.ts (in subdirectory)
927
- // - src/sub/file.ts (deeply nested)
928
- // Split pattern by **/ to handle each segment
929
- const parts = normalizedPattern.split('**/');
930
- if (parts.length === 2) {
931
- const prefix = parts[0];
932
- const suffix = parts[1];
933
- // Build regex for the suffix part
934
- let suffixRegex = suffix
935
- .replace(/[.+^${}()|[\]\\]/g, '\\$&')
936
- .replace(/\*\*/g, '<<<DOUBLESTAR>>>')
937
- .replace(/\*/g, '[^/]*')
938
- .replace(/\?/g, '[^/]')
939
- .replace(/<<<DOUBLESTAR>>>/g, '.*');
940
- // **/*.ts at start means match optional path segments followed by suffix
941
- if (prefix === '') {
942
- // Match: optional(path/) + suffix
943
- const regex = new RegExp(`^(?:.*/)?${suffixRegex}$`);
944
- return regex.test(normalizedPath);
945
- }
946
- // prefix/**/*.ts means prefix + optional(path/) + suffix
947
- const prefixRegex = prefix
948
- .replace(/[.+^${}()|[\]\\]/g, '\\$&')
949
- .replace(/\*/g, '[^/]*')
950
- .replace(/\?/g, '[^/]');
951
- const regex = new RegExp(`^${prefixRegex}(?:.*/)?${suffixRegex}$`);
952
- return regex.test(normalizedPath);
953
- }
954
- // Generic ** handling
955
- let regexPattern = normalizedPattern
956
- .replace(/[.+^${}()|[\]\\]/g, '\\$&')
957
- .replace(/\*\*\//g, '(?:.*/)?')
958
- .replace(/\*\*/g, '.*')
959
- .replace(/\*/g, '[^/]*')
960
- .replace(/\?/g, '[^/]');
961
- const regex = new RegExp(`^${regexPattern}$`);
962
- return regex.test(normalizedPath);
963
- }
964
- // Simple glob matching
965
- let regexPattern = normalizedPattern
966
- .replace(/[.+^${}()|[\]\\]/g, '\\$&') // Escape special chars except * and ?
967
- .replace(/\*/g, '[^/]*') // * matches any chars except /
968
- .replace(/\?/g, '[^/]'); // ? matches single char except /
969
- // Add anchors
970
- regexPattern = `^${regexPattern}$`;
971
- try {
972
- const regex = new RegExp(regexPattern);
973
- return regex.test(normalizedPath);
974
- }
975
- catch {
976
- return false;
977
- }
978
- }
979
- //# sourceMappingURL=add.js.map