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;
25  
26  import java.util.ArrayList;
27  import java.util.Collections;
28  import java.util.Iterator;
29  import java.util.List;
30  import org.apache.maven.plugin.MojoExecutionException;
31  import org.apache.maven.scm.ScmBranch;
32  import org.apache.maven.scm.ScmException;
33  import org.apache.maven.scm.ScmFileSet;
34  import org.apache.maven.scm.ScmRevision;
35  import org.apache.maven.scm.ScmTag;
36  import org.apache.maven.scm.ScmVersion;
37  import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
38  import org.apache.maven.scm.command.list.ListScmResult;
39  import org.apache.maven.scm.manager.ScmManager;
40  import org.apache.maven.scm.repository.ScmRepository;
41  import org.codehaus.mojo.scmchangelog.changelog.Release;
42  import org.codehaus.mojo.scmchangelog.changelog.log.ScmLogEntry;
43  import org.codehaus.mojo.scmchangelog.changelog.log.grammar.GrammarEnum;
44  import org.codehaus.mojo.scmchangelog.scm.hg.command.changelog.BetterChangeSet;
45  import org.codehaus.mojo.scmchangelog.scm.util.ScmAdapter;
46  import org.codehaus.mojo.scmchangelog.scm.util.ScmTarget;
47  import org.codehaus.mojo.scmchangelog.tags.Tag;
48  
49  /**
50   * Adapter wrapping the Mercurial implementation.
51   * @author ehsavoie
52   * @version $Id$
53   */
54  public class HgScmAdapter extends ScmAdapter
55  {
56  
57    /**
58     * Constructor of ScmAdapter.
59     * @param currentManager the ScmManager to access SCM elements.
60     * @param currentGrammar the grammar used to extract elements from the comments.
61     */
62    public HgScmAdapter( ScmManager currentManager, GrammarEnum currentGrammar )
63    {
64      super( currentManager, currentGrammar );
65    }
66  
67    /**
68     * Returns the list of releases defined in the mercurial repository.
69     * @param repository the SCM repository.
70     * @param fileSet the base fileset.
71     * @return the list of releases defined in the SCM. <code>List&lt;Release&gt;</code>
72     * @throws org.apache.maven.scm.ScmException in case of an error with the SCM.
73     * @throws org.apache.maven.plugin.MojoExecutionException in case of an error in executing the Mojo.
74     */
75    public List getListOfReleases( ScmRepository repository, ScmFileSet fileSet )
76      throws ScmException, MojoExecutionException
77    {
78      ListScmResult result = this.manager.list( repository, fileSet, false,
79        getScmVersion( HgTargetEnum.TAG, "" ) );
80      final List tags = result.getFiles();
81      final List releases = new ArrayList( 10 );
82      Iterator iter = tags.iterator();
83      String startRevision = "0";
84  
85      while ( iter.hasNext() )
86      {
87        Tag tag = ( Tag ) iter.next();
88        getLogger().info( tag.toString() );
89  
90        final ChangeLogScmResult logs = this.manager.changeLog( repository,
91          fileSet, getScmVersion( HgTargetEnum.TRUNK, startRevision ),
92          getScmVersion( HgTargetEnum.TRUNK, tag.getEndRevision() ), "" );
93        startRevision = tag.getEndRevision();
94        getLogger().info( logs.getChangeLog().toString() );
95        tag.setDate( logs.getChangeLog().getEndDate() );
96  
97        Release release = new Release( tag,
98          getEntries( logs.getChangeLog().getChangeSets() ) );
99        releases.add( release );
100     }
101     Collections.reverse( releases );
102     return releases;
103   }
104 
105   /**
106    * Returns the list of log entries defined in the list of ChangeSet.
107    * @param changeSets the list of ChangeSet.
108    * @return the list of log entries defined in the list of ChangeSet. <code>List&lt;ScmLogEntry&gt;</code>
109    */
110   protected List getEntries( List changeSets )
111   {
112     List elements = new ArrayList( changeSets.size() );
113     Iterator iter = changeSets.iterator();
114     while ( iter.hasNext() )
115     {
116       BetterChangeSet changeSet = ( BetterChangeSet ) iter.next();
117       ScmLogEntry entry = new ScmLogEntry();
118       entry.setAuthor( changeSet.getAuthor() );
119       entry.setDate( changeSet.getDate() );
120       getLogger().info( changeSet.getComment() );
121       entry.setMessage( grammar.extractMessage( changeSet.getComment() ) );
122       entry.setRevision( changeSet.getRevision() );
123       elements.add( entry );
124     }
125     return elements;
126   }
127 
128   /**
129    * Returns the Scm version.
130    * @param versionType the type of version (tag, trunk, branch).
131    * @param version the tag/branche name.
132    * @return the corresponding ScmVersion.
133    * @throws org.apache.maven.plugin.MojoExecutionException in case of an error in executing the Mojo.
134    */
135   public ScmVersion getScmVersion( ScmTarget versionType, String version )
136       throws MojoExecutionException
137   {
138     if ( HgTargetEnum.TAG.equals( versionType ) )
139     {
140       return new ScmTag( version );
141     }
142     else if ( HgTargetEnum.BRANCH.equals( versionType ) )
143     {
144       return new ScmBranch( version );
145     }
146     else if ( HgTargetEnum.TRUNK.equals( versionType ) )
147     {
148       return new ScmRevision( version );
149     }
150     throw new MojoExecutionException( "Unknown version type : "
151         + versionType );
152   }
153 
154 }
155