Skip to content

Commit

Permalink
fix(vscode): fix gitProvider init. (TabbyML#2316)
Browse files Browse the repository at this point in the history
  • Loading branch information
icycodes authored May 31, 2024
1 parent 4e1e061 commit 98f1e32
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
3 changes: 1 addition & 2 deletions clients/vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
"Other"
],
"activationEvents": [
"onStartupFinished",
"onView:chatView"
"onStartupFinished"
],
"main": "./dist/node/extension.js",
"browser": "./dist/browser/extension.js",
Expand Down
35 changes: 29 additions & 6 deletions clients/vscode/src/git/GitProvider.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,36 @@
import { extensions, workspace, Extension, Uri } from "vscode";
import type { GitExtension, Repository as GitRepository, API } from "./git";
import { extensions, workspace, Uri } from "vscode";
import type { Repository as GitRepository, API } from "./git";
export type Repository = GitRepository;
import { getLogger } from "../logger";

function getGitExtensionApi(): API | undefined {
const ext = extensions.getExtension("vscode.git");
return ext?.isActive ? ext.exports.getAPI(1) : undefined;
}

export class GitProvider {
private ext: Extension<GitExtension> | undefined;
private api: API | undefined;
private readonly logger = getLogger();
private api: API | undefined = undefined;

constructor() {
this.ext = extensions.getExtension("vscode.git");
this.api = this.ext?.isActive ? this.ext.exports.getAPI(1) : undefined;
this.init();
}

private init(tries = 0) {
this.api = getGitExtensionApi();
if (this.api) {
this.logger.info("GitProvider created.");
} else {
if (tries > 10) {
this.logger.warn(`Failed to create GitProvider after ${tries} tries, giving up.`);
} else {
const delay = (tries + 1) * 1000;
this.logger.info(`Failed to create GitProvider, retry after ${delay}ms`);
setTimeout(() => {
this.init(tries + 1);
}, delay);
}
}
}

getRepositories(): Repository[] | undefined {
Expand Down

0 comments on commit 98f1e32

Please sign in to comment.