forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremote.ts
More file actions
191 lines (170 loc) · 5.69 KB
/
Copy pathremote.ts
File metadata and controls
191 lines (170 loc) · 5.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { AuthProvider, GitHubServerType } from './authentication';
import Logger from './logger';
import { Protocol } from './protocol';
import { Repository } from '../api/api';
import { getEnterpriseUri, isEnterprise } from '../github/utils';
export class Remote {
public get host(): string {
return this.gitProtocol.host;
}
public get owner(): string {
return this.gitProtocol.owner;
}
public get repositoryName(): string {
return this.gitProtocol.repositoryName;
}
public get normalizedHost(): string {
const normalizedUri = this.gitProtocol.normalizeUri();
return `${normalizedUri!.scheme}://${normalizedUri!.authority}`;
}
public get authProviderId(): AuthProvider {
return this.host === getEnterpriseUri()?.authority ? AuthProvider.githubEnterprise : AuthProvider.github;
}
public get isEnterprise(): boolean {
return isEnterprise(this.authProviderId);
}
constructor(
public readonly remoteName: string,
public readonly url: string,
public readonly gitProtocol: Protocol,
) { }
equals(remote: Remote): boolean {
if (this.remoteName !== remote.remoteName) {
return false;
}
if (!this.host.includes(remote.host) && !remote.host.includes(this.host)) {
return false;
}
if (this.owner.toLocaleLowerCase() !== remote.owner.toLocaleLowerCase()) {
return false;
}
if (this.repositoryName.toLocaleLowerCase() !== remote.repositoryName.toLocaleLowerCase()) {
return false;
}
return true;
}
}
export function parseRemote(remoteName: string, url: string, originalProtocol?: Protocol): Remote | null {
if (!url) {
return null;
}
const gitProtocol = new Protocol(url);
if (originalProtocol) {
gitProtocol.update({
type: originalProtocol.type,
});
}
if (gitProtocol.host) {
return new Remote(remoteName, url, gitProtocol);
}
return null;
}
/**
* Resolves git URL aliases by applying insteadOf substitutions from git config.
* For example, if git config has:
* [url "git@github.com:"]
* insteadOf = "gh:"
* Then "gh:user/repo" will be resolved to "git@github.com:user/repo"
*
* @param url The URL to resolve
* @param repository The repository to get config from
* @returns The resolved URL, or the original URL if no substitution found
*/
async function resolveGitUrl(url: string, repository: Repository): Promise<string> {
try {
// Get all git config entries
const configs = await repository.getConfigs();
// Find all url.*.insteadOf entries
const urlSubstitutions: { prefix: string; replacement: string }[] = [];
for (const config of configs) {
// Match patterns like "url.https://github.com/.insteadOf" or "url.git@github.com:.insteadOf"
const match = config.key.match(/^url\.(.+)\.insteadof$/i);
if (match) {
const replacement = match[1];
const prefix = config.value;
urlSubstitutions.push({ prefix, replacement });
}
}
// Sort by prefix length (longest first) to handle overlapping prefixes correctly
urlSubstitutions.sort((a, b) => b.prefix.length - a.prefix.length);
// Apply the first matching substitution
for (const { prefix, replacement } of urlSubstitutions) {
if (url.startsWith(prefix)) {
const resolvedUrl = replacement + url.substring(prefix.length);
Logger.appendLine(`Resolved git URL alias: "${url}" -> "${resolvedUrl}"`, 'Remote');
return resolvedUrl;
}
}
} catch (error) {
Logger.error(`Failed to resolve git URL aliases for "${url}": ${error}`, 'Remote');
}
// No substitution found or error occurred, return original URL
return url;
}
export function parseRepositoryRemotes(repository: Repository): Remote[] {
const remotes: Remote[] = [];
for (const r of repository.state.remotes) {
const urls: string[] = [];
if (r.fetchUrl) {
urls.push(r.fetchUrl);
}
if (r.pushUrl && r.pushUrl !== r.fetchUrl) {
urls.push(r.pushUrl);
}
urls.forEach(url => {
const remote = parseRemote(r.name, url);
if (remote) {
remotes.push(remote);
}
});
}
return remotes;
}
/**
* Asynchronously parses repository remotes with git URL alias resolution.
* This version resolves git URL aliases (e.g., "gh:" -> "git@github.com:") before parsing.
* Use this version when you need accurate remote parsing with alias resolution.
*
* @param repository The repository to parse remotes from
* @returns Promise resolving to array of Remote objects
*/
export async function parseRepositoryRemotesAsync(repository: Repository): Promise<Remote[]> {
const remotes: Remote[] = [];
for (const r of repository.state.remotes) {
const urls: string[] = [];
if (r.fetchUrl) {
// Resolve git URL aliases before parsing
const resolvedUrl = await resolveGitUrl(r.fetchUrl, repository);
urls.push(resolvedUrl);
}
if (r.pushUrl && r.pushUrl !== r.fetchUrl) {
// Resolve git URL aliases before parsing
const resolvedUrl = await resolveGitUrl(r.pushUrl, repository);
urls.push(resolvedUrl);
}
urls.forEach(url => {
const remote = parseRemote(r.name, url);
if (remote) {
remotes.push(remote);
}
});
}
return remotes;
}
export class GitHubRemote extends Remote {
static remoteAsGitHub(remote: Remote, githubServerType: GitHubServerType): GitHubRemote {
return new GitHubRemote(remote.remoteName, remote.url, remote.gitProtocol, githubServerType);
}
constructor(
remoteName: string,
url: string,
gitProtocol: Protocol,
public readonly githubServerType: GitHubServerType
) {
super(remoteName, url, gitProtocol);
}
}