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.local.command.list;
20
21 import java.io.File;
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.maven.scm.ScmException;
27 import org.apache.maven.scm.ScmFile;
28 import org.apache.maven.scm.ScmFileSet;
29 import org.apache.maven.scm.ScmFileStatus;
30 import org.apache.maven.scm.ScmVersion;
31 import org.apache.maven.scm.command.list.AbstractListCommand;
32 import org.apache.maven.scm.command.list.ListScmResult;
33 import org.apache.maven.scm.provider.ScmProviderRepository;
34 import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
35
36
37
38
39
40 public class LocalListCommand extends AbstractListCommand {
41
42 protected ListScmResult executeListCommand(
43 ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive, ScmVersion version) throws ScmException {
44 if (version != null) {
45 throw new ScmException("The local scm doesn't support tags.");
46 }
47
48 LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
49
50 File root = new File(repository.getRoot());
51
52 String module = repository.getModule();
53
54 File source = new File(root, module);
55
56 if (!root.exists()) {
57 throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ").");
58 }
59
60 if (!source.exists()) {
61 throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ").");
62 }
63
64 if (logger.isInfoEnabled()) {
65 logger.info("Listing files of '" + source.getAbsolutePath() + "'.");
66 }
67
68 try {
69 if (fileSet.getFileList() == null || fileSet.getFileList().isEmpty()) {
70 return new LocalListScmResult(null, getFiles(source, source, recursive));
71 } else {
72 List<ScmFile> files = new ArrayList<>();
73
74 for (File file : fileSet.getFileList()) {
75 files.addAll(getFiles(source, new File(source, file.getPath()), recursive));
76 }
77
78 return new LocalListScmResult(null, files);
79 }
80 } catch (Exception e) {
81 return new ListScmResult(null, "The svn command failed.", e.getMessage(), false);
82 }
83 }
84
85 private List<ScmFile> getFiles(File source, File directory, boolean recursive) throws Exception {
86 if (!directory.exists()) {
87 throw new Exception("Directory '" + directory.getAbsolutePath() + "' doesn't exist.");
88 }
89
90 List<ScmFile> files = new ArrayList<>();
91
92 File[] filesArray = directory.listFiles();
93
94 if (filesArray != null) {
95 for (int i = 0; i < filesArray.length; i++) {
96 File f = filesArray[i];
97
98 String path =
99 f.getAbsolutePath().substring(source.getAbsolutePath().length());
100 path = StringUtils.replace(path, "\\", "/");
101 path = StringUtils.replace(path, "/./", "/");
102
103 files.add(new ScmFile(path, ScmFileStatus.CHECKED_IN));
104
105 if (f.isDirectory() && recursive) {
106 files.addAll(getFiles(source, f, recursive));
107 }
108 }
109 }
110
111 return files;
112 }
113 }