View Javadoc

1   /*
2   The MIT License
3   
4   Copyright (c) 2004, The Codehaus
5   
6   Permission is hereby granted, free of charge, to any person obtaining a copy of
7   this software and associated documentation files (the "Software"), to deal in
8   the Software without restriction, including without limitation the rights to
9   use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
10  of the Software, and to permit persons to whom the Software is furnished to do
11  so, subject to the following conditions:
12  
13  The above copyright notice and this permission notice shall be included in all
14  copies or substantial portions of the Software.
15  
16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  SOFTWARE.
23   */
24  package org.codehaus.mojo.scmchangelog.scm.hg.command.changelog;
25  
26  import java.util.ArrayList;
27  import java.util.Date;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  import org.apache.maven.scm.ChangeSet;
32  import org.apache.maven.scm.ScmBranch;
33  import org.apache.maven.scm.ScmException;
34  import org.apache.maven.scm.ScmFileSet;
35  import org.apache.maven.scm.ScmResult;
36  import org.apache.maven.scm.ScmVersion;
37  import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
38  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
39  import org.apache.maven.scm.command.changelog.ChangeLogSet;
40  import org.apache.maven.scm.log.ScmLogger;
41  import org.apache.maven.scm.provider.ScmProviderRepository;
42  import org.apache.maven.scm.provider.hg.HgUtils;
43  import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
44  
45  
46  /**
47   * Excute the command <code>hg log --verbose -rx:y</code>.
48   * @author ehsavoie
49   * @version $Id: HgChangeLogCommand.java 7975 2008-10-30 14:39:53Z ehsavoie $
50   *
51   * @see org.apache.maven.scm.provider.hg.command.HgCommand
52   * @see org.codehaus.mojo.scmchangelog.scm.hg.changelog.HgChangeLogConsumer
53   */
54  public class HgChangeLogCommand
55      extends AbstractChangeLogCommand    
56  {
57    /**
58     * Execute the command <code>hg log --verbose -rx:y</code>.
59     * @param repository the Mercurial repository.
60     * @param fileSet the files on which to execute the command.
61     * @param startVersion the starting revision (x).
62     * @param endVersion the last revision (y).
63     * @param datePattern the pattern for parsing dates.
64     * @return the changelog results.
65     * @throws org.apache.maven.scm.ScmException  in case of an error with the scm command.
66     */
67    protected ChangeLogScmResult executeChangeLogCommand(
68        ScmProviderRepository repository, ScmFileSet fileSet,
69        ScmVersion startVersion, ScmVersion endVersion, String datePattern )
70        throws ScmException
71    {
72      String revisions = "-r"
73          + startVersion.getName() 
74          + ':'
75          + endVersion.getName();
76      String[] cmd = new String[]
77      {
78        HgCommandConstants.LOG_CMD, HgCommandConstants.VERBOSE_OPTION, revisions
79      };
80      ScmLogger logger = getLogger();
81      HgChangeLogConsumer consumer = new HgChangeLogConsumer( logger,
82          datePattern );
83      ScmResult result = HgUtils.execute( consumer, logger,
84          fileSet.getBasedir(), cmd );
85      logger.debug( result.toString() );
86  
87      List logEntries = consumer.getModifications();
88      List inRangeAndValid = new ArrayList();
89      Date startDate = new Date( 0 ); //From 1. Jan 1970
90      Date endDate = new Date(); //Upto now
91      Iterator it = logEntries.iterator();
92  
93      while ( it.hasNext() )
94      {
95        ChangeSet change = ( ChangeSet ) it.next();
96  
97        if ( change.getFiles().size() > 0 )
98        {
99          inRangeAndValid.add( change );
100       }
101     }
102 
103     ChangeLogSet changeLogSet = new ChangeLogSet( inRangeAndValid,
104         startDate, endDate );
105 
106     return new ChangeLogScmResult( changeLogSet, result );
107   }
108 
109   /**
110    * Execute the command <code>hg log --verbose -rx:y</code>.
111    * @param repository the Mercurial repository.
112    * @param fileSet the files on which to execute the command.
113    * @param startDate the start date.
114    * @param endDate the end date.
115    * @param branch used to get the revisions.
116    * @param datePattern the pattern for parsing dates.
117    * @return the changelog results.
118    * @throws org.apache.maven.scm.ScmException  in case of an error with the scm command.
119    */
120   protected ChangeLogScmResult executeChangeLogCommand(
121       ScmProviderRepository repository, ScmFileSet fileSet, Date startDate,
122       Date endDate, ScmBranch branch, String datePattern )
123       throws ScmException
124   {
125     String revisions = "-r " 
126         + branch.getName();
127     String[] cmd = new String[]
128     {
129       HgCommandConstants.LOG_CMD, HgCommandConstants.VERBOSE_OPTION, revisions
130     };
131     HgChangeLogConsumer consumer = new HgChangeLogConsumer( getLogger(),
132         datePattern );
133     ScmResult result = HgUtils.execute( consumer, getLogger(),
134         fileSet.getBasedir(), cmd );
135     List logEntries = consumer.getModifications();
136     List inRangeAndValid = new ArrayList();
137     startDate = ( startDate == null ) ? new Date( 0 ) : startDate; //From 1. Jan 1970
138     endDate = ( endDate == null ) ? new Date() : endDate; //Upto now
139     Iterator it = logEntries.iterator();
140     while ( it.hasNext() )
141     {
142       ChangeSet change = ( ChangeSet ) it.next();
143       if ( change.getFiles().size() > 0 )
144       {
145         if ( !change.getDate().before( startDate )
146             && !change.getDate().after( endDate ) )
147         {
148           inRangeAndValid.add( change );
149         }
150       }
151     }
152     ChangeLogSet changeLogSet = new ChangeLogSet( inRangeAndValid,
153         startDate, endDate );
154     return new ChangeLogScmResult( changeLogSet, result );
155   }
156 }