001package org.apache.maven.scm.provider.hg.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
022import org.apache.maven.scm.ChangeSet;
023import org.apache.maven.scm.ScmBranch;
024import org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFileSet;
026import org.apache.maven.scm.ScmResult;
027import org.apache.maven.scm.ScmVersion;
028import org.apache.maven.scm.command.Command;
029import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
030import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
031import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
032import org.apache.maven.scm.command.changelog.ChangeLogSet;
033import org.apache.maven.scm.provider.ScmProviderRepository;
034import org.apache.maven.scm.provider.hg.HgUtils;
035import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
036
037import java.text.SimpleDateFormat;
038import java.util.ArrayList;
039import java.util.Arrays;
040import java.util.Date;
041import java.util.List;
042
043/**
044 * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
045 * @author Olivier Lamy
046 */
047public class HgChangeLogCommand
048    extends AbstractChangeLogCommand
049    implements Command
050{
051    /**
052     * {@inheritDoc}
053     */
054    @Override
055    protected ChangeLogScmResult executeChangeLogCommand( ChangeLogScmRequest request )
056        throws ScmException
057    {
058        final ScmVersion startVersion = request.getStartRevision();
059        final ScmVersion endVersion = request.getEndRevision();
060        final ScmFileSet fileSet = request.getScmFileSet();
061        final String datePattern = request.getDatePattern();
062        if ( startVersion != null || endVersion != null )
063        {
064            final ScmProviderRepository scmProviderRepository = request.getScmRepository().getProviderRepository();
065            return executeChangeLogCommand( scmProviderRepository, fileSet, startVersion, endVersion, datePattern );
066        }
067        return executeChangeLogCommand( fileSet, request.getStartDate(), request.getEndDate(), datePattern,
068                                        request.getLimit() );
069    }
070
071    /**
072     * {@inheritDoc}
073     */
074    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository scmProviderRepository,
075                                                          ScmFileSet fileSet, Date startDate, Date endDate,
076                                                          ScmBranch branch, String datePattern )
077        throws ScmException
078    {
079        return executeChangeLogCommand( fileSet, startDate, endDate, datePattern, null );
080    }
081
082    private ChangeLogScmResult executeChangeLogCommand( ScmFileSet fileSet, Date startDate, Date endDate,
083                                                        String datePattern, Integer limit )
084        throws ScmException
085    {
086        SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
087        StringBuilder dateInterval = new StringBuilder();
088        // TRICK: Mercurial 1.9.3 don't accept 1970-01-01
089        dateInterval.append(
090            dateFormat.format( startDate == null ? new Date( 1000L * 60 * 60 * 24 ) : startDate ) ); // From 2. Jan 1970
091        dateInterval.append( " to " );
092        dateInterval.append( dateFormat.format( endDate == null ? new Date() : endDate ) ); // Upto now
093
094        List<String> cmd = new ArrayList<String>();
095        cmd.addAll( Arrays.asList( HgCommandConstants.LOG_CMD, HgCommandConstants.TEMPLATE_OPTION,
096                                   HgCommandConstants.TEMPLATE_FORMAT, HgCommandConstants.NO_MERGES_OPTION,
097                                   HgCommandConstants.DATE_OPTION, dateInterval.toString() ) );
098
099        if ( limit != null && limit > 0 )
100        {
101            cmd.add( HgCommandConstants.LIMIT_OPTION );
102            cmd.add( Integer.toString( limit ) );
103        }
104
105        HgChangeLogConsumer consumer = new HgChangeLogConsumer( getLogger(), datePattern );
106        ScmResult result =
107            HgUtils.execute( consumer, getLogger(), fileSet.getBasedir(), cmd.toArray( new String[cmd.size()] ) );
108
109        List<ChangeSet> logEntries = consumer.getModifications();
110        ChangeLogSet changeLogSet = new ChangeLogSet( logEntries, startDate, endDate );
111        return new ChangeLogScmResult( changeLogSet, result );
112    }
113
114    @Override
115    protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repository, ScmFileSet fileSet,
116                                                          ScmVersion startVersion, ScmVersion endVersion,
117                                                          String datePattern )
118        throws ScmException
119    {
120        StringBuilder revisionInterval = new StringBuilder();
121        if ( startVersion != null )
122        {
123            revisionInterval.append( startVersion.getName() );
124        }
125        revisionInterval.append( ":" );
126        if ( endVersion != null )
127        {
128            revisionInterval.append( endVersion.getName() );
129        }
130
131        String[] cmd = new String[]{ HgCommandConstants.LOG_CMD, HgCommandConstants.TEMPLATE_OPTION,
132            HgCommandConstants.TEMPLATE_FORMAT, HgCommandConstants.NO_MERGES_OPTION, HgCommandConstants.REVISION_OPTION,
133            revisionInterval.toString() };
134        HgChangeLogConsumer consumer = new HgChangeLogConsumer( getLogger(), datePattern );
135        ScmResult result = HgUtils.execute( consumer, getLogger(), fileSet.getBasedir(), cmd );
136
137        List<ChangeSet> logEntries = consumer.getModifications();
138        Date startDate = null;
139        Date endDate = null;
140        if ( !logEntries.isEmpty() )
141        {
142            startDate = logEntries.get( 0 ).getDate();
143            endDate = logEntries.get( logEntries.size() - 1 ).getDate();
144        }
145        ChangeLogSet changeLogSet = new ChangeLogSet( logEntries, startDate, endDate );
146        changeLogSet.setStartVersion( startVersion );
147        changeLogSet.setEndVersion( endVersion );
148        return new ChangeLogScmResult( changeLogSet, result );
149    }
150}