git-blame-line
Version:
Execute git blame for a single line and get a JSON as result
43 lines (42 loc) • 1.39 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseBlameInfoLine = void 0;
const camel_case_1 = require("camel-case");
const fromUnixTime_1 = __importDefault(require("date-fns/fromUnixTime"));
const BLAME_INFO_LINE_REGEX = /^(?<token>[a-z]+(-(?<subtoken>[a-z]+))?)\s(?<data>.+)$/;
function parseBlameInfoLine(line) {
if (line === "boundary") {
return {
boundary: "",
};
}
const commitInfo = BLAME_INFO_LINE_REGEX.exec(line);
if (!(commitInfo === null || commitInfo === void 0 ? void 0 : commitInfo.groups)) {
throw new Error(`
Given line string is not a blame info line
${line}
`);
}
const { token, subtoken, data } = commitInfo.groups;
const property = camel_case_1.camelCase(token);
let value = data;
switch (subtoken) {
case "mail":
// remove <> from email
value = value.slice(1, -1);
break;
case "time":
// parse datestamp into date
value = fromUnixTime_1.default(parseInt(value, 10));
break;
default:
break;
}
return {
[property]: value,
};
}
exports.parseBlameInfoLine = parseBlameInfoLine;