git-cat-file
Version:
Pure JavaScript `git cat-file -p` for node.js
60 lines (59 loc) • 1.62 kB
JavaScript
;
/**
* https://github.com/kawanet/git-cat-file
*/
Object.defineProperty(exports, "__esModule", { value: true });
const __1 = require("..");
const fs_1 = require("fs");
const git_ls_tree_js_1 = require("./git-ls-tree-js");
const cli_lib_1 = require("../lib/cli-lib");
const longParams = {
help: true,
};
const shortParams = {
C: "path",
h: true,
p: true,
t: true, // show object content
};
async function CLI(args) {
const options = (0, cli_lib_1.parseOptions)({
long: longParams,
short: shortParams,
args: args,
});
args = options.args;
const { C } = options.short;
if (C)
process.chdir(C);
if (await fs_1.promises.readdir(".git").catch(_ => null)) {
process.chdir(".git");
}
const { help } = options.long;
const { h, t, p } = options.short;
const revision = options.args.shift();
if (help || h || !revision || !(t || p)) {
process.stderr.write(`Usage:\n`);
showHelp();
process.exit(1);
}
const repo = (0, __1.openLocalRepo)(".");
const { oid, type, data } = await repo.getObject(revision);
if (t) {
process.stdout.write(`${type}\n`);
process.exit(0);
}
if (type === "tree") {
const tree = await repo.getTree(oid);
await (0, git_ls_tree_js_1.showEnties)(tree);
}
else {
process.stdout.write(data);
}
}
function showHelp() {
process.stderr.write(` git-cat-file-js [-C path] [-t | -p] <object>\n`);
}
if (!module.parent)
CLI(process.argv.slice(2)).catch(console.error);