diff --git a/src/__tests__/git-operations.test.ts b/src/__tests__/git-operations.test.ts index 23ee8c3..c29107e 100644 --- a/src/__tests__/git-operations.test.ts +++ b/src/__tests__/git-operations.test.ts @@ -31,7 +31,7 @@ describe('GitOperations', () => { mockExecSync .mockReturnValueOnce('') // rev-parse --git-dir .mockReturnValueOnce('git@github.com:synctree/releasebot.git\n') // remote get-url origin - .mockReturnValueOnce('refs/remotes/origin/main\n'); // symbolic-ref + .mockReturnValueOnce('main\n'); // rev-parse --abbrev-ref HEAD const result = gitOps.validateRepository(); @@ -51,7 +51,7 @@ describe('GitOperations', () => { mockExecSync .mockReturnValueOnce('') // rev-parse --git-dir .mockReturnValueOnce('https://github.com/synctree/releasebot.git\n') // remote get-url origin - .mockReturnValueOnce('refs/remotes/origin/main\n'); // symbolic-ref + .mockReturnValueOnce('main\n'); // rev-parse --abbrev-ref HEAD const result = gitOps.validateRepository(); @@ -59,6 +59,34 @@ describe('GitOperations', () => { expect(result.name).toBe('releasebot'); }); + it('should fallback to "main" when current branch detection fails', () => { + mockExecSync + .mockReturnValueOnce('') // rev-parse --git-dir + .mockReturnValueOnce('git@github.com:synctree/releasebot.git\n') // remote get-url origin + .mockImplementationOnce(() => { + // rev-parse --abbrev-ref HEAD fails + throw new Error('Unable to get current branch'); + }); + + const result = gitOps.validateRepository(); + + expect(result.defaultBranch).toBe('main'); + expect(mockCore.debug).toHaveBeenCalledWith( + 'Could not determine current branch, using "main" as default' + ); + }); + + it('should fallback to "main" when current branch is HEAD', () => { + mockExecSync + .mockReturnValueOnce('') // rev-parse --git-dir + .mockReturnValueOnce('git@github.com:synctree/releasebot.git\n') // remote get-url origin + .mockReturnValueOnce('HEAD\n'); // rev-parse --abbrev-ref HEAD returns HEAD (detached) + + const result = gitOps.validateRepository(); + + expect(result.defaultBranch).toBe('main'); + }); + it('should throw error if not in git repository', () => { mockExecSync.mockImplementation(() => { throw new Error('Not a git repository'); diff --git a/src/git-operations.ts b/src/git-operations.ts index 5de60e7..e4a168d 100644 --- a/src/git-operations.ts +++ b/src/git-operations.ts @@ -41,9 +41,20 @@ export class GitOperations { // Get repository information const remoteUrl = this.executeGitCommand('remote get-url origin').trim(); - const defaultBranch = this.executeGitCommand('symbolic-ref refs/remotes/origin/HEAD') - .replace('refs/remotes/origin/', '') - .trim(); + + // For the default branch, we'll use a simple fallback approach + // since the exact default branch isn't critical for most operations + let defaultBranch = 'main'; // Safe fallback + try { + // Try to get the current branch if available + const currentBranch = this.executeGitCommand('rev-parse --abbrev-ref HEAD').trim(); + if (currentBranch !== '' && currentBranch !== 'HEAD') { + defaultBranch = currentBranch; + } + } catch { + // If we can't get current branch, stick with 'main' fallback + core.debug('Could not determine current branch, using "main" as default'); + } // Parse repository info from remote URL const repoInfo = this.parseRepositoryUrl(remoteUrl);