@@ -80,34 +80,47 @@ async function validateGitRepository(projectPath) {
8080// Get git status for a project
8181router . get ( '/status' , async ( req , res ) => {
8282 const { project } = req . query ;
83-
83+
8484 if ( ! project ) {
8585 return res . status ( 400 ) . json ( { error : 'Project name is required' } ) ;
8686 }
8787
8888 try {
8989 const projectPath = await getActualProjectPath ( project ) ;
90-
90+
9191 // Validate git repository
9292 await validateGitRepository ( projectPath ) ;
9393
94- // Get current branch
95- const { stdout : branch } = await execAsync ( 'git rev-parse --abbrev-ref HEAD' , { cwd : projectPath } ) ;
96-
94+ // Get current branch - handle case where there are no commits yet
95+ let branch = 'main' ;
96+ let hasCommits = true ;
97+ try {
98+ const { stdout : branchOutput } = await execAsync ( 'git rev-parse --abbrev-ref HEAD' , { cwd : projectPath } ) ;
99+ branch = branchOutput . trim ( ) ;
100+ } catch ( error ) {
101+ // No HEAD exists - repository has no commits yet
102+ if ( error . message . includes ( 'unknown revision' ) || error . message . includes ( 'ambiguous argument' ) ) {
103+ hasCommits = false ;
104+ branch = 'main' ;
105+ } else {
106+ throw error ;
107+ }
108+ }
109+
97110 // Get git status
98111 const { stdout : statusOutput } = await execAsync ( 'git status --porcelain' , { cwd : projectPath } ) ;
99-
112+
100113 const modified = [ ] ;
101114 const added = [ ] ;
102115 const deleted = [ ] ;
103116 const untracked = [ ] ;
104-
117+
105118 statusOutput . split ( '\n' ) . forEach ( line => {
106119 if ( ! line . trim ( ) ) return ;
107-
120+
108121 const status = line . substring ( 0 , 2 ) ;
109122 const file = line . substring ( 3 ) ;
110-
123+
111124 if ( status === 'M ' || status === ' M' || status === 'MM' ) {
112125 modified . push ( file ) ;
113126 } else if ( status === 'A ' || status === 'AM' ) {
@@ -118,19 +131,20 @@ router.get('/status', async (req, res) => {
118131 untracked . push ( file ) ;
119132 }
120133 } ) ;
121-
134+
122135 res . json ( {
123- branch : branch . trim ( ) ,
136+ branch,
137+ hasCommits,
124138 modified,
125139 added,
126140 deleted,
127141 untracked
128142 } ) ;
129143 } catch ( error ) {
130144 console . error ( 'Git status error:' , error ) ;
131- res . json ( {
132- error : error . message . includes ( 'not a git repository' ) || error . message . includes ( 'Project directory is not a git repository' )
133- ? error . message
145+ res . json ( {
146+ error : error . message . includes ( 'not a git repository' ) || error . message . includes ( 'Project directory is not a git repository' )
147+ ? error . message
134148 : 'Git operation failed' ,
135149 details : error . message . includes ( 'not a git repository' ) || error . message . includes ( 'Project directory is not a git repository' )
136150 ? error . message
@@ -264,6 +278,50 @@ router.get('/file-with-diff', async (req, res) => {
264278 }
265279} ) ;
266280
281+ // Create initial commit
282+ router . post ( '/initial-commit' , async ( req , res ) => {
283+ const { project } = req . body ;
284+
285+ if ( ! project ) {
286+ return res . status ( 400 ) . json ( { error : 'Project name is required' } ) ;
287+ }
288+
289+ try {
290+ const projectPath = await getActualProjectPath ( project ) ;
291+
292+ // Validate git repository
293+ await validateGitRepository ( projectPath ) ;
294+
295+ // Check if there are already commits
296+ try {
297+ await execAsync ( 'git rev-parse HEAD' , { cwd : projectPath } ) ;
298+ return res . status ( 400 ) . json ( { error : 'Repository already has commits. Use regular commit instead.' } ) ;
299+ } catch ( error ) {
300+ // No HEAD - this is good, we can create initial commit
301+ }
302+
303+ // Add all files
304+ await execAsync ( 'git add .' , { cwd : projectPath } ) ;
305+
306+ // Create initial commit
307+ const { stdout } = await execAsync ( 'git commit -m "Initial commit"' , { cwd : projectPath } ) ;
308+
309+ res . json ( { success : true , output : stdout , message : 'Initial commit created successfully' } ) ;
310+ } catch ( error ) {
311+ console . error ( 'Git initial commit error:' , error ) ;
312+
313+ // Handle the case where there's nothing to commit
314+ if ( error . message . includes ( 'nothing to commit' ) ) {
315+ return res . status ( 400 ) . json ( {
316+ error : 'Nothing to commit' ,
317+ details : 'No files found in the repository. Add some files first.'
318+ } ) ;
319+ }
320+
321+ res . status ( 500 ) . json ( { error : error . message } ) ;
322+ }
323+ } ) ;
324+
267325// Commit changes
268326router . post ( '/commit' , async ( req , res ) => {
269327 const { project, message, files } = req . body ;
0 commit comments