Skip to content

Commit 2d0bdc0

Browse files
committed
#15 Support for portable git installations
1 parent e45f9ef commit 2d0bdc0

File tree

5 files changed

+39
-25
lines changed

5 files changed

+39
-25
lines changed

media/main.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ body{
33
margin:0;
44
padding:0;
55
}
6-
body.notGitRepository h1, body.notGitRepository p{
6+
body.unableToLoad h1, body.unableToLoad p{
77
text-align:center;
88
}
99

src/config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,8 @@ export class Config {
3333
public showUncommittedChanges() {
3434
return this.workspaceConfiguration.get('showUncommittedChanges', true);
3535
}
36+
public gitPath(): string {
37+
let path = vscode.workspace.getConfiguration('git').get('path', null);
38+
return path !== null ? path : 'git';
39+
}
3640
}

src/dataSource.ts

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,31 @@ const gitLogFormat = ['%H', '%P', '%an', '%ae', '%at', '%s'].join(gitLogSeparato
88
const gitCommitDetailsFormat = ['%H', '%P', '%an', '%ae', '%at', '%cn', '%B'].join(gitLogSeparator);
99

1010
export class DataSource {
11-
private workspaceDir: string;
11+
private execOptions: cp.ExecOptions;
12+
private gitPath!: string;
13+
private gitExecPath!: string;
1214

1315
constructor(workspaceDir: string) {
14-
this.workspaceDir = workspaceDir;
16+
this.execOptions = { cwd: workspaceDir };
17+
this.registerGitPath();
18+
}
19+
20+
public registerGitPath() {
21+
this.gitPath = (new Config()).gitPath();
22+
this.gitExecPath = this.gitPath.indexOf(' ') > -1 ? '"' + this.gitPath + '"' : this.gitPath;
1523
}
1624

1725
public isGitRepository() {
1826
return new Promise<boolean>((resolve) => {
19-
cp.exec('git rev-parse --git-dir', { cwd: this.workspaceDir }, (err) => {
27+
cp.exec(this.gitExecPath + ' rev-parse --git-dir', this.execOptions, (err) => {
2028
resolve(!err);
2129
});
2230
});
2331
}
2432

2533
public getBranches(showRemoteBranches: boolean) {
2634
return new Promise<string[]>((resolve) => {
27-
cp.exec('git branch' + (showRemoteBranches ? ' -a' : ''), { cwd: this.workspaceDir }, (err, stdout) => {
35+
cp.exec(this.gitExecPath + ' branch' + (showRemoteBranches ? ' -a' : ''), this.execOptions, (err, stdout) => {
2836
if (!err) {
2937
let lines = stdout.split(eolRegex);
3038
let branches: string[] = [];
@@ -98,7 +106,7 @@ export class DataSource {
98106
public async commitDetails(commitHash: string) {
99107
try {
100108
let details = await new Promise<GitCommitDetails>((resolve, reject) => {
101-
cp.exec('git show --quiet ' + commitHash + ' --format="' + gitCommitDetailsFormat + '"', { cwd: this.workspaceDir }, (err, stdout) => {
109+
cp.exec(this.gitExecPath + ' show --quiet ' + commitHash + ' --format="' + gitCommitDetailsFormat + '"', this.execOptions, (err, stdout) => {
102110
if (!err) {
103111
let lines = stdout.split(eolRegex);
104112
let commitInfo = lines[0].split(gitLogSeparator);
@@ -119,7 +127,7 @@ export class DataSource {
119127
});
120128
let fileLookup: { [file: string]: number } = {};
121129
await new Promise((resolve, reject) => {
122-
cp.exec('git diff-tree --name-status -r -m --root --find-renames --diff-filter=AMDR ' + commitHash, { cwd: this.workspaceDir }, (err, stdout) => {
130+
cp.exec(this.gitExecPath + ' diff-tree --name-status -r -m --root --find-renames --diff-filter=AMDR ' + commitHash, this.execOptions, (err, stdout) => {
123131
if (!err) {
124132
let lines = stdout.split(eolRegex);
125133
for (let i = 1; i < lines.length - 1; i++) {
@@ -136,7 +144,7 @@ export class DataSource {
136144
});
137145
});
138146
await new Promise((resolve, reject) => {
139-
cp.exec('git diff-tree --numstat -r -m --root --find-renames --diff-filter=AMDR ' + commitHash, { cwd: this.workspaceDir }, (err, stdout) => {
147+
cp.exec(this.gitExecPath + ' diff-tree --numstat -r -m --root --find-renames --diff-filter=AMDR ' + commitHash, this.execOptions, (err, stdout) => {
140148
if (!err) {
141149
let lines = stdout.split(eolRegex);
142150
for (let i = 1; i < lines.length - 1; i++) {
@@ -164,7 +172,7 @@ export class DataSource {
164172
public async getCommitFile(commitHash: string, filePath: string) {
165173
return new Promise<string>((resolve) => {
166174
let args = ['show', commitHash + ':' + filePath], stdout = '', err = false;
167-
const cmd = cp.spawn('git', args, { cwd: this.workspaceDir });
175+
const cmd = cp.spawn(this.gitPath, args, this.execOptions);
168176
cmd.stdout.on('data', (d) => { stdout += d; });
169177
cmd.on('error', () => {
170178
resolve('');
@@ -178,48 +186,48 @@ export class DataSource {
178186
}
179187

180188
public addTag(tagName: string, commitHash: string) {
181-
return this.runGitCommand('git tag -a ' + escapeRefName(tagName) + ' -m "" ' + commitHash);
189+
return this.runGitCommand('tag -a ' + escapeRefName(tagName) + ' -m "" ' + commitHash);
182190
}
183191

184192
public deleteTag(tagName: string) {
185-
return this.runGitCommand('git tag -d ' + escapeRefName(tagName));
193+
return this.runGitCommand('tag -d ' + escapeRefName(tagName));
186194
}
187195

188196
public createBranch(branchName: string, commitHash: string) {
189-
return this.runGitCommand('git branch ' + escapeRefName(branchName) + ' ' + commitHash);
197+
return this.runGitCommand('branch ' + escapeRefName(branchName) + ' ' + commitHash);
190198
}
191199

192200
public checkoutBranch(branchName: string, remoteBranch: string | null) {
193-
return this.runGitCommand('git checkout ' + (remoteBranch === null ? escapeRefName(branchName) : ' -b ' + escapeRefName(branchName) + ' ' + escapeRefName(remoteBranch)));
201+
return this.runGitCommand('checkout ' + (remoteBranch === null ? escapeRefName(branchName) : ' -b ' + escapeRefName(branchName) + ' ' + escapeRefName(remoteBranch)));
194202
}
195203

196204
public deleteBranch(branchName: string, forceDelete: boolean) {
197-
return this.runGitCommand('git branch --delete' + (forceDelete ? ' --force' : '') + ' ' + escapeRefName(branchName));
205+
return this.runGitCommand('branch --delete' + (forceDelete ? ' --force' : '') + ' ' + escapeRefName(branchName));
198206
}
199207

200208
public renameBranch(oldName: string, newName: string) {
201-
return this.runGitCommand('git branch -m ' + escapeRefName(oldName) + ' ' + escapeRefName(newName));
209+
return this.runGitCommand('branch -m ' + escapeRefName(oldName) + ' ' + escapeRefName(newName));
202210
}
203211

204-
public mergeBranch(branchName: string){
205-
return this.runGitCommand('git merge ' + escapeRefName(branchName));
212+
public mergeBranch(branchName: string) {
213+
return this.runGitCommand('merge ' + escapeRefName(branchName));
206214
}
207215

208216
public cherrypickCommit(commitHash: string, parentIndex: number) {
209-
return this.runGitCommand('git cherry-pick ' + commitHash + (parentIndex > 0 ? ' -m ' + parentIndex : ''));
217+
return this.runGitCommand('cherry-pick ' + commitHash + (parentIndex > 0 ? ' -m ' + parentIndex : ''));
210218
}
211219

212220
public revertCommit(commitHash: string, parentIndex: number) {
213-
return this.runGitCommand('git revert --no-edit ' + commitHash + (parentIndex > 0 ? ' -m ' + parentIndex : ''));
221+
return this.runGitCommand('revert --no-edit ' + commitHash + (parentIndex > 0 ? ' -m ' + parentIndex : ''));
214222
}
215223

216224
public resetToCommit(commitHash: string, resetMode: GitResetMode) {
217-
return this.runGitCommand('git reset --' + resetMode + ' ' + commitHash);
225+
return this.runGitCommand('reset --' + resetMode + ' ' + commitHash);
218226
}
219227

220228
private async runGitCommand(command: string) {
221229
return new Promise<GitCommandStatus>((resolve) => {
222-
cp.exec(command, { cwd: this.workspaceDir }, (err) => {
230+
cp.exec(this.gitExecPath + ' ' + command, this.execOptions, (err) => {
223231
if (!err) {
224232
resolve(null);
225233
} else {
@@ -232,7 +240,7 @@ export class DataSource {
232240

233241
private async getRefs(showRemoteBranches: boolean) {
234242
return new Promise<GitRef[]>((resolve) => {
235-
cp.exec('git show-ref ' + (showRemoteBranches ? '' : '--heads --tags') + ' -d', { cwd: this.workspaceDir }, (err, stdout) => {
243+
cp.exec(this.gitExecPath + ' show-ref ' + (showRemoteBranches ? '' : '--heads --tags') + ' -d', this.execOptions, (err, stdout) => {
236244
if (!err) {
237245
let lines = stdout.split(eolRegex);
238246
let refs: GitRef[] = [];
@@ -269,7 +277,7 @@ export class DataSource {
269277
if (showRemoteBranches) args.push('--remotes');
270278
}
271279

272-
const cmd = cp.spawn('git', args, { cwd: this.workspaceDir });
280+
const cmd = cp.spawn(this.gitPath, args, this.execOptions);
273281
cmd.stdout.on('data', (d) => { stdout += d; });
274282
cmd.on('error', () => {
275283
resolve([]);
@@ -296,7 +304,7 @@ export class DataSource {
296304
private getGitUnsavedChanges() {
297305
try {
298306
return new Promise<GitUnsavedChangesCmdResp>((resolve, reject) => {
299-
cp.exec('git status -s --branch --untracked-files --porcelain', { cwd: this.workspaceDir }, (err, stdout) => {
307+
cp.exec(this.gitExecPath + ' status -s --branch --untracked-files --porcelain', this.execOptions, (err, stdout) => {
300308
if (!err) {
301309
let lines = stdout.split(eolRegex);
302310
resolve(lines.length > 2 ? { branch: lines[0].substring(3).split('...')[0], changes: lines.length - 2 } : null);

src/extension.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ export function activate(context: vscode.ExtensionContext) {
3232
} else {
3333
statusBarItem.hide();
3434
}
35+
} else if (e.affectsConfiguration('git.path') && dataSource !== null) {
36+
dataSource.registerGitPath();
3537
}
3638
}));
3739
}

src/gitGraphView.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export class GitGraphView {
187187
<script src="${jsUri}"></script>
188188
</body>`;
189189
} else {
190-
body = `<body class="notGitRepository"><h1>Git Graph</h1><p>The current workspace is not a Git Repository, unable to show Git Graph.</p></body>`;
190+
body = `<body class="unableToLoad"><h1>Git Graph</h1><p>Unable to load Git Graph. Either the current workspace is not a Git Repository, or the Git executable could not found.</p></body>`;
191191
}
192192

193193
return `<!DOCTYPE html>

0 commit comments

Comments
 (0)