@@ -23,13 +23,24 @@ const GIT_OUTPUT_CHARACTER_LIMIT = 100 * 1024
2323// Node's default `exec` buffer is 1MB, which real-world diffs routinely exceed.
2424const GIT_DIFF_MAX_BUFFER = 10 * 1024 * 1024
2525
26+ // Rename and copy detection is asked for explicitly rather than left to `diff.renames`, so a file
27+ // that moved is classified the same way for everyone. With the user's configuration deciding it, a
28+ // rename reaches the model as an unrelated delete plus add wherever that setting is off.
29+ const RENAME_DETECTION_ARGS = [ "--find-renames" , "--find-copies" ]
30+
2631// A commit message needs to know what changed, not every line of how. One line of surrounding
2732// context per hunk is enough to tell the model where an edit landed, and shrinking the prompt is
2833// the one latency factor we control without affecting the model's output.
29- const COMMIT_DIFF_ARGS = [ "--unified=1" ]
34+ const COMMIT_DIFF_ARGS = [ "--unified=1" , ... RENAME_DETECTION_ARGS ]
3035
3136const RECENT_COMMIT_COUNT = 5
3237
38+ // An untracked file has no diff to read, so its contents are inlined instead. These bounds are what
39+ // keep a dropped build directory or a stray archive from becoming the entire prompt: a file count,
40+ // a per-file byte cap read without loading the whole file, and a skip for anything binary.
41+ const UNTRACKED_FILE_LIMIT = 10
42+ const UNTRACKED_FILE_BYTE_LIMIT = 8 * 1024
43+
3344/**
3445 * Extracts git repository information from the workspace's .git directory
3546 * @param workspaceRoot The root path of the workspace
@@ -385,7 +396,7 @@ export interface CommitContext {
385396
386397export type CommitContextResult =
387398 | { ok : true ; context : CommitContext }
388- | { ok : false ; reason : "git-missing" | "not-a-repo" | "no-changes" | "nothing-staged" | " failed"; error ?: string }
399+ | { ok : false ; reason : "git-missing" | "not-a-repo" | "no-changes" | "failed" ; error ?: string }
389400
390401async function runGit ( args : string [ ] , cwd : string ) : Promise < string > {
391402 const { stdout } = await execFileAsync ( "git" , args , { cwd, maxBuffer : GIT_DIFF_MAX_BUFFER } )
@@ -509,9 +520,9 @@ async function getRecentCommits(cwd: string): Promise<string[]> {
509520/**
510521 * Collects the changes to describe in a commit message.
511522 *
512- * Only the index is described, since that is exactly what a commit will contain. An empty index
513- * returns `nothing-staged` rather than falling back to the working tree, so the message can never
514- * describe changes the commit would not include .
523+ * Staged changes are described whenever there are any , since that is exactly what a commit will
524+ * contain. Only when the index is empty does this fall back to the working tree - unstaged edits
525+ * plus untracked files - so the two are never mixed into one message .
515526 *
516527 * Every command runs through `execFile` with an argument array, so no path is ever interpolated
517528 * into a shell string, and every listing is read in NUL-delimited form.
@@ -529,21 +540,33 @@ export async function getCommitContext(cwd: string): Promise<CommitContextResult
529540 }
530541
531542 try {
532- const staged = parseNameStatus ( await runGit ( [ "diff" , "--cached" , "--name-status" , "-z" ] , cwd ) )
543+ const staged = parseNameStatus (
544+ await runGit ( [ "diff" , "--cached" , "--name-status" , "-z" , ...RENAME_DETECTION_ARGS ] , cwd ) ,
545+ )
533546
534547 if ( staged . length > 0 ) {
535548 const diff = await runGit ( [ "diff" , "--cached" , ...COMMIT_DIFF_ARGS ] , cwd )
536549 return { ok : true , context : await buildContext ( cwd , staged , diff ) }
537550 }
538551
539- // Only the index is described, so an empty one has nothing to summarize. Whether the
540- // working tree is dirty decides which of the two messages the caller shows: "stage
541- // something first" is only useful advice when there is in fact something to stage.
552+ // Nothing is staged, so fall back to the working tree rather than refusing: a commit made
553+ // from here would stage these files first, so they are what the message has to describe.
542554 const worktree = parsePorcelainStatus (
543555 await runGit ( [ "status" , "--porcelain=v1" , "-z" , "--untracked-files=all" ] , cwd ) ,
544556 )
545557
546- return { ok : false , reason : worktree . length > 0 ? "nothing-staged" : "no-changes" }
558+ if ( worktree . length === 0 ) {
559+ return { ok : false , reason : "no-changes" }
560+ }
561+
562+ // Tracked edits still come from `git diff`. Untracked files are absent from it by
563+ // definition, so their contents are appended separately or the model would be naming files
564+ // it has never seen.
565+ const tracked = await runGit ( [ "diff" , ...COMMIT_DIFF_ARGS ] , cwd )
566+ const untracked = await readUntrackedFiles ( cwd , worktree )
567+ const diff = [ tracked . trim ( ) , untracked ] . filter ( Boolean ) . join ( "\n\n" )
568+
569+ return { ok : true , context : await buildContext ( cwd , worktree , diff ) }
547570 } catch ( error ) {
548571 // Failures here are expected rather than exceptional - an oversized diff exceeding
549572 // `maxBuffer`, a repository in a state git refuses to describe - so the caller gets a
@@ -552,6 +575,61 @@ export async function getCommitContext(cwd: string): Promise<CommitContextResult
552575 }
553576}
554577
578+ /**
579+ * Reads the beginning of each untracked file, rendered as an addition so it sits alongside the real
580+ * diff without needing its own section in the prompt.
581+ *
582+ * Bounded on every axis that can grow without limit - how many files are read, how much of each is
583+ * read, and whether the bytes are text at all - because an untracked listing is whatever happens to
584+ * be sitting in the working directory.
585+ */
586+ async function readUntrackedFiles ( cwd : string , files : GitFileChange [ ] ) : Promise < string > {
587+ const untracked = files . filter ( ( file ) => file . status === "untracked" )
588+ const blocks : string [ ] = [ ]
589+
590+ for ( const file of untracked . slice ( 0 , UNTRACKED_FILE_LIMIT ) ) {
591+ blocks . push ( `--- /dev/null\n+++ b/${ file . path } \n${ await readUntrackedFile ( cwd , file . path ) } ` )
592+ }
593+
594+ // The rest are still worth naming - that files were added is part of the change even when there
595+ // is no room to show what is in them.
596+ const remaining = untracked . slice ( UNTRACKED_FILE_LIMIT )
597+
598+ if ( remaining . length > 0 ) {
599+ blocks . push ( remaining . map ( ( file ) => `+++ b/${ file . path } (contents omitted)` ) . join ( "\n" ) )
600+ }
601+
602+ return blocks . join ( "\n\n" )
603+ }
604+
605+ /** Reads at most `UNTRACKED_FILE_BYTE_LIMIT` bytes, so a huge file costs one bounded read. */
606+ async function readUntrackedFile ( cwd : string , filePath : string ) : Promise < string > {
607+ let handle
608+
609+ try {
610+ handle = await fs . open ( path . resolve ( cwd , filePath ) , "r" )
611+
612+ const buffer = Buffer . alloc ( UNTRACKED_FILE_BYTE_LIMIT )
613+ const { bytesRead } = await handle . read ( buffer , 0 , UNTRACKED_FILE_BYTE_LIMIT , 0 )
614+ const contents = buffer . subarray ( 0 , bytesRead )
615+
616+ // The same heuristic git uses: a NUL byte early in the file means it is not text.
617+ if ( contents . includes ( 0 ) ) {
618+ return "(binary file)"
619+ }
620+
621+ const text = contents . toString ( "utf8" )
622+
623+ return bytesRead < UNTRACKED_FILE_BYTE_LIMIT ? text : `${ text } \n(truncated)`
624+ } catch {
625+ // A file listed a moment ago can be gone, or unreadable. Its path is already in the changed
626+ // files list, so the message can still mention it.
627+ return "(unreadable)"
628+ } finally {
629+ await handle ?. close ( ) . catch ( ( ) => { } )
630+ }
631+ }
632+
555633async function buildContext ( cwd : string , files : GitFileChange [ ] , diff : string ) : Promise < CommitContext > {
556634 return {
557635 branch : await getCurrentBranch ( cwd ) ,
0 commit comments