11import { Range , TextDocument } from "vscode" ;
2- import { API , GitExtension , Remote , Repository } from "../typings/git" ;
2+ import { API , Remote , Repository } from "../typings/git" ;
33import { getActiveFileSchemaEditor } from "../util/getActiveEditor" ;
4+ import { getGitExtension } from "../util/getExtension" ;
45
56export type GitParameters = {
67 useSelection : boolean ;
78 useBranch : boolean ;
89} ;
910
10- export class GitUtil {
11- private gitApi : API ;
11+ let _gitApi : API ;
1212
13- constructor ( gitExtension : GitExtension ) {
14- this . gitApi = gitExtension . getAPI ( 1 ) ;
13+ async function gitApi ( ) : Promise < API > {
14+ if ( _gitApi == null ) {
15+ const gitExtension = await getGitExtension ( ) ;
16+ _gitApi = gitExtension . getAPI ( 1 ) ;
1517 }
18+ return _gitApi ;
19+ }
1620
17- getFileURL ( { useSelection = false , useBranch = false } : GitParameters ) : string {
21+ export class GitUtil {
22+ async getFileURL ( { useSelection = false , useBranch = false } : GitParameters ) : Promise < string > {
1823 const { document, selections } = getActiveFileSchemaEditor ( ) ;
19- const repository = this . getRepository ( ) ;
24+ const repository = await this . getRepository ( ) ;
2025 const platform = getPlatform ( repository ) ;
2126 const relativeFilePath = getRelativeFilepath ( repository , document . uri . path ) ;
2227 const range = useSelection ? selections [ 0 ] : undefined ;
@@ -35,28 +40,28 @@ export class GitUtil {
3540 return platform . getFileUrl ( commitOrBranch , relativeFilePath , range ) ;
3641 }
3742
38- getRepoURL ( ) : string {
39- const repository = this . getRepository ( ) ;
43+ async getRepoURL ( ) : Promise < string > {
44+ const repository = await this . getRepository ( ) ;
4045 return getPlatform ( repository ) . getRepoUrl ( ) ;
4146 }
4247
43- getIssuesURL ( ) : string {
44- const repository = this . getRepository ( ) ;
48+ async getIssuesURL ( ) : Promise < string > {
49+ const repository = await this . getRepository ( ) ;
4550 return getPlatform ( repository ) . getIssuesUrl ( ) ;
4651 }
4752
48- getNewIssueURL ( ) : string {
49- const repository = this . getRepository ( ) ;
53+ async getNewIssueURL ( ) : Promise < string > {
54+ const repository = await this . getRepository ( ) ;
5055 return getPlatform ( repository ) . getNewIssueUrl ( ) ;
5156 }
5257
53- getPullRequestsURL ( ) : string {
54- const repository = this . getRepository ( ) ;
58+ async getPullRequestsURL ( ) : Promise < string > {
59+ const repository = await this . getRepository ( ) ;
5560 return getPlatform ( repository ) . getPullRequestsURL ( ) ;
5661 }
5762
5863 async checkout ( branches : string [ ] ) {
59- const repository = this . getRepository ( ) ;
64+ const repository = await this . getRepository ( ) ;
6065 for ( const branch of branches ) {
6166 try {
6267 await repository . checkout ( branch ) ;
@@ -68,8 +73,8 @@ export class GitUtil {
6873 throw Error ( `Can't checkout branch '${ branches . join ( ", " ) } '` ) ;
6974 }
7075
71- private getRepository ( ) : Repository {
72- const { repositories } = this . gitApi ;
76+ private async getRepository ( ) : Promise < Repository > {
77+ const { repositories } = await gitApi ( ) ;
7378 if ( repositories . length === 0 ) {
7479 throw Error ( "No git repositories available" ) ;
7580 }
0 commit comments