001    package org.apache.maven.scm.provider.local.command.checkin;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import java.io.File;
023    import java.io.IOException;
024    import java.util.ArrayList;
025    import java.util.List;
026    
027    import org.apache.maven.scm.ScmException;
028    import org.apache.maven.scm.ScmFile;
029    import org.apache.maven.scm.ScmFileSet;
030    import org.apache.maven.scm.ScmFileStatus;
031    import org.apache.maven.scm.ScmVersion;
032    import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
033    import org.apache.maven.scm.command.checkin.CheckInScmResult;
034    import org.apache.maven.scm.provider.ScmProviderRepository;
035    import org.apache.maven.scm.provider.local.command.LocalCommand;
036    import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
037    import org.codehaus.plexus.util.FileUtils;
038    import org.codehaus.plexus.util.StringUtils;
039    
040    /**
041     * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
042     * @version $Id: LocalCheckInCommand.java 1054455 2011-01-02 18:40:29Z olamy $
043     */
044    public class LocalCheckInCommand
045        extends AbstractCheckInCommand
046        implements LocalCommand
047    {
048        /** {@inheritDoc} */
049        protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
050                                                          ScmVersion version )
051            throws ScmException
052        {
053            LocalScmProviderRepository repository = (LocalScmProviderRepository) repo;
054    
055            if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
056            {
057                throw new ScmException( "The local scm doesn't support tags." );
058            }
059    
060            File root = new File( repository.getRoot() );
061    
062            String module = repository.getModule();
063    
064            File source = new File( root, module );
065    
066            File baseDestination = fileSet.getBasedir();
067    
068            if ( !baseDestination.exists() )
069            {
070                throw new ScmException(
071                    "The working directory doesn't exist (" + baseDestination.getAbsolutePath() + ")." );
072            }
073    
074            if ( !root.exists() )
075            {
076                throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." );
077            }
078    
079            if ( !source.exists() )
080            {
081                throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." );
082            }
083    
084            List<ScmFile> checkedInFiles = new ArrayList<ScmFile>();
085    
086            try
087            {
088                // Only copy files newer than in the repo
089                File repoRoot = new File( repository.getRoot(), repository.getModule() );
090    
091                List<File> files = fileSet.getFileList();
092    
093                if ( files.isEmpty() )
094                {
095                    @SuppressWarnings( "unchecked" )
096                    List<File> listFiles = FileUtils.getFiles( baseDestination, "**", null, false ); 
097                   
098                    files = listFiles;
099                }
100    
101                for ( File file : files )
102                {
103                    String path = file.getPath().replace( '\\', '/' );
104                    File repoFile = new File( repoRoot, path );
105                    file = new File( baseDestination, path );
106    
107                    ScmFileStatus status;
108    
109                    if ( repoFile.exists() )
110                    {
111                        String repoFileContents = FileUtils.fileRead( repoFile );
112    
113                        String fileContents = FileUtils.fileRead( file );
114    
115                        if ( getLogger().isDebugEnabled() )
116                        {
117                            getLogger().debug( "fileContents:" + fileContents );
118                            getLogger().debug( "repoFileContents:" + repoFileContents );
119                        }
120                        if ( fileContents.equals( repoFileContents ) )
121                        {
122                            continue;
123                        }
124    
125                        status = ScmFileStatus.CHECKED_IN;
126                    }
127                    else if ( repository.isFileAdded( path ) )
128                    {
129                        status = ScmFileStatus.CHECKED_IN;
130                    }
131                    else
132                    {
133                        if ( getLogger().isWarnEnabled() )
134                        {
135                            getLogger().warn( "skipped unknown file in checkin:" + path );
136                        }
137                        // unknown file, skip
138                        continue;
139                    }
140    
141                    FileUtils.copyFile( file, repoFile );
142                    ScmFile scmFile = new ScmFile( path, status);
143                    getLogger().info( scmFile.toString() );
144                    checkedInFiles.add( scmFile );
145                }
146            }
147            catch ( IOException ex )
148            {
149                throw new ScmException( "Error while checking in the files.", ex );
150            }
151    
152            return new CheckInScmResult( null, checkedInFiles );
153        }
154    }