Skip to content

Commit 7ccbd27

Browse files
authored
Merge pull request #1802 from vladak/message_get_repos
Message get repos
2 parents 168050b + d683b7d commit 7ccbd27

File tree

4 files changed

+88
-9
lines changed

4 files changed

+88
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ Currently supported message types:
139139
* **delete** – removes project(s) and its repositores from the configuration.
140140
Also deletes its data under data root (but not the source code).
141141
* **indexed** – mark the project(s) as indexed so it becomes visible in the UI
142+
* **get-repos** – get list of repositories in the form of relative paths to source root for given project(s)
143+
* **get-repos-type** – get repository type(s) for given project(s)
142144

143145
## 4. OpenGrok install
144146

src/org/opensolaris/opengrok/configuration/messages/ProjectMessage.java

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@
2626
import java.io.IOException;
2727
import java.nio.file.Paths;
2828
import java.util.ArrayList;
29+
import java.util.Arrays;
2930
import java.util.List;
3031
import java.util.Map;
3132
import java.util.Set;
3233
import java.util.TreeSet;
3334
import java.util.logging.Level;
3435
import java.util.logging.Logger;
35-
import java.util.stream.Collector;
3636
import java.util.stream.Collectors;
3737
import org.opensolaris.opengrok.configuration.Group;
3838
import org.opensolaris.opengrok.configuration.Project;
@@ -44,7 +44,6 @@
4444
import org.opensolaris.opengrok.index.IndexDatabase;
4545
import org.opensolaris.opengrok.logger.LoggerFactory;
4646
import org.opensolaris.opengrok.util.IOUtils;
47-
import org.opensolaris.opengrok.web.ProjectHelper;
4847

4948

5049
/**
@@ -257,6 +256,42 @@ protected byte[] applyMessage(RuntimeEnvironment env) throws Exception {
257256
case "list-indexed":
258257
return (env.getProjectList().stream().filter(p -> p.isIndexed()).
259258
map(p -> p.getName()).collect(Collectors.joining("\n")).getBytes());
259+
case "get-repos":
260+
List<String> repos = new ArrayList<>();
261+
262+
for (String projectName : getTags()) {
263+
Project project;
264+
if ((project = env.getProjects().get(projectName)) == null) {
265+
continue;
266+
}
267+
List<RepositoryInfo> infos = env.getProjectRepositoriesMap().
268+
get(project);
269+
if (infos != null) {
270+
repos.addAll(infos.stream().
271+
map(ri -> ri.getDirectoryNameRelative()).
272+
collect(Collectors.toList()));
273+
}
274+
}
275+
276+
return repos.stream().collect(Collectors.joining("\n")).getBytes();
277+
case "get-repos-type":
278+
Set<String> types = new TreeSet<>();
279+
280+
for (String projectName : getTags()) {
281+
Project project;
282+
if ((project = env.getProjects().get(projectName)) == null) {
283+
continue;
284+
}
285+
List<RepositoryInfo> infos = env.getProjectRepositoriesMap().
286+
get(project);
287+
if (infos != null) {
288+
types.addAll(infos.stream().
289+
map(ri -> ri.getType()).
290+
collect(Collectors.toList()));
291+
}
292+
}
293+
294+
return types.stream().collect(Collectors.joining("\n")).getBytes();
260295
}
261296

262297
return ("command \"" + getText() + "\" for projects " +
@@ -271,20 +306,20 @@ protected byte[] applyMessage(RuntimeEnvironment env) throws Exception {
271306
@Override
272307
public void validate() throws Exception {
273308
String command = getText();
309+
Set<String> allowedText = new TreeSet<>(Arrays.asList("add", "delete",
310+
"list", "list-indexed", "indexed", "get-repos",
311+
"get-repos-type"));
274312

275313
// Text field carries the command.
276314
if (command == null) {
277315
throw new Exception("The message must contain a text - \"add\", \"delete\" or \"indexed\"");
278316
}
279-
if (command.compareTo("add") != 0 &&
280-
command.compareTo("delete") != 0 &&
281-
command.compareTo("list") != 0 &&
282-
command.compareTo("list-indexed") != 0 &&
283-
command.compareTo("indexed") != 0) {
284-
throw new Exception("The message must contain either 'add', 'delete' or 'indexed' text");
317+
if (!allowedText.contains(command)) {
318+
throw new Exception("The message must contain either 'add', " +
319+
"'delete', 'indexed', 'list', 'list-indexed' or 'get-repos' text");
285320
}
286321

287-
if (!command.contains("list") && getTags().isEmpty()) {
322+
if (!command.startsWith("list") && getTags().isEmpty()) {
288323
throw new Exception("The message must contain a tag (project name(s))");
289324
}
290325

test/org/opensolaris/opengrok/configuration/messages/ProjectMessageTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,4 +391,44 @@ public void testList() throws Exception {
391391
Assert.assertTrue(out.contains("mercurial"));
392392
Assert.assertFalse(out.contains("git"));
393393
}
394+
395+
@Test
396+
public void testGetRepos() throws Exception {
397+
// Try to get repos for non-existent project first.
398+
Message m = new ProjectMessage();
399+
m.setText("get-repos");
400+
m.addTag("totally-nonexistent-project");
401+
String out = new String(m.apply(env));
402+
Assert.assertEquals("", out);
403+
404+
// Create subrepository.
405+
File mercurialRoot = new File(repository.getSourceRoot() + File.separator + "mercurial");
406+
MercurialRepositoryTest.runHgCommand(mercurialRoot,
407+
"clone", mercurialRoot.getAbsolutePath(),
408+
mercurialRoot.getAbsolutePath() + File.separator + "closed");
409+
410+
// Add a project
411+
m = new ProjectMessage();
412+
m.setText("add");
413+
m.addTag("mercurial");
414+
m.apply(env);
415+
416+
// Get repositories of the project.
417+
m.setText("get-repos");
418+
out = new String(m.apply(env));
419+
420+
// Perform cleanup of the subrepository in order not to interefere
421+
// with other tests.
422+
removeRecursive(new File(mercurialRoot.getAbsolutePath() +
423+
File.separator + "closed").toPath());
424+
425+
// test
426+
Assert.assertEquals("/mercurial\n/mercurial/closed", out);
427+
428+
// Test the types. There should be only one type for project with
429+
// multiple nested Mercurial repositories.
430+
m.setText("get-repos-type");
431+
out = new String(m.apply(env));
432+
Assert.assertEquals("Mercurial", out);
433+
}
394434
}

tools/Messages

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ Usage()
7575
echo " - \"indexed\" - mark the project(s) as indexed so it becomes visible in the UI"
7676
echo " - \"list\" - list all projects (both indexed and not indexed)"
7777
echo " - \"list-indexed\" - list indexed projects"
78+
echo " - \"get-repos\" - get list of repositories in the form of relative paths to source root for given project(s)"
79+
echo " - \"get-repos-type\" - get repository type(s) for given project(s)"
7880
echo " normal:"
7981
echo " - assign a <text> to the main page or a project"
8082
echo " - can be more precise with <tags> (for specific project)"

0 commit comments

Comments
 (0)