diff options
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/submodule')
4 files changed, 375 insertions, 208 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleConflict.java b/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleConflict.java new file mode 100644 index 0000000000..e324ab1afc --- /dev/null +++ b/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleConflict.java @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2017, Two Sigma Open Source + * and other copyright owners as documented in the project's IP log. + * + * This program and the accompanying materials are made available + * under the terms of the Eclipse Distribution License v1.0 which + * accompanies this distribution, is reproduced below, and is + * available at http://www.eclipse.org/org/documents/edl-v10.php + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or + * without modification, are permitted provided that the following + * conditions are met: + * + * - Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * - Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. +* + * - Neither the name of the Eclipse Foundation, Inc. nor the + * names of its contributors may be used to endorse or promote + * products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND + * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, + * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.eclipse.jgit.submodule; + +import org.eclipse.jgit.diff.Sequence; +import org.eclipse.jgit.lib.ObjectId; + +/** + * Merges expect that conflicts will consist of Sequences, but that doesn't + * really make sense for submodules. So this represents a submodule conflict. + * + * @since 4.11 + */ +public class SubmoduleConflict extends Sequence { + private final ObjectId objectId; + + /** + * Create a SubmoduleConflict for the given submodule object id + * + * @param objectId + * the id of the object to create a submodule conflict for + */ + public SubmoduleConflict(ObjectId objectId) { + super(); + this.objectId = objectId; + } + + @Override + public int size() { + return 1; + } + + /** + * Get objectId for the conflicting submodule + * + * @return the object id for the conflicting submodule + */ + public ObjectId getObjectId() { + return objectId; + } +} diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleStatus.java b/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleStatus.java index 2298f4b17c..06b21a6bb1 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleStatus.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleStatus.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2011, GitHub Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2011, GitHub Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.submodule; @@ -61,8 +28,12 @@ public class SubmoduleStatus { * Create submodule status * * @param type + * a {@link org.eclipse.jgit.submodule.SubmoduleStatusType} + * object. * @param path + * submodule path * @param indexId + * an {@link org.eclipse.jgit.lib.ObjectId} object. */ public SubmoduleStatus(final SubmoduleStatusType type, final String path, final ObjectId indexId) { @@ -73,9 +44,14 @@ public class SubmoduleStatus { * Create submodule status * * @param type + * a {@link org.eclipse.jgit.submodule.SubmoduleStatusType} + * object. * @param path + * submodule path * @param indexId + * index id * @param headId + * head id */ public SubmoduleStatus(final SubmoduleStatusType type, final String path, final ObjectId indexId, final ObjectId headId) { @@ -86,6 +62,8 @@ public class SubmoduleStatus { } /** + * Get type + * * @return type */ public SubmoduleStatusType getType() { @@ -93,13 +71,17 @@ public class SubmoduleStatus { } /** - * @return path + * Get submodule path + * + * @return path submodule path */ public String getPath() { return path; } /** + * Get index object id + * * @return index object id */ public ObjectId getIndexId() { @@ -107,6 +89,8 @@ public class SubmoduleStatus { } /** + * Get HEAD object id + * * @return HEAD object id */ public ObjectId getHeadId() { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleStatusType.java b/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleStatusType.java index be145f3d60..6c548a0faf 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleStatusType.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleStatusType.java @@ -1,44 +1,11 @@ /* - * Copyright (C) 2011, GitHub Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2011, GitHub Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.submodule; diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java b/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java index 7d9bca0d1b..105cba7d28 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/submodule/SubmoduleWalk.java @@ -1,50 +1,19 @@ /* - * Copyright (C) 2011, GitHub Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2011, GitHub Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.submodule; import java.io.File; import java.io.IOException; import java.text.MessageFormat; +import java.util.HashMap; +import java.util.Map; import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.dircache.DirCacheIterator; @@ -55,6 +24,7 @@ import org.eclipse.jgit.errors.MissingObjectException; import org.eclipse.jgit.errors.RepositoryNotFoundException; import org.eclipse.jgit.internal.JGitText; import org.eclipse.jgit.lib.AnyObjectId; +import org.eclipse.jgit.lib.BaseRepositoryBuilder; import org.eclipse.jgit.lib.BlobBasedConfig; import org.eclipse.jgit.lib.Config; import org.eclipse.jgit.lib.ConfigConstants; @@ -64,6 +34,7 @@ import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.Ref; import org.eclipse.jgit.lib.Repository; import org.eclipse.jgit.lib.RepositoryBuilder; +import org.eclipse.jgit.lib.RepositoryBuilderFactory; import org.eclipse.jgit.lib.StoredConfig; import org.eclipse.jgit.storage.file.FileBasedConfig; import org.eclipse.jgit.treewalk.AbstractTreeIterator; @@ -79,7 +50,7 @@ import org.eclipse.jgit.util.FS; public class SubmoduleWalk implements AutoCloseable { /** - * The values for the config param submodule.<name>.ignore + * The values for the config parameter submodule.<name>.ignore * * @since 3.6 */ @@ -112,11 +83,15 @@ public class SubmoduleWalk implements AutoCloseable { * The {@code .gitmodules} file is read from the index. * * @param repository - * @return generator over submodule index entries - * @throws IOException + * a {@link org.eclipse.jgit.lib.Repository} object. + * @return generator over submodule index entries. The caller is responsible + * for calling {@link #close()}. + * @throws java.io.IOException + * if an IO error occurred */ public static SubmoduleWalk forIndex(Repository repository) throws IOException { + @SuppressWarnings("resource") // The caller closes it SubmoduleWalk generator = new SubmoduleWalk(repository); try { DirCache index = repository.readDirCache(); @@ -133,12 +108,16 @@ public class SubmoduleWalk implements AutoCloseable { * path * * @param repository + * a {@link org.eclipse.jgit.lib.Repository} object. * @param treeId - * the root of a tree containing both a submodule at the given path - * and .gitmodules at the root. + * the root of a tree containing both a submodule at the given + * path and .gitmodules at the root. * @param path - * @return generator at given path, null if no submodule at given path - * @throws IOException + * a {@link java.lang.String} object. + * @return generator at given path. The caller is responsible for calling + * {@link #close()}. Null if no submodule at given path. + * @throws java.io.IOException + * if an IO error occurred */ public static SubmoduleWalk forPath(Repository repository, AnyObjectId treeId, String path) throws IOException { @@ -164,12 +143,16 @@ public class SubmoduleWalk implements AutoCloseable { * path * * @param repository + * a {@link org.eclipse.jgit.lib.Repository} object. * @param iterator - * the root of a tree containing both a submodule at the given path - * and .gitmodules at the root. + * the root of a tree containing both a submodule at the given + * path and .gitmodules at the root. * @param path - * @return generator at given path, null if no submodule at given path - * @throws IOException + * a {@link java.lang.String} object. + * @return generator at given path. The caller is responsible for calling + * {@link #close()}. Null if no submodule at given path. + * @throws java.io.IOException + * if an IO error occurred */ public static SubmoduleWalk forPath(Repository repository, AbstractTreeIterator iterator, String path) throws IOException { @@ -194,7 +177,9 @@ public class SubmoduleWalk implements AutoCloseable { * Get submodule directory * * @param parent + * the {@link org.eclipse.jgit.lib.Repository}. * @param path + * submodule path * @return directory */ public static File getSubmoduleDirectory(final Repository parent, @@ -206,34 +191,87 @@ public class SubmoduleWalk implements AutoCloseable { * Get submodule repository * * @param parent + * the {@link org.eclipse.jgit.lib.Repository}. * @param path + * submodule path * @return repository or null if repository doesn't exist - * @throws IOException + * @throws java.io.IOException + * if an IO error occurred */ public static Repository getSubmoduleRepository(final Repository parent, final String path) throws IOException { - return getSubmoduleRepository(parent.getWorkTree(), path); + return getSubmoduleRepository(parent.getWorkTree(), path, + parent.getFS()); } /** * Get submodule repository at path * * @param parent + * the parent * @param path + * submodule path * @return repository or null if repository doesn't exist - * @throws IOException + * @throws java.io.IOException + * if an IO error occurred */ public static Repository getSubmoduleRepository(final File parent, final String path) throws IOException { + return getSubmoduleRepository(parent, path, FS.DETECTED); + } + + /** + * Get submodule repository at path, using the specified file system + * abstraction + * + * @param parent + * {@link Repository} that contains the submodule + * @param path + * of the working tree of the submodule + * @param fs + * the file system abstraction to be used + * @return repository or null if repository doesn't exist + * @throws IOException + * if an IO error occurred + * @since 4.10 + */ + public static Repository getSubmoduleRepository(final File parent, + final String path, FS fs) throws IOException { + return getSubmoduleRepository(parent, path, fs, + new RepositoryBuilder()); + } + + /** + * Get submodule repository at path, using the specified file system + * abstraction and the specified builder + * + * @param parent + * {@link Repository} that contains the submodule + * @param path + * of the working tree of the submodule + * @param fs + * {@link FS} to use + * @param builder + * {@link BaseRepositoryBuilder} to use to build the submodule + * repository + * @return the {@link Repository} of the submodule, or {@code null} if it + * doesn't exist + * @throws IOException + * if an IO error occurred + * @since 5.6 + */ + public static Repository getSubmoduleRepository(File parent, String path, + FS fs, BaseRepositoryBuilder<?, ? extends Repository> builder) + throws IOException { File subWorkTree = new File(parent, path); - if (!subWorkTree.isDirectory()) + if (!subWorkTree.isDirectory()) { return null; - File workTree = new File(parent, path); + } try { - return new RepositoryBuilder() // + return builder // .setMustExist(true) // - .setFS(FS.DETECTED) // - .setWorkTree(workTree) // + .setFS(fs) // + .setWorkTree(subWorkTree) // .build(); } catch (RepositoryNotFoundException e) { return null; @@ -255,7 +293,8 @@ public class SubmoduleWalk implements AutoCloseable { * @param url * absolute or relative URL of the submodule repository * @return resolved URL - * @throws IOException + * @throws java.io.IOException + * if an IO error occurred */ public static String getSubmoduleRemoteUrl(final Repository parent, final String url) throws IOException { @@ -264,7 +303,7 @@ public class SubmoduleWalk implements AutoCloseable { String remoteName = null; // Look up remote URL associated wit HEAD ref - Ref ref = parent.getRef(Constants.HEAD); + Ref ref = parent.exactRef(Constants.HEAD); if (ref != null) { if (ref.isSymbolic()) ref = ref.getLeaf(); @@ -329,13 +368,19 @@ public class SubmoduleWalk implements AutoCloseable { private String path; + private Map<String, String> pathToName; + + private RepositoryBuilderFactory factory; + /** * Create submodule generator * * @param repository - * @throws IOException + * the {@link org.eclipse.jgit.lib.Repository}. + * @throws java.io.IOException + * if an IO error occurred */ - public SubmoduleWalk(final Repository repository) throws IOException { + public SubmoduleWalk(Repository repository) throws IOException { this.repository = repository; repoConfig = repository.getConfig(); walk = new TreeWalk(repository); @@ -352,8 +397,9 @@ public class SubmoduleWalk implements AutoCloseable { * .gitmodules config object * @return this generator */ - public SubmoduleWalk setModulesConfig(final Config config) { + public SubmoduleWalk setModulesConfig(Config config) { modulesConfig = config; + loadPathNames(); return this; } @@ -370,9 +416,10 @@ public class SubmoduleWalk implements AutoCloseable { * tree containing .gitmodules * @return this generator */ - public SubmoduleWalk setRootTree(final AbstractTreeIterator tree) { + public SubmoduleWalk setRootTree(AbstractTreeIterator tree) { rootTree = tree; modulesConfig = null; + pathToName = null; return this; } @@ -382,19 +429,21 @@ public class SubmoduleWalk implements AutoCloseable { * The root tree is not read until the first submodule is encountered by the * walk. * <p> - * This method need only be called if constructing a walk manually instead of - * with one of the static factory methods above. + * This method need only be called if constructing a walk manually instead + * of with one of the static factory methods above. * * @param id * ID of a tree containing .gitmodules * @return this generator - * @throws IOException + * @throws java.io.IOException + * if an IO error occurred */ - public SubmoduleWalk setRootTree(final AnyObjectId id) throws IOException { + public SubmoduleWalk setRootTree(AnyObjectId id) throws IOException { final CanonicalTreeParser p = new CanonicalTreeParser(); p.reset(walk.getObjectReader(), id); rootTree = p; modulesConfig = null; + pathToName = null; return this; } @@ -407,8 +456,10 @@ public class SubmoduleWalk implements AutoCloseable { * If no submodule config is found, loads an empty config. * * @return this generator - * @throws IOException if an error occurred, or if the repository is bare - * @throws ConfigInvalidException + * @throws java.io.IOException + * if an error occurred, or if the repository is bare + * @throws org.eclipse.jgit.errors.ConfigInvalidException + * if .gitmodules config is invalid */ public SubmoduleWalk loadModulesConfig() throws IOException, ConfigInvalidException { if (rootTree == null) { @@ -418,6 +469,7 @@ public class SubmoduleWalk implements AutoCloseable { repository.getFS()); config.load(); modulesConfig = config; + loadPathNames(); } else { try (TreeWalk configWalk = new TreeWalk(repository)) { configWalk.addTree(rootTree); @@ -437,10 +489,12 @@ public class SubmoduleWalk implements AutoCloseable { if (filter.isDone(configWalk)) { modulesConfig = new BlobBasedConfig(null, repository, configWalk.getObjectId(0)); + loadPathNames(); return this; } } modulesConfig = new Config(); + pathToName = null; } finally { if (idx > 0) rootTree.next(idx); @@ -450,38 +504,61 @@ public class SubmoduleWalk implements AutoCloseable { return this; } + private void loadPathNames() { + pathToName = null; + if (modulesConfig != null) { + HashMap<String, String> pathNames = new HashMap<>(); + for (String name : modulesConfig + .getSubsections(ConfigConstants.CONFIG_SUBMODULE_SECTION)) { + pathNames.put(modulesConfig.getString( + ConfigConstants.CONFIG_SUBMODULE_SECTION, name, + ConfigConstants.CONFIG_KEY_PATH), name); + } + pathToName = pathNames; + } + } + /** - * Checks whether the working tree (or the index in case of a bare repo) - * contains a .gitmodules file. That's a hint that the repo contains - * submodules. + * Checks whether the working tree contains a .gitmodules file. That's a + * hint that the repo contains submodules. * * @param repository * the repository to check - * @return <code>true</code> if the repo contains a .gitmodules file - * @throws IOException + * @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 java.io.IOException + * if an IO error occurred * @throws CorruptObjectException + * if a corrupt object was found * @since 3.6 */ public static boolean containsGitModulesFile(Repository repository) throws IOException { if (repository.isBare()) { - DirCache dc = repository.readDirCache(); - return (dc.findEntry(Constants.DOT_GIT_MODULES) >= 0); + return false; } File modulesFile = new File(repository.getWorkTree(), Constants.DOT_GIT_MODULES); - return (modulesFile.exists()); + return modulesFile.exists(); } private void lazyLoadModulesConfig() throws IOException, ConfigInvalidException { - if (modulesConfig == null) + if (modulesConfig == null) { loadModulesConfig(); + } + } + + private String getModuleName(String modulePath) { + String name = pathToName != null ? pathToName.get(modulePath) : null; + return name != null ? name : modulePath; } /** * Set tree filter * * @param filter + * a {@link org.eclipse.jgit.treewalk.filter.TreeFilter} object. * @return this generator */ public SubmoduleWalk setFilter(TreeFilter filter) { @@ -493,10 +570,13 @@ public class SubmoduleWalk implements AutoCloseable { * Set the tree iterator used for finding submodule entries * * @param iterator + * an {@link org.eclipse.jgit.treewalk.AbstractTreeIterator} + * object. * @return this generator - * @throws CorruptObjectException + * @throws org.eclipse.jgit.errors.CorruptObjectException + * if a corrupt object was found */ - public SubmoduleWalk setTree(final AbstractTreeIterator iterator) + public SubmoduleWalk setTree(AbstractTreeIterator iterator) throws CorruptObjectException { walk.addTree(iterator); return this; @@ -506,12 +586,16 @@ public class SubmoduleWalk implements AutoCloseable { * Set the tree used for finding submodule entries * * @param treeId + * an {@link org.eclipse.jgit.lib.AnyObjectId} object. * @return this generator - * @throws IOException + * @throws java.io.IOException + * if an IO error occurred * @throws IncorrectObjectTypeException + * if object has unexpected type * @throws MissingObjectException + * if object is missing */ - public SubmoduleWalk setTree(final AnyObjectId treeId) throws IOException { + public SubmoduleWalk setTree(AnyObjectId treeId) throws IOException { walk.addTree(treeId); return this; } @@ -524,6 +608,7 @@ public class SubmoduleWalk implements AutoCloseable { public SubmoduleWalk reset() { repoConfig = repository.getConfig(); modulesConfig = null; + pathToName = null; walk.reset(); return this; } @@ -544,7 +629,8 @@ public class SubmoduleWalk implements AutoCloseable { * {@link #getObjectId()} and {@link #getPath()}. * * @return true if entry found, false otherwise - * @throws IOException + * @throws java.io.IOException + * if an IO error occurred */ public boolean next() throws IOException { while (walk.next()) { @@ -567,6 +653,38 @@ public class SubmoduleWalk implements AutoCloseable { } /** + * Sets the {@link RepositoryBuilderFactory} to use for creating submodule + * repositories. If none is set, a plain {@link RepositoryBuilder} is used. + * + * @param factory + * to set + * @since 5.6 + */ + public void setBuilderFactory(RepositoryBuilderFactory factory) { + this.factory = factory; + } + + private BaseRepositoryBuilder<?, ? extends Repository> getBuilder() { + return factory != null ? factory.get() : new RepositoryBuilder(); + } + + /** + * The module name for the current submodule entry (used for the section + * name of .git/config) + * + * @since 4.10 + * @return name of the submodule + * @throws ConfigInvalidException + * if the .gitmodules config is invalid + * @throws IOException + * if an IO error occurred + */ + public String getModuleName() throws IOException, ConfigInvalidException { + lazyLoadModulesConfig(); + return getModuleName(path); + } + + /** * Get object id of current submodule entry * * @return object id @@ -580,14 +698,15 @@ public class SubmoduleWalk implements AutoCloseable { * the .gitmodules file in the current repository's working tree. * * @return configured path - * @throws ConfigInvalidException - * @throws IOException + * @throws org.eclipse.jgit.errors.ConfigInvalidException + * if .gitmodules config is invalid + * @throws java.io.IOException + * if an IO error occurred */ public String getModulesPath() throws IOException, ConfigInvalidException { lazyLoadModulesConfig(); - return modulesConfig.getString( - ConfigConstants.CONFIG_SUBMODULE_SECTION, path, - ConfigConstants.CONFIG_KEY_PATH); + return modulesConfig.getString(ConfigConstants.CONFIG_SUBMODULE_SECTION, + getModuleName(), ConfigConstants.CONFIG_KEY_PATH); } /** @@ -595,12 +714,14 @@ public class SubmoduleWalk implements AutoCloseable { * from the repository's config. * * @return configured URL - * @throws ConfigInvalidException - * @throws IOException + * @throws org.eclipse.jgit.errors.ConfigInvalidException + * if .gitmodules config is invalid + * @throws java.io.IOException + * if an IO error occurred */ public String getConfigUrl() throws IOException, ConfigInvalidException { return repoConfig.getString(ConfigConstants.CONFIG_SUBMODULE_SECTION, - path, ConfigConstants.CONFIG_KEY_URL); + getModuleName(), ConfigConstants.CONFIG_KEY_URL); } /** @@ -608,14 +729,15 @@ public class SubmoduleWalk implements AutoCloseable { * from the .gitmodules file in the current repository's working tree. * * @return configured URL - * @throws ConfigInvalidException - * @throws IOException + * @throws org.eclipse.jgit.errors.ConfigInvalidException + * if .gitmodules config is invalid + * @throws java.io.IOException + * if an IO error occurred */ public String getModulesUrl() throws IOException, ConfigInvalidException { lazyLoadModulesConfig(); - return modulesConfig.getString( - ConfigConstants.CONFIG_SUBMODULE_SECTION, path, - ConfigConstants.CONFIG_KEY_URL); + return modulesConfig.getString(ConfigConstants.CONFIG_SUBMODULE_SECTION, + getModuleName(), ConfigConstants.CONFIG_KEY_URL); } /** @@ -623,12 +745,14 @@ public class SubmoduleWalk implements AutoCloseable { * from the repository's config. * * @return update value - * @throws ConfigInvalidException - * @throws IOException + * @throws org.eclipse.jgit.errors.ConfigInvalidException + * if .gitmodules config is invalid + * @throws java.io.IOException + * if an IO error occurred */ public String getConfigUpdate() throws IOException, ConfigInvalidException { return repoConfig.getString(ConfigConstants.CONFIG_SUBMODULE_SECTION, - path, ConfigConstants.CONFIG_KEY_UPDATE); + getModuleName(), ConfigConstants.CONFIG_KEY_UPDATE); } /** @@ -636,14 +760,15 @@ public class SubmoduleWalk implements AutoCloseable { * from the .gitmodules file in the current repository's working tree. * * @return update value - * @throws ConfigInvalidException - * @throws IOException + * @throws org.eclipse.jgit.errors.ConfigInvalidException + * if .gitmodules config is invalid + * @throws java.io.IOException + * if an IO error occurred */ public String getModulesUpdate() throws IOException, ConfigInvalidException { lazyLoadModulesConfig(); - return modulesConfig.getString( - ConfigConstants.CONFIG_SUBMODULE_SECTION, path, - ConfigConstants.CONFIG_KEY_UPDATE); + return modulesConfig.getString(ConfigConstants.CONFIG_SUBMODULE_SECTION, + getModuleName(), ConfigConstants.CONFIG_KEY_UPDATE); } /** @@ -651,45 +776,52 @@ public class SubmoduleWalk implements AutoCloseable { * value from the .gitmodules file in the current repository's working tree. * * @return ignore value - * @throws ConfigInvalidException - * @throws IOException + * @throws org.eclipse.jgit.errors.ConfigInvalidException + * if .gitmodules config is invalid + * @throws java.io.IOException + * if an IO error occurred * @since 3.6 */ public IgnoreSubmoduleMode getModulesIgnore() throws IOException, ConfigInvalidException { - lazyLoadModulesConfig(); - String name = modulesConfig.getString( - ConfigConstants.CONFIG_SUBMODULE_SECTION, path, + IgnoreSubmoduleMode mode = repoConfig.getEnum( + IgnoreSubmoduleMode.values(), + ConfigConstants.CONFIG_SUBMODULE_SECTION, getModuleName(), ConfigConstants.CONFIG_KEY_IGNORE); - if (name == null) - return null; - return IgnoreSubmoduleMode.valueOf(name.trim().toUpperCase()); + if (mode != null) { + return mode; + } + lazyLoadModulesConfig(); + return modulesConfig.getEnum(ConfigConstants.CONFIG_SUBMODULE_SECTION, + getModuleName(), ConfigConstants.CONFIG_KEY_IGNORE, + IgnoreSubmoduleMode.NONE); } /** * Get repository for current submodule entry * * @return repository or null if non-existent - * @throws IOException + * @throws java.io.IOException + * if an IO error occurred */ public Repository getRepository() throws IOException { - return getSubmoduleRepository(repository, path); + return getSubmoduleRepository(repository.getWorkTree(), path, + repository.getFS(), getBuilder()); } /** * Get commit id that HEAD points to in the current submodule's repository * * @return object id of HEAD reference - * @throws IOException + * @throws java.io.IOException + * if an IO error occurred */ public ObjectId getHead() throws IOException { - Repository subRepo = getRepository(); - if (subRepo == null) - return null; - try { + try (Repository subRepo = getRepository()) { + if (subRepo == null) { + return null; + } return subRepo.resolve(Constants.HEAD); - } finally { - subRepo.close(); } } @@ -697,17 +829,16 @@ public class SubmoduleWalk implements AutoCloseable { * Get ref that HEAD points to in the current submodule's repository * * @return ref name, null on failures - * @throws IOException + * @throws java.io.IOException + * if an IO error occurred */ public String getHeadRef() throws IOException { - Repository subRepo = getRepository(); - if (subRepo == null) - return null; - try { - Ref head = subRepo.getRef(Constants.HEAD); + try (Repository subRepo = getRepository()) { + if (subRepo == null) { + return null; + } + Ref head = subRepo.exactRef(Constants.HEAD); return head != null ? head.getLeaf().getName() : null; - } finally { - subRepo.close(); } } @@ -718,8 +849,10 @@ public class SubmoduleWalk implements AutoCloseable { * URL * * @return resolved remote URL - * @throws IOException - * @throws ConfigInvalidException + * @throws java.io.IOException + * if an IO error occurred + * @throws org.eclipse.jgit.errors.ConfigInvalidException + * if .gitmodules config is invalid */ public String getRemoteUrl() throws IOException, ConfigInvalidException { String url = getModulesUrl(); @@ -727,6 +860,8 @@ public class SubmoduleWalk implements AutoCloseable { } /** + * {@inheritDoc} + * <p> * Release any resources used by this walker's reader. * * @since 4.0 |