1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm.provider.git;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.maven.scm.CommandParameters;
26 import org.apache.maven.scm.ScmException;
27 import org.apache.maven.scm.ScmFileSet;
28 import org.apache.maven.scm.ScmResult;
29 import org.apache.maven.scm.command.add.AddScmResult;
30 import org.apache.maven.scm.command.blame.BlameScmResult;
31 import org.apache.maven.scm.command.branch.BranchScmResult;
32 import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
33 import org.apache.maven.scm.command.checkin.CheckInScmResult;
34 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
35 import org.apache.maven.scm.command.diff.DiffScmResult;
36 import org.apache.maven.scm.command.export.ExportScmResult;
37 import org.apache.maven.scm.command.info.InfoScmResult;
38 import org.apache.maven.scm.command.remoteinfo.RemoteInfoScmResult;
39 import org.apache.maven.scm.command.remove.RemoveScmResult;
40 import org.apache.maven.scm.command.status.StatusScmResult;
41 import org.apache.maven.scm.command.tag.TagScmResult;
42 import org.apache.maven.scm.command.untag.UntagScmResult;
43 import org.apache.maven.scm.command.update.UpdateScmResult;
44 import org.apache.maven.scm.provider.AbstractScmProvider;
45 import org.apache.maven.scm.provider.ScmProviderRepository;
46 import org.apache.maven.scm.provider.git.command.GitCommand;
47 import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
48 import org.apache.maven.scm.repository.ScmRepository;
49 import org.apache.maven.scm.repository.ScmRepositoryException;
50 import org.apache.maven.scm.repository.UnknownRepositoryStructure;
51
52
53
54
55
56
57
58
59 public abstract class AbstractGitScmProvider extends AbstractScmProvider {
60
61
62
63
64
65
66
67
68 private static class ScmUrlParserResult {
69 private final List<String> messages = new ArrayList<>();
70
71 private ScmProviderRepository repository;
72 }
73
74
75
76
77
78
79 public String getScmSpecificFilename() {
80 return ".git";
81 }
82
83
84 public ScmProviderRepository makeProviderScmRepository(String scmSpecificUrl, char delimiter)
85 throws ScmRepositoryException {
86 try {
87 ScmUrlParserResult result = parseScmUrl(scmSpecificUrl, delimiter);
88
89 if (result.messages.size() > 0) {
90 throw new ScmRepositoryException("The scm url " + scmSpecificUrl + " is invalid.", result.messages);
91 }
92
93 return result.repository;
94 } catch (ScmException e) {
95
96 throw new ScmRepositoryException("Error creating the scm repository", e);
97 }
98 }
99
100
101 public ScmProviderRepository makeProviderScmRepository(File path)
102 throws ScmRepositoryException, UnknownRepositoryStructure {
103 if (path == null) {
104 throw new NullPointerException("Path argument is null");
105 }
106
107 if (!path.isDirectory()) {
108 throw new ScmRepositoryException(path.getAbsolutePath() + " isn't a valid directory.");
109 }
110
111 if (!new File(path, ".git").exists()) {
112 throw new ScmRepositoryException(path.getAbsolutePath() + " isn't a git checkout directory.");
113 }
114
115 try {
116 return makeProviderScmRepository(getRepositoryURL(path), ':');
117 } catch (ScmException e) {
118
119 throw new ScmRepositoryException("Error creating the scm repository", e);
120 }
121 }
122
123 protected abstract String getRepositoryURL(File path) throws ScmException;
124
125
126 public List<String> validateScmUrl(String scmSpecificUrl, char delimiter) {
127 List<String> messages = new ArrayList<>();
128 try {
129 makeProviderScmRepository(scmSpecificUrl, delimiter);
130 } catch (ScmRepositoryException e) {
131 messages = e.getValidationMessages();
132 }
133 return messages;
134 }
135
136
137 public String getScmType() {
138 return "git";
139 }
140
141
142
143
144
145
146
147
148
149 private ScmUrlParserResult parseScmUrl(String scmSpecificUrl, char delimiter) throws ScmException {
150 ScmUrlParserResult result = new ScmUrlParserResult();
151
152 result.repository = new GitScmProviderRepository(scmSpecificUrl);
153
154 return result;
155 }
156
157 protected abstract GitCommand getAddCommand();
158
159
160 public AddScmResult add(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
161 throws ScmException {
162 return (AddScmResult) executeCommand(getAddCommand(), repository, fileSet, parameters);
163 }
164
165 protected abstract GitCommand getBranchCommand();
166
167
168 protected BranchScmResult branch(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
169 throws ScmException {
170 return (BranchScmResult) executeCommand(getBranchCommand(), repository, fileSet, parameters);
171 }
172
173 protected abstract GitCommand getChangeLogCommand();
174
175
176 public ChangeLogScmResult changelog(
177 ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
178 return (ChangeLogScmResult) executeCommand(getChangeLogCommand(), repository, fileSet, parameters);
179 }
180
181 protected abstract GitCommand getCheckInCommand();
182
183
184 public CheckInScmResult checkin(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
185 throws ScmException {
186 return (CheckInScmResult) executeCommand(getCheckInCommand(), repository, fileSet, parameters);
187 }
188
189 @Override
190 public CheckInScmResult checkIn(ScmRepository repository, ScmFileSet fileSet, CommandParameters parameters)
191 throws ScmException {
192 return (CheckInScmResult) getCheckInCommand().execute(repository.getProviderRepository(), fileSet, parameters);
193 }
194
195 protected abstract GitCommand getCheckOutCommand();
196
197
198 public CheckOutScmResult checkout(
199 ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
200 return (CheckOutScmResult) executeCommand(getCheckOutCommand(), repository, fileSet, parameters);
201 }
202
203 protected abstract GitCommand getDiffCommand();
204
205
206 public DiffScmResult diff(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
207 throws ScmException {
208 return (DiffScmResult) executeCommand(getDiffCommand(), repository, fileSet, parameters);
209 }
210
211 protected abstract GitCommand getExportCommand();
212
213
214 protected ExportScmResult export(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
215 throws ScmException {
216 return (ExportScmResult) executeCommand(getExportCommand(), repository, fileSet, parameters);
217 }
218
219 protected abstract GitCommand getRemoveCommand();
220
221
222 public RemoveScmResult remove(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
223 throws ScmException {
224 return (RemoveScmResult) executeCommand(getRemoveCommand(), repository, fileSet, parameters);
225 }
226
227 protected abstract GitCommand getStatusCommand();
228
229
230 public StatusScmResult status(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
231 throws ScmException {
232 return (StatusScmResult) executeCommand(getStatusCommand(), repository, fileSet, parameters);
233 }
234
235 protected abstract GitCommand getTagCommand();
236
237
238 public TagScmResult tag(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
239 throws ScmException {
240 return (TagScmResult) executeCommand(getTagCommand(), repository, fileSet, parameters);
241 }
242
243 protected abstract GitCommand getUntagCommand();
244
245
246 public UntagScmResult untag(ScmRepository repository, ScmFileSet fileSet, CommandParameters parameters)
247 throws ScmException {
248 return (UntagScmResult)
249 executeCommand(getUntagCommand(), repository.getProviderRepository(), fileSet, parameters);
250 }
251
252 protected abstract GitCommand getUpdateCommand();
253
254
255 public UpdateScmResult update(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
256 throws ScmException {
257 return (UpdateScmResult) executeCommand(getUpdateCommand(), repository, fileSet, parameters);
258 }
259
260 protected ScmResult executeCommand(
261 GitCommand command, ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
262 throws ScmException {
263 return command.execute(repository, fileSet, parameters);
264 }
265
266 protected abstract GitCommand getInfoCommand();
267
268 public InfoScmResult info(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
269 throws ScmException {
270 GitCommand cmd = getInfoCommand();
271
272 return (InfoScmResult) executeCommand(cmd, repository, fileSet, parameters);
273 }
274
275
276 protected BlameScmResult blame(ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters)
277 throws ScmException {
278 GitCommand cmd = getBlameCommand();
279
280 return (BlameScmResult) executeCommand(cmd, repository, fileSet, parameters);
281 }
282
283 protected abstract GitCommand getBlameCommand();
284
285
286 public RemoteInfoScmResult remoteInfo(
287 ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
288 GitCommand cmd = getRemoteInfoCommand();
289
290 return (RemoteInfoScmResult) executeCommand(cmd, repository, fileSet, parameters);
291 }
292
293 protected abstract GitCommand getRemoteInfoCommand();
294 }