Skip to content

Commit 6574eab

Browse files
committed
refresh current version of project's repositories upon "indexed" message
fixes #1738
1 parent 81784be commit 6574eab

File tree

6 files changed

+75
-24
lines changed

6 files changed

+75
-24
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.opensolaris.opengrok.configuration.Project;
3535
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
3636
import org.opensolaris.opengrok.history.HistoryGuru;
37+
import org.opensolaris.opengrok.history.Repository;
38+
import static org.opensolaris.opengrok.history.RepositoryFactory.getRepository;
3739
import org.opensolaris.opengrok.history.RepositoryInfo;
3840
import org.opensolaris.opengrok.index.IndexDatabase;
3941
import org.opensolaris.opengrok.logger.LoggerFactory;
@@ -204,6 +206,19 @@ protected byte[] applyMessage(RuntimeEnvironment env) throws Exception {
204206
Project project;
205207
if ((project = env.getProjects().get(projectName)) != null) {
206208
project.setIndexed(true);
209+
210+
// Refresh current version of the project's repositories.
211+
for (RepositoryInfo ri : env.getProjectRepositoriesMap().get(project)) {
212+
Repository repo = getRepository(ri);
213+
214+
if (repo != null && repo.getCurrentVersion() != null &&
215+
repo.getCurrentVersion().length() > 0) {
216+
// getRepository() always creates fresh instance
217+
// of the Repository object so there is no need
218+
// to call setCurrentVersion() on it.
219+
ri.setCurrentVersion(repo.determineCurrentVersion());
220+
}
221+
}
207222
} else {
208223
LOGGER.log(Level.WARNING, "cannot find project " +
209224
projectName + " to mark as indexed");

src/org/opensolaris/opengrok/history/GitRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,7 @@ String determineBranch() throws IOException {
707707
private static final SimpleDateFormat outputDateFormat = new SimpleDateFormat("YYYY-MM-dd HH:mm");
708708

709709
@Override
710-
String determineCurrentVersion() throws IOException {
710+
public String determineCurrentVersion() throws IOException {
711711
File directory = new File(directoryName);
712712
List<String> cmd = new ArrayList<>();
713713
// The delimiter must not be contained in the date format emitted by

src/org/opensolaris/opengrok/history/MercurialRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ String determineParent() throws IOException {
702702
}
703703

704704
@Override
705-
String determineCurrentVersion() throws IOException {
705+
public String determineCurrentVersion() throws IOException {
706706
String line = null;
707707
File directory = new File(directoryName);
708708

src/org/opensolaris/opengrok/history/Repository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ public List<String> getIgnoredDirs() {
400400
*
401401
* @return the version
402402
*/
403-
String determineCurrentVersion() throws IOException {
403+
public String determineCurrentVersion() throws IOException {
404404
return null;
405405
}
406406

src/org/opensolaris/opengrok/history/RepositoryFactory.java

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,55 +89,54 @@ public static List<Class<? extends Repository>> getRepositoryClasses() {
8989
*/
9090
public static Repository getRepository(File file) throws InstantiationException, IllegalAccessException {
9191
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
92-
Repository res = null;
92+
Repository repo = null;
9393
for (Repository rep : repositories) {
9494
if (rep.isRepositoryFor(file)) {
95-
res = rep.getClass().newInstance();
95+
repo = rep.getClass().newInstance();
9696
try {
97-
res.setDirectoryName(file.getCanonicalPath());
97+
repo.setDirectoryName(file.getCanonicalPath());
9898
} catch (IOException e) {
9999
LOGGER.log(Level.SEVERE,
100100
"Failed to get canonical path name for "
101101
+ file.getAbsolutePath(), e);
102102
}
103103

104-
if (!res.isWorking()) {
105-
LOGGER.log(
106-
Level.WARNING,
104+
if (!repo.isWorking()) {
105+
LOGGER.log(Level.WARNING,
107106
"{0} not working (missing binaries?): {1}",
108107
new Object[]{
109-
res.getClass().getSimpleName(),
108+
repo.getClass().getSimpleName(),
110109
file.getPath()
111110
});
112111
}
113112

114-
if (res.getType() == null || res.getType().length() == 0) {
115-
res.setType(res.getClass().getSimpleName());
113+
if (repo.getType() == null || repo.getType().length() == 0) {
114+
repo.setType(repo.getClass().getSimpleName());
116115
}
117116

118-
if (res.getParent() == null || res.getParent().length() == 0) {
117+
if (repo.getParent() == null || repo.getParent().length() == 0) {
119118
try {
120-
res.setParent(res.determineParent());
119+
repo.setParent(repo.determineParent());
121120
} catch (IOException ex) {
122121
LOGGER.log(Level.WARNING,
123122
"Failed to get parent for {0}: {1}",
124123
new Object[]{file.getAbsolutePath(), ex});
125124
}
126125
}
127126

128-
if (res.getBranch() == null || res.getBranch().length() == 0) {
127+
if (repo.getBranch() == null || repo.getBranch().length() == 0) {
129128
try {
130-
res.setBranch(res.determineBranch());
129+
repo.setBranch(repo.determineBranch());
131130
} catch (IOException ex) {
132131
LOGGER.log(Level.WARNING,
133132
"Failed to get branch for {0}: {1}",
134133
new Object[]{file.getAbsolutePath(), ex});
135134
}
136135
}
137136

138-
if (res.getCurrentVersion() == null || res.getCurrentVersion().length() == 0) {
137+
if (repo.getCurrentVersion() == null || repo.getCurrentVersion().length() == 0) {
139138
try {
140-
res.setCurrentVersion(res.determineCurrentVersion());
139+
repo.setCurrentVersion(repo.determineCurrentVersion());
141140
} catch (IOException ex) {
142141
LOGGER.log(Level.WARNING,
143142
"Failed to determineCurrentVersion for {0}: {1}",
@@ -147,14 +146,14 @@ public static Repository getRepository(File file) throws InstantiationException,
147146

148147
// If this repository displays tags only for files changed by tagged
149148
// revision, we need to prepare list of all tags in advance.
150-
if (env.isTagsEnabled() && res.hasFileBasedTags()) {
151-
res.buildTagList(file);
149+
if (env.isTagsEnabled() && repo.hasFileBasedTags()) {
150+
repo.buildTagList(file);
152151
}
153152

154153
break;
155154
}
156155
}
157-
return res;
156+
return repo;
158157
}
159158

160159
/**

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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import java.io.File;
2626
import java.io.IOException;
2727
import java.util.ArrayList;
28+
import java.util.List;
2829
import java.util.TreeSet;
2930
import java.util.concurrent.ConcurrentHashMap;
3031
import org.junit.After;
@@ -162,6 +163,10 @@ public void testAdd() throws Exception {
162163
Assert.assertTrue(env.getProjects().containsKey("svn"));
163164
}
164165

166+
/**
167+
* Test that if the "add" message is applied on already existing project,
168+
* the repository list is refreshed.
169+
*/
165170
@Test
166171
public void testRepositoryRefresh() throws Exception {
167172
Message m = new ProjectMessage();
@@ -261,13 +266,45 @@ public void testDelete() throws Exception {
261266

262267
@Test
263268
public void testIndexed() throws Exception {
269+
String projectName = "mercurial";
270+
271+
// When a project is added, it should be marked as not indexed.
264272
Message m = new ProjectMessage();
265273
m.setText("add");
266-
m.addTag("mercurial");
274+
m.addTag(projectName);
267275
m.apply(env);
268-
Assert.assertFalse(env.getProjects().get("mercurial").isIndexed());
276+
Assert.assertFalse(env.getProjects().get(projectName).isIndexed());
277+
278+
// Get repository info for the project.
279+
Project project = env.getProjects().get(projectName);
280+
Assert.assertNotNull(project);
281+
List<RepositoryInfo> riList = env.getProjectRepositoriesMap().get(project);
282+
Assert.assertNotNull(riList);
283+
Assert.assertEquals("there should be just 1 repository", 1, riList.size());
284+
RepositoryInfo ri = riList.get(0);
285+
Assert.assertNotNull(ri);
286+
Assert.assertTrue(ri.getCurrentVersion().contains("8b340409b3a8"));
287+
288+
// Add some changes to the repository.
289+
File mercurialRoot = new File(repository.getSourceRoot() + File.separator + "mercurial");
290+
MercurialRepositoryTest.runHgCommand(mercurialRoot,
291+
"import", HistoryGuru.getInstance().getClass().
292+
getResource("hg-export-subdir.txt").getPath());
293+
294+
// Test that the project's indexed flag becomes true only after
295+
// the message is applied.
269296
m.setText("indexed");
270297
m.apply(env);
271-
Assert.assertTrue(env.getProjects().get("mercurial").isIndexed());
298+
Assert.assertTrue("indexed flag should be set to true",
299+
env.getProjects().get(projectName).isIndexed());
300+
301+
// Test that the "indexed" message triggers refresh of current version
302+
// info in related repositories.
303+
riList = env.getProjectRepositoriesMap().get(project);
304+
Assert.assertNotNull(riList);
305+
ri = riList.get(0);
306+
Assert.assertNotNull(ri);
307+
Assert.assertTrue("current version should be refreshed",
308+
ri.getCurrentVersion().contains("c78fa757c524"));
272309
}
273310
}

0 commit comments

Comments
 (0)