001    package org.apache.maven.scm.provider.cvslib.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 org.apache.maven.scm.ScmBranch;
023    import org.apache.maven.scm.ScmException;
024    import org.apache.maven.scm.ScmFileSet;
025    import org.apache.maven.scm.ScmVersion;
026    import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
027    import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
028    import org.apache.maven.scm.provider.ScmProviderRepository;
029    import org.apache.maven.scm.provider.cvslib.command.CvsCommand;
030    import org.apache.maven.scm.provider.cvslib.command.CvsCommandUtils;
031    import org.apache.maven.scm.provider.cvslib.repository.CvsScmProviderRepository;
032    import org.apache.maven.scm.provider.cvslib.util.CvsUtil;
033    import org.codehaus.plexus.util.Os;
034    import org.codehaus.plexus.util.StringUtils;
035    import org.codehaus.plexus.util.cli.Commandline;
036    
037    import java.text.SimpleDateFormat;
038    import java.util.Date;
039    
040    /**
041     * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse </a>
042     * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
043     * @version $Id: AbstractCvsChangeLogCommand.java 1306867 2012-03-29 13:45:10Z olamy $
044     */
045    public abstract class AbstractCvsChangeLogCommand
046        extends AbstractChangeLogCommand
047        implements CvsCommand
048    {
049        /** {@inheritDoc} */
050        protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
051                                                              ScmVersion startVersion, ScmVersion endVersion,
052                                                              String datePattern )
053            throws ScmException
054        {
055            return executeChangeLogCommand( repo, fileSet, null, null, null, startVersion, endVersion, datePattern );
056        }
057    
058        /** {@inheritDoc} */
059        protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
060                                                              Date startDate, Date endDate, ScmBranch branch,
061                                                              String datePattern )
062            throws ScmException
063        {
064            return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, null, null, datePattern );
065        }
066    
067        private ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet, Date startDate,
068                                                            Date endDate, ScmBranch branch, ScmVersion startVersion,
069                                                            ScmVersion endVersion, String datePattern )
070            throws ScmException
071        {
072            CvsScmProviderRepository repository = (CvsScmProviderRepository) repo;
073    
074            Commandline cl = CvsCommandUtils.getBaseCommand( "log", repository, fileSet );
075    
076            if ( startDate != null )
077            {
078                SimpleDateFormat outputDate = new SimpleDateFormat( getDateFormat() );
079    
080                String dateRange;
081    
082                if ( endDate == null )
083                {
084                    dateRange = ">" + outputDate.format( startDate );
085                }
086                else
087                {
088                    dateRange = outputDate.format( startDate ) + "<" + outputDate.format( endDate );
089                }
090    
091                cl.createArg().setValue( "-d" );
092    
093                addDateRangeParameter( cl, dateRange );
094            }
095    
096            if ( branch != null && StringUtils.isNotEmpty( branch.getName() ) )
097            {
098                cl.createArg().setValue( "-r" + branch.getName() );
099            }
100    
101            if ( startVersion != null  || endVersion != null )
102            {
103                StringBuilder sb = new StringBuilder();
104                sb.append( "-r" );
105                if ( startVersion != null && StringUtils.isNotEmpty( startVersion.getName() ) )
106                {
107                    sb.append( startVersion.getName() );
108                }
109                sb.append( "::" );
110                if ( endVersion != null && StringUtils.isNotEmpty( endVersion.getName() ) )
111                {
112                    sb.append( endVersion.getName() );
113                }
114    
115                cl.createArg().setValue( sb.toString() );
116            }
117    
118            if ( getLogger().isInfoEnabled() )
119            {
120                getLogger().info( "Executing: " + cl );
121                getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
122            }
123    
124            return executeCvsCommand( cl, startDate, endDate, startVersion, endVersion, datePattern );
125        }
126    
127        protected abstract ChangeLogScmResult executeCvsCommand( Commandline cl, Date startDate, Date endDate,
128                                                                 ScmVersion startVersion, ScmVersion endVersion,
129                                                                 String datePattern )
130            throws ScmException;
131    
132        protected String getDateFormat()
133        {
134            return CvsUtil.getSettings().getChangeLogCommandDateFormat();
135        }
136    
137        protected void addDateRangeParameter( Commandline cl, String dateRange )
138        {
139            // There's a difference between UNIX-like OS and Windows
140            // See http://jira.codehaus.org/browse/SCM-187
141            if ( Os.isFamily( "windows" ) )
142            {
143                cl.createArg().setValue( "\"" + dateRange + "\"" );
144            }
145            else
146            {
147                cl.createArg().setValue( dateRange );
148            }
149        }
150    }