UNPKG

git-cat-file

Version:

Pure JavaScript `git cat-file -p` for node.js

53 lines (52 loc) 1.69 kB
"use strict"; /** * https://github.com/kawanet/git-cat-file */ Object.defineProperty(exports, "__esModule", { value: true }); exports.Commit = void 0; const tree_1 = require("./tree"); const obj_item_1 = require("./obj-item"); class Commit extends obj_item_1.ObjItem { constructor(obj, store) { super(obj, store); if (obj.type !== "commit" && obj.type !== "tag") { throw new TypeError(`Invalid commit object: ${obj.oid} (${obj.type})`); } } getDate() { const author = this.getMeta("author") || this.getMeta("committer"); const match = author === null || author === void 0 ? void 0 : author.match(/\s+(\d+)(\s+[+\-]\d+)?$/); if (match) return new Date(+match[1] * 1000); } async getTree() { const oid = this.getMeta("tree"); const obj = await this.store.getObject(oid); return new tree_1.Tree(obj, this.store); } async getFile(path) { const tree = await this.getTree(); const entry = await tree.getEntry(path); if (!entry) return; const { oid, mode } = entry; const obj = await this.store.getObject(oid); if (obj.type !== "blob") return; const { data } = obj; return { oid, mode, data }; } async getParents() { const parent = this.getMetaArray("parent"); if (!parent) return; const array = []; for (const oid of parent) { const obj = await this.store.getObject(oid); if (obj) array.push(new Commit(obj, this.store)); } return array; } } exports.Commit = Commit;