5
5
6
6
import * as vscode from 'vscode' ;
7
7
import { API as GitAPI , RefType , Repository } from './typings/git' ;
8
- import { getRepositoryFromUrl } from './util' ;
8
+ import { getRepositoryFromUrl , repositoryHasGitHubRemote } from './util' ;
9
9
10
10
export function isFileInRepo ( repository : Repository , file : vscode . Uri ) : boolean {
11
11
return file . path . toLowerCase ( ) === repository . rootUri . path . toLowerCase ( ) ||
@@ -132,19 +132,17 @@ export function encodeURIComponentExceptSlashes(path: string) {
132
132
export async function getLink ( gitAPI : GitAPI , useSelection : boolean , shouldEnsurePublished : boolean , hostPrefix ?: string , linkType : 'permalink' | 'headlink' = 'permalink' , context ?: LinkContext , useRange ?: boolean ) : Promise < string | undefined > {
133
133
hostPrefix = hostPrefix ?? 'https://github.com' ;
134
134
const fileAndPosition = getFileAndPosition ( context ) ;
135
- if ( ! fileAndPosition ) {
136
- return ;
137
- }
138
- const uri = fileAndPosition . uri ;
135
+ const fileUri = fileAndPosition ?. uri ;
139
136
140
137
// Use the first repo if we cannot determine a repo from the uri.
141
- const gitRepo = ( uri ? getRepositoryForFile ( gitAPI , uri ) : gitAPI . repositories [ 0 ] ) ?? gitAPI . repositories [ 0 ] ;
138
+ const githubRepository = gitAPI . repositories . find ( repo => repositoryHasGitHubRemote ( repo ) ) ;
139
+ const gitRepo = ( fileUri ? getRepositoryForFile ( gitAPI , fileUri ) : githubRepository ) ?? githubRepository ;
142
140
if ( ! gitRepo ) {
143
141
return ;
144
142
}
145
143
146
- if ( shouldEnsurePublished ) {
147
- await ensurePublished ( gitRepo , uri ) ;
144
+ if ( shouldEnsurePublished && fileUri ) {
145
+ await ensurePublished ( gitRepo , fileUri ) ;
148
146
}
149
147
150
148
let repo : { owner : string ; repo : string } | undefined ;
@@ -165,13 +163,17 @@ export async function getLink(gitAPI: GitAPI, useSelection: boolean, shouldEnsur
165
163
}
166
164
167
165
const blobSegment = gitRepo . state . HEAD ? ( `/blob/${ linkType === 'headlink' && gitRepo . state . HEAD . name ? encodeURIComponentExceptSlashes ( gitRepo . state . HEAD . name ) : gitRepo . state . HEAD ?. commit } ` ) : '' ;
168
- const encodedFilePath = encodeURIComponentExceptSlashes ( uri . path . substring ( gitRepo . rootUri . path . length ) ) ;
166
+ const uriWithoutFileSegments = `${ hostPrefix } /${ repo . owner } /${ repo . repo } ${ blobSegment } ` ;
167
+ if ( ! fileUri ) {
168
+ return uriWithoutFileSegments ;
169
+ }
170
+
171
+ const encodedFilePath = encodeURIComponentExceptSlashes ( fileUri . path . substring ( gitRepo . rootUri . path . length ) ) ;
169
172
const fileSegments = fileAndPosition . type === LinkType . File
170
173
? ( useSelection ? `${ encodedFilePath } ${ useRange ? rangeString ( fileAndPosition . range ) : '' } ` : '' )
171
174
: ( useSelection ? `${ encodedFilePath } ${ useRange ? notebookCellRangeString ( fileAndPosition . cellIndex , fileAndPosition . range ) : '' } ` : '' ) ;
172
175
173
- return `${ hostPrefix } /${ repo . owner } /${ repo . repo } ${ blobSegment
174
- } ${ fileSegments } `;
176
+ return `${ uriWithoutFileSegments } ${ fileSegments } ` ;
175
177
}
176
178
177
179
export function getBranchLink ( url : string , branch : string , hostPrefix : string = 'https://github.com' ) {
0 commit comments