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.ChangeFile;
23  import org.apache.maven.scm.ChangeSet;
24  import org.apache.maven.scm.ScmFileStatus;
25  import org.apache.maven.scm.log.ScmLogger;
26  import org.apache.maven.scm.provider.hg.command.HgConsumer;
27  
28  import java.util.ArrayList;
29  import java.util.Date;
30  import java.util.List;
31  import java.util.Locale;
32  
33  /**
34   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
35   */
36  public class HgChangeLogConsumer
37      extends HgConsumer
38  {
39  
40      private static final String TIME_PATTERN = "EEE MMM dd HH:mm:ss yyyy Z";
41  
42      private static final String REVNO_TAG = "changeset: ";
43  
44      private static final String TAG_TAG = "tag:         ";
45  
46      private static final String AUTHOR_TAG = "user: ";
47  
48      private static final String TIME_STAMP_TOKEN = "date: ";
49  
50      private static final String MESSAGE_TOKEN = "description:";
51  
52      private static final String MERGED_TOKEN = "merged: ";
53  
54      private static final String FILES_TOKEN = "files: ";
55  
56      private String prevLine = "";
57  
58      private String prevPrevLine = "";
59  
60      private ArrayList logEntries = new ArrayList();
61  
62      private ChangeSet currentChange;
63  
64      private ChangeSet lastChange;
65  
66      private boolean isMergeEntry;
67  
68      private String currentRevision;
69  
70      private String currentTag; // don't know what to do with this
71  
72      private String userDatePattern;
73  
74      private boolean spoolingComments;
75  
76      private List currentComment = null;
77  
78      public HgChangeLogConsumer( ScmLogger logger, String userDatePattern )
79      {
80          super( logger );
81  
82          this.userDatePattern = userDatePattern;
83      }
84  
85      public List getModifications()
86      {
87          return logEntries;
88      }
89  
90      public void consumeLine( String line )
91      {
92  
93          // override default behaviour which tries to pick through things for some standard messages.  that
94          // does not apply here
95          doConsume( null, line );
96      }
97  
98      public void doConsume( ScmFileStatus status, String line )
99      {
100         String tmpLine = line;
101         // If current status == null then this is a new entry
102         // If the line == "" and previous line was "", then this is also a new entry
103         if ( ( line.equals( "" ) && ( prevLine.equals( "" ) && prevPrevLine.equals( "" ) ) ) || currentComment == null )
104         {
105             if ( currentComment != null )
106             {
107                 StringBuffer comment = new StringBuffer();
108                 for ( int i = 0; i < currentComment.size() - 1; i++ )
109                 {
110                     comment.append( currentComment.get( i ) );
111                     if ( i + 1 < currentComment.size() - 1 )
112                     {
113                         comment.append( '\n' );
114                     }
115                 }
116                 currentChange.setComment( comment.toString() );
117 
118             }
119 
120             spoolingComments = false;
121 
122             //If last entry was part a merged entry
123             if ( isMergeEntry && lastChange != null )
124             {
125                 String comment = lastChange.getComment();
126                 comment += "\n[MAVEN]: Merged from " + currentChange.getAuthor();
127                 comment += "\n[MAVEN]:    " + currentChange.getDateFormatted();
128                 comment += "\n[MAVEN]:    " + currentChange.getComment();
129                 lastChange.setComment( comment );
130             }
131 
132             //Init a new changeset
133             currentChange = new ChangeSet();
134             currentChange.setFiles( new ArrayList() );
135             logEntries.add( currentChange );
136 
137             //Reset memeber vars
138             currentComment = new ArrayList();
139             currentRevision = "";
140             isMergeEntry = false;
141         }
142 
143         if ( spoolingComments )
144         {
145             currentComment.add( line );
146         }
147         else if ( line.startsWith( MESSAGE_TOKEN ) )
148         {
149             spoolingComments = true;
150         }
151         else if ( line.startsWith( MERGED_TOKEN ) )
152         {
153             //This is part of lastChange and is not a separate log entry
154             isMergeEntry = true;
155             logEntries.remove( currentChange );
156             if ( logEntries.size() > 0 )
157             {
158                 lastChange = (ChangeSet) logEntries.get( logEntries.size() - 1 );
159             }
160             else
161             {
162                 getLogger().warn( "First entry was unexpectedly a merged entry" );
163                 lastChange = null;
164             }
165         }
166         else if ( line.startsWith( REVNO_TAG ) )
167         {
168             tmpLine = line.substring( REVNO_TAG.length() );
169             tmpLine = tmpLine.trim();
170             currentRevision = tmpLine;
171         }
172         else if ( line.startsWith( TAG_TAG ) )
173         {
174             tmpLine = line.substring( TAG_TAG.length() ).trim();
175             currentTag = tmpLine;
176         }
177         else if ( line.startsWith( AUTHOR_TAG ) )
178         {
179             tmpLine = line.substring( AUTHOR_TAG.length() );
180             tmpLine = tmpLine.trim();
181             currentChange.setAuthor( tmpLine );
182         }
183         else if ( line.startsWith( TIME_STAMP_TOKEN ) )
184         {
185             // TODO: FIX Date Parsing to match Mercurial or fix with template
186             tmpLine = line.substring( TIME_STAMP_TOKEN.length() ).trim();
187             Date date = parseDate( tmpLine, userDatePattern, TIME_PATTERN, Locale.ENGLISH );
188             currentChange.setDate( date );
189         }
190         else if ( line.startsWith( FILES_TOKEN ) )
191         {
192             tmpLine = line.substring( FILES_TOKEN.length() ).trim();
193             String[] files = tmpLine.split( " " );
194             for ( int i = 0; i < files.length; i++ )
195             {
196                 String file = files[i];
197                 ChangeFile changeFile = new ChangeFile( file, currentRevision );
198                 currentChange.addFile( changeFile );
199             }
200         }
201         else
202         {
203             getLogger().warn( "Could not figure out: " + line );
204         }
205 
206         // record previous line
207         prevLine = line;
208         prevPrevLine = prevLine;
209     }
210 }