View Javadoc

1   package org.apache.maven.scm.provider.hg.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 org.apache.maven.scm.ChangeSet;
23  import org.apache.maven.scm.ScmBranch;
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.ScmResult;
27  import org.apache.maven.scm.ScmVersion;
28  import org.apache.maven.scm.command.Command;
29  import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
30  import org.apache.maven.scm.command.changelog.ChangeLogScmRequest;
31  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
32  import org.apache.maven.scm.command.changelog.ChangeLogSet;
33  import org.apache.maven.scm.provider.ScmProviderRepository;
34  import org.apache.maven.scm.provider.hg.HgUtils;
35  import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
36  
37  import java.text.SimpleDateFormat;
38  import java.util.ArrayList;
39  import java.util.Arrays;
40  import java.util.Date;
41  import java.util.List;
42  
43  /**
44   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
45   * @author Olivier Lamy
46   */
47  public class HgChangeLogCommand
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(), datePattern,
68                                          request.getLimit() );
69      }
70  
71      /**
72       * {@inheritDoc}
73       */
74      protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository scmProviderRepository,
75                                                            ScmFileSet fileSet, Date startDate, Date endDate,
76                                                            ScmBranch branch, String datePattern )
77          throws ScmException
78      {
79          return executeChangeLogCommand( fileSet, startDate, endDate, datePattern, null );
80      }
81  
82      private ChangeLogScmResult executeChangeLogCommand( ScmFileSet fileSet, Date startDate, Date endDate,
83                                                          String datePattern, Integer limit )
84          throws ScmException
85      {
86          SimpleDateFormat dateFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
87          StringBuilder dateInterval = new StringBuilder();
88          // TRICK: Mercurial 1.9.3 don't accept 1970-01-01
89          dateInterval.append(
90              dateFormat.format( startDate == null ? new Date( 1000L * 60 * 60 * 24 ) : startDate ) ); // From 2. Jan 1970
91          dateInterval.append( " to " );
92          dateInterval.append( dateFormat.format( endDate == null ? new Date() : endDate ) ); // Upto now
93  
94          List<String> cmd = new ArrayList<String>();
95          cmd.addAll( Arrays.asList( HgCommandConstants.LOG_CMD, HgCommandConstants.TEMPLATE_OPTION,
96                                     HgCommandConstants.TEMPLATE_FORMAT, HgCommandConstants.NO_MERGES_OPTION,
97                                     HgCommandConstants.DATE_OPTION, dateInterval.toString() ) );
98  
99          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 }