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.jgit.command.status;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.List;
24
25 import org.apache.maven.scm.ScmException;
26 import org.apache.maven.scm.ScmFile;
27 import org.apache.maven.scm.ScmFileSet;
28 import org.apache.maven.scm.ScmFileStatus;
29 import org.apache.maven.scm.command.status.AbstractStatusCommand;
30 import org.apache.maven.scm.command.status.StatusScmResult;
31 import org.apache.maven.scm.provider.ScmProviderRepository;
32 import org.apache.maven.scm.provider.git.command.GitCommand;
33 import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
34 import org.eclipse.jgit.api.Git;
35 import org.eclipse.jgit.api.Status;
36
37
38
39
40
41
42 public class JGitStatusCommand extends AbstractStatusCommand implements GitCommand {
43
44
45
46 protected StatusScmResult executeStatusCommand(ScmProviderRepository repo, ScmFileSet fileSet) throws ScmException {
47 Git git = null;
48 try {
49 git = JGitUtils.openRepo(fileSet.getBasedir());
50 Status status = git.status().call();
51 List<ScmFile> changedFiles = getFileStati(status);
52
53 return new StatusScmResult("JGit status", changedFiles);
54 } catch (Exception e) {
55 throw new ScmException("JGit status failure!", e);
56 } finally {
57 JGitUtils.closeRepo(git);
58 }
59 }
60
61 private List<ScmFile> getFileStati(Status status) {
62 List<ScmFile> all = new ArrayList<>();
63 addAsScmFiles(all, status.getAdded(), ScmFileStatus.ADDED);
64 addAsScmFiles(all, status.getChanged(), ScmFileStatus.UPDATED);
65 addAsScmFiles(all, status.getConflicting(), ScmFileStatus.CONFLICT);
66 addAsScmFiles(all, status.getModified(), ScmFileStatus.MODIFIED);
67 addAsScmFiles(all, status.getRemoved(), ScmFileStatus.DELETED);
68 return all;
69 }
70
71 private void addAsScmFiles(Collection<ScmFile> all, Collection<String> files, ScmFileStatus status) {
72 for (String f : files) {
73 all.add(new ScmFile(f, status));
74 }
75 }
76 }