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