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
| 1 | import Limits from '../../../limits' |
| 2 | import Argv from '../../argv' |
| 3 | import type Types from '../../types' |
| 4 | import { isSafeRefName } from '../is-safe-ref-name' |
| 5 | import { stampIfFile } from '../stamp-if-file' |
| 6 | import { stampProbeOf } from '../stamp-probe-of' |
| 7 | |
| 8 | /** |
| 9 | * A string that changes when HEAD moves: HEAD's text and the timestamps |
| 10 | * of HEAD, its ref file and packed-refs, each a listed real file. |
| 11 | * |
| 12 | * A HEAD that is not a plain file, or a `ref:` outside the safe refname |
| 13 | * grammar, is read through `git rev-parse` instead and nothing is stat'ed; |
| 14 | * every directory above a stat'ed file was listed as a real directory. |
| 15 | * |
| 16 | * @param deps git, listings, timestamps, a file read |
| 17 | * @param repository the repository's directories |
| 18 | * @returns the key for this tick |
| 19 | */ |
| 20 | export async function headKeyOf( |
| 21 | deps: Types.HeadKeyDeps, |
| 22 | repository: Types.Repository, |
| 23 | ): Promise<string> { |
| 24 | const own = stampProbeOf( |
| 25 | deps, |
| 26 | repository.gitDir, |
| 27 | Limits.MAX_LISTED_DIRECTORIES, |
| 28 | ) |
| 29 | |
| 30 | const isShared = repository.commonDir === repository.gitDir |
| 31 | |
| 32 | const common = isShared |
| 33 | ? own |
| 34 | : stampProbeOf(deps, repository.commonDir, Limits.MAX_LISTED_DIRECTORIES) |
| 35 | |
| 36 | async function viaGit() { |
| 37 | const parsed = await deps.run([ |
| 38 | Argv.NO_OPTIONAL_LOCKS, |
| 39 | 'rev-parse', |
| 40 | '--verify', |
| 41 | '--quiet', |
| 42 | 'HEAD', |
| 43 | ]) |
| 44 | |
| 45 | return `rev:${parsed.exitCode === 0 ? parsed.stdout.trim() : ''}` |
| 46 | } |
| 47 | |
| 48 | if ((await own.walk.kindOf('HEAD')) !== 'file') { |
| 49 | return viaGit() |
| 50 | } |
| 51 | |
| 52 | const head = await deps.readFile(`${repository.gitDir}/HEAD`).catch(() => '') |
| 53 | const ref = head.startsWith('ref: ') ? head.slice('ref: '.length).trim() : '' |
| 54 | const isSymbolic = ref !== '' |
| 55 | |
| 56 | const isUnsafeRef = isSymbolic && !isSafeRefName(ref) |
| 57 | |
| 58 | return isUnsafeRef |
| 59 | ? viaGit() |
| 60 | : [ |
| 61 | head, |
| 62 | await stampIfFile(own, 'HEAD'), |
| 63 | isSymbolic ? await stampIfFile(common, ref) : null, |
| 64 | await stampIfFile(common, 'packed-refs'), |
| 65 | ].join('|') |
| 66 | } |