@@ -12,7 +12,7 @@ import { truncateOutput } from "../../../integrations/misc/extract-text"
1212
1313const execAsync = promisify ( exec )
1414
15- const GIT_OUTPUT_CHAR_LIMIT = 40000
15+ const GIT_OUTPUT_CHAR_LIMIT = 50_000
1616/**
1717 * Commit message generator service
1818 */
@@ -71,40 +71,56 @@ export class CommitMessageGenerator {
7171 */
7272 private async getGitDiffStreaming ( ) : Promise < string > {
7373 return new Promise ( ( resolve , reject ) => {
74- const git = spawn ( "git" , [ "diff" , "HEAD" ] , { cwd : this . workspaceRoot } )
75- let output = ""
76- let outputSize = 0
77- const MAX_OUTPUT_SIZE = 1024 * 1024 * 10 // 10MB limit
78-
79- git . stdout . on ( "data" , ( data ) => {
80- const chunk = data . toString ( )
81- outputSize += chunk . length
82-
83- // Check if we're approaching the size limit
84- if ( outputSize > MAX_OUTPUT_SIZE ) {
85- git . kill ( )
86- reject ( new Error ( `Git diff output exceeds size limit of ${ MAX_OUTPUT_SIZE } bytes` ) )
87- return
88- }
74+ // 首先检查是否有任何提交
75+ execAsync ( "git rev-parse --verify HEAD" , { cwd : this . workspaceRoot } )
76+ . then ( ( ) => {
77+ // 有提交,使用 git diff HEAD
78+ this . runGitDiff ( [ "diff" , "HEAD" ] , resolve , reject )
79+ } )
80+ . catch ( ( ) => {
81+ // 没有提交,使用 git diff 获取所有更改
82+ this . runGitDiff ( [ "diff" ] , resolve , reject )
83+ } )
84+ } )
85+ }
8986
90- output += chunk
91- } )
87+ /**
88+ * Run git diff command with specified arguments
89+ */
90+ private runGitDiff ( args : string [ ] , resolve : ( value : string ) => void , reject : ( reason ?: any ) => void ) : void {
91+ const git = spawn ( "git" , args , { cwd : this . workspaceRoot } )
92+ let output = ""
93+ let outputSize = 0
94+ const MAX_OUTPUT_SIZE = 1024 * 1024 * 10 // 10MB limit
95+
96+ git . stdout . on ( "data" , ( data ) => {
97+ const chunk = data . toString ( )
98+ outputSize += chunk . length
99+
100+ // Check if we're approaching the size limit
101+ if ( outputSize > MAX_OUTPUT_SIZE ) {
102+ git . kill ( )
103+ reject ( new Error ( `Git diff output exceeds size limit of ${ MAX_OUTPUT_SIZE } bytes` ) )
104+ return
105+ }
92106
93- git . stderr . on ( "data" , ( data ) => {
94- console . error ( "Git diff stderr:" , data . toString ( ) )
95- } )
107+ output += chunk
108+ } )
96109
97- git . on ( "close" , ( code ) => {
98- if ( code === 0 ) {
99- resolve ( output )
100- } else {
101- reject ( new Error ( `Git diff process exited with code ${ code } ` ) )
102- }
103- } )
110+ git . stderr . on ( "data" , ( data ) => {
111+ console . error ( "Git diff stderr:" , data . toString ( ) )
112+ } )
104113
105- git . on ( "error" , ( error ) => {
106- reject ( error )
107- } )
114+ git . on ( "close" , ( code ) => {
115+ if ( code === 0 ) {
116+ resolve ( output )
117+ } else {
118+ reject ( new Error ( `Git diff process exited with code ${ code } ` ) )
119+ }
120+ } )
121+
122+ git . on ( "error" , ( error ) => {
123+ reject ( error )
108124 } )
109125 }
110126
0 commit comments