add default filter

This commit is contained in:
程广 2025-02-26 19:21:21 +08:00
parent c64a658df4
commit 7b03823424
2 changed files with 40 additions and 12 deletions

View File

@ -25,10 +25,13 @@ export class GitService {
}
this._repoPath = workspaceFolders[0].uri.fsPath;
}
private defaultFilter(): string[] {
return ['--author=$(git config user.name)', '--since=\"7 days ago\"'];
}
public async getCommits(filter: string = ""): Promise<any[]> {
const filterParts = filter.split(" ");
const nFilter: string[] = [];
let nFilter: string[] = [];
filterParts.forEach((part) => {
const t = part.split(":");
const key = t[0];
@ -37,9 +40,9 @@ export class GitService {
nFilter.push(`--${GitFilter[key]}=${v}`);
}
});
const command = `git log --oneline ${nFilter.join(
" "
)} --pretty=format:"{\\\"hash\\\":\\\"%h\\\",\\\"author\\\":\\\"%an\\\",\\\"date\\\":\\\"%ad\\\",\\\"msg\\\":\\\"%s\\\"}%n---" --date=format:'%Y-%m-%d' --name-only`;
if(nFilter.length === 0){
nFilter = this.defaultFilter();
}
const gitArgs = [
"log",
"--oneline",
@ -48,15 +51,16 @@ export class GitService {
"--date=format:%Y-%m-%d",
"--name-only",
];
const gitProcess = child_process.spawn(this.gitPath, gitArgs, {
cwd: this._repoPath,
});
return new Promise((resolve, reject) => {
let lines: string[] = [];
let commits: GitCommit[] = [];
function makeCommit(lines: any[]): GitCommit | undefined {
try {
if(lines.length === 1) {
return undefined;
}
const commitLine = lines[0];
console.log(commitLine);
const commit: GitCommit = JSON.parse(lines[0]);
@ -73,13 +77,31 @@ export class GitService {
return undefined;
}
const gitProcess = child_process.spawn(this.gitPath, gitArgs, {
cwd: this._repoPath,
shell: true,
});
gitProcess.stderr.setEncoding("utf8");
gitProcess.stderr.on("data", (data) => {
console.log(`stderr: ${data}`);
});
gitProcess.stdout.setEncoding("utf8");
gitProcess.on("error", (error) => {
console.log(`error: ${error.message}`);
reject(error);
});
gitProcess.stdout.on("data", (data) => {
const nlines = data.split("\n");
const nlines = data.split("\n").filter((l:string) => l.trim() !== "");
for (let nline of nlines) {
if (nline.startsWith("\"{")) {
if (nline.startsWith("{")) {
if(lines.length > 0){
const commit = makeCommit(lines);
if(commit){
commits.push(commit);
}
}
lines = [];
lines.push(JSON.parse(nline));
lines.push(nline);
} else if (nline.startsWith("---") ) {
continue
}else if( nline.trim() === ""){
@ -91,6 +113,12 @@ export class GitService {
lines.push(nline);
}
}
if(lines.length > 0){
const commit = makeCommit(lines);
if(commit){
commits.push(commit);
}
}
});
gitProcess.on("exit",(code,signal)=>{
if(code === 0){

View File

@ -215,7 +215,7 @@ private async openDiff(commitId: string, filePath: string) {
const nodes = [];
for(let commit of commits){
const node = {
message: commit.msg + ' -- ' + commit.author + '@' + commit.date,
message: commit.msg + ' -- ' + commit.author + '@'+ commit.hash+ " " + commit.date,
type: 'commit',
children:[]
}