mirror of
https://github.com/pomber/git-history.git
synced 2024-08-16 09:50:32 +03:00
Add pagination
This commit is contained in:
parent
da993e17b2
commit
348babd263
42
cli/git.js
42
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", [
|
||||
const { stdout } = await execa(
|
||||
"git",
|
||||
[
|
||||
"log",
|
||||
// "--follow",
|
||||
"--reverse",
|
||||
`--max-count=${before ? last + 1 : last}`,
|
||||
`--pretty=format:${format}`,
|
||||
"--date=iso",
|
||||
`${before || "HEAD"}`,
|
||||
"--",
|
||||
path
|
||||
]);
|
||||
pather.basename(path)
|
||||
],
|
||||
{ cwd: pather.dirname(path) }
|
||||
);
|
||||
const json = `[${stdout.slice(0, -1)}]`;
|
||||
|
||||
const messagesOutput = await execa("git", [
|
||||
const messagesOutput = await execa(
|
||||
"git",
|
||||
[
|
||||
"log",
|
||||
// "--follow",
|
||||
"--reverse",
|
||||
`--max-count=${last}`,
|
||||
`--pretty=format:%s`,
|
||||
`${before || "HEAD"}`,
|
||||
"--",
|
||||
path
|
||||
]);
|
||||
pather.basename(path)
|
||||
],
|
||||
{ cwd: pather.dirname(path) }
|
||||
);
|
||||
|
||||
const messages = messagesOutput.stdout.replace('"', '\\"').split(/\r?\n/);
|
||||
|
||||
const result = JSON.parse(json)
|
||||
.map((commit, i) => ({
|
||||
const result = JSON.parse(json).map((commit, i) => ({
|
||||
...commit,
|
||||
date: new Date(commit.date),
|
||||
message: messages[i]
|
||||
}))
|
||||
.slice(-20);
|
||||
}));
|
||||
|
||||
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);
|
||||
|
@ -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);
|
||||
|
@ -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) => ({
|
||||
const result = JSON.parse(json).map((commit, i) => ({
|
||||
...commit,
|
||||
date: new Date(commit.date),
|
||||
message: messages[i]
|
||||
}))
|
||||
.slice(-20);
|
||||
}));
|
||||
|
||||
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
17
vscode-ext/test-git.js
Normal 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")
|
||||
)
|
||||
);
|
Loading…
Reference in New Issue
Block a user