Skip to content

Commit 87ac58c

Browse files
chore(polish): polish extension code
1 parent 6325df7 commit 87ac58c

File tree

4 files changed

+62
-66
lines changed

4 files changed

+62
-66
lines changed

src/extension.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export async function activate(context: vscode.ExtensionContext) {
4242
const knownRepos = await getOpenedRepoHistory(context);
4343
if (!knownRepos) {
4444
vscode.window.showInformationMessage('No known repositories');
45-
return;
4645
} else {
4746
const json = JSON.stringify(knownRepos, null, 2); // pretty print
4847
vscode.workspace.openTextDocument({ content: json, language: 'json' });

src/opener.ts

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ function isAvoidableDir(dir: string): boolean {
2929
);
3030
}
3131

32-
async function isGitRepo(dirUri: vscode.Uri) {
32+
export async function isGitRepository(dirUri: vscode.Uri) {
3333
const gitFolder = vscode.Uri.joinPath(dirUri, '.git');
3434
try {
35-
await vscode.workspace.fs.stat(gitFolder);
36-
return true;
35+
const stat = await vscode.workspace.fs.stat(gitFolder);
36+
return stat.type === vscode.FileType.Directory;
3737
} catch (error) {
3838
return false;
3939
}
@@ -44,7 +44,6 @@ export class Opener {
4444
public readonly repoUri?: string;
4545
public readonly file?: string;
4646
public readonly repoRef?: string;
47-
4847
public readonly range?: vscode.Range;
4948

5049
constructor(
@@ -81,46 +80,52 @@ export class Opener {
8180
);
8281

8382
if (correctWorkspace) {
84-
// did the user asked for a particular ref?
85-
const gitRepo = await this.git?.openRepository(correctWorkspace.uri);
86-
if (gitRepo && this.repoRef) {
87-
// are we in the correct ref?
88-
let currentCommit = (await this.getCurrentCommit(gitRepo))?.commit;
89-
let repoRefCommit = await gitRepo.getCommit(this.repoRef);
90-
console.log(`CurrentCommit ${currentCommit} === ${repoRefCommit.hash}`);
91-
if (
92-
currentCommit &&
93-
repoRefCommit &&
94-
currentCommit !== repoRefCommit.hash
95-
) {
96-
// it seems that's not the case!!
97-
// ask the user if they want to checkout the correct branch
98-
const response = await vscode.window.showInformationMessage(
99-
`It seems that you're not in the correct branch. Do you want to checkout ${this.repoRef}?`,
100-
'Yes',
101-
'No',
102-
);
103-
if (response === 'Yes') {
104-
await gitRepo.checkout(this.repoRef);
105-
}
83+
await this.openWorkspace(correctWorkspace);
84+
} else {
85+
// it's not in the correct workspace, let's open it in a new window
86+
await this.openNewWorkspace(true);
87+
}
88+
}
89+
90+
private async openWorkspace(
91+
workspace: vscode.WorkspaceFolder,
92+
): Promise<void> {
93+
// did the user ask for a particular ref?
94+
const gitRepo = await this.git?.openRepository(workspace.uri);
95+
if (gitRepo && this.repoRef) {
96+
// are we in the correct ref?
97+
const currentCommit = (await this.getCurrentCommit(gitRepo))?.commit;
98+
const repoRefCommit = await gitRepo.getCommit(this.repoRef);
99+
console.log(`CurrentCommit ${currentCommit} === ${repoRefCommit.hash}`);
100+
if (
101+
currentCommit &&
102+
repoRefCommit &&
103+
currentCommit !== repoRefCommit.hash
104+
) {
105+
// it seems that's not the case!!
106+
// ask the user if they want to checkout the correct branch
107+
const response = await vscode.window.showInformationMessage(
108+
`It seems that you're not in the correct branch. Do you want to checkout ${this.repoRef}?`,
109+
'Yes',
110+
'No',
111+
);
112+
if (response === 'Yes') {
113+
await gitRepo.checkout(this.repoRef);
106114
}
107-
} else {
108-
console.log('NO GIT REPO????');
109115
}
116+
} else {
117+
console.log('NO GIT REPO????');
118+
}
110119

111-
if (!this.file) {
112-
// we wanted to open precisely this particular repo (not a file) which is already open.
113-
return;
114-
}
120+
if (!this.file) {
121+
// we wanted to open precisely this particular repo (not a file) which is already open.
122+
return;
123+
}
115124

116-
// for the moment, we'll check that the file exists (in case it has been deleted in the current branch)
117-
const files = await vscode.workspace.findFiles(this.file);
118-
if (files) {
119-
await this.openFile(correctWorkspace, this.file);
120-
}
121-
} else {
122-
// it's not in the correct workspace, let's open it in a new window
123-
await this.openNewWorkspace(true);
125+
// for the moment, we'll check that the file exists (in case it has been deleted in the current branch)
126+
const files = await vscode.workspace.findFiles(this.file);
127+
if (files) {
128+
await this.openFile(workspace, this.file);
124129
}
125130
}
126131

@@ -129,7 +134,7 @@ export class Opener {
129134
const knownRepoInfo = await getOpenedRepoHistory(this.context);
130135
const knownRepo = knownRepoInfo[this.repoName];
131136
console.log(`knownRepo ${this.repoName}: ${knownRepo}`);
132-
// if the repo is not found in the history, we're going to search it in the config roots
137+
// if the repo is not found in the history, we're going to search for it in the config roots
133138
const repoPath = knownRepo
134139
? vscode.Uri.file(knownRepo)
135140
: await this.findFolderInConfigRoots();
@@ -248,7 +253,7 @@ export class Opener {
248253
continue;
249254
}
250255
const dirUri = vscode.Uri.joinPath(dir, itemName);
251-
if (itemName === this.repoName && (await isGitRepo(dirUri))) {
256+
if (itemName === this.repoName && (await isGitRepository(dirUri))) {
252257
// found!
253258
return dirUri;
254259
} else {
@@ -320,9 +325,8 @@ export class Opener {
320325
: process.platform === 'darwin'
321326
? 'gitFoldersMac'
322327
: 'gitFolders';
323-
const roots = (config.get(prop) ||
324-
config.get('gitFolders') ||
325-
[]) as string[];
328+
const roots =
329+
config.get<string[]>(prop) || config.get<string[]>('gitFolders') || [];
326330
return roots;
327331
}
328332

src/repoHistory.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as vscode from 'vscode';
22

3-
export const OPENED_REPO_HISTORY_KEY = 'openedRepoHistory';
4-
export const PENDING_URI_TO_OPEN = 'vscode-open.last-uri';
3+
const OPENED_REPO_HISTORY_KEY = 'openedRepoHistory';
4+
const PENDING_URI_TO_OPEN = 'vscode-open.last-uri';
55

66
export async function getOpenedRepoHistory(
77
context: vscode.ExtensionContext,
@@ -12,17 +12,14 @@ export async function getOpenedRepoHistory(
1212
export async function addOpenedRepoToHistory(
1313
folder: vscode.WorkspaceFolder,
1414
context: vscode.ExtensionContext,
15-
attempts = 0,
1615
): Promise<void> {
1716
const repos = await getOpenedRepoHistory(context);
1817
repos[folder.name] = folder.uri.fsPath;
1918
await context.globalState.update(OPENED_REPO_HISTORY_KEY, repos);
2019
// re-check, there are some cases where this might not work
2120
const check = await getOpenedRepoHistory(context);
22-
if (!check[folder.name] && attempts < 3) {
23-
console.error(
24-
`Failed to save ${folder.name} to repo history, attempt ${attempts}`,
25-
);
21+
if (!check[folder.name]) {
22+
console.error(`Failed to save ${folder.name} to repo history`);
2623
} else {
2724
console.log(`Added repo ${folder.name} to the repo history`);
2825
}

src/vscodeUriHandler.ts

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as vscode from 'vscode';
22
import type { API } from './git';
3-
import { Opener } from './opener';
3+
import { Opener, isGitRepository } from './opener';
44
import {
55
addOpenedRepoToHistory,
66
getPendingUriToOpen,
@@ -29,11 +29,13 @@ export class VSCodeOpenUriHandler implements vscode.UriHandler {
2929
// check if the workspace folders are git repos.
3030
// in case they are, store the path of the repo.
3131
// { repoName: repoPath }
32-
for (const folder of folders) {
33-
if (await this.isGitRepository(folder)) {
34-
await addOpenedRepoToHistory(folder, this.context);
35-
}
36-
}
32+
await Promise.all(
33+
folders.map(async (folder) => {
34+
if (await this.isGitRepository(folder)) {
35+
await addOpenedRepoToHistory(folder, this.context);
36+
}
37+
}),
38+
);
3739
}
3840

3941
private async handlePendingUriToOpen() {
@@ -58,12 +60,6 @@ export class VSCodeOpenUriHandler implements vscode.UriHandler {
5860
if (folder.uri.scheme !== 'file') {
5961
return false;
6062
}
61-
const dotGit = vscode.Uri.joinPath(folder.uri, '.git');
62-
try {
63-
const stat = await vscode.workspace.fs.stat(dotGit);
64-
return stat.type === vscode.FileType.Directory;
65-
} catch (err) {
66-
return false;
67-
}
63+
return isGitRepository(folder.uri);
6864
}
6965
}

0 commit comments

Comments
 (0)