From 964f226cd0f6be299b7591c878a44b7abca3b3ee Mon Sep 17 00:00:00 2001 From: Michael Hutchison Date: Mon, 18 Feb 2019 14:25:18 +1100 Subject: [PATCH] #3 Fix: Use child_process spawn instead of exec for git commands producing large output (more than 200Kb) --- CHANGELOG.md | 4 ++++ package.json | 2 +- src/dataSource.ts | 31 +++++++++++++++++++++++++++---- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21f6bec1..dad7bc55 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Change Log +## 1.3.2 - 2019-02-18 +* Fixes an issue when viewing some large graphs of more than 500 commits. +* Significantly reduced package size. + ## 1.3.1 - 2019-02-17 * View the Visual Studio Code Diff of a file change in a commit, by clicking on the file in the commit details view. * All git commands are run asynchronously to improve responsiveness. diff --git a/package.json b/package.json index b9ce5281..a329b285 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "git-graph", "displayName": "Git Graph", - "version": "1.3.1", + "version": "1.3.2", "publisher": "mhutchie", "author": { "name": "Michael Hutchison", diff --git a/src/dataSource.ts b/src/dataSource.ts index c2df95cf..77faff6e 100644 --- a/src/dataSource.ts +++ b/src/dataSource.ts @@ -170,8 +170,16 @@ export class DataSource { public async getCommitFile(commitHash: string, filePath: string) { return new Promise((resolve) => { - cp.exec('git show "' + commitHash + '":"' + filePath + '"', { cwd: this.workspaceDir }, (err, stdout) => { - resolve(!err ? stdout : ''); + let args = ['show', commitHash + ':' + filePath], stdout = '', err = false; + const cmd = cp.spawn('git', args, { cwd: this.workspaceDir }); + cmd.stdout.on('data', (d) => { stdout += d; }); + cmd.on('error', () => { + resolve(''); + err = true; + }); + cmd.on('exit', (code) => { + if (err) return; + resolve(code === 0 ? stdout : ''); }); }); } @@ -248,8 +256,23 @@ export class DataSource { private async getGitLog(branch: string, num: number, showRemoteBranches: boolean) { return new Promise((resolve) => { - cp.exec('git log ' + (branch !== '' ? escapeRefName(branch) : '--branches' + (showRemoteBranches ? ' --remotes' : '')) + ' --max-count=' + num + ' --format="' + gitLogFormat + '"', { cwd: this.workspaceDir }, (err, stdout) => { - if (!err) { + let args = ['log', '--max-count=' + num, '--format=' + gitLogFormat], stdout = '', err = false; + if (branch !== '') { + args.push(escapeRefName(branch)); + } else { + args.push('--branches'); + if (showRemoteBranches) args.push('--remotes'); + } + + const cmd = cp.spawn('git', args, { cwd: this.workspaceDir }); + cmd.stdout.on('data', (d) => { stdout += d; }); + cmd.on('error', () => { + resolve([]); + err = true; + }); + cmd.on('exit', (code) => { + if (err) return; + if (code === 0) { let lines = stdout.split(eolRegex); let gitCommits: GitCommit[] = []; for (let i = 0; i < lines.length - 1; i++) {