Browse Source

containsGitModulesFile() should not crash on bare repository

Change-Id: Iba7e4674b3d33c730613a6ac703977f48b015853
Signed-off-by: Andrey Loskutov <loskutov@gmx.de>
stable-4.1
Andrey Loskutov 9 years ago
parent
commit
a28ae3995f
  1. 8
      org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java
  2. 12
      org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java

8
org.eclipse.jgit.test/tst/org/eclipse/jgit/submodule/SubmoduleWalkTest.java

@ -65,6 +65,7 @@ import org.eclipse.jgit.dircache.DirCacheEditor.PathEdit;
import org.eclipse.jgit.dircache.DirCacheEntry; import org.eclipse.jgit.dircache.DirCacheEntry;
import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.errors.NoWorkTreeException; import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.junit.RepositoryTestCase; import org.eclipse.jgit.junit.RepositoryTestCase;
import org.eclipse.jgit.junit.TestRepository; import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.Config;
@ -100,6 +101,13 @@ public class SubmoduleWalkTest extends RepositoryTestCase {
assertEquals(ObjectId.zeroId(), gen.getObjectId()); assertEquals(ObjectId.zeroId(), gen.getObjectId());
} }
@Test
public void bareRepositoryWithNoSubmodules() throws IOException {
FileRepository bareRepo = createBareRepository();
boolean result = SubmoduleWalk.containsGitModulesFile(bareRepo);
assertFalse(result);
}
@Test @Test
public void repositoryWithRootLevelSubmodule() throws IOException, public void repositoryWithRootLevelSubmodule() throws IOException,
ConfigInvalidException, NoWorkTreeException, GitAPIException { ConfigInvalidException, NoWorkTreeException, GitAPIException {

12
org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java

@ -451,13 +451,14 @@ public class SubmoduleWalk implements AutoCloseable {
} }
/** /**
* Checks whether the working tree (or the index in case of a bare repo) * Checks whether the working tree contains a .gitmodules file. That's a
* contains a .gitmodules file. That's a hint that the repo contains * hint that the repo contains submodules.
* submodules.
* *
* @param repository * @param repository
* the repository to check * the repository to check
* @return <code>true</code> if the repo contains a .gitmodules file * @return <code>true</code> if the working tree contains a .gitmodules file,
* <code>false</code> otherwise. Always returns <code>false</code>
* for bare repositories.
* @throws IOException * @throws IOException
* @throws CorruptObjectException * @throws CorruptObjectException
* @since 3.6 * @since 3.6
@ -465,8 +466,7 @@ public class SubmoduleWalk implements AutoCloseable {
public static boolean containsGitModulesFile(Repository repository) public static boolean containsGitModulesFile(Repository repository)
throws IOException { throws IOException {
if (repository.isBare()) { if (repository.isBare()) {
DirCache dc = repository.readDirCache(); return false;
return (dc.findEntry(Constants.DOT_GIT_MODULES) >= 0);
} }
File modulesFile = new File(repository.getWorkTree(), File modulesFile = new File(repository.getWorkTree(),
Constants.DOT_GIT_MODULES); Constants.DOT_GIT_MODULES);

Loading…
Cancel
Save