1 package org.apache.maven.scm.provider.hg.command.diff;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
35
36 public class HgDiffConsumer
37 extends HgConsumer
38 {
39
40
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
86
87
88 public void consumeLine( String line )
89 {
90 if ( line.startsWith( INDEX_TOKEN ) )
91 {
92
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
116 patch.append( line ).append( "\n" );
117 }
118 else if ( line.startsWith( START_REVISION_TOKEN ) )
119 {
120
121 patch.append( line ).append( "\n" );
122 }
123 else if ( line.startsWith( END_REVISION_TOKEN ) )
124 {
125
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
133 currentDifference.append( line ).append( "\n" );
134 patch.append( line ).append( "\n" );
135 }
136 else
137 {
138
139
140 logger.warn( "Unparseable line: '" + line + "'" );
141 patch.append( line ).append( "\n" );
142
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 }