Use spawn instead of exec to handle large git log outputs

This commit is contained in:
lowlighter
2021-06-17 20:15:37 +02:00
parent 2bf5fa925c
commit c13582ec52
2 changed files with 63 additions and 34 deletions

View File

@@ -21,6 +21,7 @@ import twemojis from "twemoji-parser"
import url from "url" import url from "url"
import util from "util" import util from "util"
import fetch from "node-fetch" import fetch from "node-fetch"
import readline from "readline"
prism_lang() prism_lang()
//Exports //Exports
@@ -140,18 +141,18 @@ export async function chartist() {
.replace(/class="ct-chart-line">/, `class="ct-chart-line">${css}`) .replace(/class="ct-chart-line">/, `class="ct-chart-line">${css}`)
} }
/**Run command */ /**Run command (use this to execute commands and process whole output at once, may not be suitable for large outputs) */
export async function run(command, options, {prefixed = true, log = true} = {}) { export async function run(command, options, {prefixed = true, log = true} = {}) {
const prefix = {win32:"wsl"}[process.platform] ?? "" const prefix = {win32:"wsl"}[process.platform] ?? ""
command = `${prefixed ? prefix : ""} ${command}`.trim() command = `${prefixed ? prefix : ""} ${command}`.trim()
return new Promise((solve, reject) => { return new Promise((solve, reject) => {
console.debug(`metrics/command > ${command}`) console.debug(`metrics/command/run > ${command}`)
const child = processes.exec(command, options) const child = processes.exec(command, options)
let [stdout, stderr] = ["", ""] let [stdout, stderr] = ["", ""]
child.stdout.on("data", data => stdout += data) child.stdout.on("data", data => stdout += data)
child.stderr.on("data", data => stderr += data) child.stderr.on("data", data => stderr += data)
child.on("close", code => { child.on("close", code => {
console.debug(`metrics/command > ${command} > exited with code ${code}`) console.debug(`metrics/command/run > ${command} > exited with code ${code}`)
if (log) { if (log) {
console.debug(stdout) console.debug(stdout)
console.debug(stderr) console.debug(stderr)
@@ -161,6 +162,30 @@ export async function run(command, options, {prefixed = true, log = true} = {})
}) })
} }
/**Spawn command (use this to execute commands and process output on the fly) */
export async function spawn(command, args = [], options = {}, {prefixed = true, timeout = 300*1000, stdout} = {}) {
const prefix = {win32:"wsl"}[process.platform] ?? ""
if ((prefixed)&&(prefix)) {
args.unshift(command)
command = prefix
}
if (!stdout)
throw new Error("`stdout` argument was not provided, use run() instead of spawn() if processing output is not needed")
return new Promise((solve, reject) => {
console.debug(`metrics/command/spawn > ${command} with ${args.join(" ")}`)
const child = processes.spawn(command, args, {...options, shell:true, timeout})
const reader = readline.createInterface({input:child.stdout})
reader.on("line", stdout)
const closed = new Promise(close => reader.on("close", close))
child.on("close", async code => {
console.debug(`metrics/command/spawn > ${command} with ${args.join(" ")} > exited with code ${code}`)
await closed
console.debug(`metrics/command/spawn > ${command} with ${args.join(" ")} > reader closed`)
return code === 0 ? solve(stdout) : reject(stderr)
})
})
}
/**Check command existance */ /**Check command existance */
export async function which(command) { export async function which(command) {
try { try {

View File

@@ -151,32 +151,31 @@ async function analyze({login, imports, data}, {results, path}) {
console.debug(`metrics/compute/${login}/plugins > languages > indepth > checking git log`) console.debug(`metrics/compute/${login}/plugins > languages > indepth > checking git log`)
for (let page = 0; ; page++) { for (let page = 0; ; page++) {
try { try {
const stdout = await imports.run(`git log ${data.shared["commits.authoring"].map(authoring => `--author="${authoring}"`).join(" ")} --regexp-ignore-case --format=short --patch --max-count=${per_page} --skip=${page*per_page}`, {cwd:path}, {log:false})
let file = null, lang = null
if (!stdout.trim().length) {
console.debug(`metrics/compute/${login}/plugins > languages > indepth > no more commits`)
break
}
console.debug(`metrics/compute/${login}/plugins > languages > indepth > processing commits ${page*per_page} from ${(page+1)*per_page}`) console.debug(`metrics/compute/${login}/plugins > languages > indepth > processing commits ${page*per_page} from ${(page+1)*per_page}`)
for (const line of stdout.split("\n").map(line => line.trim())) { let file = null, lang = null, empty = true
await imports.spawn("git", ["log", ...data.shared["commits.authoring"].map(authoring => `--author="${authoring}"`), "--regexp-ignore-case", "--format=short", "--patch", `--max-count=${per_page}`, `--skip=${page*per_page}`], {cwd:path}, {
stdout(line) {
//Unflag empty output
if ((empty)&&(line.trim().length))
empty = false
//Commits counter //Commits counter
if (/^commit [0-9a-f]{40}$/.test(line)) { if (/^commit [0-9a-f]{40}$/.test(line)) {
results.commits++ results.commits++
continue return
} }
//Ignore empty lines or unneeded lines //Ignore empty lines or unneeded lines
if ((!/^[+]/.test(line))||(!line.length)) if ((!/^[+]/.test(line))||(!line.length))
continue return
//File marker //File marker
if (/^[+]{3}\sb[/](?<file>[\s\S]+)$/.test(line)) { if (/^[+]{3}\sb[/](?<file>[\s\S]+)$/.test(line)) {
file = line.match(/^[+]{3}\sb[/](?<file>[\s\S]+)$/)?.groups?.file ?? null file = line.match(/^[+]{3}\sb[/](?<file>[\s\S]+)$/)?.groups?.file ?? null
lang = files[file] ?? null lang = files[file] ?? null
edited.add(file) edited.add(file)
continue return
} }
//Ignore unkonwn languages //Ignore unkonwn languages
if (!lang) if (!lang)
continue return
//Added line marker //Added line marker
if (/^[+]\s*(?<line>[\s\S]+)$/.test(line)) { if (/^[+]\s*(?<line>[\s\S]+)$/.test(line)) {
const size = Buffer.byteLength(line.match(/^[+]\s*(?<line>[\s\S]+)$/)?.groups?.line ?? "", "utf-8") const size = Buffer.byteLength(line.match(/^[+]\s*(?<line>[\s\S]+)$/)?.groups?.line ?? "", "utf-8")
@@ -185,6 +184,11 @@ async function analyze({login, imports, data}, {results, path}) {
results.total += size results.total += size
} }
} }
})
if (empty) {
console.debug(`metrics/compute/${login}/plugins > languages > indepth > no more commits`)
break
}
} }
catch { catch {
console.debug(`metrics/compute/${login}/plugins > languages > indepth > an error occured on page ${page}, skipping...`) console.debug(`metrics/compute/${login}/plugins > languages > indepth > an error occured on page ${page}, skipping...`)