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.Constants;
22 import org.eclipse.jgit.lib.ObjectId;
23 import org.eclipse.jgit.lib.Repository;
24 import org.eclipse.jgit.submodule.SubmoduleStatus;
25 import org.eclipse.jgit.submodule.SubmoduleStatusType;
26 import org.eclipse.jgit.submodule.SubmoduleWalk;
27 import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
28
29
30
31
32
33
34
35
36 public class SubmoduleStatusCommand extends
37 GitCommand<Map<String, SubmoduleStatus>> {
38
39 private final Collection<String> paths;
40
41
42
43
44
45
46
47 public SubmoduleStatusCommand(Repository repo) {
48 super(repo);
49 paths = new ArrayList<>();
50 }
51
52
53
54
55
56
57
58
59 public SubmoduleStatusCommand addPath(String path) {
60 paths.add(path);
61 return this;
62 }
63
64
65 @Override
66 public Map<String, SubmoduleStatus> call() throws GitAPIException {
67 checkCallable();
68
69 try (SubmoduleWalk generator = SubmoduleWalk.forIndex(repo)) {
70 if (!paths.isEmpty())
71 generator.setFilter(PathFilterGroup.createFromStrings(paths));
72 Map<String, SubmoduleStatus> statuses = new HashMap<>();
73 while (generator.next()) {
74 SubmoduleStatus status = getStatus(generator);
75 statuses.put(status.getPath(), status);
76 }
77 return statuses;
78 } catch (IOException | ConfigInvalidException e) {
79 throw new JGitInternalException(e.getMessage(), e);
80 }
81 }
82
83 private SubmoduleStatus getStatus(SubmoduleWalk generator)
84 throws IOException, ConfigInvalidException {
85 ObjectId id = generator.getObjectId();
86 String path = generator.getPath();
87
88
89 if (generator.getModulesPath() == null)
90 return new SubmoduleStatus(SubmoduleStatusType.MISSING, path, id);
91
92
93 if (generator.getConfigUrl() == null)
94 return new SubmoduleStatus(SubmoduleStatusType.UNINITIALIZED, path,
95 id);
96
97
98 ObjectId headId = null;
99 try (Repository subRepo = generator.getRepository()) {
100 if (subRepo == null) {
101 return new SubmoduleStatus(SubmoduleStatusType.UNINITIALIZED,
102 path, id);
103 }
104
105 headId = subRepo.resolve(Constants.HEAD);
106 }
107
108
109 if (headId == null)
110 return new SubmoduleStatus(SubmoduleStatusType.UNINITIALIZED, path,
111 id, headId);
112
113
114 if (!headId.equals(id))
115 return new SubmoduleStatus(SubmoduleStatusType.REV_CHECKED_OUT,
116 path, id, headId);
117
118
119 return new SubmoduleStatus(SubmoduleStatusType.INITIALIZED, path, id,
120 headId);
121 }
122 }