UNPKG

git-blame-line

Version:

Execute git blame for a single line and get a JSON as result

34 lines (33 loc) 1.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.blameLine = void 0; const exec_promise_1 = require("./exec-promise"); const parse_line_1 = require("./parse-line"); async function blameLine(filepathWithLine) { const [filename, lineNumber] = filepathWithLine.split(":"); // TODO: remove this check and add a regex check at the top. if (!lineNumber) { throw new Error("Error: filepathWithLine syntax is wrong, you have to pass a single filepath and linenumber seperated with `:` character"); } const gitCommandString = `git blame --line-porcelain -L ${lineNumber},+1 ${filename}`; const blameOutput = await exec_promise_1.execPromise(gitCommandString); const lineInfos = []; const lines = blameOutput.split("\n"); // * First line is not important so we skip it by starting from second line // <40-byte hex sha1> <sourceline> <resultline> <num_lines> // * Last line is empty // * Second last line is content of the source code with a /t at the // start of the line // So we skip these lines. for (let index = 1; index < lines.length - 2; index += 1) { const line = lines[index]; const infoLine = parse_line_1.parseBlameInfoLine(line); if (infoLine) { lineInfos.push(infoLine); } } const mergedInfo = lineInfos.reduce((prev, current) => { return Object.assign(Object.assign({}, prev), current); }, {}); mergedInfo.sourceCode = lines[lines.length - 2].replace("\t", ""); return mergedInfo; } exports.blameLine = blameLine;