Add pagination

This commit is contained in:
Rodrigo Pombo 2019-02-18 16:06:59 -03:00
parent da993e17b2
commit 348babd263
4 changed files with 66 additions and 46 deletions

View File

@ -1,39 +1,45 @@
const execa = require("execa");
const pather = require("path");
async function getCommits(path) {
async function getCommits(path, last, before) {
const format = `{"hash":"%h","author":{"login":"%aN"},"date":"%ad"},`;
const { stdout } = await execa("git", [
"log",
// "--follow",
"--reverse",
`--pretty=format:${format}`,
"--date=iso",
"--",
path
]);
const { stdout } = await execa(
"git",
[
"log",
`--max-count=${before ? last + 1 : last}`,
`--pretty=format:${format}`,
"--date=iso",
`${before || "HEAD"}`,
"--",
pather.basename(path)
],
{ cwd: pather.dirname(path) }
);
const json = `[${stdout.slice(0, -1)}]`;
const messagesOutput = await execa("git", [
"log",
// "--follow",
"--reverse",
`--pretty=format:%s`,
"--",
path
]);
const messagesOutput = await execa(
"git",
[
"log",
`--max-count=${last}`,
`--pretty=format:%s`,
`${before || "HEAD"}`,
"--",
pather.basename(path)
],
{ cwd: pather.dirname(path) }
);
const messages = messagesOutput.stdout.replace('"', '\\"').split(/\r?\n/);
const result = JSON.parse(json)
.map((commit, i) => ({
...commit,
date: new Date(commit.date),
message: messages[i]
}))
.slice(-20);
const result = JSON.parse(json).map((commit, i) => ({
...commit,
date: new Date(commit.date),
message: messages[i]
}));
return result;
return before ? result.slice(1) : result;
}
async function getContent(commit, path) {
@ -45,8 +51,8 @@ async function getContent(commit, path) {
return stdout;
}
module.exports = async function(path) {
const commits = await getCommits(path);
module.exports = async function(path, last, before) {
const commits = await getCommits(path, last, before);
await Promise.all(
commits.map(async commit => {
commit.content = await getContent(commit, path);

View File

@ -63,10 +63,9 @@ function activate(context) {
message => {
switch (message.command) {
case "commits":
const path = message.params.path;
getCommits(path)
const { path, last = 15, before = null } = message.params.path;
getCommits(path, last, before)
.then(commits => {
console.log(path, commits);
panel.webview.postMessage(commits);
})
.catch(console.error);

View File

@ -1,16 +1,16 @@
const execa = require("execa");
const pather = require("path");
async function getCommits(path) {
async function getCommits(path, last, before) {
const format = `{"hash":"%h","author":{"login":"%aN"},"date":"%ad"},`;
const { stdout } = await execa(
"git",
[
"log",
// "--follow",
"--reverse",
`--max-count=${before ? last + 1 : last}`,
`--pretty=format:${format}`,
"--date=iso",
`${before || "HEAD"}`,
"--",
pather.basename(path)
],
@ -22,9 +22,9 @@ async function getCommits(path) {
"git",
[
"log",
// "--follow",
"--reverse",
`--max-count=${last}`,
`--pretty=format:%s`,
`${before || "HEAD"}`,
"--",
pather.basename(path)
],
@ -33,15 +33,13 @@ async function getCommits(path) {
const messages = messagesOutput.stdout.replace('"', '\\"').split(/\r?\n/);
const result = JSON.parse(json)
.map((commit, i) => ({
...commit,
date: new Date(commit.date),
message: messages[i]
}))
.slice(-20);
const result = JSON.parse(json).map((commit, i) => ({
...commit,
date: new Date(commit.date),
message: messages[i]
}));
return result;
return before ? result.slice(1) : result;
}
async function getContent(commit, path) {
@ -53,8 +51,8 @@ async function getContent(commit, path) {
return stdout;
}
module.exports = async function(path) {
const commits = await getCommits(path);
module.exports = async function(path, last, before) {
const commits = await getCommits(path, last, before);
await Promise.all(
commits.map(async commit => {
commit.content = await getContent(commit, path);

17
vscode-ext/test-git.js Normal file
View File

@ -0,0 +1,17 @@
#!/usr/bin/env node
// node test-git.js extension.js 2 94c91d9
const getCommits = require("./git");
const [, , path, last, before] = process.argv;
getCommits(path, last, before).then(cs =>
console.log(
cs
.map(c => {
return `${c.hash} ${c.date.toDateString()} ${c.message}`;
})
.join("\n")
)
);