({
path,
cloneUrl,
}: {
path: string,
cloneUrl: string,
})
| 374 | * @returns The branch name (e.g., "main", "master") or undefined if it cannot be determined |
| 375 | */ |
| 376 | export const getRemoteDefaultBranch = async ({ |
| 377 | path, |
| 378 | cloneUrl, |
| 379 | }: { |
| 380 | path: string, |
| 381 | cloneUrl: string, |
| 382 | }) => { |
| 383 | const git = createGitClientForPath(path); |
| 384 | try { |
| 385 | const remoteHead = await git.raw(['ls-remote', '--symref', cloneUrl, 'HEAD']); |
| 386 | const match = remoteHead.match(/^ref: refs\/heads\/(\S+)\s+HEAD/m); |
| 387 | if (match) { |
| 388 | return match[1]; |
| 389 | } |
| 390 | } catch (error: unknown) { |
| 391 | // Avoid printing error here since cloneUrl may contain credentials. |
| 392 | console.error(`Failed to get remote default branch for repository: ${path}`); |
| 393 | return undefined; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /** |
| 398 | * Gets the branch name that the local HEAD symbolic ref points to. |
no test coverage detected