Skip to content

Commit c46b5c8

Browse files
committed
git: better error handling
1 parent 87eea6c commit c46b5c8

4 files changed

Lines changed: 34 additions & 11 deletions

File tree

extensions/git/src/commands.ts

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
'use strict';
77

8-
import { Uri, commands, scm, Disposable, SCMResourceGroup, SCMResource, window, workspace, QuickPickItem } from 'vscode';
8+
import { Uri, commands, scm, Disposable, SCMResourceGroup, SCMResource, window, workspace, QuickPickItem, OutputChannel } from 'vscode';
99
import { IRef, RefType } from './git';
1010
import { Model, Resource } from './model';
1111
import { log } from './util';
@@ -15,7 +15,30 @@ import * as path from 'path';
1515
type Command = (...args: any[]) => any;
1616

1717
function catchErrors(fn: (...args) => Promise<any>): (...args) => void {
18-
return (...args) => fn.call(this, ...args).catch(err => console.log(err));
18+
return (...args) => fn.call(this, ...args).catch(async err => {
19+
if (err.gitErrorCode) {
20+
let message: string;
21+
22+
switch (err.gitErrorCode) {
23+
case 'DirtyWorkTree':
24+
message = 'Please clean your repository working tree before checkout.';
25+
break;
26+
default:
27+
message = (err.stderr || err.message).replace(/^error: /, '');
28+
break;
29+
}
30+
31+
const outputChannel = this.outputChannel as OutputChannel;
32+
const openOutputChannelChoice = 'Open Git Log';
33+
const choice = await window.showErrorMessage(message, openOutputChannelChoice);
34+
35+
if (choice === openOutputChannelChoice) {
36+
outputChannel.show();
37+
}
38+
} else {
39+
console.error(err);
40+
}
41+
});
1942
}
2043

2144
function resolveGitURI(uri: Uri): SCMResource | SCMResourceGroup | undefined {
@@ -79,7 +102,7 @@ class CommandCenter {
79102

80103
private disposables: Disposable[] = [];
81104

82-
constructor(private model: Model) {
105+
constructor(private model: Model, private outputChannel: OutputChannel) {
83106
this.disposables.push(
84107
commands.registerCommand('git.refresh', this.refresh, this),
85108
commands.registerCommand('git.openChange', this.openChange, this),
@@ -229,6 +252,6 @@ class CommandCenter {
229252
}
230253
}
231254

232-
export function registerCommands(model: Model): Disposable {
233-
return new CommandCenter(model);
255+
export function registerCommands(model: Model, outputChannel: OutputChannel): Disposable {
256+
return new CommandCenter(model, outputChannel);
234257
}

extensions/git/src/git.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ export class Git {
331331
}
332332

333333
if (options.log !== false) {
334-
this.log(`ERROR: ${result.stderr}\n`);
334+
this.log(`${result.stderr}\n`);
335335
}
336336

337337
return Promise.reject<IExecutionResult>(new GitError({
@@ -363,7 +363,7 @@ export class Git {
363363
options.env = _.assign({}, process.env, options.env || {});
364364

365365
if (options.log !== false) {
366-
this.log(`SPAWN: git ${args.join(' ')}\n`);
366+
this.log(`git ${args.join(' ')}\n`);
367367
}
368368

369369
return cp.spawn(this.gitPath, args, options);

extensions/git/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ async function init(disposables: Disposable[]): Promise<void> {
115115
const syncStatusBar = new SyncStatusBar(model);
116116

117117
disposables.push(
118-
registerCommands(model),
118+
registerCommands(model, outputChannel),
119119
scm.registerSCMProvider('git', provider),
120120
workspace.registerTextDocumentContentProvider('git-index', textDocumentContentProvider),
121121
textDocumentContentProvider,

extensions/git/src/statusbar.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export class SyncStatusBar {
7070
}
7171

7272
const HEAD = this.model.HEAD;
73-
let icon = '$(sync) ';
73+
let icon = '$(sync)';
7474
let text = '';
7575

7676
if (HEAD && HEAD.name && HEAD.commit) {
@@ -82,15 +82,15 @@ export class SyncStatusBar {
8282
}
8383
this.raw.command = 'git.sync';
8484
} else {
85-
icon = '$(cloud-upload) ';
85+
icon = '$(cloud-upload)';
8686
this.raw.command = 'git.publish';
8787
}
8888
} else {
8989
this.raw.color = 'rgba(255,255,255,0.7)';
9090
this.raw.command = '';
9191
}
9292

93-
this.raw.text = `${icon}${text}`;
93+
this.raw.text = [icon, text].join(' ').trim();
9494
this.raw.show();
9595
}
9696

0 commit comments

Comments
 (0)