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
|
// Copyright (C) 2011 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.util;
import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.SubmoduleSubscription;
import com.google.gerrit.server.git.GitRepositoryManager;
import org.eclipse.jgit.lib.BlobBasedConfig;
import org.eclipse.jgit.lib.Constants;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
/**
* It parses from a configuration file submodule sections.
* <p>
* Example of submodule sections:
*
* <pre>
* [submodule "project-a"]
* url = http://localhost/a
* path = a
* branch = .
*
* [submodule "project-b"]
* url = http://localhost/b
* path = b
* branch = refs/heads/test
* </pre>
*/
public class SubmoduleSectionParser {
private final BlobBasedConfig bbc;
private final String thisServer;
private final Branch.NameKey superProjectBranch;
private final GitRepositoryManager repoManager;
public SubmoduleSectionParser(final BlobBasedConfig bbc,
final String thisServer, final Branch.NameKey superProjectBranch,
final GitRepositoryManager repoManager) {
this.bbc = bbc;
this.thisServer = thisServer;
this.superProjectBranch = superProjectBranch;
this.repoManager = repoManager;
}
public List<SubmoduleSubscription> parseAllSections() {
List<SubmoduleSubscription> parsedSubscriptions =
new ArrayList<SubmoduleSubscription>();
for (final String id : bbc.getSubsections("submodule")) {
final SubmoduleSubscription subscription = parse(id);
if (subscription != null) {
parsedSubscriptions.add(subscription);
}
}
return parsedSubscriptions;
}
private SubmoduleSubscription parse(final String id) {
final String url = bbc.getString("submodule", id, "url");
final String path = bbc.getString("submodule", id, "path");
String branch = bbc.getString("submodule", id, "branch");
try {
if (url != null && url.length() > 0 && path != null && path.length() > 0
&& branch != null && branch.length() > 0) {
// All required fields filled.
boolean urlIsRelative = url.startsWith("/");
String server = null;
if (!urlIsRelative) {
// It is actually an URI. It could be ssh://localhost/project-a.
server = new URI(url).getHost();
}
if ((urlIsRelative)
|| (server != null && server.equalsIgnoreCase(thisServer))) {
// Subscription really related to this running server.
if (branch.equals(".")) {
branch = superProjectBranch.get();
} else if (!branch.startsWith(Constants.R_REFS)) {
branch = Constants.R_HEADS + branch;
}
final String urlExtractedPath = new URI(url).getPath();
String projectName = urlExtractedPath;
int fromIndex = urlExtractedPath.length() - 1;
while (fromIndex > 0) {
fromIndex = urlExtractedPath.lastIndexOf('/', fromIndex - 1);
projectName = urlExtractedPath.substring(fromIndex + 1);
if (projectName.endsWith(".git")) {
projectName = projectName.substring(0, projectName.length() - 4);
}
if (repoManager.list().contains(new Project.NameKey(projectName))) {
return new SubmoduleSubscription(
superProjectBranch,
new Branch.NameKey(new Project.NameKey(projectName), branch),
path);
}
}
}
}
} catch (URISyntaxException e) {
// Error in url syntax (in fact it is uri syntax)
}
return null;
}
}
|