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
106 lines (93 loc) · 2.73 KB
/
Copy pathremote.ts
File metadata and controls
106 lines (93 loc) · 2.73 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { Repository } from '../api/api';
import { getEnterpriseUri } from '../github/utils';
import { AuthProvider, GitHubServerType } from './authentication';
import { Protocol } from './protocol';
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['github-enterprise'] : AuthProvider.github;
}
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 !== remote.host) {
return false;
}
if (this.owner !== remote.owner) {
return false;
}
if (this.repositoryName !== remote.repositoryName) {
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;
}
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;
}
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);
}
}