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øl</a> 042 * 043 */ 044public class LocalCheckInCommand extends AbstractCheckInCommand implements LocalCommand { 045 /** {@inheritDoc} */ 046 protected CheckInScmResult executeCheckInCommand( 047 ScmProviderRepository repo, ScmFileSet fileSet, String message, ScmVersion version) throws ScmException { 048 LocalScmProviderRepository repository = (LocalScmProviderRepository) repo; 049 050 if (version != null && StringUtils.isNotEmpty(version.getName())) { 051 throw new ScmException("The local scm doesn't support tags."); 052 } 053 054 File root = new File(repository.getRoot()); 055 056 String module = repository.getModule(); 057 058 File source = new File(root, module); 059 060 File basedir = fileSet.getBasedir(); 061 062 if (!basedir.exists()) { 063 throw new ScmException("The working directory doesn't exist (" + basedir.getAbsolutePath() + ")."); 064 } 065 066 if (!root.exists()) { 067 throw new ScmException("The base directory doesn't exist (" + root.getAbsolutePath() + ")."); 068 } 069 070 if (!source.exists()) { 071 throw new ScmException("The module directory doesn't exist (" + source.getAbsolutePath() + ")."); 072 } 073 074 List<ScmFile> checkedInFiles = new ArrayList<>(); 075 076 try { 077 // Only copy files newer than in the repo 078 File repoRoot = new File(repository.getRoot(), repository.getModule()); 079 080 List<File> files = fileSet.getFileList(); 081 082 if (files.isEmpty()) { 083 files = FileUtils.getFiles(basedir, "**", null, false); 084 } 085 086 for (File file : files) { 087 String path = FilenameUtils.normalizeFilename(file.getPath()); 088 File repoFile = new File(repoRoot, path); 089 file = new File(basedir, path); 090 091 ScmFileStatus status; 092 093 if (repoFile.exists()) { 094 String repoFileContents = FileUtils.fileRead(repoFile); 095 096 String fileContents = FileUtils.fileRead(file); 097 098 if (logger.isDebugEnabled()) { 099 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}