Coverage Report - org.apache.maven.scm.provider.bazaar.command.diff.BazaarDiffConsumer
 
Classes in this File Line Coverage Branch Coverage Complexity
BazaarDiffConsumer
0 %
0/52
0 %
0/32
3,571
 
 1  
 package org.apache.maven.scm.provider.bazaar.command.diff;
 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.ScmFile;
 23  
 import org.apache.maven.scm.ScmFileStatus;
 24  
 import org.apache.maven.scm.log.ScmLogger;
 25  
 import org.apache.maven.scm.provider.bazaar.command.BazaarConsumer;
 26  
 
 27  
 import java.io.File;
 28  
 import java.util.ArrayList;
 29  
 import java.util.HashMap;
 30  
 import java.util.List;
 31  
 import java.util.Map;
 32  
 
 33  
 /**
 34  
  * @author <a href="mailto:torbjorn@smorgrav.org">Torbj�rn Eikli Sm�rgrav</a>
 35  
  * @version $Id: BazaarDiffConsumer.java 1306867 2012-03-29 13:45:10Z olamy $
 36  
  */
 37  
 public class BazaarDiffConsumer
 38  
     extends BazaarConsumer
 39  
 {
 40  
 
 41  
     private static final String MODIFIED_FILE_TOKEN = "=== modified file ";
 42  
 
 43  
     private static final String ADDED_FILE_TOKEN = "=== added file ";
 44  
 
 45  
     private static final String DELETED_FILE_TOKEN = "=== deleted file ";
 46  
 
 47  
     private static final String NO_NEWLINE_TOKEN = "\\ No newline at end of file";
 48  
 
 49  
     private static final String FROM_FILE_TOKEN = "---";
 50  
 
 51  
     private static final String TO_FILE_TOKEN = "+++";
 52  
 
 53  
     private static final String ADDED_LINE_TOKEN = "+";
 54  
 
 55  
     private static final String REMOVED_LINE_TOKEN = "-";
 56  
 
 57  
     private static final String UNCHANGED_LINE_TOKEN = " ";
 58  
 
 59  
     private static final String RANGE_TOKEN = "@@";
 60  
 
 61  
     private ScmLogger logger;
 62  
 
 63  
     private File workingDirectory;
 64  
 
 65  
     private String currentFile;
 66  
 
 67  
     private StringBuilder currentDifference;
 68  
 
 69  0
     private List<ScmFile> changedFiles = new ArrayList<ScmFile>();
 70  
 
 71  0
     private Map<String,CharSequence> differences = new HashMap<String,CharSequence>();
 72  
 
 73  0
     private StringBuilder patch = new StringBuilder();
 74  
 
 75  
     public BazaarDiffConsumer( ScmLogger logger, File workingDirectory )
 76  
     {
 77  0
         super( logger );
 78  0
         this.logger = logger;
 79  0
         this.workingDirectory = workingDirectory;
 80  0
     }
 81  
 
 82  
     /** {@inheritDoc} */
 83  
     public void doConsume( ScmFileStatus status, String line )
 84  
     {
 85  0
         String tmpLine = new String( line );
 86  0
         patch.append( line ).append( "\n" );
 87  
 
 88  
         // Parse line
 89  0
         if ( line.startsWith( MODIFIED_FILE_TOKEN ) )
 90  
         {
 91  0
             tmpLine = line.substring( MODIFIED_FILE_TOKEN.length() );
 92  0
             tmpLine = tmpLine.trim();
 93  0
             status = ScmFileStatus.MODIFIED;
 94  0
             addChangedFile( status, line, tmpLine );
 95  
         }
 96  0
         else if ( line.startsWith( ADDED_FILE_TOKEN ) )
 97  
         {
 98  0
             tmpLine = line.substring( ADDED_FILE_TOKEN.length() );
 99  0
             tmpLine = tmpLine.trim();
 100  0
             status = ScmFileStatus.ADDED;
 101  0
             addChangedFile( status, line, tmpLine );
 102  
         }
 103  0
         else if ( line.startsWith( DELETED_FILE_TOKEN ) )
 104  
         {
 105  0
             tmpLine = line.substring( DELETED_FILE_TOKEN.length() );
 106  0
             tmpLine = tmpLine.trim();
 107  0
             status = ScmFileStatus.DELETED;
 108  0
             addChangedFile( status, line, tmpLine );
 109  
         }
 110  0
         else if ( line.startsWith( TO_FILE_TOKEN ) || line.startsWith( FROM_FILE_TOKEN ) )
 111  
         {
 112  
             // ignore (to avoid conflicts with add and remove tokens)
 113  
         }
 114  0
         else if ( line.startsWith( ADDED_LINE_TOKEN ) || line.startsWith( REMOVED_LINE_TOKEN )
 115  
             || line.startsWith( UNCHANGED_LINE_TOKEN ) || line.startsWith( RANGE_TOKEN )
 116  
             || line.startsWith( NO_NEWLINE_TOKEN ) )
 117  
         {
 118  0
             currentDifference.append( line ).append( "\n" );
 119  
         }
 120  0
     }
 121  
 
 122  
     /**
 123  
      * This method takes into account two types of diff output. <br>
 124  
      * - Bazaar 0.7 format: dir/dir/myfile  <br>
 125  
      * - Bazaar 0.8 format: a/dir/dir/myfile <br>
 126  
      *
 127  
      * @param status  Eg. modified or added
 128  
      * @param line    The original bazaar output to process (for logging)
 129  
      * @param tmpLine The bazaar output to process
 130  
      */
 131  
     private void addChangedFile( ScmFileStatus status, String line, String tmpLine )
 132  
     {
 133  0
         tmpLine = tmpLine.substring( 1, tmpLine.length() - 1 );
 134  0
         boolean ok = addChangedFile( status, tmpLine );
 135  
 
 136  0
         if ( !ok )
 137  
         {
 138  0
             int index = tmpLine.indexOf( '/' );
 139  0
             if ( index > -1 )
 140  
             {
 141  0
                 tmpLine = tmpLine.substring( index + 1 );
 142  0
                 ok = addChangedFile( status, tmpLine );
 143  
             }
 144  
         }
 145  
 
 146  0
         if ( !ok )
 147  
         {
 148  0
             if ( logger.isWarnEnabled() )
 149  
             {
 150  0
                 logger.warn( "Could not figure out of line: " + line );
 151  
             }
 152  
         }
 153  0
     }
 154  
 
 155  
     /**
 156  
      * @param status
 157  
      * @param tmpLine
 158  
      * @return True if tmpLine was a valid file and thus added to the changeset
 159  
      */
 160  
     private boolean addChangedFile( ScmFileStatus status, String tmpLine )
 161  
     {
 162  0
         File tmpFile = new File( workingDirectory, tmpLine );
 163  0
         if ( status.equals( ScmFileStatus.DELETED ) )
 164  
         {
 165  0
             return true;
 166  
         }
 167  
 
 168  0
         if ( tmpFile.isFile() )
 169  
         {
 170  0
             currentFile = tmpLine;
 171  0
             currentDifference = new StringBuilder();
 172  0
             differences.put( currentFile, currentDifference );
 173  0
             changedFiles.add( new ScmFile( tmpLine, status ) );
 174  0
             return true;
 175  
         }
 176  
 
 177  0
         return false;
 178  
     }
 179  
 
 180  
     public List<ScmFile> getChangedFiles()
 181  
     {
 182  0
         return changedFiles;
 183  
     }
 184  
 
 185  
     public Map<String,CharSequence> getDifferences()
 186  
     {
 187  0
         return differences;
 188  
     }
 189  
 
 190  
     public String getPatch()
 191  
     {
 192  0
         return patch.toString();
 193  
     }
 194  
 }