git-history/cli/git.js

37 lines
872 B
JavaScript
Raw Normal View History

2019-02-10 05:21:05 +03:00
const execa = require("execa");
async function getCommits(path) {
const format = `{"hash":"%h","author":{"login":"%aN"},"date":"%ad","message":"%f"},`;
2019-02-10 22:59:26 +03:00
const { stdout } = await execa("git", [
"log",
"--follow",
"--reverse",
"--abbrev-commit",
`--pretty=format:${format}`,
"--date=iso",
"--",
path
]);
2019-02-10 05:21:05 +03:00
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) {
2019-02-10 22:59:26 +03:00
const { stdout } = await execa("git", ["show", `${commit.hash}:${path}`]);
2019-02-10 05:21:05 +03:00
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;
};