From 7b038234247eb5bb02169c281cabba242381495d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=B9=BF?= Date: Wed, 26 Feb 2025 19:21:21 +0800 Subject: [PATCH] add default filter --- src/GitService.ts | 50 ++++++++++++++++++++++++++++++++++---------- src/view.provider.ts | 2 +- 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/GitService.ts b/src/GitService.ts index f1f55b4..4dc12b0 100644 --- a/src/GitService.ts +++ b/src/GitService.ts @@ -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 { 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){ diff --git a/src/view.provider.ts b/src/view.provider.ts index ef90543..bdb28e8 100644 --- a/src/view.provider.ts +++ b/src/view.provider.ts @@ -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:[] }