1 | |
package org.apache.maven.scm.provider.cvslib.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.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 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public class CvsDiffConsumer |
40 | |
implements StreamConsumer |
41 | |
{ |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
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 | 0 | private List<ScmFile> changedFiles = new ArrayList<ScmFile>(); |
84 | |
|
85 | 0 | private Map<String,CharSequence> differences = new HashMap<String,CharSequence>(); |
86 | |
|
87 | 0 | private StringBuilder patch = new StringBuilder(); |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public CvsDiffConsumer( ScmLogger logger, File workingDirectory ) |
94 | 0 | { |
95 | 0 | this.logger = logger; |
96 | 0 | } |
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
public void consumeLine( String line ) |
104 | |
{ |
105 | 0 | if ( logger.isDebugEnabled() ) |
106 | |
{ |
107 | 0 | logger.debug( line ); |
108 | |
} |
109 | |
|
110 | 0 | if ( line.startsWith( INDEX_TOKEN ) ) |
111 | |
{ |
112 | |
|
113 | 0 | currentFile = line.substring( INDEX_TOKEN.length() ); |
114 | |
|
115 | 0 | changedFiles.add( new ScmFile( currentFile, ScmFileStatus.MODIFIED ) ); |
116 | |
|
117 | 0 | currentDifference = new StringBuilder(); |
118 | |
|
119 | 0 | differences.put( currentFile, currentDifference ); |
120 | |
|
121 | 0 | patch.append( line ).append( "\n" ); |
122 | |
|
123 | 0 | return; |
124 | |
} |
125 | |
|
126 | 0 | if ( currentFile == null ) |
127 | |
{ |
128 | 0 | if ( logger.isWarnEnabled() ) |
129 | |
{ |
130 | 0 | logger.warn( "Unparseable line: '" + line + "'" ); |
131 | |
} |
132 | 0 | patch.append( line ).append( "\n" ); |
133 | 0 | return; |
134 | |
} |
135 | |
|
136 | 0 | if ( line.startsWith( FILE_SEPARATOR_TOKEN ) ) |
137 | |
{ |
138 | |
|
139 | 0 | patch.append( line ).append( "\n" ); |
140 | |
} |
141 | 0 | else if ( line.startsWith( START_REVISION_TOKEN ) ) |
142 | |
{ |
143 | |
|
144 | 0 | patch.append( line ).append( "\n" ); |
145 | |
} |
146 | 0 | else if ( line.startsWith( END_REVISION_TOKEN ) ) |
147 | |
{ |
148 | |
|
149 | 0 | patch.append( line ).append( "\n" ); |
150 | |
} |
151 | 0 | else if ( line.startsWith( RCS_TOKEN ) ) |
152 | |
{ |
153 | |
|
154 | |
} |
155 | 0 | else if ( line.startsWith( RETRIEVING_TOKEN ) ) |
156 | |
{ |
157 | |
|
158 | |
} |
159 | 0 | else if ( line.startsWith( DIFF_TOKEN ) ) |
160 | |
{ |
161 | |
|
162 | |
} |
163 | 0 | 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 | |
|
168 | 0 | currentDifference.append( line ).append( "\n" ); |
169 | 0 | patch.append( line ).append( "\n" ); |
170 | |
} |
171 | |
else |
172 | |
{ |
173 | 0 | if ( logger.isWarnEnabled() ) |
174 | |
{ |
175 | 0 | logger.warn( "Unparseable line: '" + line + "'" ); |
176 | |
} |
177 | 0 | patch.append( line ).append( "\n" ); |
178 | |
|
179 | 0 | currentFile = null; |
180 | 0 | currentDifference = null; |
181 | |
} |
182 | 0 | } |
183 | |
|
184 | |
public List<ScmFile> getChangedFiles() |
185 | |
{ |
186 | 0 | return changedFiles; |
187 | |
} |
188 | |
|
189 | |
public Map<String,CharSequence> getDifferences() |
190 | |
{ |
191 | 0 | return differences; |
192 | |
} |
193 | |
|
194 | |
public String getPatch() |
195 | |
{ |
196 | 0 | return patch.toString(); |
197 | |
} |
198 | |
|
199 | |
} |