View Javadoc
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   *
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      private List<ScmFile> changedFiles = new ArrayList<ScmFile>();
70  
71      private Map<String,CharSequence> differences = new HashMap<String,CharSequence>();
72  
73      private StringBuilder patch = new StringBuilder();
74  
75      public BazaarDiffConsumer( ScmLogger logger, File workingDirectory )
76      {
77          super( logger );
78          this.logger = logger;
79          this.workingDirectory = workingDirectory;
80      }
81  
82      /** {@inheritDoc} */
83      public void doConsume( ScmFileStatus status, String line )
84      {
85          String tmpLine = new String( line );
86          patch.append( line ).append( "\n" );
87  
88          // Parse line
89          if ( line.startsWith( MODIFIED_FILE_TOKEN ) )
90          {
91              tmpLine = line.substring( MODIFIED_FILE_TOKEN.length() );
92              tmpLine = tmpLine.trim();
93              status = ScmFileStatus.MODIFIED;
94              addChangedFile( status, line, tmpLine );
95          }
96          else if ( line.startsWith( ADDED_FILE_TOKEN ) )
97          {
98              tmpLine = line.substring( ADDED_FILE_TOKEN.length() );
99              tmpLine = tmpLine.trim();
100             status = ScmFileStatus.ADDED;
101             addChangedFile( status, line, tmpLine );
102         }
103         else if ( line.startsWith( DELETED_FILE_TOKEN ) )
104         {
105             tmpLine = line.substring( DELETED_FILE_TOKEN.length() );
106             tmpLine = tmpLine.trim();
107             status = ScmFileStatus.DELETED;
108             addChangedFile( status, line, tmpLine );
109         }
110         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         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             currentDifference.append( line ).append( "\n" );
119         }
120     }
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         tmpLine = tmpLine.substring( 1, tmpLine.length() - 1 );
134         boolean ok = addChangedFile( status, tmpLine );
135 
136         if ( !ok )
137         {
138             int index = tmpLine.indexOf( '/' );
139             if ( index > -1 )
140             {
141                 tmpLine = tmpLine.substring( index + 1 );
142                 ok = addChangedFile( status, tmpLine );
143             }
144         }
145 
146         if ( !ok )
147         {
148             if ( logger.isWarnEnabled() )
149             {
150                 logger.warn( "Could not figure out of line: " + line );
151             }
152         }
153     }
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         File tmpFile = new File( workingDirectory, tmpLine );
163         if ( status.equals( ScmFileStatus.DELETED ) )
164         {
165             return true;
166         }
167 
168         if ( tmpFile.isFile() )
169         {
170             currentFile = tmpLine;
171             currentDifference = new StringBuilder();
172             differences.put( currentFile, currentDifference );
173             changedFiles.add( new ScmFile( tmpLine, status ) );
174             return true;
175         }
176 
177         return false;
178     }
179 
180     public List<ScmFile> getChangedFiles()
181     {
182         return changedFiles;
183     }
184 
185     public Map<String,CharSequence> getDifferences()
186     {
187         return differences;
188     }
189 
190     public String getPatch()
191     {
192         return patch.toString();
193     }
194 }