spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / mods / diff / hooks / git / probes / default-branch-of / default-branch-of.ts
47 lines · 1.2 KBRaw
1import Argv from '../../argv'
2import type Types from '../../types'
3import { CONVENTIONAL_DEFAULT_BRANCHES } from '../conventional-default-branches'
4
5/**
6 * The remote's default branch: what `origin/HEAD` names while that ref
7 * resolves, else the first conventional name with a remote ref, else main.
8 *
9 * The engine's own default-branch confidence ladder, over `$.process.run`.
10 *
11 * @param run runs git
12 * @returns the branch name, never empty
13 */
14export async function defaultBranchOf(run: Types.GitRun): Promise<string> {
15 const hasRemoteRef = async (branch: string): Promise<boolean> =>
16 (
17 await run([
18 Argv.NO_OPTIONAL_LOCKS,
19 'show-ref',
20 '--verify',
21 '--quiet',
22 `refs/remotes/origin/${branch}`,
23 ])
24 ).exitCode === 0
25
26 const symref = await run([
27 Argv.NO_OPTIONAL_LOCKS,
28 'symbolic-ref',
29 '--short',
30 'refs/remotes/origin/HEAD',
31 ])
32
33 const named =
34 symref.exitCode === 0 ? symref.stdout.trim().replace(/^origin\//, '') : ''
35
36 if (named !== '' && (await hasRemoteRef(named))) {
37 return named
38 }
39
40 for (const candidate of CONVENTIONAL_DEFAULT_BRANCHES) {
41 if (await hasRemoteRef(candidate)) {
42 return candidate
43 }
44 }
45
46 return 'main'
47}