* Resolves a ref to its object ID. * * @param {Object} args * @param {FSClient} args.fs - A file system implementation. * @param {string} [args.gitdir] - [required] The [git directory](dir-vs-gitdir.md) path * @param {string} args.ref - The ref to resolve. * @param {number} [args.d
({
fs,
gitdir,
ref,
depth = undefined,
visited = new Set(),
})
| 257 | * @returns {Promise<string>} - The resolved object ID. |
| 258 | */ |
| 259 | static async resolve({ |
| 260 | fs, |
| 261 | gitdir, |
| 262 | ref, |
| 263 | depth = undefined, |
| 264 | visited = new Set(), |
| 265 | }) { |
| 266 | if (depth !== undefined) { |
| 267 | depth-- |
| 268 | if (depth === -1) { |
| 269 | return ref |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | // Is it a ref pointer? |
| 274 | if (ref.startsWith('ref: ')) { |
| 275 | ref = ref.slice('ref: '.length) |
| 276 | return GitRefManager.resolve({ fs, gitdir, ref, depth, visited }) |
| 277 | } |
| 278 | // Is it a complete and valid SHA? |
| 279 | if (ref.length === 40 && /[0-9a-f]{40}/.test(ref)) { |
| 280 | return ref |
| 281 | } |
| 282 | // We need to alternate between the file system and the packed-refs |
| 283 | const packedMap = await GitRefManager.packedRefs({ fs, gitdir }) |
| 284 | // Look in all the proper paths, in this order |
| 285 | const allpaths = refpaths(ref).filter(p => !GIT_FILES.includes(p)) // exclude git system files (#709) |
| 286 | |
| 287 | for (const ref of allpaths) { |
| 288 | const sha = await acquireLock( |
| 289 | ref, |
| 290 | async () => |
| 291 | (await fs.read(`${gitdir}/${ref}`, { encoding: 'utf8' })) || |
| 292 | packedMap.get(ref) |
| 293 | ) |
| 294 | if (sha) { |
| 295 | // Guard against circular symbolic references (e.g. a -> b -> a). Without |
| 296 | // tracking visited refs this recursion would not terminate. |
| 297 | if (visited.has(ref)) { |
| 298 | throw new InternalError( |
| 299 | `Circular reference detected while resolving ref "${ref}"` |
| 300 | ) |
| 301 | } |
| 302 | visited.add(ref) |
| 303 | return GitRefManager.resolve({ |
| 304 | fs, |
| 305 | gitdir, |
| 306 | ref: sha.trim(), |
| 307 | depth, |
| 308 | visited, |
| 309 | }) |
| 310 | } |
| 311 | } |
| 312 | // Do we give up? |
| 313 | throw new NotFoundError(ref) |
| 314 | } |
| 315 | |
| 316 | /** |