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