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.HashMap;
16 import java.util.Map;
17
18 import org.eclipse.jgit.api.errors.GitAPIException;
19 import org.eclipse.jgit.api.errors.JGitInternalException;
20 import org.eclipse.jgit.errors.ConfigInvalidException;
21 import org.eclipse.jgit.lib.ConfigConstants;
22 import org.eclipse.jgit.lib.Constants;
23 import org.eclipse.jgit.lib.Ref;
24 import org.eclipse.jgit.lib.Repository;
25 import org.eclipse.jgit.lib.StoredConfig;
26 import org.eclipse.jgit.submodule.SubmoduleWalk;
27 import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
28
29
30
31
32
33
34
35
36
37
38
39 public class SubmoduleSyncCommand extends GitCommand<Map<String, String>> {
40
41 private final Collection<String> paths;
42
43
44
45
46
47
48
49 public SubmoduleSyncCommand(Repository repo) {
50 super(repo);
51 paths = new ArrayList<>();
52 }
53
54
55
56
57
58
59
60
61 public SubmoduleSyncCommand addPath(String path) {
62 paths.add(path);
63 return this;
64 }
65
66
67
68
69
70
71
72
73
74 protected String getHeadBranch(Repository subRepo) throws IOException {
75 Ref head = subRepo.exactRef(Constants.HEAD);
76 if (head != null && head.isSymbolic()) {
77 return Repository.shortenRefName(head.getLeaf().getName());
78 }
79 return null;
80 }
81
82
83 @Override
84 public Map<String, String> call() throws GitAPIException {
85 checkCallable();
86
87 try (SubmoduleWalk generator = SubmoduleWalk.forIndex(repo)) {
88 if (!paths.isEmpty())
89 generator.setFilter(PathFilterGroup.createFromStrings(paths));
90 Map<String, String> synced = new HashMap<>();
91 StoredConfig config = repo.getConfig();
92 while (generator.next()) {
93 String remoteUrl = generator.getRemoteUrl();
94 if (remoteUrl == null)
95 continue;
96
97 String path = generator.getPath();
98 config.setString(ConfigConstants.CONFIG_SUBMODULE_SECTION,
99 path, ConfigConstants.CONFIG_KEY_URL, remoteUrl);
100 synced.put(path, remoteUrl);
101
102 try (Repository subRepo = generator.getRepository()) {
103 if (subRepo == null) {
104 continue;
105 }
106
107 StoredConfig subConfig;
108 String branch;
109
110 subConfig = subRepo.getConfig();
111
112
113 branch = getHeadBranch(subRepo);
114 String remote = null;
115 if (branch != null) {
116 remote = subConfig.getString(
117 ConfigConstants.CONFIG_BRANCH_SECTION, branch,
118 ConfigConstants.CONFIG_KEY_REMOTE);
119 }
120 if (remote == null) {
121 remote = Constants.DEFAULT_REMOTE_NAME;
122 }
123
124 subConfig.setString(ConfigConstants.CONFIG_REMOTE_SECTION,
125 remote, ConfigConstants.CONFIG_KEY_URL, remoteUrl);
126 subConfig.save();
127 }
128 }
129 if (!synced.isEmpty())
130 config.save();
131 return synced;
132 } catch (IOException | ConfigInvalidException e) {
133 throw new JGitInternalException(e.getMessage(), e);
134 }
135 }
136 }