001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.local.command.checkin;
020
021import java.io.File;
022import java.io.IOException;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.commons.lang3.StringUtils;
027import org.apache.maven.scm.ScmException;
028import org.apache.maven.scm.ScmFile;
029import org.apache.maven.scm.ScmFileSet;
030import org.apache.maven.scm.ScmFileStatus;
031import org.apache.maven.scm.ScmVersion;
032import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
033import org.apache.maven.scm.command.checkin.CheckInScmResult;
034import org.apache.maven.scm.provider.ScmProviderRepository;
035import org.apache.maven.scm.provider.local.command.LocalCommand;
036import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
037import org.apache.maven.scm.util.FilenameUtils;
038import org.codehaus.plexus.util.FileUtils;
039
040/**
041 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
042 */
043public class LocalCheckInCommand extends AbstractCheckInCommand implements LocalCommand {
044    /**
045     * {@inheritDoc}
046     */
047    protected CheckInScmResult executeCheckInCommand(
048            ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion version) throws ScmException {
049        LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
050
051        if (version != null && StringUtils.isNotEmpty(version.getName())) {
052            throw new ScmException("The local scm doesn't support tags.");
053        }
054
055        File root = new File(repository.getRoot());
056
057        String module = repository.getModule();
058
059        File source = new File(root, module);
060
061        File basedir = fileSet.getBasedir();
062
063        if (!basedir.exists()) {
064            throw new ScmException("The working directory doesn't exist (" + basedir.getAbsolutePath() + ").");
065        }
066
067        if (!root.exists()) {
068            throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ").");
069        }
070
071        if (!source.exists()) {
072            throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ").");
073        }
074
075        List<ScmFile> checkedInFiles = new ArrayList<>();
076
077        try {
078            // Only copy files newer than in the repo
079            File repoRoot = new File(repository.getRoot(), repository.getModule());
080
081            List<File> files = fileSet.getFileList();
082
083            if (files.isEmpty()) {
084                files = FileUtils.getFiles(basedir, "**", null, false);
085            }
086
087            for (File file : files) {
088                String path = FilenameUtils.normalizeFilename(file.getPath());
089                File repoFile = new File(repoRoot, path);
090                file = new File(basedir, path);
091
092                ScmFileStatus status;
093
094                if (repoFile.exists()) {
095                    String repoFileContents = FileUtils.fileRead(repoFile);
096
097                    String fileContents = FileUtils.fileRead(file);
098
099                    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}