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 `ref:` outside the safe refname grammar is dated by HEAD's own log, the |
| 13 | * listed real file `logs/HEAD`; without one, or where HEAD is not a plain |
| 14 | * file, `git rev-parse` answers instead. |
| 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 | const logged = isUnsafeRef ? await stampIfFile(own, 'logs/HEAD') : null |
| 58 | const isUndated = isUnsafeRef && typeof logged !== 'number' |
| 59 | const isRefDated = isSymbolic && !isUnsafeRef |
| 60 | |
| 61 | return isUndated |
| 62 | ? viaGit() |
| 63 | : [ |
| 64 | head, |
| 65 | await stampIfFile(own, 'HEAD'), |
| 66 | isRefDated ? await stampIfFile(common, ref) : logged, |
| 67 | await stampIfFile(common, 'packed-refs'), |
| 68 | ].join('|') |
| 69 | } |