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 | 0 | 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 | 0 | if ( version != null ) |
51 | |
{ |
52 | 0 | throw new ScmException( "The local scm doesn't support tags." ); |
53 | |
} |
54 | |
|
55 | 0 | LocalScmProviderRepository repository = (LocalScmProviderRepository) repo; |
56 | |
|
57 | 0 | File root = new File( repository.getRoot() ); |
58 | |
|
59 | 0 | String module = repository.getModule(); |
60 | |
|
61 | 0 | File source = new File( root, module ); |
62 | |
|
63 | 0 | if ( !root.exists() ) |
64 | |
{ |
65 | 0 | throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." ); |
66 | |
} |
67 | |
|
68 | 0 | if ( !source.exists() ) |
69 | |
{ |
70 | 0 | throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." ); |
71 | |
} |
72 | |
|
73 | 0 | if ( getLogger().isInfoEnabled() ) |
74 | |
{ |
75 | 0 | getLogger().info( "Listing files of '" + source.getAbsolutePath() + "'." ); |
76 | |
} |
77 | |
|
78 | |
try |
79 | |
{ |
80 | 0 | if ( fileSet.getFileList() == null || fileSet.getFileList().isEmpty() ) |
81 | |
{ |
82 | 0 | return new LocalListScmResult( null, getFiles( source, source, recursive ) ); |
83 | |
} |
84 | |
else |
85 | |
{ |
86 | 0 | List<ScmFile> files = new ArrayList<ScmFile>(); |
87 | 0 | Iterator<File> it = fileSet.getFileList().iterator(); |
88 | |
|
89 | 0 | while ( it.hasNext() ) |
90 | |
{ |
91 | 0 | File file = (File) it.next(); |
92 | |
|
93 | 0 | files.addAll( getFiles( source, new File( source, file.getPath() ), recursive ) ); |
94 | 0 | } |
95 | |
|
96 | 0 | return new LocalListScmResult( null, files ); |
97 | |
} |
98 | |
} |
99 | 0 | catch ( Exception e ) |
100 | |
{ |
101 | 0 | 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 | 0 | if ( !directory.exists() ) |
109 | |
{ |
110 | 0 | throw new Exception( "Directory '" + directory.getAbsolutePath() + "' doesn't exist." ); |
111 | |
} |
112 | |
|
113 | 0 | List<ScmFile> files = new ArrayList<ScmFile>(); |
114 | |
|
115 | 0 | File[] filesArray = directory.listFiles(); |
116 | |
|
117 | 0 | if ( filesArray != null ) |
118 | |
{ |
119 | 0 | for ( int i = 0; i < filesArray.length; i++ ) |
120 | |
{ |
121 | 0 | File f = filesArray[i]; |
122 | |
|
123 | 0 | String path = f.getAbsolutePath().substring( source.getAbsolutePath().length() ); |
124 | 0 | path = StringUtils.replace( path, "\\", "/" ); |
125 | 0 | path = StringUtils.replace( path, "/./", "/" ); |
126 | |
|
127 | 0 | files.add( new ScmFile( path, ScmFileStatus.CHECKED_IN ) ); |
128 | |
|
129 | 0 | if ( f.isDirectory() && recursive ) |
130 | |
{ |
131 | 0 | files.addAll( getFiles( source, f, recursive ) ); |
132 | |
} |
133 | |
} |
134 | |
} |
135 | |
|
136 | 0 | return files; |
137 | |
} |
138 | |
} |