View Javadoc

1   package org.apache.maven.scm.provider.cvslib.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.codehaus.plexus.util.cli.StreamConsumer;
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:brett@apache.org">Brett Porter</a>
35   * @version $Id: CvsDiffConsumer.java 518629 2007-03-15 13:51:13Z evenisse $
36   * @todo share with SVN (3 extra lines can be ignored)
37   */
38  public class CvsDiffConsumer
39      implements StreamConsumer
40  {
41      //
42      // Index: plugin.jelly
43      // ===================================================================
44      // RCS file: /home/cvs/maven-scm/maven-scm-providers/maven-scm-provider-cvs/src/main/resources/META-INF/plexus/components.xml,v
45      // retrieving revision 1.2
46      // diff -u -r1.2 components.xml
47      // --- plugin.jelly        (revision 124799)
48      // +++ plugin.jelly        (working copy)
49      //
50  
51      private final static String RCS_TOKEN = "RCS file: ";
52  
53      private final static String RETRIEVING_TOKEN = "retrieving revision ";
54  
55      private final static String DIFF_TOKEN = "diff ";
56  
57      private final static String INDEX_TOKEN = "Index: ";
58  
59      private final static String FILE_SEPARATOR_TOKEN = "===";
60  
61      private final static String START_REVISION_TOKEN = "---";
62  
63      private final static String END_REVISION_TOKEN = "+++";
64  
65      private final static String ADDED_LINE_TOKEN = "+";
66  
67      private final static String REMOVED_LINE_TOKEN = "-";
68  
69      private final static String UNCHANGED_LINE_TOKEN = " ";
70  
71      private final static String CHANGE_SEPARATOR_TOKEN = "@@";
72  
73      private final static String NO_NEWLINE_TOKEN = "\\ No newline at end of file";
74  
75      private ScmLogger logger;
76  
77      private String currentFile;
78  
79      private StringBuffer currentDifference;
80  
81      private List changedFiles = new ArrayList();
82  
83      private Map differences = new HashMap();
84  
85      private StringBuffer patch = new StringBuffer();
86  
87      // ----------------------------------------------------------------------
88      //
89      // ----------------------------------------------------------------------
90  
91      public CvsDiffConsumer( ScmLogger logger, File workingDirectory )
92      {
93          this.logger = logger;
94      }
95  
96      // ----------------------------------------------------------------------
97      // StreamConsumer Implementation
98      // ----------------------------------------------------------------------
99  
100     public void consumeLine( String line )
101     {
102         logger.debug( line );
103 
104         if ( line.startsWith( INDEX_TOKEN ) )
105         {
106             // start a new file
107             currentFile = line.substring( INDEX_TOKEN.length() );
108 
109             changedFiles.add( new ScmFile( currentFile, ScmFileStatus.MODIFIED ) );
110 
111             currentDifference = new StringBuffer();
112 
113             differences.put( currentFile, currentDifference );
114 
115             patch.append( line ).append( "\n" );
116 
117             return;
118         }
119 
120         if ( currentFile == null )
121         {
122             logger.warn( "Unparseable line: '" + line + "'" );
123             patch.append( line ).append( "\n" );
124             return;
125         }
126 
127         if ( line.startsWith( FILE_SEPARATOR_TOKEN ) )
128         {
129             // skip
130             patch.append( line ).append( "\n" );
131         }
132         else if ( line.startsWith( START_REVISION_TOKEN ) )
133         {
134             // skip, though could parse to verify filename, start revision
135             patch.append( line ).append( "\n" );
136         }
137         else if ( line.startsWith( END_REVISION_TOKEN ) )
138         {
139             // skip, though could parse to verify filename, end revision
140             patch.append( line ).append( "\n" );
141         }
142         else if ( line.startsWith( RCS_TOKEN ) )
143         {
144             // skip, though could parse to verify filename
145         }
146         else if ( line.startsWith( RETRIEVING_TOKEN ) )
147         {
148             // skip, though could parse to verify version
149         }
150         else if ( line.startsWith( DIFF_TOKEN ) )
151         {
152             // skip, though could parse to verify command
153         }
154         else if ( line.startsWith( ADDED_LINE_TOKEN ) || line.startsWith( REMOVED_LINE_TOKEN ) ||
155             line.startsWith( UNCHANGED_LINE_TOKEN ) || line.startsWith( CHANGE_SEPARATOR_TOKEN ) ||
156             line.equals( NO_NEWLINE_TOKEN ) )
157         {
158             // add to buffer
159             currentDifference.append( line ).append( "\n" );
160             patch.append( line ).append( "\n" );
161         }
162         else
163         {
164             logger.warn( "Unparseable line: '" + line + "'" );
165             patch.append( line ).append( "\n" );
166             // skip to next file
167             currentFile = null;
168             currentDifference = null;
169         }
170     }
171 
172     public List getChangedFiles()
173     {
174         return changedFiles;
175     }
176 
177     public Map getDifferences()
178     {
179         return differences;
180     }
181 
182     public String getPatch()
183     {
184         return patch.toString();
185     }
186 
187 }