001    package org.apache.maven.scm.provider.local.command.changelog;
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.Date;
026    import java.util.List;
027    
028    import org.apache.maven.scm.ChangeFile;
029    import org.apache.maven.scm.ChangeSet;
030    import org.apache.maven.scm.ScmBranch;
031    import org.apache.maven.scm.ScmException;
032    import org.apache.maven.scm.ScmFileSet;
033    import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
034    import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
035    import org.apache.maven.scm.command.changelog.ChangeLogSet;
036    import org.apache.maven.scm.provider.ScmProviderRepository;
037    import org.apache.maven.scm.provider.local.repository.LocalScmProviderRepository;
038    import org.codehaus.plexus.util.FileUtils;
039    
040    /**
041     * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
042     * @author Olivier Lamy
043     * @version $Id: LocalChangeLogCommand.java 1054408 2011-01-02 14:05:25Z olamy $
044     */
045    public class LocalChangeLogCommand
046        extends AbstractChangeLogCommand
047    {
048        /** {@inheritDoc} */
049        protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repository, ScmFileSet fileSet,
050                                                              Date startDate, Date endDate, ScmBranch branch,
051                                                              String datePattern )
052            throws ScmException
053        {
054            LocalScmProviderRepository repo = (LocalScmProviderRepository) repository;
055    
056            if ( branch != null )
057            {
058                throw new ScmException( "The local scm doesn't support tags." );
059            }
060    
061            File root = new File( repo.getRoot() );
062    
063            String module = repo.getModule();
064    
065            File source = new File( root, module );
066    
067            File baseDestination = fileSet.getBasedir();
068    
069            if ( !baseDestination.exists() )
070            {
071                throw new ScmException(
072                    "The working directory doesn't exist (" + baseDestination.getAbsolutePath() + ")." );
073            }
074    
075            if ( !root.exists() )
076            {
077                throw new ScmException( "The base directory doesn't exist (" + root.getAbsolutePath() + ")." );
078            }
079    
080            if ( !source.exists() )
081            {
082                throw new ScmException( "The module directory doesn't exist (" + source.getAbsolutePath() + ")." );
083            }
084    
085            List<ChangeSet> changeLogList = new ArrayList<ChangeSet>();
086    
087            try
088            {
089                File repoRoot = new File( repo.getRoot(), repo.getModule() );
090    
091                List<File> files = fileSet.getFileList();
092    
093                if ( files.isEmpty() )
094                {
095                    @SuppressWarnings( "unchecked" )
096                    List<File> fileList = FileUtils.getFiles( baseDestination, "**", null, false );
097                    files = fileList;
098                }
099    
100                for ( File file : files )
101                {
102    
103                    String path = file.getPath().replace( '\\', '/' );
104    
105                    File repoFile = new File( repoRoot, path );
106    
107                    file = new File( baseDestination, path );
108    
109                    ChangeSet changeSet = new ChangeSet();
110    
111                    int chop = repoRoot.getAbsolutePath().length();
112    
113                    String fileName = "/" + repoFile.getAbsolutePath().substring( chop + 1 );
114    
115                    changeSet.addFile( new ChangeFile( fileName, null ) );
116    
117                    if ( repoFile.exists() )
118                    {
119                        long lastModified = repoFile.lastModified();
120    
121                        Date modifiedDate = new Date( lastModified );
122    
123                        if ( startDate != null )
124                        {
125                            if ( startDate.before( modifiedDate ) || startDate.equals( modifiedDate ) )
126                            {
127                                if ( endDate != null )
128                                {
129                                    if ( endDate.after( modifiedDate ) || endDate.equals( modifiedDate ) )
130                                    {
131                                        // nop
132                                    }
133                                    else
134                                    {
135                                        continue;
136                                    }
137                                }
138                            }
139                            else
140                            {
141                                continue;
142                            }
143                        }
144    
145                        changeSet.setDate( modifiedDate );
146    
147                        changeLogList.add( changeSet );
148                    }
149                    else
150                    {
151                        // This file is deleted
152                        changeLogList.add( changeSet );
153                    }
154                }
155            }
156            catch ( IOException ex )
157            {
158                throw new ScmException( "Error while getting change logs.", ex );
159            }
160    
161            return new ChangeLogScmResult( null, new ChangeLogSet( changeLogList, startDate, endDate ) );
162        }
163    }