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