1 | |
package org.apache.maven.scm.provider.local.command.update; |
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.changelog.ChangeLogCommand; |
28 | |
import org.apache.maven.scm.command.update.AbstractUpdateCommand; |
29 | |
import org.apache.maven.scm.command.update.UpdateScmResult; |
30 | |
import org.apache.maven.scm.provider.ScmProviderRepository; |
31 | |
import org.apache.maven.scm.provider.local.command.LocalCommand; |
32 | |
import org.apache.maven.scm.provider.local.command.changelog.LocalChangeLogCommand; |
33 | |
import org.apache.maven.scm.provider.local.metadata.LocalScmMetadata; |
34 | |
import org.apache.maven.scm.provider.local.metadata.LocalScmMetadataUtils; |
35 | |
import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository; |
36 | |
import org.codehaus.plexus.util.FileUtils; |
37 | |
|
38 | |
import java.io.File; |
39 | |
import java.io.IOException; |
40 | |
import java.util.ArrayList; |
41 | |
import java.util.Iterator; |
42 | |
import java.util.List; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | 0 | public class LocalUpdateCommand |
50 | |
extends AbstractUpdateCommand |
51 | |
implements LocalCommand |
52 | |
{ |
53 | |
|
54 | |
protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version ) |
55 | |
throws ScmException |
56 | |
{ |
57 | 0 | LocalScmProviderRepository repository = (LocalScmProviderRepository) repo; |
58 | |
|
59 | 0 | if ( version != null ) |
60 | |
{ |
61 | 0 | throw new ScmException( "The local scm doesn't support tags." ); |
62 | |
} |
63 | |
|
64 | 0 | File root = new File( repository.getRoot() ); |
65 | |
|
66 | 0 | String module = repository.getModule(); |
67 | |
|
68 | 0 | File source = new File( root, module ); |
69 | |
|
70 | 0 | File baseDestination = fileSet.getBasedir(); |
71 | |
|
72 | 0 | if ( !baseDestination.exists() ) |
73 | |
{ |
74 | 0 | throw new ScmException( |
75 | |
"The working directory doesn't exist (" + baseDestination.getAbsolutePath() + ")." ); |
76 | |
} |
77 | |
|
78 | 0 | if ( !root.exists() ) |
79 | |
{ |
80 | 0 | throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." ); |
81 | |
} |
82 | |
|
83 | 0 | if ( !source.exists() ) |
84 | |
{ |
85 | 0 | throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." ); |
86 | |
} |
87 | |
|
88 | 0 | if ( !baseDestination.exists() && !baseDestination.isDirectory() ) |
89 | |
{ |
90 | 0 | throw new ScmException( "The destination directory isn't a directory or doesn't exist (" |
91 | |
+ baseDestination.getAbsolutePath() + ")." ); |
92 | |
} |
93 | |
|
94 | |
List<ScmFile> updatedFiles; |
95 | |
|
96 | |
try |
97 | |
{ |
98 | 0 | if ( getLogger().isInfoEnabled() ) |
99 | |
{ |
100 | 0 | getLogger().info( |
101 | |
"Updating '" + baseDestination.getAbsolutePath() + "' from '" |
102 | |
+ source.getAbsolutePath() + "'." ); |
103 | |
} |
104 | |
|
105 | |
@SuppressWarnings( "unchecked" ) |
106 | 0 | List<File> fileList = FileUtils.getFiles( source.getAbsoluteFile(), "**", null ); |
107 | 0 | List<File> list = fileList; |
108 | 0 | updatedFiles = update( source, baseDestination, list ); |
109 | |
|
110 | |
|
111 | 0 | LocalScmMetadataUtils metadataUtils = new LocalScmMetadataUtils( getLogger() ); |
112 | 0 | LocalScmMetadata originalMetadata = metadataUtils.readMetadata( baseDestination ); |
113 | 0 | if ( originalMetadata != null ) |
114 | |
{ |
115 | 0 | LocalScmMetadata newMetadata = metadataUtils.buildMetadata( source ); |
116 | 0 | for ( Iterator<String> it = originalMetadata.getRepositoryFileNames().iterator(); it.hasNext(); ) |
117 | |
{ |
118 | 0 | String filename = it.next(); |
119 | 0 | if ( !newMetadata.getRepositoryFileNames().contains( filename ) ) |
120 | |
{ |
121 | 0 | File localFile = new File( baseDestination, filename ); |
122 | 0 | if ( localFile.exists() ) |
123 | |
{ |
124 | 0 | localFile.delete(); |
125 | 0 | updatedFiles.add( new ScmFile( "/" + filename, ScmFileStatus.UPDATED ) ); |
126 | |
} |
127 | |
} |
128 | 0 | } |
129 | |
} |
130 | |
|
131 | |
|
132 | 0 | metadataUtils.writeMetadata( baseDestination, metadataUtils.buildMetadata( source ) ); |
133 | |
|
134 | |
} |
135 | 0 | catch ( IOException ex ) |
136 | |
{ |
137 | 0 | throw new ScmException( "Error while checking out the files.", ex ); |
138 | 0 | } |
139 | |
|
140 | 0 | return new LocalUpdateScmResult( null, updatedFiles ); |
141 | |
} |
142 | |
|
143 | |
private List<ScmFile> update( File source, File baseDestination, List<File> files ) |
144 | |
throws ScmException, IOException |
145 | |
{ |
146 | 0 | String sourcePath = source.getAbsolutePath(); |
147 | |
|
148 | 0 | List<ScmFile> updatedFiles = new ArrayList<ScmFile>(); |
149 | |
|
150 | 0 | for ( Iterator<File> i = files.iterator(); i.hasNext(); ) |
151 | |
{ |
152 | 0 | File repositoryFile = i.next(); |
153 | |
|
154 | 0 | File repositoryDirectory = repositoryFile.getParentFile(); |
155 | |
|
156 | |
|
157 | 0 | if ( repositoryDirectory != null && repositoryDirectory.getName().equals( "CVS" ) ) |
158 | |
{ |
159 | 0 | continue; |
160 | |
} |
161 | |
|
162 | 0 | String dest = repositoryFile.getAbsolutePath().substring( sourcePath.length() + 1 ); |
163 | |
|
164 | 0 | File destinationFile = new File( baseDestination, dest ); |
165 | |
|
166 | 0 | String repositoryFileContents = FileUtils.fileRead( repositoryFile ); |
167 | |
|
168 | 0 | if ( destinationFile.exists() ) |
169 | |
{ |
170 | 0 | String destionationFileContents = FileUtils.fileRead( destinationFile ); |
171 | |
|
172 | 0 | if ( repositoryFileContents.equals( destionationFileContents ) ) |
173 | |
{ |
174 | 0 | continue; |
175 | |
} |
176 | |
} |
177 | |
|
178 | 0 | File destinationDirectory = destinationFile.getParentFile(); |
179 | |
|
180 | 0 | if ( !destinationDirectory.exists() && !destinationDirectory.mkdirs() ) |
181 | |
{ |
182 | 0 | throw new ScmException( |
183 | |
"Could not create destination directory '" + destinationDirectory.getAbsolutePath() + "'." ); |
184 | |
} |
185 | |
|
186 | |
ScmFileStatus status; |
187 | |
|
188 | 0 | if ( destinationFile.exists() ) |
189 | |
{ |
190 | 0 | status = ScmFileStatus.UPDATED; |
191 | |
} |
192 | |
else |
193 | |
{ |
194 | 0 | status = ScmFileStatus.ADDED; |
195 | |
} |
196 | |
|
197 | 0 | FileUtils.copyFileToDirectory( repositoryFile, destinationDirectory ); |
198 | |
|
199 | 0 | int chop = baseDestination.getAbsolutePath().length(); |
200 | |
|
201 | 0 | String fileName = "/" + destinationFile.getAbsolutePath().substring( chop + 1 ); |
202 | |
|
203 | 0 | updatedFiles.add( new ScmFile( fileName, status ) ); |
204 | 0 | } |
205 | |
|
206 | 0 | return updatedFiles; |
207 | |
} |
208 | |
|
209 | |
|
210 | |
protected ChangeLogCommand getChangeLogCommand() |
211 | |
{ |
212 | 0 | return new LocalChangeLogCommand(); |
213 | |
} |
214 | |
} |