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