Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions src/__tests__/git-operations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -51,14 +51,42 @@ 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();

expect(result.owner).toBe('synctree');
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');
Expand Down
17 changes: 14 additions & 3 deletions src/git-operations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down