git-history/cli/git.js
2019-02-09 23:21:05 -03:00

48 lines
987 B
JavaScript

const execa = require("execa");
// TODO remove
const options = { cwd: ".." };
async function getCommits(path) {
const format = `{"hash":"%h","author":{"login":"%aN"},"date":"%ad","message":"%f"},`;
const { stdout } = await execa(
"git",
[
"log",
"--follow",
"--reverse",
"--abbrev-commit",
`--pretty=format:${format}`,
"--date=iso",
"--",
path
],
options
);
const json = `[${stdout.slice(0, -1)}]`;
const result = JSON.parse(json).map(commit => ({
...commit,
date: new Date(commit.date)
}));
return result;
}
async function getContent(commit, path) {
const { stdout } = await execa(
"git",
["show", `${commit.hash}:${path}`],
options
);
return stdout;
}
module.exports = async function(path) {
const commits = await getCommits(path);
await Promise.all(
commits.map(async commit => {
commit.content = await getContent(commit, path);
})
);
return commits;
};