MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / readGitHead

Function readGitHead

source code/utils/git/gitFilesystem.ts:151–185  ·  view source on GitHub ↗
(
  gitDir: string,
)

Source from the content-addressed store, hash-verified

149 * this by trimming after slicing past "ref:".
150 */
151export async function readGitHead(
152 gitDir: string,
153): Promise<
154 { type: 'branch'; name: string } | { type: 'detached'; sha: string } | null
155> {
156 try {
157 const content = (await readFile(join(gitDir, 'HEAD'), 'utf-8')).trim()
158 if (content.startsWith('ref:')) {
159 const ref = content.slice('ref:'.length).trim()
160 if (ref.startsWith('refs/heads/')) {
161 const name = ref.slice('refs/heads/'.length)
162 // Reject path traversal and argument injection from a tampered HEAD.
163 if (!isSafeRefName(name)) {
164 return null
165 }
166 return { type: 'branch', name }
167 }
168 // Unusual symref (not a local branch) — resolve to SHA
169 if (!isSafeRefName(ref)) {
170 return null
171 }
172 const sha = await resolveRef(gitDir, ref)
173 return sha ? { type: 'detached', sha } : { type: 'detached', sha: '' }
174 }
175 // Raw SHA (detached HEAD). Validate: an attacker-controlled HEAD file
176 // could contain shell metacharacters that flow into downstream shell
177 // contexts.
178 if (!isValidGitSha(content)) {
179 return null
180 }
181 return { type: 'detached', sha: content }
182 } catch {
183 return null
184 }
185}
186
187// ---------------------------------------------------------------------------
188// resolveRef — resolve loose/packed refs to SHAs

Callers 5

watchCurrentBranchRefMethod · 0.85
computeBranchFunction · 0.85
computeHeadFunction · 0.85
getHeadForDirFunction · 0.85
readWorktreeHeadShaFunction · 0.85

Calls 4

readFileFunction · 0.85
isSafeRefNameFunction · 0.85
resolveRefFunction · 0.85
isValidGitShaFunction · 0.85

Tested by

no test coverage detected