Skip to content

Instantly share code, notes, and snippets.

@astuanax
Created May 28, 2020 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astuanax/e35631c7633cd27040e9ac28a86f35bf to your computer and use it in GitHub Desktop.
Save astuanax/e35631c7633cd27040e9ac28a86f35bf to your computer and use it in GitHub Desktop.
const GitDb = repository => ({
save: async function (filePath, fileName, value, msg = `Updating file ${filePath}`) {
const repo = await repository
const HEAD = await repo.getBranchCommit("master");
const currentTree = await HEAD.getTree();
let builder = await Git.Treebuilder.create(repo,currentTree);
const update = new Git.TreeUpdate();
update.action = Git.Tree.UPDATE.UPSERT;
update.filemode = Git.TreeEntry.FILEMODE.TREE;
update.id = currentTree.id();
update.path = filePath;
console.log({update})
const treeOid = await currentTree.createUpdated(repo, 1, [update]);
await builder.insert(filePath,treeOid,Git.TreeEntry.FILEMODE.TREE);
await builder.write();
const buf = new Buffer.from(value,"utf-8");
const oid = await Git.Blob.createFromBuffer(repo,buf,buf.length);
await builder.insert(fileName,oid,Git.TreeEntry.FILEMODE.BLOB);
const treeOID = await builder.write();
console.log({buf})
console.log({oid})
console.log({builder})
console.log({treeOID})
console.log({repo})
// commit changes
return await commitChanges(
repo,
treeOID,
HEAD,
msg
);
}
})
const repo = Git.Repository.open(`../gittest/.git`)
const db = GitDb(repo)
db.save("dir", "file.txt", "Some text", "TEST Another Commit" + new Date())
.then(res => console.log(res))
.catch(err => console.error(err))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment