View Javadoc

1   package org.apache.maven.scm.provider.local.command.update;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
46   * @version $Id: LocalUpdateCommand.java 524909 2007-04-02 20:02:44Z evenisse $
47   */
48  public class LocalUpdateCommand
49      extends AbstractUpdateCommand
50      implements LocalCommand
51  {
52      protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion version )
53          throws ScmException
54      {
55          LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
56  
57          if ( version != null )
58          {
59              throw new ScmException( "The local scm doesn't support tags." );
60          }
61  
62          File root = new File( repository.getRoot() );
63  
64          String module = repository.getModule();
65  
66          File source = new File( root, module );
67  
68          File baseDestination = fileSet.getBasedir();
69  
70          if ( !baseDestination.exists() )
71          {
72              throw new ScmException(
73                  "The working directory doesn't exist (" + baseDestination.getAbsolutePath() + ")." );
74          }
75  
76          if ( !root.exists() )
77          {
78              throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." );
79          }
80  
81          if ( !source.exists() )
82          {
83              throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." );
84          }
85  
86          if ( !baseDestination.exists() && !baseDestination.isDirectory() )
87          {
88              throw new ScmException( "The destination directory isn't a directory or doesn't exist (" +
89                  baseDestination.getAbsolutePath() + ")." );
90          }
91  
92          List updatedFiles;
93  
94          try
95          {
96              getLogger().info(
97                  "Updating '" + baseDestination.getAbsolutePath() + "' from '" + source.getAbsolutePath() + "'." );
98  
99              List fileList = FileUtils.getFiles( source.getAbsoluteFile(), "**", null );
100 
101             updatedFiles = update( source, baseDestination, fileList );
102 
103             // process deletions in repository
104             LocalScmMetadataUtils metadataUtils = new LocalScmMetadataUtils( getLogger() );
105             LocalScmMetadata originalMetadata = metadataUtils.readMetadata( baseDestination );
106             if ( originalMetadata != null )
107             {
108                 LocalScmMetadata newMetadata = metadataUtils.buildMetadata( source );
109                 for ( Iterator it = originalMetadata.getRepositoryFileNames().iterator(); it.hasNext(); )
110                 {
111                     String filename = (String) it.next();
112                     if ( !newMetadata.getRepositoryFileNames().contains( filename ) )
113                     {
114                         File localFile = new File( baseDestination, filename );
115                         if ( localFile.exists() )
116                         {
117                             localFile.delete();
118                             updatedFiles.add( new ScmFile( "/" + filename, ScmFileStatus.UPDATED ) );
119                         }
120                     }
121                 }
122             }
123 
124             // rewrite metadata file
125             metadataUtils.writeMetadata( baseDestination, metadataUtils.buildMetadata( source ) );
126 
127         }
128         catch ( IOException ex )
129         {
130             throw new ScmException( "Error while checking out the files.", ex );
131         }
132 
133         return new LocalUpdateScmResult( null, updatedFiles );
134     }
135 
136     private List update( File source, File baseDestination, List files )
137         throws ScmException, IOException
138     {
139         String sourcePath = source.getAbsolutePath();
140 
141         List updatedFiles = new ArrayList();
142 
143         for ( Iterator i = files.iterator(); i.hasNext(); )
144         {
145             File repositoryFile = (File) i.next();
146 
147             File repositoryDirectory = repositoryFile.getParentFile();
148 
149             // TODO: Add more excludes here
150             if ( repositoryDirectory != null && repositoryDirectory.getName().equals( "CVS" ) )
151             {
152                 continue;
153             }
154 
155             String dest = repositoryFile.getAbsolutePath().substring( sourcePath.length() + 1 );
156 
157             File destinationFile = new File( baseDestination, dest );
158 
159             String repositoryFileContents = FileUtils.fileRead( repositoryFile );
160 
161             if ( destinationFile.exists() )
162             {
163                 String destionationFileContents = FileUtils.fileRead( destinationFile );
164 
165                 if ( repositoryFileContents.equals( destionationFileContents ) )
166                 {
167                     continue;
168                 }
169             }
170 
171             File destinationDirectory = destinationFile.getParentFile();
172 
173             if ( !destinationDirectory.exists() && !destinationDirectory.mkdirs() )
174             {
175                 throw new ScmException(
176                     "Could not create destination directory '" + destinationDirectory.getAbsolutePath() + "'." );
177             }
178 
179             ScmFileStatus status;
180 
181             if ( destinationFile.exists() )
182             {
183                 status = ScmFileStatus.UPDATED;
184             }
185             else
186             {
187                 status = ScmFileStatus.ADDED;
188             }
189 
190             FileUtils.copyFileToDirectory( repositoryFile, destinationDirectory );
191 
192             int chop = baseDestination.getAbsolutePath().length();
193 
194             String fileName = "/" + destinationFile.getAbsolutePath().substring( chop + 1 );
195 
196             updatedFiles.add( new ScmFile( fileName, status ) );
197         }
198 
199         return updatedFiles;
200     }
201 
202     /**
203      * @see org.apache.maven.scm.command.update.AbstractUpdateCommand#getChangeLogCommand()
204      */
205     protected ChangeLogCommand getChangeLogCommand()
206     {
207         return new LocalChangeLogCommand();
208     }
209 }