1
2
3
4
5
6
7
8
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
28
29
30
31
32
33
34
35
36
37 public class SubmoduleInitCommand extends GitCommand<Collection<String>> {
38
39 private final Collection<String> paths;
40
41
42
43
44
45
46
47 public SubmoduleInitCommand(Repository repo) {
48 super(repo);
49 paths = new ArrayList<>();
50 }
51
52
53
54
55
56
57
58
59 public SubmoduleInitCommand addPath(String path) {
60 paths.add(path);
61 return this;
62 }
63
64
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
76 if (generator.getConfigUrl() != null)
77 continue;
78
79 String path = generator.getPath();
80 String name = generator.getModuleName();
81
82
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
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 }