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.checkin;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  import org.apache.commons.lang3.StringUtils;
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.checkin.AbstractCheckInCommand;
33  import org.apache.maven.scm.command.checkin.CheckInScmResult;
34  import org.apache.maven.scm.provider.ScmProviderRepository;
35  import org.apache.maven.scm.provider.local.command.LocalCommand;
36  import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
37  import org.apache.maven.scm.util.FilenameUtils;
38  import org.codehaus.plexus.util.FileUtils;
39  
40  /**
41   * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
42   */
43  public class LocalCheckInCommand extends AbstractCheckInCommand implements LocalCommand {
44      /**
45       * {@inheritDoc}
46       */
47      protected CheckInScmResult executeCheckInCommand(
48              ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion version) throws ScmException {
49          LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
50  
51          if (version != null && StringUtils.isNotEmpty(version.getName())) {
52              throw new ScmException("The local scm doesn't support tags.");
53          }
54  
55          File root = new File(repository.getRoot());
56  
57          String module = repository.getModule();
58  
59          File source = new File(root, module);
60  
61          File basedir = fileSet.getBasedir();
62  
63          if (!basedir.exists()) {
64              throw new ScmException("The working directory doesn't exist (" + basedir.getAbsolutePath() + ").");
65          }
66  
67          if (!root.exists()) {
68              throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ").");
69          }
70  
71          if (!source.exists()) {
72              throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ").");
73          }
74  
75          List<ScmFile> checkedInFiles = new ArrayList<>();
76  
77          try {
78              // Only copy files newer than in the repo
79              File repoRoot = new File(repository.getRoot(), repository.getModule());
80  
81              List<File> files = fileSet.getFileList();
82  
83              if (files.isEmpty()) {
84                  files = FileUtils.getFiles(basedir, "**", null, false);
85              }
86  
87              for (File file : files) {
88                  String path = FilenameUtils.normalizeFilename(file.getPath());
89                  File repoFile = new File(repoRoot, path);
90                  file = new File(basedir, path);
91  
92                  ScmFileStatus status;
93  
94                  if (repoFile.exists()) {
95                      String repoFileContents = FileUtils.fileRead(repoFile);
96  
97                      String fileContents = FileUtils.fileRead(file);
98  
99                      if (logger.isDebugEnabled()) {
100                         logger.debug("fileContents:" + fileContents);
101                         logger.debug("repoFileContents:" + repoFileContents);
102                     }
103                     if (fileContents.equals(repoFileContents)) {
104                         continue;
105                     }
106 
107                     status = ScmFileStatus.CHECKED_IN;
108                 } else if (repository.isFileAdded(path)) {
109                     status = ScmFileStatus.CHECKED_IN;
110                 } else {
111                     if (logger.isWarnEnabled()) {
112                         logger.warn("skipped unknown file in checkin:" + path);
113                     }
114                     // unknown file, skip
115                     continue;
116                 }
117 
118                 FileUtils.copyFile(file, repoFile);
119                 ScmFile scmFile = new ScmFile(path, status);
120                 logger.info(scmFile.toString());
121                 checkedInFiles.add(scmFile);
122             }
123         } catch (IOException ex) {
124             throw new ScmException("Error while checking in the files.", ex);
125         }
126 
127         return new CheckInScmResult(null, checkedInFiles);
128     }
129 }