View Javadoc
1   /*
2    * Copyright (C) 2011, GitHub Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.api;
11  
12  import java.io.IOException;
13  import java.util.ArrayList;
14  import java.util.Collection;
15  import java.util.List;
16  
17  import org.eclipse.jgit.api.errors.GitAPIException;
18  import org.eclipse.jgit.api.errors.JGitInternalException;
19  import org.eclipse.jgit.errors.ConfigInvalidException;
20  import org.eclipse.jgit.lib.ConfigConstants;
21  import org.eclipse.jgit.lib.Repository;
22  import org.eclipse.jgit.lib.StoredConfig;
23  import org.eclipse.jgit.submodule.SubmoduleWalk;
24  import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
25  
26  /**
27   * A class used to execute a submodule init command.
28   *
29   * This will copy the 'url' and 'update' fields from the working tree
30   * .gitmodules file to a repository's config file for each submodule not
31   * currently present in the repository's config file.
32   *
33   * @see <a href=
34   *      "http://www.kernel.org/pub/software/scm/git/docs/git-submodule.html"
35   *      >Git documentation about submodules</a>
36   */
37  public class SubmoduleInitCommand extends GitCommand<Collection<String>> {
38  
39  	private final Collection<String> paths;
40  
41  	/**
42  	 * Constructor for SubmoduleInitCommand.
43  	 *
44  	 * @param repo
45  	 *            a {@link org.eclipse.jgit.lib.Repository} object.
46  	 */
47  	public SubmoduleInitCommand(Repository repo) {
48  		super(repo);
49  		paths = new ArrayList<>();
50  	}
51  
52  	/**
53  	 * Add repository-relative submodule path to initialize
54  	 *
55  	 * @param path
56  	 *            (with <code>/</code> as separator)
57  	 * @return this command
58  	 */
59  	public SubmoduleInitCommand addPath(String path) {
60  		paths.add(path);
61  		return this;
62  	}
63  
64  	/** {@inheritDoc} */
65  	@Override
66  	public Collection<String> call() throws GitAPIException {
67  		checkCallable();
68  
69  		try (SubmoduleWalk generator = SubmoduleWalk.forIndex(repo)) {
70  			if (!paths.isEmpty())
71  				generator.setFilter(PathFilterGroup.createFromStrings(paths));
72  			StoredConfig config = repo.getConfig();
73  			List<String> initialized = new ArrayList<>();
74  			while (generator.next()) {
75  				// Ignore entry if URL is already present in config file
76  				if (generator.getConfigUrl() != null)
77  					continue;
78  
79  				String path = generator.getPath();
80  				String name = generator.getModuleName();
81  				// Copy 'url' and 'update' fields from .gitmodules to config
82  				// file
83  				String url = generator.getRemoteUrl();
84  				String update = generator.getModulesUpdate();
85  				if (url != null)
86  					config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
87  							name, ConfigConstants.CONFIG_KEY_URL, url);
88  				if (update != null)
89  					config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
90  							name, ConfigConstants.CONFIG_KEY_UPDATE, update);
91  				if (url != null || update != null)
92  					initialized.add(path);
93  			}
94  			// Save repository config if any values were updated
95  			if (!initialized.isEmpty())
96  				config.save();
97  			return initialized;
98  		} catch (IOException | ConfigInvalidException e) {
99  			throw new JGitInternalException(e.getMessage(), e);
100 		}
101 	}
102 }