-
Notifications
You must be signed in to change notification settings - Fork 284
refactor: extract shared VCS infrastructure #1005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --- | ||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| { | ||
| "name": "@hunk/vcs", | ||
| "version": "0.0.0", | ||
| "private": true, | ||
| "description": "Shared implementation infrastructure for Hunk VCS backends.", | ||
| "license": "MIT", | ||
| "files": [ | ||
| "src" | ||
| ], | ||
| "type": "module", | ||
| "sideEffects": false, | ||
| "exports": { | ||
| "./async-process": { | ||
| "types": "./src/async-process.ts", | ||
| "import": "./src/async-process.ts" | ||
| }, | ||
| "./diff-target": { | ||
| "types": "./src/diff-target.ts", | ||
| "import": "./src/diff-target.ts" | ||
| }, | ||
| "./large-file": { | ||
| "types": "./src/large-file.ts", | ||
| "import": "./src/large-file.ts" | ||
| }, | ||
| "./path": { | ||
| "types": "./src/path.ts", | ||
| "import": "./src/path.ts" | ||
| }, | ||
| "./source": { | ||
| "types": "./src/source.ts", | ||
| "import": "./src/source.ts" | ||
| } | ||
| }, | ||
| "engines": { | ||
| "bun": ">=1.3.14", | ||
| "node": ">=22" | ||
| } | ||
| } | ||
2 changes: 1 addition & 1 deletion
2
...tensions/default/vcs/asyncProcess.test.ts → packages/hunk-vcs/src/async-process.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import fs from "node:fs"; | ||
| import { join } from "node:path"; | ||
|
|
||
| /** | ||
| * Thresholds past which Hunk lists a file instead of rendering its diff. | ||
| * | ||
| * Shared by every backend so "too large to review" means one thing across the | ||
| * product, and by the host's own untracked-file synthesizer. | ||
| */ | ||
| export const LARGE_DIFF_FILE_MAX_BYTES = 1_000_000; | ||
| export const LARGE_DIFF_FILE_MAX_LINES = 20_000; | ||
|
|
||
| /** How much of a file line counting reads before giving up and reporting a truncated count. */ | ||
| const LARGE_DIFF_FILE_SNIFF_BYTES = 256 * 1024; | ||
|
|
||
| interface CountedLines { | ||
| complete: boolean; | ||
| lines: number; | ||
| } | ||
|
|
||
| /** Count text lines with a byte cap so huge skipped-file stats do not block startup. */ | ||
| function countLinesInFile(path: string, maxBytes: number, size: number): CountedLines { | ||
| let fd: number | undefined; | ||
|
|
||
| try { | ||
| fd = fs.openSync(path, "r"); | ||
| const buffer = Buffer.alloc(Math.min(64 * 1024, maxBytes)); | ||
| let position = 0; | ||
| let lineCount = 0; | ||
| let lastByte: number | undefined; | ||
|
|
||
| while (position < maxBytes) { | ||
| const bytesToRead = Math.min(buffer.length, maxBytes - position); | ||
| const bytesRead = fs.readSync(fd, buffer, 0, bytesToRead, position); | ||
| if (bytesRead === 0) { | ||
| break; | ||
| } | ||
|
|
||
| position += bytesRead; | ||
| for (let index = 0; index < bytesRead; index += 1) { | ||
| lastByte = buffer[index]; | ||
| if (lastByte === 0x0a) { | ||
| lineCount += 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| complete: position >= size, | ||
| lines: lastByte !== undefined && lastByte !== 0x0a ? lineCount + 1 : lineCount, | ||
| }; | ||
| } catch { | ||
| return { complete: true, lines: 0 }; | ||
| } finally { | ||
| if (fd !== undefined) { | ||
| fs.closeSync(fd); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| export interface LargeFileCheck { | ||
| shouldSkip: boolean; | ||
| stats?: { additions: number; deletions: number }; | ||
| /** True when `stats` came from a capped read and undercount the real file. */ | ||
| statsTruncated?: boolean; | ||
| } | ||
|
|
||
| /** | ||
| * Return whether a whole file on disk is too large to synthesize into a patch. | ||
| * | ||
| * Used for files that would be rendered from their current contents — untracked | ||
| * files, and anything else a backend adds as a full-file addition — where the | ||
| * cost is the file itself rather than the size of a change to it. | ||
| */ | ||
| export function inspectLargeUntrackedFile(repoRoot: string, filePath: string): LargeFileCheck { | ||
| const absolutePath = join(repoRoot, filePath); | ||
|
|
||
| let stat: fs.Stats; | ||
| try { | ||
| stat = fs.statSync(absolutePath); | ||
| } catch { | ||
| return { shouldSkip: false }; | ||
| } | ||
|
|
||
| const byteLimit = | ||
| stat.size > LARGE_DIFF_FILE_MAX_BYTES ? LARGE_DIFF_FILE_MAX_BYTES : LARGE_DIFF_FILE_SNIFF_BYTES; | ||
| const counted = countLinesInFile(absolutePath, byteLimit, stat.size); | ||
| const shouldSkip = | ||
| stat.size > LARGE_DIFF_FILE_MAX_BYTES || counted.lines > LARGE_DIFF_FILE_MAX_LINES; | ||
|
|
||
| return { | ||
| shouldSkip, | ||
| stats: shouldSkip ? { additions: counted.lines, deletions: 0 } : undefined, | ||
| statsTruncated: shouldSkip ? !counted.complete : undefined, | ||
| }; | ||
| } |
2 changes: 1 addition & 1 deletion
2
packages/hunk/src/lib/osPath.test.ts → packages/hunk-vcs/src/path.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /** Normalize compatibility-layer paths into native paths for the current OS. */ | ||
| export function normalizePathForOS(path: string, platform = process.platform) { | ||
| switch (platform) { | ||
| case "win32": | ||
| return normalizeWindowsCompatibilityPath(path); | ||
| default: | ||
| return path; | ||
| } | ||
| } | ||
|
|
||
| /** Convert Unix-style Windows paths to native paths usable as Bun cwd. */ | ||
| function normalizeWindowsCompatibilityPath(path: string) { | ||
| const normalized = path | ||
| // Some Windows tools can report slash-prefixed drive paths as `/C:/...`. | ||
| .replace(/^\/([a-zA-Z]):(?:[\\/]|$)/, (_, drive) => `${drive.toUpperCase()}:/`) | ||
| // Keep specific compatibility-layer prefixes before the generic `/c/...` form. | ||
| // Cygwin commonly reports drive paths as `/cygdrive/c/...`. | ||
| .replace(/^\/cygdrive\/([a-zA-Z])(?:[\\/]|$)/, (_, drive) => `${drive.toUpperCase()}:/`) | ||
| // WSL-style paths are commonly reported as `/mnt/c/...`. | ||
| .replace(/^\/mnt\/([a-zA-Z])(?:[\\/]|$)/, (_, drive) => `${drive.toUpperCase()}:/`) | ||
| // Git Bash/MSYS2 commonly reports drive paths as `/c/...`. | ||
| .replace(/^\/([a-zA-Z])(?:[\\/]|$)/, (_, drive) => `${drive.toUpperCase()}:/`); | ||
|
|
||
| return normalized === path ? path : normalized.replaceAll("/", "\\"); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This package declares support for Node 22+, but its exported runtime modules use Bun-only APIs such as
Bun.spawnandBun.file, and the exports point directly to TypeScript source. A Node consumer can therefore meet the stated engine requirement but fail when loading or invoking these helpers. Please remove the Node engine claim or provide and test a Node-compatible implementation.Prompt To Fix With AI