Skip to content

Commit

Permalink
[engine] 주석 제거 및 prettier 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
BeA-Pro committed Oct 15, 2024
1 parent e9ad183 commit 883453d
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 96 deletions.
74 changes: 32 additions & 42 deletions packages/vscode/src/utils/git.util.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { COMMIT_SEPARATOR, GIT_LOG_SEPARATOR } from "@githru-vscode-ext/analysis-engine";
import * as cp from "child_process";
import * as fs from "fs";
import os from 'os';
import os from "os";
import * as path from "path";
import { Worker } from 'worker_threads';


import { Worker } from "worker_threads";

export interface GitExecutable {
readonly path: string;
Expand Down Expand Up @@ -204,79 +202,71 @@ export async function getGitLog(gitPath: string, currentWorkspacePath: string):
}

export async function getLogCount(gitPath: string, currentWorkspacePath: string): Promise<number> {
const BASE_10 = 10;
return new Promise((resolve, reject) => {
const args = [
"rev-list",
"--count",
"--all",
];
const BASE_10 = 10;
return new Promise((resolve, reject) => {
const args = ["rev-list", "--count", "--all"];

resolveSpawnOutput(
cp.spawn(gitPath, args, {
cwd: currentWorkspacePath,
env: Object.assign({}, process.env),
})
).then(([status, stdout, stderr]) => {
const { code, error } = status;

if (code === 0 && !error) {
const commitCount = parseInt(stdout.toString().trim(), BASE_10); // Buffer를 문자열로 변환 후 숫자로 변환
resolve(commitCount); // 숫자를 반환
} else {
reject(stderr);
}
});
cp.spawn(gitPath, args, {
cwd: currentWorkspacePath,
env: Object.assign({}, process.env),
})
).then(([status, stdout, stderr]) => {
const { code, error } = status;

if (code === 0 && !error) {
const commitCount = parseInt(stdout.toString().trim(), BASE_10);
resolve(commitCount);
} else {
reject(stderr);
}
});
});
}


export async function fetchGitLogInParallel(gitPath: string, currentWorkspacePath: string): Promise<string> {
const numCores = os.cpus().length;

const totalCnt = await getLogCount(gitPath, currentWorkspacePath);
let numberOfThreads = 1;

const taskThreshold = 1000;
const coreCountThreshold = 4;

if (totalCnt > taskThreshold) {
if (numCores < coreCountThreshold) numberOfThreads = 2;
else numberOfThreads = 3;
}
const chunkSize = Math.ceil(totalCnt/ numberOfThreads);
if (totalCnt > taskThreshold) {
if (numCores < coreCountThreshold) numberOfThreads = 2;
else numberOfThreads = 3;
}

const chunkSize = Math.ceil(totalCnt / numberOfThreads);
const promises: Promise<string>[] = [];

for (let i = 0; i < numberOfThreads; i++) {
const skipCount = i * chunkSize;
const limitCount = chunkSize;

console.log('__dirname:', __dirname);
const worker = new Worker(path.resolve(__dirname, './worker.js'), {
const worker = new Worker(path.resolve(__dirname, "./worker.js"), {
workerData: {
gitPath,
currentWorkspacePath,
skipCount,
limitCount,
COMMIT_SEPARATOR,
GIT_LOG_SEPARATOR
COMMIT_SEPARATOR,
GIT_LOG_SEPARATOR,
},
});

promises.push(
new Promise((resolve, reject) => {
worker.on('message', resolve);
worker.on('error', reject);
worker.on("message", resolve);
worker.on("error", reject);
})
);
}

return Promise.all(promises).then((logs) => logs.join('\n'));
return Promise.all(promises).then((logs) => logs.join("\n"));
}



export async function getGitConfig(
gitPath: string,
currentWorkspacePath: string,
Expand Down
107 changes: 53 additions & 54 deletions packages/vscode/src/utils/git.worker.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,59 @@
import * as cp from 'child_process';
import { parentPort, workerData } from 'worker_threads';
import * as cp from "child_process";
import { parentPort, workerData } from "worker_threads";

import { resolveSpawnOutput } from './git.util'

const { gitPath, currentWorkspacePath, skipCount, limitCount,COMMIT_SEPARATOR,GIT_LOG_SEPARATOR } = workerData;


import { resolveSpawnOutput } from "./git.util";

const { gitPath, currentWorkspacePath, skipCount, limitCount, COMMIT_SEPARATOR, GIT_LOG_SEPARATOR } = workerData;

async function getPartialGitLog() {
const gitLogFormat =
COMMIT_SEPARATOR +
[
"%H", // commit hash (id)
"%P", // parent hashes
"%D", // ref names (branches, tags)
"%an", // author name
"%ae", // author email
"%ad", // author date
"%cn",
"%ce",
"%cd", // committer name, committer email and committer date
"%B", // commit message (subject and body)
].join(GIT_LOG_SEPARATOR) +
GIT_LOG_SEPARATOR;

const args = [
'--no-pager',
'log',
'--all',
'--parents',
'--numstat',
'--date-order',
`--pretty=format:${gitLogFormat}`,
'--decorate',
'-c',
`--skip=${skipCount}`,
`-n ${limitCount}`,
];

resolveSpawnOutput(
cp.spawn(gitPath, args, {
cwd: currentWorkspacePath,
env: Object.assign({}, process.env),
})
).then(([status, stdout, stderr]) => {
const { code, error } = status;

if (code === 0 && !error && parentPort !== null) {
parentPort.postMessage(stdout.toString());
} else {
if (parentPort !== null) parentPort.postMessage(stderr);
}
}).catch(error => {
console.error('Spawn Error:', error);
});
const gitLogFormat =
COMMIT_SEPARATOR +
[
"%H", // commit hash (id)
"%P", // parent hashes
"%D", // ref names (branches, tags)
"%an", // author name
"%ae", // author email
"%ad", // author date
"%cn",
"%ce",
"%cd", // committer name, committer email and committer date
"%B", // commit message (subject and body)
].join(GIT_LOG_SEPARATOR) +
GIT_LOG_SEPARATOR;

const args = [
"--no-pager",
"log",
"--all",
"--parents",
"--numstat",
"--date-order",
`--pretty=format:${gitLogFormat}`,
"--decorate",
"-c",
`--skip=${skipCount}`,
`-n ${limitCount}`,
];

resolveSpawnOutput(
cp.spawn(gitPath, args, {
cwd: currentWorkspacePath,
env: Object.assign({}, process.env),
})
)
.then(([status, stdout, stderr]) => {
const { code, error } = status;

if (code === 0 && !error && parentPort !== null) {
parentPort.postMessage(stdout.toString());
} else {
if (parentPort !== null) parentPort.postMessage(stderr);
}
})
.catch((error) => {
console.error("Spawn Error:", error);
});
}

getPartialGitLog();
getPartialGitLog();

0 comments on commit 883453d

Please sign in to comment.