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