forked from microsoft/vscode-pull-request-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.ts
More file actions
156 lines (130 loc) · 3.9 KB
/
Copy pathprotocol.ts
File metadata and controls
156 lines (130 loc) · 3.9 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
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as vscode from 'vscode';
import Logger from './logger';
export enum ProtocolType {
Local,
HTTP,
SSH,
GIT,
OTHER
}
const sshProtocolRegex = /^([^@:]+@)?([^:]+):(.+)$/;
export class Protocol {
public type: ProtocolType = ProtocolType.OTHER;
public host: string = '';
public owner: string = '';
public repositoryName: string = '';
public get nameWithOwner(): string {
return this.owner ? `${this.owner}/${this.repositoryName}` : this.repositoryName;
}
public readonly url: vscode.Uri;
constructor(
uriString: string
) {
if (uriString.indexOf('://') === -1) {
if (sshProtocolRegex.test(uriString)) {
this.parseSshProtocol(uriString);
return;
}
}
try {
this.url = vscode.Uri.parse(uriString);
this.type = this.getType(this.url.scheme);
if (this.type === ProtocolType.SSH) {
const urlWithoutScheme = this.url.authority + this.url.path;
if (sshProtocolRegex.test(urlWithoutScheme)) {
this.parseSshProtocol(urlWithoutScheme);
return;
}
}
this.host = this.getHostName(this.url.authority);
if (this.host) {
this.repositoryName = this.getRepositoryName(this.url.path);
this.owner = this.getOwnerName(this.url.path);
}
} catch (e) {
Logger.appendLine(`Failed to parse '${uriString}'`);
vscode.window.showWarningMessage(`Unable to parse remote '${uriString}'. Please check that it is correctly formatted.`);
}
}
private getType(scheme: string): ProtocolType {
switch (scheme) {
case 'file':
return ProtocolType.Local;
case 'http':
case 'https':
return ProtocolType.HTTP;
case 'git':
return ProtocolType.GIT;
case 'ssh':
return ProtocolType.SSH;
default:
return ProtocolType.OTHER;
}
}
private parseSshProtocol(uriString: string): void {
const result = uriString.match(sshProtocolRegex);
if (result) {
this.host = result[2];
const path = result[3];
this.owner = this.getOwnerName(path);
this.repositoryName = this.getRepositoryName(path);
this.type = ProtocolType.SSH;
return;
}
}
getHostName(authority: string) {
// <username>:<password>@<authority>:<port>
let matches = /^(?:.*:?@)?([^:]*)(?::.*)?$/.exec(authority);
if (matches && matches.length >= 2) {
return matches[1];
}
return '';
}
getRepositoryName(path: string) {
let normalized = path.replace('\\', '/');
if (normalized.endsWith('/')) {
normalized = normalized.substr(0, normalized.length - 1);
}
let lastIndex = normalized.lastIndexOf('/');
let lastSegment = normalized.substr(lastIndex + 1);
if (lastSegment === '' || lastSegment === '/') {
return null;
}
return lastSegment.replace(/\/$/, '').replace(/\.git$/, '');
}
getOwnerName(path: string) {
let normalized = path.replace('\\', '/');
if (normalized.endsWith('/')) {
normalized = normalized.substr(0, normalized.length - 1);
}
let fragments = normalized.split('/');
if (fragments.length > 1) {
return fragments[fragments.length - 2];
}
return null;
}
normalizeUri(): vscode.Uri {
if (this.type === ProtocolType.OTHER && !this.url) {
return null;
}
if (this.type === ProtocolType.Local) {
return this.url;
}
let scheme = 'https';
if (this.url && (this.url.scheme === 'http' || this.url.scheme === 'https')) {
scheme = this.url.scheme;
}
try {
return vscode.Uri.parse(`${scheme}://${this.host.toLocaleLowerCase()}/${this.nameWithOwner.toLocaleLowerCase()}`);
} catch (e) {
return null;
}
}
equals(other: Protocol) {
return this.normalizeUri().toString().toLocaleLowerCase() === other.normalizeUri().toString().toLocaleLowerCase();
}
}