diff options
Diffstat (limited to 'org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleAddCommand.java')
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleAddCommand.java | 120 |
1 files changed, 64 insertions, 56 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleAddCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleAddCommand.java index 06c8f414e3..5105dfc2e5 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleAddCommand.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/SubmoduleAddCommand.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.api; @@ -51,6 +18,7 @@ import org.eclipse.jgit.api.errors.JGitInternalException; import org.eclipse.jgit.api.errors.NoFilepatternException; import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.internal.JGitText; +import org.eclipse.jgit.internal.submodule.SubmoduleValidator; import org.eclipse.jgit.lib.ConfigConstants; import org.eclipse.jgit.lib.Constants; import org.eclipse.jgit.lib.NullProgressMonitor; @@ -69,13 +37,15 @@ import org.eclipse.jgit.treewalk.filter.TreeFilter; * .gitmodules file and the repository config file, and also add the submodule * and .gitmodules file to the index. * - * @see <a - * href="http://www.kernel.org/pub/software/scm/git/docs/git-submodule.html" + * @see <a href= + * "http://www.kernel.org/pub/software/scm/git/docs/git-submodule.html" * >Git documentation about submodules</a> */ public class SubmoduleAddCommand extends TransportCommand<SubmoduleAddCommand, Repository> { + private String name; + private String path; private String uri; @@ -83,20 +53,36 @@ public class SubmoduleAddCommand extends private ProgressMonitor monitor; /** + * Constructor for SubmoduleAddCommand. + * * @param repo + * a {@link org.eclipse.jgit.lib.Repository} object. */ - public SubmoduleAddCommand(final Repository repo) { + public SubmoduleAddCommand(Repository repo) { super(repo); } /** + * Set the submodule name + * + * @param name + * name of the submodule + * @return this command + * @since 5.1 + */ + public SubmoduleAddCommand setName(String name) { + this.name = name; + return this; + } + + /** * Set repository-relative path of submodule * * @param path * (with <code>/</code> as separator) * @return this command */ - public SubmoduleAddCommand setPath(final String path) { + public SubmoduleAddCommand setPath(String path) { this.path = path; return this; } @@ -105,9 +91,10 @@ public class SubmoduleAddCommand extends * Set URI to clone submodule from * * @param uri + * a {@link java.lang.String} object. * @return this command */ - public SubmoduleAddCommand setURI(final String uri) { + public SubmoduleAddCommand setURI(String uri) { this.uri = uri; return this; } @@ -118,9 +105,10 @@ public class SubmoduleAddCommand extends * * @see NullProgressMonitor * @param monitor + * a {@link org.eclipse.jgit.lib.ProgressMonitor} object. * @return this command */ - public SubmoduleAddCommand setProgressMonitor(final ProgressMonitor monitor) { + public SubmoduleAddCommand setProgressMonitor(ProgressMonitor monitor) { this.monitor = monitor; return this; } @@ -129,30 +117,45 @@ public class SubmoduleAddCommand extends * Is the configured already a submodule in the index? * * @return true if submodule exists in index, false otherwise - * @throws IOException + * @throws java.io.IOException + * if an IO error occurred */ protected boolean submoduleExists() throws IOException { TreeFilter filter = PathFilter.create(path); - return SubmoduleWalk.forIndex(repo).setFilter(filter).next(); + try (SubmoduleWalk w = SubmoduleWalk.forIndex(repo)) { + return w.setFilter(filter).next(); + } } /** + * {@inheritDoc} + * <p> * Executes the {@code SubmoduleAddCommand} * * The {@code Repository} instance returned by this command needs to be * closed by the caller to free resources held by the {@code Repository} * instance. It is recommended to call this method as soon as you don't need * a reference to this {@code Repository} instance anymore. - * - * @return the newly created {@link Repository} - * @throws GitAPIException */ + @Override public Repository call() throws GitAPIException { checkCallable(); if (path == null || path.length() == 0) throw new IllegalArgumentException(JGitText.get().pathNotConfigured); if (uri == null || uri.length() == 0) throw new IllegalArgumentException(JGitText.get().uriNotConfigured); + if (name == null || name.length() == 0) { + // Use the path as the default. + name = path; + } + + try { + SubmoduleValidator.assertValidSubmoduleName(name); + SubmoduleValidator.assertValidSubmodulePath(path); + SubmoduleValidator.assertValidSubmoduleUri(uri); + } catch (SubmoduleValidator.SubmoduleValidationException e) { + throw new IllegalArgumentException(e.getMessage()); + } try { if (submoduleExists()) @@ -173,14 +176,21 @@ public class SubmoduleAddCommand extends CloneCommand clone = Git.cloneRepository(); configure(clone); clone.setDirectory(moduleDirectory); + clone.setGitDir(new File( + new File(repo.getCommonDirectory(), Constants.MODULES), path)); + clone.setRelativePaths(true); clone.setURI(resolvedUri); if (monitor != null) clone.setProgressMonitor(monitor); - Repository subRepo = clone.call().getRepository(); + Repository subRepo = null; + try (Git git = clone.call()) { + subRepo = git.getRepository(); + subRepo.incrementOpen(); + } // Save submodule URL to parent repository's config StoredConfig config = repo.getConfig(); - config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, path, + config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, name, ConfigConstants.CONFIG_KEY_URL, resolvedUri); try { config.save(); @@ -194,13 +204,11 @@ public class SubmoduleAddCommand extends try { modulesConfig.load(); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, - path, ConfigConstants.CONFIG_KEY_PATH, path); + name, ConfigConstants.CONFIG_KEY_PATH, path); modulesConfig.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION, - path, ConfigConstants.CONFIG_KEY_URL, uri); + name, ConfigConstants.CONFIG_KEY_URL, uri); modulesConfig.save(); - } catch (IOException e) { - throw new JGitInternalException(e.getMessage(), e); - } catch (ConfigInvalidException e) { + } catch (IOException | ConfigInvalidException e) { throw new JGitInternalException(e.getMessage(), e); } |