( adv: RefAdvertisement, ref?: string, )
| 111 | |
| 112 | // Resolve which sha a caller wants given a ref advertisement + optional ref name. |
| 113 | export function resolveWant( |
| 114 | adv: RefAdvertisement, |
| 115 | ref?: string, |
| 116 | ): { sha: string; resolvedRef: string } { |
| 117 | if (ref) { |
| 118 | // try exact, then heads/, then tags/ |
| 119 | const candidates = [ref, `refs/heads/${ref}`, `refs/tags/${ref}`, `refs/${ref}`]; |
| 120 | for (const c of candidates) { |
| 121 | const sha = adv.refs.get(c); |
| 122 | if (sha) return { sha, resolvedRef: c }; |
| 123 | } |
| 124 | // maybe they passed a sha directly |
| 125 | if (/^[0-9a-f]{40,64}$/.test(ref)) return { sha: ref, resolvedRef: ref }; |
| 126 | throw new Error(`ref not found: ${ref}`); |
| 127 | } |
| 128 | // default: HEAD symref target, else HEAD ref, else first |
| 129 | if (adv.headTarget) { |
| 130 | const sha = adv.refs.get(adv.headTarget); |
| 131 | if (sha) return { sha, resolvedRef: adv.headTarget }; |
| 132 | } |
| 133 | const head = adv.refs.get("HEAD"); |
| 134 | if (head) return { sha: head, resolvedRef: "HEAD" }; |
| 135 | const first = adv.refs.entries().next(); |
| 136 | if (!first.done) return { sha: first.value[1], resolvedRef: first.value[0] }; |
| 137 | throw new Error("no refs advertised"); |
| 138 | } |
| 139 | |
| 140 | export { td, te }; |
no test coverage detected