Skip to content

Commit 32c956e

Browse files
author
Vladimir Kotal
authored
add sub-repositories of the same type as parent (#2817)
fixes #1461
1 parent dd0185f commit 32c956e

File tree

4 files changed

+69
-17
lines changed

4 files changed

+69
-17
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/history/HistoryGuru.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -387,12 +387,13 @@ public Map<String, Date> getLastModifiedTimes(File directory)
387387
* @param files list of files to check if they contain a repository
388388
* @param ignoredNames what files to ignore
389389
* @param recursiveSearch whether to use recursive search
390+
* @param type type of the repository to search for or {@code null}
390391
* @param depth current depth - using global scanningDepth - one can limit
391392
* this to improve scanning performance
392393
* @return collection of added repositories
393394
*/
394395
private Collection<RepositoryInfo> addRepositories(File[] files,
395-
IgnoredNames ignoredNames, boolean recursiveSearch, int depth) {
396+
IgnoredNames ignoredNames, boolean recursiveSearch, String type, int depth) {
396397

397398
List<RepositoryInfo> repoList = new ArrayList<>();
398399

@@ -407,7 +408,7 @@ private Collection<RepositoryInfo> addRepositories(File[] files,
407408

408409
Repository repository = null;
409410
try {
410-
repository = RepositoryFactory.getRepository(file);
411+
repository = RepositoryFactory.getRepository(file, type);
411412
} catch (InstantiationException | NoSuchMethodException | InvocationTargetException e) {
412413
LOGGER.log(Level.WARNING, "Could not create repository for '"
413414
+ file + "', could not instantiate the repository.", e);
@@ -429,7 +430,7 @@ private Collection<RepositoryInfo> addRepositories(File[] files,
429430
file.getAbsolutePath());
430431
} else if (depth <= scanningDepth) {
431432
repoList.addAll(HistoryGuru.this.addRepositories(subFiles, ignoredNames,
432-
recursiveSearch, depth + 1));
433+
recursiveSearch, null,depth + 1));
433434
}
434435
}
435436
} else {
@@ -439,18 +440,16 @@ private Collection<RepositoryInfo> addRepositories(File[] files,
439440
repoList.add(new RepositoryInfo(repository));
440441
putRepository(repository);
441442

442-
// @TODO: Search only for one type of repository - the one found here
443443
if (recursiveSearch && repository.supportsSubRepositories()) {
444444
File[] subFiles = file.listFiles();
445445
if (subFiles == null) {
446446
LOGGER.log(Level.WARNING,
447447
"Failed to get sub directories for ''{0}'', check access permissions.",
448448
file.getAbsolutePath());
449449
} else if (depth <= scanningDepth) {
450-
// Search only one level down - if not: too much
451-
// stat'ing for huge Mercurial repositories
450+
// Search only for one type of repository - the one found here.
452451
repoList.addAll(HistoryGuru.this.addRepositories(subFiles, ignoredNames,
453-
false, depth + 1));
452+
false, repository.getType(), depth + 1));
454453
}
455454
}
456455
}
@@ -469,7 +468,7 @@ private Collection<RepositoryInfo> addRepositories(File[] files,
469468
addRepositories(File[] files, Collection<RepositoryInfo> repos,
470469
IgnoredNames ignoredNames, int depth) {
471470

472-
return HistoryGuru.this.addRepositories(files, ignoredNames, true, depth);
471+
return HistoryGuru.this.addRepositories(files, ignoredNames, true, null, depth);
473472
}
474473

475474
/**
@@ -483,7 +482,7 @@ private Collection<RepositoryInfo> addRepositories(File[] files,
483482
public Collection<RepositoryInfo> addRepositories(File[] files,
484483
IgnoredNames ignoredNames) {
485484

486-
return HistoryGuru.this.addRepositories(files, ignoredNames, true, 0);
485+
return HistoryGuru.this.addRepositories(files, ignoredNames, true, null,0);
487486
}
488487

489488
/**

opengrok-indexer/src/main/java/org/opengrok/indexer/history/RepositoryFactory.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,14 @@ public static List<Class<? extends Repository>> getRepositoryClasses() {
8080
return list;
8181
}
8282

83+
public static Repository getRepository(File file, String type)
84+
throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, IOException, ForbiddenSymlinkException {
85+
return getRepository(file, false, type);
86+
}
87+
8388
public static Repository getRepository(File file)
8489
throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, IOException, ForbiddenSymlinkException {
85-
return getRepository(file, false);
90+
return getRepository(file, false, null);
8691
}
8792

8893
/**
@@ -97,6 +102,7 @@ public static Repository getRepository(File file)
97102
*
98103
* @param file File that might contain a repository
99104
* @param interactive true if running in interactive mode
105+
* @param type type of the repository to search for or {@code null}
100106
* @return Correct repository for the given file
101107
* @throws InstantiationException in case we cannot create the repository object
102108
* @throws IllegalAccessException in case no permissions to repository file
@@ -105,12 +111,16 @@ public static Repository getRepository(File file)
105111
* @throws IOException when resolving repository path
106112
* @throws ForbiddenSymlinkException when resolving repository path
107113
*/
108-
public static Repository getRepository(File file, boolean interactive)
114+
public static Repository getRepository(File file, boolean interactive, String type)
109115
throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, IOException, ForbiddenSymlinkException {
110116
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
111117
Repository repo = null;
112118

113119
for (Repository rep : repositories) {
120+
if (type != null && !type.equals(rep.getType())) {
121+
continue;
122+
}
123+
114124
if (rep.isRepositoryFor(file, interactive)) {
115125
repo = rep.getClass().getDeclaredConstructor().newInstance();
116126

@@ -197,7 +207,7 @@ public static Repository getRepository(File file, boolean interactive)
197207
*/
198208
public static Repository getRepository(RepositoryInfo info, boolean interactive)
199209
throws InstantiationException, IllegalAccessException, NoSuchMethodException, InvocationTargetException, IOException, ForbiddenSymlinkException {
200-
return getRepository(new File(info.getDirectoryName()), interactive);
210+
return getRepository(new File(info.getDirectoryName()), interactive, null);
201211
}
202212

203213
/**

opengrok-indexer/src/main/java/org/opengrok/indexer/history/RepositoryInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public class RepositoryInfo implements Serializable {
6060

6161
private String directoryNameRelative;
6262
protected Boolean working;
63-
protected String type;
63+
protected String type; // type of the repository, should be unique
6464
protected boolean remote;
6565
protected String[] datePatterns = new String[0];
6666
protected String parent;

opengrok-indexer/src/test/java/org/opengrok/indexer/history/HistoryGuruTest.java

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,17 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2008, 2019, Oracle and/or its affiliates. All rights reserved.
2222
*/
2323
package org.opengrok.indexer.history;
2424

25-
import static org.junit.Assert.assertEquals;
26-
import static org.junit.Assert.assertNotNull;
27-
2825
import java.io.File;
2926
import java.io.IOException;
3027
import java.io.InputStream;
28+
import java.nio.file.Paths;
3129
import java.util.ArrayList;
3230
import java.util.Collection;
31+
import java.util.Collections;
3332
import java.util.List;
3433

3534
import org.junit.AfterClass;
@@ -44,6 +43,8 @@
4443
import org.opengrok.indexer.util.FileUtilities;
4544
import org.opengrok.indexer.util.TestRepository;
4645

46+
import static org.junit.Assert.*;
47+
4748
/**
4849
* Test the functionality provided by the HistoryGuru (with friends)
4950
*
@@ -171,4 +172,46 @@ public void testAddRemoveRepositories() {
171172
Assert.assertEquals(1, added.size());
172173
Assert.assertEquals(numReposOrig, instance.getRepositories().size());
173174
}
175+
176+
@Test
177+
@ConditionalRun(RepositoryInstalled.GitInstalled.class)
178+
@ConditionalRun(RepositoryInstalled.MercurialInstalled.class)
179+
public void testAddSubRepositoryNoTypeMatch() {
180+
HistoryGuru instance = HistoryGuru.getInstance();
181+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
182+
183+
// Clone a Mercurial repository underneath a Git repository.
184+
File hgRoot = new File(repository.getSourceRoot(), "mercurial");
185+
assertTrue(hgRoot.exists());
186+
assertTrue(hgRoot.isDirectory());
187+
File gitRoot = new File(repository.getSourceRoot(), "git");
188+
assertTrue(gitRoot.exists());
189+
assertTrue(gitRoot.isDirectory());
190+
MercurialRepositoryTest.runHgCommand(gitRoot,
191+
"clone", hgRoot.getAbsolutePath(), "subrepo");
192+
193+
Collection<RepositoryInfo> addedRepos = instance.
194+
addRepositories(Collections.singleton(Paths.get(repository.getSourceRoot(), "git").toString()),
195+
env.getIgnoredNames());
196+
assertEquals(1, addedRepos.size());
197+
}
198+
199+
@Test
200+
@ConditionalRun(RepositoryInstalled.MercurialInstalled.class)
201+
public void testAddSubRepository() {
202+
HistoryGuru instance = HistoryGuru.getInstance();
203+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
204+
205+
// Clone a Mercurial repository underneath a Mercurial repository.
206+
File hgRoot = new File(repository.getSourceRoot(), "mercurial");
207+
assertTrue(hgRoot.exists());
208+
assertTrue(hgRoot.isDirectory());
209+
MercurialRepositoryTest.runHgCommand(hgRoot,
210+
"clone", hgRoot.getAbsolutePath(), "subrepo");
211+
212+
Collection<RepositoryInfo> addedRepos = instance.
213+
addRepositories(Collections.singleton(Paths.get(repository.getSourceRoot(), "mercurial").toString()),
214+
env.getIgnoredNames());
215+
assertEquals(2, addedRepos.size());
216+
}
174217
}

0 commit comments

Comments
 (0)