Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
protected openFile(change: GitFileChange, commitSha: string): void {
const uri: URI = new URI(change.uri);
let fromURI = change.oldUri ? new URI(change.oldUri) : uri; // set oldUri on renamed and copied
fromURI = fromURI.withScheme(GIT_RESOURCE_SCHEME).withQuery(commitSha + '~1');
const toURI = uri.withScheme(GIT_RESOURCE_SCHEME).withQuery(commitSha);
let uriToOpen = uri;
if (change.status === GitFileStatus.Deleted) {
uriToOpen = fromURI;
} else if (change.status === GitFileStatus.New) {
uriToOpen = toURI;
} else {
uriToOpen = DiffUris.encode(fromURI, toURI);
}
open(this.openerService, uriToOpen, { mode: 'reveal' });
}
}
execute: async uris => {
const [left, right] = uris;
const [leftExists, rightExists] = await Promise.all([
this.fileSystem.exists(left.toString()),
this.fileSystem.exists(right.toString())
]);
if (leftExists && rightExists) {
const [leftStat, rightStat] = await Promise.all([
this.fileSystem.getFileStat(left.toString()),
this.fileSystem.getFileStat(right.toString()),
]);
if (leftStat && rightStat) {
if (!leftStat.isDirectory && !rightStat.isDirectory) {
const uri = DiffUris.encode(left, right);
const opener = await this.openerService.getOpener(uri);
opener.open(uri);
} else {
const details = (() => {
if (leftStat.isDirectory && rightStat.isDirectory) {
return 'Both resource were a directory.';
} else {
if (leftStat.isDirectory) {
return `'${left.path.base}' was a directory.`;
} else {
return `'${right.path.base}' was a directory.`;
}
}
});
this.messageService.warn(`Directories cannot be compared. ${details()}`);
}
if (change.staged) {
return DiffUris.encode(
changeUri.withScheme(GIT_RESOURCE_SCHEME).withQuery('HEAD'),
changeUri.withScheme(GIT_RESOURCE_SCHEME),
changeUri.displayName + ' (Index)');
}
if (this.stagedChanges.find(c => c.uri === change.uri)) {
return DiffUris.encode(
changeUri.withScheme(GIT_RESOURCE_SCHEME),
changeUri,
changeUri.displayName + ' (Working tree)');
}
if (this.mergeChanges.find(c => c.uri === change.uri)) {
return changeUri;
}
return DiffUris.encode(
changeUri.withScheme(GIT_RESOURCE_SCHEME).withQuery('HEAD'),
changeUri,
changeUri.displayName + ' (Working tree)');
}
if (change.staged) {
return changeUri.withScheme(GIT_RESOURCE_SCHEME);
}
if (this.stagedChanges.find(c => c.uri === change.uri)) {
return DiffUris.encode(
changeUri.withScheme(GIT_RESOURCE_SCHEME),
changeUri,
changeUri.displayName + ' (Working tree)');
}
return changeUri;
}
async compareFiles(): Promise {
const uriSelected = await this.getURISelection();
if (this.firstCompareFile === undefined || uriSelected === undefined) {
return false;
}
const diffUri = DiffUris.encode(this.firstCompareFile, uriSelected);
open(this.openerService, diffUri).catch(e => {
this.notifications.error(e.message);
});
return true;
}
}
public async openDiffEditor(left: URI, right: URI, label?: string, options?: OpenerOptions): Promise {
if (left.scheme === 'file' && right.scheme === 'file') {
const [leftExists, rightExists] = await Promise.all([
this.fileSystem.exists(left.toString()),
this.fileSystem.exists(right.toString())
]);
if (leftExists && rightExists) {
const [leftStat, rightStat] = await Promise.all([
this.fileSystem.getFileStat(left.toString()),
this.fileSystem.getFileStat(right.toString()),
]);
if (leftStat && rightStat) {
if (!leftStat.isDirectory && !rightStat.isDirectory) {
const uri = DiffUris.encode(left, right, label);
await open(this.openerService, uri, options);
} else {
const details = (() => {
if (leftStat.isDirectory && rightStat.isDirectory) {
return 'Both resource were a directory.';
} else {
if (leftStat.isDirectory) {
return `'${left.path.base}' was a directory.`;
} else {
return `'${right.path.base}' was a directory.`;
}
}
});
this.messageService.warn(`Directories cannot be compared. ${details()}`);
}
}
getUriToOpen(change: GitFileChange): URI {
const changeUri: URI = new URI(change.uri);
if (change.status !== GitFileStatus.New) {
if (change.staged) {
return DiffUris.encode(
changeUri.withScheme(GIT_RESOURCE_SCHEME).withQuery('HEAD'),
changeUri.withScheme(GIT_RESOURCE_SCHEME),
changeUri.displayName + ' (Index)');
}
if (this.stagedChanges.find(c => c.uri === change.uri)) {
return DiffUris.encode(
changeUri.withScheme(GIT_RESOURCE_SCHEME),
changeUri,
changeUri.displayName + ' (Working tree)');
}
if (this.mergeChanges.find(c => c.uri === change.uri)) {
return changeUri;
}
return DiffUris.encode(
changeUri.withScheme(GIT_RESOURCE_SCHEME).withQuery('HEAD'),
changeUri,
changeUri,
changeUri.displayName + ' (Working tree)');
}
if (this.mergeChanges.find(c => c.uri === change.uri)) {
return changeUri;
}
return DiffUris.encode(
changeUri.withScheme(GIT_RESOURCE_SCHEME).withQuery('HEAD'),
changeUri,
changeUri.displayName + ' (Working tree)');
}
if (change.staged) {
return changeUri.withScheme(GIT_RESOURCE_SCHEME);
}
if (this.stagedChanges.find(c => c.uri === change.uri)) {
return DiffUris.encode(
changeUri.withScheme(GIT_RESOURCE_SCHEME),
changeUri,
changeUri.displayName + ' (Working tree)');
}
return changeUri;
}