From 348babd2639dda17c8523549581079db2489008e Mon Sep 17 00:00:00 2001 From: Rodrigo Pombo Date: Mon, 18 Feb 2019 16:06:59 -0300 Subject: [PATCH] Add pagination --- cli/git.js | 62 ++++++++++++++++++++++------------------- vscode-ext/extension.js | 5 ++-- vscode-ext/git.js | 28 +++++++++---------- vscode-ext/test-git.js | 17 +++++++++++ 4 files changed, 66 insertions(+), 46 deletions(-) create mode 100644 vscode-ext/test-git.js diff --git a/cli/git.js b/cli/git.js index 3aabcc9..7b9bee4 100644 --- a/cli/git.js +++ b/cli/git.js @@ -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); diff --git a/vscode-ext/extension.js b/vscode-ext/extension.js index dc2d55a..a6f6edb 100644 --- a/vscode-ext/extension.js +++ b/vscode-ext/extension.js @@ -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); diff --git a/vscode-ext/git.js b/vscode-ext/git.js index 002589b..7b9bee4 100644 --- a/vscode-ext/git.js +++ b/vscode-ext/git.js @@ -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); diff --git a/vscode-ext/test-git.js b/vscode-ext/test-git.js new file mode 100644 index 0000000..89e59e2 --- /dev/null +++ b/vscode-ext/test-git.js @@ -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") + ) +);