git-cat-file
Version:
Pure JavaScript `git cat-file -p` for node.js
52 lines (51 loc) • 1.67 kB
JavaScript
;
/**
* https://github.com/kawanet/git-cat-file
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Repo = void 0;
const commit_1 = require("./commit");
const tree_1 = require("./tree");
const obj_store_1 = require("./obj-store");
const tag_1 = require("./tag");
const isObjectId = (oid) => (oid && /^[0-9a-f]{40}$/i.test(oid));
const isLooseId = (oid) => (oid && /^[0-9a-f]{4,40}$/i.test(oid));
class Repo {
constructor(path) {
path = path.replace(/\/+$/, "");
this.store = new obj_store_1.ObjStore(path);
}
async getObject(object_id) {
if (isObjectId(object_id)) {
const obj = await this.store.getObject(object_id);
if (obj)
return obj;
}
if (isLooseId(object_id)) {
const oid = await this.store.findObjectId(object_id);
if (oid)
return this.store.getObject(oid);
}
const commit_id = await this.store.findCommitId(object_id);
if (commit_id)
return this.store.getObject(commit_id);
}
async getCommit(commit_id) {
let obj = await this.getObject(commit_id);
if (!obj)
return;
if (obj.type === "tag") {
const tag = new tag_1.Tag(obj, this.store);
commit_id = tag.getMeta("object");
obj = await this.getObject(commit_id);
}
return new commit_1.Commit(obj, this.store);
}
async getTree(object_id) {
const obj = await this.getObject(object_id);
if (!obj)
return;
return new tree_1.Tree(obj, this.store);
}
}
exports.Repo = Repo;