(opts: CatFileWithDeps)
| 116 | } |
| 117 | |
| 118 | export async function catFileWith(opts: CatFileWithDeps): Promise<CatFileResult> { |
| 119 | const dir = opts.dir ?? "/"; |
| 120 | try { |
| 121 | // readBlob is the right tool when we already know the oid |
| 122 | // resolves to a blob; for tree / commit oids it errors out. |
| 123 | // We try readBlob first (fast path); fall back to readObject |
| 124 | // when the caller doesn't know the type up front. |
| 125 | if (opts.filepath !== undefined) { |
| 126 | const { oid, blob } = await opts.git.readBlob({ |
| 127 | fs: opts.fs, |
| 128 | dir, |
| 129 | oid: opts.oid, |
| 130 | filepath: opts.filepath, |
| 131 | cache: opts.cache, |
| 132 | }); |
| 133 | return { oid, bytes: blob }; |
| 134 | } |
| 135 | try { |
| 136 | const { oid, blob } = await opts.git.readBlob({ |
| 137 | fs: opts.fs, |
| 138 | dir, |
| 139 | oid: opts.oid, |
| 140 | cache: opts.cache, |
| 141 | }); |
| 142 | return { oid, bytes: blob }; |
| 143 | } catch { |
| 144 | // Not a blob; fall through to readObject's content form. |
| 145 | } |
| 146 | const obj = await opts.git.readObject({ |
| 147 | fs: opts.fs, |
| 148 | dir, |
| 149 | oid: opts.oid, |
| 150 | format: "content", |
| 151 | cache: opts.cache, |
| 152 | }); |
| 153 | const bytes = |
| 154 | obj.object instanceof Uint8Array ? obj.object : new TextEncoder().encode(String(obj.object)); |
| 155 | return { oid: obj.oid, bytes }; |
| 156 | } catch (cause) { |
| 157 | if (isNotARepositoryCause(cause)) throw new NotARepositoryError(dir, { cause }); |
| 158 | throw new GitError("ECATFILEFAIL", `git cat-file failed: ${errorMessage(cause)}`, { |
| 159 | cause, |
| 160 | }); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // --------------------------------------------------------------- |
| 165 | // updateRef |
no test coverage detected