Skip to content

Commit 18b8fa0

Browse files
author
Vladimir Kotal
committed
add repository Message
fixes #1969
1 parent 17a4c34 commit 18b8fa0

File tree

6 files changed

+252
-6
lines changed

6 files changed

+252
-6
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* CDDL HEADER END
1818
*/
1919

20-
/*
20+
/*
2121
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opensolaris.opengrok.configuration.messages;

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747

4848

4949
/**
50+
* project specific message
51+
*
5052
* @author Vladimir Kotal
5153
*/
5254
public class ProjectMessage extends Message {
@@ -105,7 +107,7 @@ protected byte[] applyMessage(RuntimeEnvironment env) throws Exception {
105107

106108
validateMore(env);
107109

108-
switch (getText()) {
110+
switch (command) {
109111
case "add":
110112
for (String projectName : getTags()) {
111113
File srcRoot = env.getSourceRootFile();
@@ -304,11 +306,10 @@ public void validate() throws Exception {
304306

305307
// Text field carries the command.
306308
if (command == null) {
307-
throw new Exception("The message must contain a text - \"add\", \"delete\" or \"indexed\"");
309+
throw new Exception("The message text must contain one of '" + allowedText.toString() + "'");
308310
}
309311
if (!allowedText.contains(command)) {
310-
throw new Exception("The message must contain either 'add', " +
311-
"'delete', 'indexed', 'list', 'list-indexed' or 'get-repos' text");
312+
throw new Exception("The message text must contain one of '" + allowedText.toString() + "'");
312313
}
313314

314315
if (!command.startsWith("list") && getTags().isEmpty()) {
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opensolaris.opengrok.configuration.messages;
24+
25+
import java.util.ArrayList;
26+
import java.util.Arrays;
27+
import java.util.List;
28+
import java.util.Set;
29+
import java.util.TreeSet;
30+
import java.util.stream.Collectors;
31+
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
32+
import org.opensolaris.opengrok.history.RepositoryInfo;
33+
34+
/**
35+
* repository specific message
36+
*
37+
* @author Vladimir Kotal
38+
*/
39+
public class RepositoryMessage extends Message {
40+
41+
@Override
42+
protected byte[] applyMessage(RuntimeEnvironment env) throws Exception {
43+
String ret = null;
44+
String msgtext = getText();
45+
46+
switch (msgtext) {
47+
case "get-repo-type":
48+
List<String> types = new ArrayList<>();
49+
50+
for (String tag: getTags()) {
51+
boolean found = false;
52+
for (RepositoryInfo ri : env.getRepositories()) {
53+
if (ri.getDirectoryNameRelative().equals(tag)) {
54+
types.add(ri.getType());
55+
found = true;
56+
break;
57+
}
58+
}
59+
if (!found) {
60+
types.add("N/A");
61+
}
62+
}
63+
ret = types.stream().collect(Collectors.joining("\n")).toString();
64+
break;
65+
}
66+
67+
return ret.getBytes();
68+
}
69+
70+
/**
71+
* Validate the message.
72+
* Tag is repository path, text is command.
73+
* @throws Exception
74+
*/
75+
@Override
76+
public void validate() throws Exception {
77+
String command = getText();
78+
Set<String> allowedText = new TreeSet<>(Arrays.asList("get-repo-type"));
79+
80+
// The text field carries the command.
81+
if (command == null) {
82+
throw new Exception("The message text must contain one of '" + allowedText.toString() + "'");
83+
}
84+
if (!allowedText.contains(command)) {
85+
throw new Exception("The message text must contain one of '" + allowedText.toString() + "'");
86+
}
87+
88+
if (getTags().size() == 0) {
89+
throw new Exception("All repository messages must have at least one tag");
90+
}
91+
92+
super.validate();
93+
}
94+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* CDDL HEADER END
1818
*/
1919

20-
/*
20+
/*
2121
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, Chris Fraire <[email protected]>.
2323
*/
@@ -37,6 +37,8 @@
3737
import org.junit.Assume;
3838
import org.junit.Before;
3939
import org.junit.Test;
40+
import org.opensolaris.opengrok.condition.ConditionalRun;
41+
import org.opensolaris.opengrok.condition.RepositoryInstalled;
4042
import org.opensolaris.opengrok.configuration.Group;
4143
import org.opensolaris.opengrok.configuration.Project;
4244
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
@@ -57,6 +59,7 @@
5759
*
5860
* @author Vladimir Kotal
5961
*/
62+
@ConditionalRun(condition = RepositoryInstalled.MercurialInstalled.class)
6063
public class ProjectMessageTest {
6164

6265
RuntimeEnvironment env;
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opensolaris.opengrok.configuration.messages;
24+
25+
import java.io.File;
26+
import java.io.IOException;
27+
import java.util.ArrayList;
28+
import java.util.concurrent.ConcurrentHashMap;
29+
import org.junit.After;
30+
import org.junit.Assert;
31+
import org.junit.Assume;
32+
import org.junit.Before;
33+
import org.junit.Test;
34+
import org.opensolaris.opengrok.condition.ConditionalRun;
35+
import org.opensolaris.opengrok.condition.RepositoryInstalled;
36+
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
37+
import org.opensolaris.opengrok.history.GitRepository;
38+
import org.opensolaris.opengrok.history.HistoryGuru;
39+
import org.opensolaris.opengrok.history.MercurialRepository;
40+
import org.opensolaris.opengrok.history.MercurialRepositoryTest;
41+
import org.opensolaris.opengrok.history.RepositoryFactory;
42+
import org.opensolaris.opengrok.history.RepositoryInfo;
43+
import org.opensolaris.opengrok.history.SubversionRepository;
44+
import org.opensolaris.opengrok.index.Indexer;
45+
import org.opensolaris.opengrok.util.TestRepository;
46+
47+
/**
48+
* Test repository message handling.
49+
*
50+
* @author Vladimir Kotal
51+
*/
52+
public class RepositoryMessageTest {
53+
54+
RuntimeEnvironment env;
55+
56+
private static TestRepository repository = new TestRepository();
57+
58+
@Before
59+
public void setUp() throws IOException {
60+
Assume.assumeTrue(new MercurialRepository().isWorking());
61+
Assume.assumeTrue(new SubversionRepository().isWorking());
62+
Assume.assumeTrue(new GitRepository().isWorking());
63+
64+
repository = new TestRepository();
65+
repository.create(HistoryGuru.class.getResourceAsStream(
66+
"repositories.zip"));
67+
68+
env = RuntimeEnvironment.getInstance();
69+
env.removeAllMessages();
70+
env.setSourceRoot(repository.getSourceRoot());
71+
env.setDataRoot(repository.getDataRoot());
72+
env.setProjectsEnabled(true);
73+
RepositoryFactory.initializeIgnoredNames(env);
74+
}
75+
76+
@After
77+
public void tearDown() {
78+
if (env != null) {
79+
env.removeAllMessages();
80+
81+
// This should match Configuration constructor.
82+
env.setProjects(new ConcurrentHashMap<>());
83+
env.setRepositories(new ArrayList<RepositoryInfo>());
84+
env.getProjectRepositoriesMap().clear();
85+
}
86+
87+
repository.destroy();
88+
}
89+
90+
@Test
91+
public void testValidate() {
92+
Message m = new RepositoryMessage();
93+
Assert.assertFalse(MessageTest.assertValid(m));
94+
m.addTag("foo");
95+
Assert.assertFalse(MessageTest.assertValid(m));
96+
m.setText("text");
97+
Assert.assertFalse(MessageTest.assertValid(m));
98+
m.setText(null);
99+
Assert.assertFalse(MessageTest.assertValid(m));
100+
m.setText("get-repo-type");
101+
Assert.assertTrue(MessageTest.assertValid(m));
102+
}
103+
104+
@ConditionalRun(condition = RepositoryInstalled.MercurialInstalled.class)
105+
@Test
106+
public void testGetRepositoryType() throws Exception {
107+
Message m = new RepositoryMessage();
108+
m.setText("get-repo-type");
109+
m.addTag("/totally-nonexistent-repository");
110+
String out = new String(m.apply(env));
111+
Assert.assertEquals("N/A", out);
112+
113+
// Create subrepository.
114+
File mercurialRoot = new File(repository.getSourceRoot() + File.separator + "mercurial");
115+
MercurialRepositoryTest.runHgCommand(mercurialRoot,
116+
"clone", mercurialRoot.getAbsolutePath(),
117+
mercurialRoot.getAbsolutePath() + File.separator + "closed");
118+
119+
env.setHistoryEnabled(true);
120+
Indexer.getInstance().prepareIndexer(
121+
env,
122+
true, // search for repositories
123+
true, // scan and add projects
124+
null, // no default project
125+
false, // don't list files
126+
false, // don't create dictionary
127+
null, // subFiles - needed when refreshing history partially
128+
null, // repositories - needed when refreshing history partially
129+
new ArrayList<>(), // don't zap cache
130+
false); // don't list repos
131+
132+
m = new RepositoryMessage();
133+
m.setText("get-repo-type");
134+
m.addTag("/mercurial");
135+
out = new String(m.apply(env));
136+
Assert.assertEquals("Mercurial", out);
137+
138+
m = new RepositoryMessage();
139+
m.setText("get-repo-type");
140+
m.addTag("/mercurial/closed");
141+
out = new String(m.apply(env));
142+
Assert.assertEquals("Mercurial", out);
143+
}
144+
}

tools/Messages

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ Usage()
100100
echo " - \"list-indexed\" - list indexed projects"
101101
echo " - \"get-repos\" - get list of repositories in the form of relative paths to source root for given project(s)"
102102
echo " - \"get-repos-type\" - get repository type(s) for given project(s)"
103+
echo " repository:"
104+
echo " - repository paths relative to source root are specified as <tags>"
105+
echo " - command is specified in message <text>:"
106+
echo " - \"get-repo-type\" - returns type of the repository or \"N/A\""
103107
echo " normal:"
104108
echo " - assign a <text> to the main page or a project"
105109
echo " - can be more precise with <tags> (for specific project)"

0 commit comments

Comments
 (0)