View Javadoc

1   package org.apache.maven.scm.provider.bazaar.command.changelog;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Arrays;
24  import java.util.Date;
25  import java.util.List;
26  
27  import org.apache.maven.scm.ChangeSet;
28  import org.apache.maven.scm.ScmBranch;
29  import org.apache.maven.scm.ScmException;
30  import org.apache.maven.scm.ScmFileSet;
31  import org.apache.maven.scm.ScmResult;
32  import org.apache.maven.scm.ScmVersion;
33  import org.apache.maven.scm.command.Command;
34  import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
35  import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
36  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
37  import org.apache.maven.scm.command.changelog.ChangeLogSet;
38  import org.apache.maven.scm.provider.ScmProviderRepository;
39  import org.apache.maven.scm.provider.bazaar.BazaarUtils;
40  import org.apache.maven.scm.provider.bazaar.command.BazaarConstants;
41  
42  /**
43   * @author <a href="mailto:torbjorn@smorgrav.org">Torbjorn Eikli Smorgrav</a>
44   * @author Olivier Lamy
45   *
46   */
47  public class BazaarChangeLogCommand
48      extends AbstractChangeLogCommand
49      implements Command
50  {
51      /**
52       * {@inheritDoc}
53       */
54      @Override
55      protected ChangeLogScmResult executeChangeLogCommand( ChangeLogScmRequest request )
56          throws ScmException
57      {
58          final ScmVersion startVersion = request.getStartRevision();
59          final ScmVersion endVersion = request.getEndRevision();
60          final ScmFileSet fileSet = request.getScmFileSet();
61          final String datePattern = request.getDatePattern();
62          if ( startVersion != null || endVersion != null )
63          {
64              final ScmProviderRepository scmProviderRepository = request.getScmRepository().getProviderRepository();
65              return executeChangeLogCommand( scmProviderRepository, fileSet, startVersion, endVersion, datePattern );
66          }
67          return executeChangeLogCommand( fileSet, request.getStartDate(), request.getEndDate(),
68              datePattern, request.getLimit() );
69      }
70  
71      /** {@inheritDoc} */
72      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
73                                                            Date startDate, Date endDate, ScmBranch branch,
74                                                            String datePattern )
75          throws ScmException
76      {
77          return executeChangeLogCommand( fileSet, startDate, endDate, datePattern, null );
78      }
79  
80      private ChangeLogScmResult executeChangeLogCommand( ScmFileSet fileSet,
81                                                          Date startDate, Date endDate,
82                                                          String datePattern, Integer limit )
83          throws ScmException
84      {
85          List<String> cmd = new ArrayList<String>();
86          cmd.addAll( Arrays.asList( BazaarConstants.LOG_CMD, BazaarConstants.VERBOSE_OPTION ) );
87          if ( limit != null && limit > 0 )
88          {
89              cmd.add( BazaarConstants.LIMIT_OPTION );
90              cmd.add( Integer.toString( limit ) );
91          }
92  
93          BazaarChangeLogConsumer consumer = new BazaarChangeLogConsumer( getLogger(), datePattern );
94          ScmResult result = BazaarUtils.execute( consumer, getLogger(), fileSet.getBasedir(),
95              cmd.toArray( new String[cmd.size()] ) );
96  
97          List<ChangeSet> logEntries = consumer.getModifications();
98          List<ChangeSet> inRangeAndValid = new ArrayList<ChangeSet>();
99          startDate = startDate == null ? new Date( 0 ) : startDate; //From 1. Jan 1970
100         endDate = endDate == null ? new Date() : endDate; //Upto now
101 
102         for ( ChangeSet change : logEntries )
103         {
104             if ( change.getFiles().size() > 0 )
105             {
106                 if ( !change.getDate().before( startDate ) && !change.getDate().after( endDate ) )
107                 {
108                     inRangeAndValid.add( change );
109                 }
110             }
111         }
112 
113         ChangeLogSet changeLogSet = new ChangeLogSet( inRangeAndValid, startDate, endDate );
114         return new ChangeLogScmResult( changeLogSet, result );
115     }
116 }