1 | |
package org.apache.maven.scm.provider.svn.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 | |
public class SvnDiffConsumer |
39 | |
implements StreamConsumer |
40 | |
{ |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
private static final String INDEX_TOKEN = "Index: "; |
49 | |
|
50 | |
private static final String FILE_SEPARATOR_TOKEN = "==="; |
51 | |
|
52 | |
private static final String START_REVISION_TOKEN = "---"; |
53 | |
|
54 | |
private static final String END_REVISION_TOKEN = "+++"; |
55 | |
|
56 | |
private static final String ADDED_LINE_TOKEN = "+"; |
57 | |
|
58 | |
private static final String REMOVED_LINE_TOKEN = "-"; |
59 | |
|
60 | |
private static final String UNCHANGED_LINE_TOKEN = " "; |
61 | |
|
62 | |
private static final String CHANGE_SEPARATOR_TOKEN = "@@"; |
63 | |
|
64 | |
private static final String NO_NEWLINE_TOKEN = "\\ No newline at end of file"; |
65 | |
|
66 | |
private ScmLogger logger; |
67 | |
|
68 | |
private String currentFile; |
69 | |
|
70 | |
private StringBuilder currentDifference; |
71 | |
|
72 | 0 | private List<ScmFile> changedFiles = new ArrayList<ScmFile>(); |
73 | |
|
74 | 0 | private Map<String,CharSequence> differences = new HashMap<String,CharSequence>(); |
75 | |
|
76 | 0 | private StringBuilder patch = new StringBuilder(); |
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public SvnDiffConsumer( ScmLogger logger, File workingDirectory ) |
83 | 0 | { |
84 | 0 | this.logger = logger; |
85 | 0 | } |
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public void consumeLine( String line ) |
93 | |
{ |
94 | 0 | if ( line.startsWith( INDEX_TOKEN ) ) |
95 | |
{ |
96 | |
|
97 | 0 | currentFile = line.substring( INDEX_TOKEN.length() ); |
98 | |
|
99 | 0 | changedFiles.add( new ScmFile( currentFile, ScmFileStatus.MODIFIED ) ); |
100 | |
|
101 | 0 | currentDifference = new StringBuilder(); |
102 | |
|
103 | 0 | differences.put( currentFile, currentDifference ); |
104 | |
|
105 | 0 | patch.append( line ).append( "\n" ); |
106 | |
|
107 | 0 | return; |
108 | |
} |
109 | |
|
110 | 0 | if ( currentFile == null ) |
111 | |
{ |
112 | 0 | if ( logger.isWarnEnabled() ) |
113 | |
{ |
114 | 0 | logger.warn( "Unparseable line: '" + line + "'" ); |
115 | |
} |
116 | 0 | patch.append( line ).append( "\n" ); |
117 | 0 | return; |
118 | |
} |
119 | |
|
120 | 0 | if ( line.startsWith( FILE_SEPARATOR_TOKEN ) ) |
121 | |
{ |
122 | |
|
123 | 0 | patch.append( line ).append( "\n" ); |
124 | |
} |
125 | 0 | else if ( line.startsWith( START_REVISION_TOKEN ) ) |
126 | |
{ |
127 | |
|
128 | 0 | patch.append( line ).append( "\n" ); |
129 | |
} |
130 | 0 | else if ( line.startsWith( END_REVISION_TOKEN ) ) |
131 | |
{ |
132 | |
|
133 | 0 | patch.append( line ).append( "\n" ); |
134 | |
} |
135 | 0 | else if ( line.startsWith( ADDED_LINE_TOKEN ) || line.startsWith( REMOVED_LINE_TOKEN ) |
136 | |
|| line.startsWith( UNCHANGED_LINE_TOKEN ) || line.startsWith( CHANGE_SEPARATOR_TOKEN ) |
137 | |
|| line.equals( NO_NEWLINE_TOKEN ) ) |
138 | |
{ |
139 | |
|
140 | 0 | currentDifference.append( line ).append( "\n" ); |
141 | 0 | patch.append( line ).append( "\n" ); |
142 | |
} |
143 | |
else |
144 | |
{ |
145 | |
|
146 | |
|
147 | 0 | if ( logger.isWarnEnabled() ) |
148 | |
{ |
149 | 0 | logger.warn( "Unparseable line: '" + line + "'" ); |
150 | |
} |
151 | 0 | patch.append( line ).append( "\n" ); |
152 | |
|
153 | 0 | currentFile = null; |
154 | 0 | currentDifference = null; |
155 | |
} |
156 | 0 | } |
157 | |
|
158 | |
public List<ScmFile> getChangedFiles() |
159 | |
{ |
160 | 0 | return changedFiles; |
161 | |
} |
162 | |
|
163 | |
public Map<String,CharSequence> getDifferences() |
164 | |
{ |
165 | 0 | return differences; |
166 | |
} |
167 | |
|
168 | |
public String getPatch() |
169 | |
{ |
170 | 0 | return patch.toString(); |
171 | |
} |
172 | |
|
173 | |
} |