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

View File

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

View File

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