Coverage Report - org.apache.maven.scm.provider.bazaar.command.changelog.BazaarChangeLogConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
BazaarChangeLogConsumer
91 %
62/68
76 %
26/34
6,667
 
 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 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.bazaar.command.BazaarConsumer;
 27  
 
 28  
 import java.util.ArrayList;
 29  
 import java.util.Date;
 30  
 import java.util.List;
 31  
 
 32  
 /**
 33  
  * @author <a href="mailto:torbjorn@smorgrav.org">Torbjorn Eikli Smorgrav</a>
 34  
  * @author Olivier Lamy
 35  
  * @version $Id: BazaarChangeLogConsumer.java 1306867 2012-03-29 13:45:10Z olamy $
 36  
  */
 37  
 public class BazaarChangeLogConsumer
 38  
     extends BazaarConsumer
 39  
 {
 40  
 
 41  
     private static final String BAZAAR_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss Z";
 42  
 
 43  
     private static final String START_LOG_TAG = "-----";
 44  
 
 45  
     private static final String REVNO_TAG = "revno: ";
 46  
 
 47  
     private static final String AUTHOR_TAG = "committer: ";
 48  
 
 49  
     private static final String TIME_STAMP_TOKEN = "timestamp: ";
 50  
 
 51  
     private static final String MESSAGE_TOKEN = "message:";
 52  
 
 53  
     private static final String BRANCH_NICK_TOKEN = "branch nick: ";
 54  
 
 55  
     private static final String MERGED_TOKEN = "merged: ";
 56  
 
 57  
     private static final String RENAME_SEPARATOR = " => ";
 58  
 
 59  1
     private List<ChangeSet> logEntries = new ArrayList<ChangeSet>();
 60  
 
 61  
     private ChangeSet currentChange;
 62  
 
 63  
     private ChangeSet lastChange;
 64  
 
 65  
     private boolean isMergeEntry;
 66  
 
 67  
     private String currentRevision;
 68  
 
 69  
     private StringBuilder currentComment;
 70  
 
 71  
     private String userDatePattern;
 72  
 
 73  
     /**
 74  
      * Null means not parsing message nor files, UNKNOWN means parsing message
 75  
      */
 76  1
     private ScmFileStatus currentStatus = null;
 77  
 
 78  
     public BazaarChangeLogConsumer( ScmLogger logger, String userDatePattern )
 79  
     {
 80  1
         super( logger );
 81  
 
 82  1
         this.userDatePattern = userDatePattern;
 83  1
     }
 84  
 
 85  
     public List<ChangeSet> getModifications()
 86  
     {
 87  1
         return logEntries;
 88  
     }
 89  
 
 90  
     /** {@inheritDoc} */
 91  
     public void doConsume( ScmFileStatus status, String line )
 92  
     {
 93  52
         String tmpLine = line;
 94  
 
 95  
         // Parse line
 96  52
         if ( line.startsWith( START_LOG_TAG ) )
 97  
         {
 98  
             //If last entry was part a merged entry
 99  6
             if ( isMergeEntry && lastChange != null )
 100  
             {
 101  2
                 String comment = lastChange.getComment();
 102  2
                 comment += "\n[MAVEN]: Merged from " + currentChange.getAuthor();
 103  2
                 comment += "\n[MAVEN]:    " + currentChange.getDateFormatted();
 104  2
                 comment += "\n[MAVEN]:    " + currentChange.getComment();
 105  2
                 lastChange.setComment( comment );
 106  
             }
 107  
 
 108  
             //Init a new changeset
 109  6
             currentChange = new ChangeSet();
 110  6
             currentChange.setFiles( new ArrayList<ChangeFile>() );
 111  6
             logEntries.add( currentChange );
 112  
 
 113  
             //Reset memeber vars
 114  6
             currentComment = new StringBuilder();
 115  6
             currentStatus = null;
 116  6
             currentRevision = "";
 117  6
             isMergeEntry = false;
 118  
         }
 119  46
         else if ( line.startsWith( MERGED_TOKEN ) )
 120  
         {
 121  
             //This is part of lastChange and is not a separate log entry
 122  2
             isMergeEntry = true;
 123  2
             logEntries.remove( currentChange );
 124  2
             if ( logEntries.size() > 0 )
 125  
             {
 126  2
                 lastChange = (ChangeSet) logEntries.get( logEntries.size() - 1 );
 127  
             }
 128  
             else
 129  
             {
 130  0
                 if ( getLogger().isWarnEnabled() )
 131  
                 {
 132  0
                     getLogger().warn( "First entry was unexpectedly a merged entry" );
 133  
                 }
 134  0
                 lastChange = null;
 135  
             }
 136  
         }
 137  44
         else if ( line.startsWith( REVNO_TAG ) )
 138  
         {
 139  4
             tmpLine = line.substring( REVNO_TAG.length() );
 140  4
             tmpLine = tmpLine.trim();
 141  4
             currentRevision = tmpLine;
 142  
         }
 143  40
         else if ( line.startsWith( AUTHOR_TAG ) )
 144  
         {
 145  6
             tmpLine = line.substring( AUTHOR_TAG.length() );
 146  6
             tmpLine = tmpLine.trim();
 147  6
             currentChange.setAuthor( tmpLine );
 148  
         }
 149  34
         else if ( line.startsWith( TIME_STAMP_TOKEN ) )
 150  
         {
 151  6
             tmpLine = line.substring( TIME_STAMP_TOKEN.length() + 3 );
 152  6
             tmpLine = tmpLine.trim();
 153  6
             Date date = parseDate( tmpLine, userDatePattern, BAZAAR_TIME_PATTERN );
 154  6
             currentChange.setDate( date );
 155  6
         }
 156  28
         else if ( line.startsWith( MESSAGE_TOKEN ) )
 157  
         {
 158  6
             currentStatus = ScmFileStatus.UNKNOWN;
 159  
         }
 160  22
         else if ( status != null )
 161  
         {
 162  5
             currentStatus = status;
 163  
         }
 164  17
         else if ( currentStatus == ScmFileStatus.UNKNOWN )
 165  
         {
 166  6
             currentComment.append( line );
 167  6
             currentChange.setComment( currentComment.toString() );
 168  6
             currentComment.append( "\n" );
 169  
         }
 170  11
         else if ( currentStatus != null )
 171  
         {
 172  5
             tmpLine = tmpLine.trim();
 173  
             final ChangeFile changeFile;
 174  5
             if ( currentStatus == ScmFileStatus.RENAMED )
 175  
             {
 176  1
                 final String[] parts = tmpLine.split( RENAME_SEPARATOR );
 177  1
                 if ( parts.length != 2 )
 178  
                 {
 179  0
                     changeFile = new ChangeFile( tmpLine, currentRevision );
 180  
                 }
 181  
                 else
 182  
                 {
 183  1
                     changeFile = new ChangeFile( parts[1], currentRevision );
 184  1
                     changeFile.setOriginalName( parts[0] );
 185  
                 }
 186  1
             }
 187  
             else
 188  
             {
 189  4
                 changeFile = new ChangeFile( tmpLine, currentRevision );
 190  
             }
 191  5
             changeFile.setAction( currentStatus );
 192  5
             currentChange.addFile( changeFile );
 193  5
         }
 194  6
         else if ( line.startsWith( BRANCH_NICK_TOKEN ) )
 195  
         {
 196  
             //ignore
 197  
         }
 198  
         else
 199  
         {
 200  0
             if ( getLogger().isWarnEnabled() )
 201  
             {
 202  0
                 getLogger().warn( "Could not figure out of: " + line );
 203  
             }
 204  
         }
 205  52
     }
 206  
 }