View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.scm.provider.hg.command.diff;
20  
21  import java.io.File;
22  import java.util.ArrayList;
23  import java.util.HashMap;
24  import java.util.List;
25  import java.util.Map;
26  
27  import org.apache.maven.scm.ScmFile;
28  import org.apache.maven.scm.ScmFileStatus;
29  import org.apache.maven.scm.provider.hg.command.HgConsumer;
30  
31  /**
32   * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
33   * @author Olivier Lamy
34   */
35  public class HgDiffConsumer extends HgConsumer {
36  
37      // private static final String MODIFIED_FILE_TOKEN = "=== modified file ";
38  
39      private static final String INDEX_TOKEN = "diff -r ";
40  
41      private static final String FILE_SEPARATOR_TOKEN = "===";
42  
43      private static final String START_REVISION_TOKEN = "---";
44  
45      private static final String END_REVISION_TOKEN = "+++";
46  
47      private static final String ADDED_LINE_TOKEN = "+";
48  
49      private static final String REMOVED_LINE_TOKEN = "-";
50  
51      private static final String UNCHANGED_LINE_TOKEN = " ";
52  
53      private static final String CHANGE_SEPARATOR_TOKEN = "@@";
54  
55      private static final String NO_NEWLINE_TOKEN = "\\ No newline at end of file";
56  
57      private static final int HASH_ID_LEN = 12;
58  
59      private String currentFile;
60  
61      private StringBuilder currentDifference;
62  
63      private final List<ScmFile> changedFiles = new ArrayList<>();
64  
65      private final Map<String, CharSequence> differences = new HashMap<>();
66  
67      private final StringBuilder patch = new StringBuilder();
68  
69      @SuppressWarnings("unused")
70      private final File workingDirectory;
71  
72      public HgDiffConsumer(File workingDirectory) {
73          this.workingDirectory = workingDirectory;
74      }
75  
76      // ----------------------------------------------------------------------
77      // StreamConsumer Implementation
78      // ----------------------------------------------------------------------
79  
80      /**
81       * {@inheritDoc}
82       */
83      public void consumeLine(String line) {
84          if (line.startsWith(INDEX_TOKEN)) {
85              // start a new file
86              currentFile = line.substring(INDEX_TOKEN.length() + HASH_ID_LEN + 1);
87  
88              changedFiles.add(new ScmFile(currentFile, ScmFileStatus.MODIFIED));
89  
90              currentDifference = new StringBuilder();
91  
92              differences.put(currentFile, currentDifference);
93  
94              patch.append(line).append("\n");
95  
96              return;
97          }
98  
99          if (currentFile == null) {
100             if (logger.isWarnEnabled()) {
101                 logger.warn("Unparseable line: '" + line + "'");
102             }
103             patch.append(line).append("\n");
104             return;
105         }
106 
107         if (line.startsWith(FILE_SEPARATOR_TOKEN)) {
108             // skip
109             patch.append(line).append("\n");
110         } else if (line.startsWith(START_REVISION_TOKEN)) {
111             // skip, though could parse to verify filename, start revision
112             patch.append(line).append("\n");
113         } else if (line.startsWith(END_REVISION_TOKEN)) {
114             // skip, though could parse to verify filename, end revision
115             patch.append(line).append("\n");
116         } else if (line.startsWith(ADDED_LINE_TOKEN)
117                 || line.startsWith(REMOVED_LINE_TOKEN)
118                 || line.startsWith(UNCHANGED_LINE_TOKEN)
119                 || line.startsWith(CHANGE_SEPARATOR_TOKEN)
120                 || line.equals(NO_NEWLINE_TOKEN)) {
121             // add to buffer
122             currentDifference.append(line).append("\n");
123             patch.append(line).append("\n");
124         } else {
125             // TODO: handle property differences
126 
127             if (logger.isWarnEnabled()) {
128                 logger.warn("Unparseable line: '" + line + "'");
129             }
130             patch.append(line).append("\n");
131             // skip to next file
132             currentFile = null;
133             currentDifference = null;
134         }
135     }
136 
137     public List<ScmFile> getChangedFiles() {
138         return changedFiles;
139     }
140 
141     public Map<String, CharSequence> getDifferences() {
142         return differences;
143     }
144 
145     public String getPatch() {
146         return patch.toString();
147     }
148 }