readIndexContent
Reads one tracked file’s content out of the git index - stage 0, the bytes git commit would write.
This spawns git directly rather than going through execFromRoot, for two reasons, either of which alone would be enough: execFromRoot decodes stdout as UTF-8, which a binary blob does not survive, and it strips one trailing newline, so every text file would read as differing from its source by its last line.
The path is passed in the :./<path> form, which git resolves against the directory it runs from rather than against the top of the working tree, so this stays correct when root is a subpackage of the repository rather than its top.
No smudge filter or line-ending conversion is applied: git cat-file blob hands the blob back verbatim.
Import:
import { readIndexContent } from '@mnaoumov/typescript-dev-toolkit/git';Signature:
function readIndexContent(root: string, repoRelativePath: string): Promise<Buffer<ArrayBufferLike> | null>Parameters:
| Parameter | Type | Description |
|---|---|---|
root |
string | The directory the path is relative to, which is also the directory git is run from. |
repoRelativePath |
string | The file’s path relative to root, posix-spelled. |
Returns: Promise<Buffer<ArrayBufferLike> | null> — A Promise that resolves with the staged bytes, or with null when the path has no stage-0 index entry - it is untracked, staged for deletion, or unmerged. It rejects on any other git failure, not a git repository among them, because the bytes a commit would write have no honest answer where there are no commits.