1 package org.apache.maven.scm.provider.local.command.list;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.ScmException;
23 import org.apache.maven.scm.ScmFile;
24 import org.apache.maven.scm.ScmFileSet;
25 import org.apache.maven.scm.ScmFileStatus;
26 import org.apache.maven.scm.ScmVersion;
27 import org.apache.maven.scm.command.list.AbstractListCommand;
28 import org.apache.maven.scm.command.list.ListScmResult;
29 import org.apache.maven.scm.provider.ScmProviderRepository;
30 import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
31 import org.codehaus.plexus.util.StringUtils;
32
33 import java.io.File;
34 import java.util.ArrayList;
35 import java.util.Iterator;
36 import java.util.List;
37
38
39
40
41
42 public class LocalListCommand
43 extends AbstractListCommand
44 {
45
46 protected ListScmResult executeListCommand( ScmProviderRepository repo, ScmFileSet fileSet, boolean recursive,
47 ScmVersion version )
48 throws ScmException
49 {
50 if ( version != null )
51 {
52 throw new ScmException( "The local scm doesn't support tags." );
53 }
54
55 LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
56
57 File root = new File( repository.getRoot() );
58
59 String module = repository.getModule();
60
61 File source = new File( root, module );
62
63 if ( !root.exists() )
64 {
65 throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." );
66 }
67
68 if ( !source.exists() )
69 {
70 throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." );
71 }
72
73 if ( getLogger().isInfoEnabled() )
74 {
75 getLogger().info( "Listing files of '" + source.getAbsolutePath() + "'." );
76 }
77
78 try
79 {
80 if ( fileSet.getFileList() == null || fileSet.getFileList().isEmpty() )
81 {
82 return new LocalListScmResult( null, getFiles( source, source, recursive ) );
83 }
84 else
85 {
86 List<ScmFile> files = new ArrayList<ScmFile>();
87 Iterator<File> it = fileSet.getFileList().iterator();
88
89 while ( it.hasNext() )
90 {
91 File file = (File) it.next();
92
93 files.addAll( getFiles( source, new File( source, file.getPath() ), recursive ) );
94 }
95
96 return new LocalListScmResult( null, files );
97 }
98 }
99 catch ( Exception e )
100 {
101 return new ListScmResult( null, "The svn command failed.", e.getMessage(), false );
102 }
103 }
104
105 private List<ScmFile> getFiles( File source, File directory, boolean recursive )
106 throws Exception
107 {
108 if ( !directory.exists() )
109 {
110 throw new Exception( "Directory '" + directory.getAbsolutePath() + "' doesn't exist." );
111 }
112
113 List<ScmFile> files = new ArrayList<ScmFile>();
114
115 File[] filesArray = directory.listFiles();
116
117 if ( filesArray != null )
118 {
119 for ( int i = 0; i < filesArray.length; i++ )
120 {
121 File f = filesArray[i];
122
123 String path = f.getAbsolutePath().substring( source.getAbsolutePath().length() );
124 path = StringUtils.replace( path, "\\", "/" );
125 path = StringUtils.replace( path, "/./", "/" );
126
127 files.add( new ScmFile( path, ScmFileStatus.CHECKED_IN ) );
128
129 if ( f.isDirectory() && recursive )
130 {
131 files.addAll( getFiles( source, f, recursive ) );
132 }
133 }
134 }
135
136 return files;
137 }
138 }