Skip to content

Commit 4102bdd

Browse files
committed
Reworks git command error handling
Switches to use the new diffWith command
1 parent d420d82 commit 4102bdd

14 files changed

+229
-236
lines changed
Lines changed: 18 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
'use strict';
2-
import { commands, Range, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
2+
import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
33
import { ActiveEditorCommand, Commands, getCommandUri } from './common';
4-
import { BuiltInCommands, GlyphChars } from '../constants';
5-
import { DiffWithPreviousCommandArgs } from './diffWithPrevious';
6-
import { DiffWithWorkingCommandArgs } from './diffWithWorking';
4+
import { DiffWithCommandArgs } from './diffWith';
75
import { GitCommit, GitService, GitUri } from '../gitService';
86
import { Logger } from '../logger';
97
import { Messages } from '../messages';
10-
import * as path from 'path';
118

129
export interface DiffLineWithPreviousCommandArgs {
1310
commit?: GitCommit;
11+
1412
line?: number;
1513
showOptions?: TextDocumentShowOptions;
1614
}
@@ -43,56 +41,26 @@ export class DiffLineWithPreviousCommand extends ActiveEditorCommand {
4341
if (blame === undefined) return Messages.showFileNotUnderSourceControlWarningMessage('Unable to open compare');
4442

4543
args.commit = blame.commit;
46-
47-
// If we don't have a sha or the current commit matches the blame, show the previous
48-
if (gitUri.sha === undefined || gitUri.sha === args.commit.sha) {
49-
return commands.executeCommand(Commands.DiffWithPrevious, new GitUri(uri, args.commit), {
50-
line: args.line,
51-
showOptions: args.showOptions
52-
} as DiffWithPreviousCommandArgs);
53-
}
54-
55-
// If the line is uncommitted, find the previous commit and treat it as a DiffWithWorking
56-
if (args.commit.isUncommitted) {
57-
uri = args.commit.uri;
58-
args.commit = new GitCommit(args.commit.type, args.commit.repoPath, args.commit.previousSha!, args.commit.previousFileName!, args.commit.author, args.commit.date, args.commit.message);
59-
args.line = (blame.line.line + 1) + gitUri.offset;
60-
61-
return commands.executeCommand(Commands.DiffWithWorking, uri, {
62-
commit: args.commit,
63-
line: args.line,
64-
showOptions: args.showOptions
65-
} as DiffWithWorkingCommandArgs);
66-
}
6744
}
6845
catch (ex) {
69-
Logger.error(ex, 'DiffWithPreviousLineCommand', `getBlameForLine(${blameline})`);
46+
Logger.error(ex, 'DiffLineWithPreviousCommand', `getBlameForLine(${blameline})`);
7047
return window.showErrorMessage(`Unable to open compare. See output channel for more details`);
7148
}
7249
}
7350

74-
try {
75-
const [rhs, lhs] = await Promise.all([
76-
this.git.getVersionedFile(gitUri.repoPath, gitUri.fsPath, gitUri.sha!),
77-
this.git.getVersionedFile(args.commit.repoPath, args.commit.uri.fsPath, args.commit.sha)
78-
]);
79-
80-
if (args.line !== undefined && args.line !== 0) {
81-
if (args.showOptions === undefined) {
82-
args.showOptions = {};
83-
}
84-
args.showOptions.selection = new Range(args.line, 0, args.line, 0);
85-
}
86-
87-
await commands.executeCommand(BuiltInCommands.Diff,
88-
Uri.file(lhs),
89-
Uri.file(rhs),
90-
`${path.basename(args.commit.uri.fsPath)} (${args.commit.shortSha}) ${GlyphChars.ArrowLeftRight} ${path.basename(gitUri.fsPath)} (${gitUri.shortSha})`,
91-
args.showOptions);
92-
}
93-
catch (ex) {
94-
Logger.error(ex, 'DiffWithPreviousLineCommand', 'getVersionedFile');
95-
return window.showErrorMessage(`Unable to open compare. See output channel for more details`);
96-
}
51+
const diffArgs: DiffWithCommandArgs = {
52+
repoPath: args.commit.repoPath,
53+
lhs: {
54+
sha: args.commit.previousSha !== undefined ? args.commit.previousSha : GitService.fakeSha,
55+
uri: args.commit.previousUri
56+
},
57+
rhs: {
58+
sha: args.commit.sha,
59+
uri: args.commit.uri
60+
},
61+
line: args.line,
62+
showOptions: args.showOptions
63+
};
64+
return commands.executeCommand(Commands.DiffWith, diffArgs);
9765
}
9866
}

src/commands/diffLineWithWorking.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
'use strict';
22
import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
33
import { ActiveEditorCommand, Commands, getCommandUri } from './common';
4-
import { DiffWithWorkingCommandArgs } from './diffWithWorking';
4+
import { DiffWithCommandArgs } from './diffWith';
55
import { GitCommit, GitService, GitUri } from '../gitService';
66
import { Messages } from '../messages';
77
import { Logger } from '../logger';
88

99
export interface DiffLineWithWorkingCommandArgs {
1010
commit?: GitCommit;
11+
1112
line?: number;
1213
showOptions?: TextDocumentShowOptions;
1314
}
@@ -52,6 +53,19 @@ export class DiffLineWithWorkingCommand extends ActiveEditorCommand {
5253
}
5354
}
5455

55-
return commands.executeCommand(Commands.DiffWithWorking, uri, args as DiffWithWorkingCommandArgs);
56+
const diffArgs: DiffWithCommandArgs = {
57+
repoPath: args.commit.repoPath,
58+
lhs: {
59+
sha: args.commit.sha,
60+
uri: args.commit.uri
61+
},
62+
rhs: {
63+
sha: '',
64+
uri: args.commit.uri
65+
},
66+
line: args.line,
67+
showOptions: args.showOptions
68+
};
69+
return commands.executeCommand(Commands.DiffWith, diffArgs);
5670
}
5771
}

src/commands/diffWith.ts

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ export class DiffWithCommand extends ActiveEditorCommand {
3535
args = {
3636
repoPath: commit1.repoPath,
3737
lhs: {
38-
sha: commit1.sha,
38+
sha: 'HEAD',
3939
uri: commit1.uri
4040
},
4141
rhs: {
42-
sha: 'HEAD',
42+
sha: '',
4343
uri: commit1.uri
4444
}
4545
};
@@ -81,25 +81,15 @@ export class DiffWithCommand extends ActiveEditorCommand {
8181
}
8282

8383
async execute(editor?: TextEditor, uri?: Uri, args: DiffWithCommandArgs = {}): Promise<any> {
84+
args = { ...args };
8485
if (args.repoPath === undefined || args.lhs === undefined || args.rhs === undefined) return undefined;
8586

86-
if (args.lhs.title === undefined) {
87-
args.lhs.title = (args.lhs.sha === 'HEAD')
88-
? `${path.basename(args.lhs.uri.fsPath)}`
89-
: `${path.basename(args.lhs.uri.fsPath)} (${GitService.shortenSha(args.lhs.sha)})`;
90-
}
91-
if (args.rhs.title === undefined) {
92-
args.rhs.title = (args.rhs.sha === 'HEAD')
93-
? `${path.basename(args.rhs.uri.fsPath)}`
94-
: `${path.basename(args.rhs.uri.fsPath)} (${GitService.shortenSha(args.rhs.sha)})`;
95-
}
96-
9787
try {
9888
const [lhs, rhs] = await Promise.all([
99-
args.lhs.sha !== 'HEAD'
89+
args.lhs.sha !== '' && !GitService.isUncommitted(args.lhs.sha)
10090
? this.git.getVersionedFile(args.repoPath, args.lhs.uri.fsPath, args.lhs.sha)
10191
: args.lhs.uri.fsPath,
102-
args.rhs.sha !== 'HEAD'
92+
args.rhs.sha !== '' && !GitService.isUncommitted(args.rhs.sha)
10393
? this.git.getVersionedFile(args.repoPath, args.rhs.uri.fsPath, args.rhs.sha)
10494
: args.rhs.uri.fsPath
10595
]);
@@ -111,10 +101,45 @@ export class DiffWithCommand extends ActiveEditorCommand {
111101
args.showOptions.selection = new Range(args.line, 0, args.line, 0);
112102
}
113103

114-
await commands.executeCommand(BuiltInCommands.Diff,
115-
Uri.file(lhs),
116-
Uri.file(rhs),
117-
`${args.lhs.title} ${GlyphChars.ArrowLeftRight} ${args.rhs.title}`,
104+
let lhsPrefix = '';
105+
if (lhs === undefined) {
106+
lhsPrefix = 'deleted in ';
107+
}
108+
else if (args.rhs.sha === GitService.fakeSha) {
109+
lhsPrefix = 'added in ';
110+
}
111+
112+
let rhsPrefix = '';
113+
if (rhs === undefined) {
114+
rhsPrefix = 'deleted in ';
115+
}
116+
else if (args.lhs.sha === GitService.fakeSha) {
117+
rhsPrefix = 'added in ';
118+
}
119+
120+
if (args.lhs.title === undefined && args.lhs.sha !== GitService.fakeSha) {
121+
args.lhs.title = (args.lhs.sha === '' || GitService.isUncommitted(args.lhs.sha))
122+
? `${path.basename(args.lhs.uri.fsPath)}`
123+
: `${path.basename(args.lhs.uri.fsPath)} (${lhsPrefix}${GitService.shortenSha(args.lhs.sha)})`;
124+
}
125+
if (args.rhs.title === undefined && args.rhs.sha !== GitService.fakeSha) {
126+
args.rhs.title = (args.rhs.sha === '' || GitService.isUncommitted(args.rhs.sha))
127+
? `${path.basename(args.rhs.uri.fsPath)}`
128+
: `${path.basename(args.rhs.uri.fsPath)} (${rhsPrefix}${GitService.shortenSha(args.rhs.sha)})`;
129+
}
130+
131+
const title = (args.lhs.title !== undefined && args.rhs.title !== undefined)
132+
? `${args.lhs.title} ${GlyphChars.ArrowLeftRight} ${args.rhs.title}`
133+
: args.lhs.title || args.rhs.title;
134+
135+
return await commands.executeCommand(BuiltInCommands.Diff,
136+
lhs === undefined
137+
? GitService.toGitContentUri(GitService.fakeSha, args.lhs.uri.fsPath, args.repoPath)
138+
: Uri.file(lhs),
139+
rhs === undefined
140+
? GitService.toGitContentUri(GitService.fakeSha, args.rhs.uri.fsPath, args.repoPath)
141+
: Uri.file(rhs),
142+
title,
118143
args.showOptions);
119144
}
120145
catch (ex) {

src/commands/diffWithBranch.ts

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict';
2-
import { commands, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
2+
import { commands, TextDocumentShowOptions, TextEditor, Uri } from 'vscode';
33
import { ActiveEditorCommand, Commands, getCommandUri } from './common';
44
import { GlyphChars } from '../constants';
55
import { DiffWithCommandArgs } from './diffWith';
66
import { GitService, GitUri } from '../gitService';
7-
import { Logger } from '../logger';
87
import { Messages } from '../messages';
98
import { BranchesQuickPick, CommandQuickPickItem } from '../quickPicks';
109
import * as path from 'path';
@@ -43,26 +42,20 @@ export class DiffWithBranchCommand extends ActiveEditorCommand {
4342
const branch = pick.branch.name;
4443
if (branch === undefined) return undefined;
4544

46-
try {
47-
const diffArgs: DiffWithCommandArgs = {
48-
repoPath: gitUri.repoPath,
49-
lhs: {
50-
sha: pick.branch.remote ? `remotes/${branch}` : branch,
51-
uri: gitUri as Uri,
52-
title: `${path.basename(gitUri.fsPath)} (${branch})`
53-
},
54-
rhs: {
55-
sha: 'HEAD',
56-
uri: gitUri as Uri
57-
},
58-
line: args.line,
59-
showOptions: args.showOptions
60-
};
61-
await commands.executeCommand(Commands.DiffWith, diffArgs);
62-
}
63-
catch (ex) {
64-
Logger.error(ex, 'DiffWithBranchCommand', 'getVersionedFile');
65-
return window.showErrorMessage(`Unable to open branch compare. See output channel for more details`);
66-
}
45+
const diffArgs: DiffWithCommandArgs = {
46+
repoPath: gitUri.repoPath,
47+
lhs: {
48+
sha: pick.branch.remote ? `remotes/${branch}` : branch,
49+
uri: gitUri as Uri,
50+
title: `${path.basename(gitUri.fsPath)} (${branch})`
51+
},
52+
rhs: {
53+
sha: '',
54+
uri: gitUri as Uri
55+
},
56+
line: args.line,
57+
showOptions: args.showOptions
58+
};
59+
return commands.executeCommand(Commands.DiffWith, diffArgs);
6760
}
6861
}

src/commands/diffWithNext.ts

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22
import { Iterables } from '../system';
33
import { commands, Range, TextDocumentShowOptions, TextEditor, Uri, window } from 'vscode';
44
import { ActiveEditorCommand, Commands, getCommandUri } from './common';
5-
import { BuiltInCommands, GlyphChars } from '../constants';
5+
import { DiffWithCommandArgs } from './diffWith';
66
import { GitLogCommit, GitService, GitUri } from '../gitService';
77
import { Logger } from '../logger';
88
import { Messages } from '../messages';
9-
import * as path from 'path';
109

1110
export interface DiffWithNextCommandArgs {
1211
commit?: GitLogCommit;
13-
line?: number;
1412
range?: Range;
13+
14+
line?: number;
1515
showOptions?: TextDocumentShowOptions;
1616
}
1717

@@ -54,28 +54,19 @@ export class DiffWithNextCommand extends ActiveEditorCommand {
5454

5555
if (args.commit.nextSha === undefined) return commands.executeCommand(Commands.DiffWithWorking, uri);
5656

57-
try {
58-
const [rhs, lhs] = await Promise.all([
59-
this.git.getVersionedFile(args.commit.repoPath, args.commit.nextUri.fsPath, args.commit.nextSha),
60-
this.git.getVersionedFile(args.commit.repoPath, args.commit.uri.fsPath, args.commit.sha)
61-
]);
62-
63-
if (args.line !== undefined && args.line !== 0) {
64-
if (args.showOptions === undefined) {
65-
args.showOptions = {};
66-
}
67-
args.showOptions.selection = new Range(args.line, 0, args.line, 0);
68-
}
69-
70-
await commands.executeCommand(BuiltInCommands.Diff,
71-
Uri.file(lhs),
72-
Uri.file(rhs),
73-
`${path.basename(args.commit.uri.fsPath)} (${args.commit.shortSha}) ${GlyphChars.ArrowLeftRight} ${path.basename(args.commit.nextUri.fsPath)} (${args.commit.nextShortSha})`,
74-
args.showOptions);
75-
}
76-
catch (ex) {
77-
Logger.error(ex, 'DiffWithNextCommand', 'getVersionedFile');
78-
return window.showErrorMessage(`Unable to open compare. See output channel for more details`);
79-
}
57+
const diffArgs: DiffWithCommandArgs = {
58+
repoPath: args.commit.repoPath,
59+
lhs: {
60+
sha: args.commit.sha,
61+
uri: args.commit.uri
62+
},
63+
rhs: {
64+
sha: args.commit.nextSha,
65+
uri: args.commit.nextUri
66+
},
67+
line: args.line,
68+
showOptions: args.showOptions
69+
};
70+
return commands.executeCommand(Commands.DiffWith, diffArgs);
8071
}
8172
}

0 commit comments

Comments
 (0)