@@ -17,17 +17,55 @@ async function getStagedDiff(repoPath: string): Promise<DiffResult> {
1717 return { diff : diffResult . stdout || "" , stats : statsResult . stdout || "" } ;
1818}
1919
20+ async function hasCommits ( repoPath : string ) : Promise < boolean > {
21+ try {
22+ await execAsync ( "git rev-parse HEAD" , { cwd : repoPath } ) ;
23+ return true ;
24+ } catch {
25+ return false ;
26+ }
27+ }
28+
2029async function getAllDiff ( repoPath : string ) : Promise < DiffResult > {
21- const [ diffResult , statsResult ] = await Promise . all ( [
22- execAsync ( "git diff HEAD --unified=1" , {
23- cwd : repoPath ,
24- maxBuffer : 10 * 1024 * 1024 ,
25- } ) ,
26- execAsync ( "git diff HEAD --stat" , {
27- cwd : repoPath ,
28- } ) ,
29- ] ) ;
30- return { diff : diffResult . stdout || "" , stats : statsResult . stdout || "" } ;
30+ const repoHasCommits = await hasCommits ( repoPath ) ;
31+
32+ if ( repoHasCommits ) {
33+ // Repository has commits - use HEAD to get all changes
34+ const [ diffResult , statsResult ] = await Promise . all ( [
35+ execAsync ( "git diff HEAD --unified=1" , {
36+ cwd : repoPath ,
37+ maxBuffer : 10 * 1024 * 1024 ,
38+ } ) ,
39+ execAsync ( "git diff HEAD --stat" , {
40+ cwd : repoPath ,
41+ } ) ,
42+ ] ) ;
43+ return { diff : diffResult . stdout || "" , stats : statsResult . stdout || "" } ;
44+ }
45+
46+ // New repository without commits - combine staged and unstaged diffs
47+ const [ stagedDiff , stagedStats , unstagedDiff , unstagedStats ] =
48+ await Promise . all ( [
49+ execAsync ( "git diff --cached --unified=1" , {
50+ cwd : repoPath ,
51+ maxBuffer : 10 * 1024 * 1024 ,
52+ } ) ,
53+ execAsync ( "git diff --cached --stat" , { cwd : repoPath } ) ,
54+ execAsync ( "git diff --unified=1" , {
55+ cwd : repoPath ,
56+ maxBuffer : 10 * 1024 * 1024 ,
57+ } ) ,
58+ execAsync ( "git diff --stat" , { cwd : repoPath } ) ,
59+ ] ) ;
60+
61+ const diff = [ stagedDiff . stdout , unstagedDiff . stdout ]
62+ . filter ( Boolean )
63+ . join ( "\n" ) ;
64+ const stats = [ stagedStats . stdout , unstagedStats . stdout ]
65+ . filter ( Boolean )
66+ . join ( "\n" ) ;
67+
68+ return { diff, stats } ;
3169}
3270
3371export async function getDiff (
0 commit comments