View Javadoc

1   package org.apache.maven.scm.provider.hg.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.hg.command.HgConsumer;
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:thurner.rupert@ymono.net">thurner rupert</a>
35   */
36  public class HgDiffConsumer
37      extends HgConsumer
38  {
39  
40      // private final static String MODIFIED_FILE_TOKEN = "=== modified file ";
41  
42      private final static String INDEX_TOKEN = "diff -r ";
43  
44      private final static String FILE_SEPARATOR_TOKEN = "===";
45  
46      private final static String START_REVISION_TOKEN = "---";
47  
48      private final static String END_REVISION_TOKEN = "+++";
49  
50      private final static String ADDED_LINE_TOKEN = "+";
51  
52      private final static String REMOVED_LINE_TOKEN = "-";
53  
54      private final static String UNCHANGED_LINE_TOKEN = " ";
55  
56      private final static String CHANGE_SEPARATOR_TOKEN = "@@";
57  
58      private final static String NO_NEWLINE_TOKEN = "\\ No newline at end of file";
59  
60      private final static int HASH_ID_LEN = 12;
61  
62      private ScmLogger logger;
63  
64      private String currentFile;
65  
66      private StringBuffer currentDifference;
67  
68      private List changedFiles = new ArrayList();
69  
70      private Map differences = new HashMap();
71  
72      private StringBuffer patch = new StringBuffer();
73  
74      private File workingDirectory;
75  
76  
77      public HgDiffConsumer( ScmLogger logger, File workingDirectory )
78      {
79          super( logger );
80          this.logger = logger;
81          this.workingDirectory = workingDirectory;
82      }
83  
84      // ----------------------------------------------------------------------
85      // StreamConsumer Implementation
86      // ----------------------------------------------------------------------
87  
88      public void consumeLine( String line )
89      {
90          if ( line.startsWith( INDEX_TOKEN ) )
91          {
92              // start a new file
93              currentFile = line.substring( INDEX_TOKEN.length() + HASH_ID_LEN + 1 );
94  
95              changedFiles.add( new ScmFile( currentFile, ScmFileStatus.MODIFIED ) );
96  
97              currentDifference = new StringBuffer();
98  
99              differences.put( currentFile, currentDifference );
100 
101             patch.append( line ).append( "\n" );
102 
103             return;
104         }
105 
106         if ( currentFile == null )
107         {
108             logger.warn( "Unparseable line: '" + line + "'" );
109             patch.append( line ).append( "\n" );
110             return;
111         }
112 
113         if ( line.startsWith( FILE_SEPARATOR_TOKEN ) )
114         {
115             // skip
116             patch.append( line ).append( "\n" );
117         }
118         else if ( line.startsWith( START_REVISION_TOKEN ) )
119         {
120             // skip, though could parse to verify filename, start revision
121             patch.append( line ).append( "\n" );
122         }
123         else if ( line.startsWith( END_REVISION_TOKEN ) )
124         {
125             // skip, though could parse to verify filename, end revision
126             patch.append( line ).append( "\n" );
127         }
128         else if ( line.startsWith( ADDED_LINE_TOKEN ) || line.startsWith( REMOVED_LINE_TOKEN ) ||
129             line.startsWith( UNCHANGED_LINE_TOKEN ) || line.startsWith( CHANGE_SEPARATOR_TOKEN ) ||
130             line.equals( NO_NEWLINE_TOKEN ) )
131         {
132             // add to buffer
133             currentDifference.append( line ).append( "\n" );
134             patch.append( line ).append( "\n" );
135         }
136         else
137         {
138             // TODO: handle property differences
139 
140             logger.warn( "Unparseable line: '" + line + "'" );
141             patch.append( line ).append( "\n" );
142             // skip to next file
143             currentFile = null;
144             currentDifference = null;
145         }
146     }
147 
148     public List getChangedFiles()
149     {
150         return changedFiles;
151     }
152 
153     public Map getDifferences()
154     {
155         return differences;
156     }
157 
158     public String getPatch()
159     {
160         return patch.toString();
161     }
162 }