Repository navigation
Expand file tree
/
Copy pathsubmodule.ts
More file actions
106 lines (87 loc) · 2.53 KB
/
Copy pathsubmodule.ts
File metadata and controls
106 lines (87 loc) · 2.53 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
/**
* @file git submodule util
* @author netcon
*/
import { FileSystemError, Uri } from 'vscode';
// the code below is come from https://github.com/microsoft/vscode/blob/1.52.1/extensions/git/src/git.ts#L661
export interface Submodule {
name: string;
path: string;
url: string;
}
// the code below is come from https://github.com/microsoft/vscode/blob/1.52.1/extensions/git/src/git.ts#L667
export const parseGitmodules = (raw: string): Submodule[] => {
const regex = /\r?\n/g;
let position = 0;
let match: RegExpExecArray | null = null;
const result: Submodule[] = [];
let submodule: Partial<Submodule> = {};
const parseLine = (line: string): void => {
const sectionMatch = /^\s*\[submodule "([^"]+)"\]\s*$/.exec(line);
if (sectionMatch) {
if (submodule.name && submodule.path && submodule.url) {
result.push(submodule as Submodule);
}
const name = sectionMatch[1];
if (name) {
submodule = { name };
return;
}
}
if (!submodule) {
return;
}
const propertyMatch = /^\s*(\w+)\s*=\s*(.*)$/.exec(line);
if (!propertyMatch) {
return;
}
const [, key, value] = propertyMatch;
switch (key) {
case 'path':
submodule.path = value;
break;
case 'url':
submodule.url = value;
break;
}
};
while ((match = regex.exec(raw))) {
parseLine(raw.substring(position, match.index));
position = match.index + match[0].length;
}
parseLine(raw.substring(position));
if (submodule.name && submodule.path && submodule.url) {
result.push(submodule as Submodule);
}
return result;
};
export const parseSubmoduleUrl = async (url: string) => {
try {
let host = '';
let path = '';
// the url is looks like git@github.com:xcv58/rc_config_files.git
if (url.startsWith('git@')) {
const colonIndex = url.indexOf(':');
host = url.slice(0, colonIndex);
path = url.slice(colonIndex + 1);
} else {
const submoduleUri = Uri.parse(url);
host = submoduleUri.authority;
path = submoduleUri.path;
}
let subScheme = 'github1s';
if (/\bgithub\.com/i.test(host)) {
subScheme = 'github1s';
} else if (/\bgitlab\.com/i.test(host)) {
subScheme = 'gitlab1s';
} else if (/\bbitbucket\.org/i.test(host)) {
subScheme = 'bitbucket1s';
} else {
throw FileSystemError.Unavailable('only github submodules are supported now');
}
const subRepoPath = path.endsWith('.git') ? path.slice(0, -4) : path;
return [subScheme, subRepoPath.split('/').filter(Boolean).join('/')];
} catch (e) {
throw FileSystemError.Unavailable('Can not found valid submodule declare');
}
};