001    package org.apache.maven.scm.provider.tfs.command;
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.util.ArrayList;
024    import java.util.Date;
025    import java.util.Iterator;
026    import java.util.List;
027    
028    import org.apache.maven.scm.ChangeSet;
029    import org.apache.maven.scm.ScmBranch;
030    import org.apache.maven.scm.ScmException;
031    import org.apache.maven.scm.ScmFileSet;
032    import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
033    import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
034    import org.apache.maven.scm.command.changelog.ChangeLogSet;
035    import org.apache.maven.scm.provider.ScmProviderRepository;
036    import org.apache.maven.scm.provider.tfs.command.consumer.ErrorStreamConsumer;
037    import org.apache.maven.scm.provider.tfs.command.consumer.TfsChangeLogConsumer;
038    
039    /**
040     * @author Olivier Lamy
041     * @version $id$
042     */
043    public class TfsChangeLogCommand
044        extends AbstractChangeLogCommand
045    {
046    
047        protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository r, ScmFileSet f, Date startDate,
048                                                              Date endDate, ScmBranch branch, String datePattern )
049            throws ScmException
050        {
051            List<ChangeSet> changeLogs = new ArrayList<ChangeSet>();
052            Iterator<File> iter = f.getFileList().iterator();
053            if ( !iter.hasNext() )
054            {
055                List<File> dir = new ArrayList<File>();
056                // No files to iterate
057                dir.add( f.getBasedir() );
058                iter = dir.iterator();
059            }
060            TfsCommand command = null;
061            // tf history takes only one file arg
062            while ( iter.hasNext() )
063            {
064                TfsChangeLogConsumer out = new TfsChangeLogConsumer( getLogger() );
065                ErrorStreamConsumer err = new ErrorStreamConsumer();
066    
067                command = createCommand( r, f, ( (File) iter.next() ) );
068                int status = command.execute( out, err );
069    
070                if ( status != 0 || ( !out.hasBeenFed() && err.hasBeenFed() ) )
071                    return new ChangeLogScmResult( command.getCommandString(), "Error code for TFS changelog command - "
072                        + status, err.getOutput(), false );
073                changeLogs.addAll( out.getLogs() );
074            }
075            return new ChangeLogScmResult( command.getCommandString(), new ChangeLogSet( changeLogs, startDate, endDate ) );
076    
077        }
078    
079        protected TfsCommand createCommand( ScmProviderRepository r, ScmFileSet f, File file )
080        {
081            TfsCommand command = new TfsCommand( "history", r, f, getLogger() );
082            command.addArgument( "-format:detailed" );
083            command.addArgument( file.getName() );
084            return command;
085        }
086    }
087