Using update-index
command
# Using ts-node
npx ts-node <path/to/git.ts> update-index <files...>
# Using node
node <path/to/git.js> update-index <files...>
Using add
command
# Using ts-node
npx ts-node <path/to/git.ts> add <files...>
# Using node
node <path/to/git.js> add <files...>
Note: In theory, both the commands are different.
The add
command is a Porcelain command while the update-index
command is a Plumbing command.
For the sake of this distinction, I added support for both of them, even though they perform the same operations in the context of my codebase.
-
files
Files to act on. These can also include directories. If you want to add all the files to the index use
update-index .
.
Register file contents in the working tree to the index.
-
First find the path of all the files relative to the root of the Git repo. Since we need to support directories, I leveraged the glob package to list files while also excluding the files present in the
.gitignore
file. The function that performs this isgetFiles()
present in utils.ts file. -
If there is no previous
.git/index
file present, then we create a new one and add the Index Entries to it using the Index class. Otherwise we first remove the file from the index and then insert it again.
View this official doc to know more about index format.