1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44 package org.eclipse.jgit.patch;
45
46 import static org.eclipse.jgit.lib.Constants.encodeASCII;
47 import static org.eclipse.jgit.patch.FileHeader.NEW_NAME;
48 import static org.eclipse.jgit.patch.FileHeader.OLD_NAME;
49 import static org.eclipse.jgit.patch.FileHeader.isHunkHdr;
50 import static org.eclipse.jgit.util.RawParseUtils.match;
51 import static org.eclipse.jgit.util.RawParseUtils.nextLF;
52
53 import java.io.IOException;
54 import java.io.InputStream;
55 import java.util.ArrayList;
56 import java.util.List;
57
58 import org.eclipse.jgit.internal.JGitText;
59 import org.eclipse.jgit.util.TemporaryBuffer;
60
61
62
63
64
65 public class Patch {
66 static final byte[] DIFF_GIT = encodeASCII("diff --git ");
67
68 private static final byte[] DIFF_CC = encodeASCII("diff --cc ");
69
70 private static final byte[] DIFF_COMBINED = encodeASCII("diff --combined ");
71
72 private static final byte[][] BIN_HEADERS = new byte[][] {
73 encodeASCII("Binary files "), encodeASCII("Files "), };
74
75 private static final byte[] BIN_TRAILER = encodeASCII(" differ\n");
76
77 private static final byte[] GIT_BINARY = encodeASCII("GIT binary patch\n");
78
79 static final byte[] SIG_FOOTER = encodeASCII("-- \n");
80
81
82 private final List<FileHeader> files;
83
84
85 private final List<FormatError> errors;
86
87
88
89
90 public Patch() {
91 files = new ArrayList<>();
92 errors = new ArrayList<>(0);
93 }
94
95
96
97
98
99
100
101
102
103
104 public void addFile(FileHeader fh) {
105 files.add(fh);
106 }
107
108
109
110
111
112
113 public List<? extends FileHeader> getFiles() {
114 return files;
115 }
116
117
118
119
120
121
122
123 public void addError(FormatError err) {
124 errors.add(err);
125 }
126
127
128
129
130
131
132 public List<FormatError> getErrors() {
133 return errors;
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149 public void parse(InputStream is) throws IOException {
150 final byte[] buf = readFully(is);
151 parse(buf, 0, buf.length);
152 }
153
154 private static byte[] readFully(InputStream is) throws IOException {
155 try (TemporaryBuffer b = new TemporaryBuffer.Heap(Integer.MAX_VALUE)) {
156 b.copy(is);
157 return b.toByteArray();
158 }
159 }
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176 public void parse(byte[] buf, int ptr, int end) {
177 while (ptr < end)
178 ptr = parseFile(buf, ptr, end);
179 }
180
181 private int parseFile(byte[] buf, int c, int end) {
182 while (c < end) {
183 if (isHunkHdr(buf, c, end) >= 1) {
184
185
186
187
188 error(buf, c, JGitText.get().hunkDisconnectedFromFile);
189 c = nextLF(buf, c);
190 continue;
191 }
192
193
194
195 if (match(buf, c, DIFF_GIT) >= 0)
196 return parseDiffGit(buf, c, end);
197 if (match(buf, c, DIFF_CC) >= 0)
198 return parseDiffCombined(DIFF_CC, buf, c, end);
199 if (match(buf, c, DIFF_COMBINED) >= 0)
200 return parseDiffCombined(DIFF_COMBINED, buf, c, end);
201
202
203
204
205 final int n = nextLF(buf, c);
206 if (n >= end) {
207
208
209
210 return end;
211 }
212
213 if (n - c < 6) {
214
215
216
217 c = n;
218 continue;
219 }
220
221 if (match(buf, c, OLD_NAME) >= 0 && match(buf, n, NEW_NAME) >= 0) {
222
223
224
225 final int f = nextLF(buf, n);
226 if (f >= end)
227 return end;
228 if (isHunkHdr(buf, f, end) == 1)
229 return parseTraditionalPatch(buf, c, end);
230 }
231
232 c = n;
233 }
234 return c;
235 }
236
237 private int parseDiffGit(byte[] buf, int start, int end) {
238 final FileHeadereader.html#FileHeader">FileHeader fh = new FileHeader(buf, start);
239 int ptr = fh.parseGitFileName(start + DIFF_GIT.length, end);
240 if (ptr < 0)
241 return skipFile(buf, start);
242
243 ptr = fh.parseGitHeaders(ptr, end);
244 ptr = parseHunks(fh, ptr, end);
245 fh.endOffset = ptr;
246 addFile(fh);
247 return ptr;
248 }
249
250 private int parseDiffCombined(final byte[] hdr, final byte[] buf,
251 final int start, final int end) {
252 final CombinedFileHeadereader.html#CombinedFileHeader">CombinedFileHeader fh = new CombinedFileHeader(buf, start);
253 int ptr = fh.parseGitFileName(start + hdr.length, end);
254 if (ptr < 0)
255 return skipFile(buf, start);
256
257 ptr = fh.parseGitHeaders(ptr, end);
258 ptr = parseHunks(fh, ptr, end);
259 fh.endOffset = ptr;
260 addFile(fh);
261 return ptr;
262 }
263
264 private int parseTraditionalPatch(final byte[] buf, final int start,
265 final int end) {
266 final FileHeadereader.html#FileHeader">FileHeader fh = new FileHeader(buf, start);
267 int ptr = fh.parseTraditionalHeaders(start, end);
268 ptr = parseHunks(fh, ptr, end);
269 fh.endOffset = ptr;
270 addFile(fh);
271 return ptr;
272 }
273
274 private static int skipFile(byte[] buf, int ptr) {
275 ptr = nextLF(buf, ptr);
276 if (match(buf, ptr, OLD_NAME) >= 0)
277 ptr = nextLF(buf, ptr);
278 return ptr;
279 }
280
281 private int parseHunks(FileHeader fh, int c, int end) {
282 final byte[] buf = fh.buf;
283 while (c < end) {
284
285
286
287
288 if (match(buf, c, DIFF_GIT) >= 0)
289 break;
290 if (match(buf, c, DIFF_CC) >= 0)
291 break;
292 if (match(buf, c, DIFF_COMBINED) >= 0)
293 break;
294 if (match(buf, c, OLD_NAME) >= 0)
295 break;
296 if (match(buf, c, NEW_NAME) >= 0)
297 break;
298
299 if (isHunkHdr(buf, c, end) == fh.getParentCount()) {
300 final HunkHeader h = fh.newHunkHeader(c);
301 h.parseHeader();
302 c = h.parseBody(this, end);
303 h.endOffset = c;
304 fh.addHunk(h);
305 if (c < end) {
306 switch (buf[c]) {
307 case '@':
308 case 'd':
309 case '\n':
310 break;
311 default:
312 if (match(buf, c, SIG_FOOTER) < 0)
313 warn(buf, c, JGitText.get().unexpectedHunkTrailer);
314 }
315 }
316 continue;
317 }
318
319 final int eol = nextLF(buf, c);
320 if (fh.getHunks().isEmpty() && match(buf, c, GIT_BINARY) >= 0) {
321 fh.patchType = FileHeader.PatchType.GIT_BINARY;
322 return parseGitBinary(fh, eol, end);
323 }
324
325 if (fh.getHunks().isEmpty() && BIN_TRAILER.length < eol - c
326 && match(buf, eol - BIN_TRAILER.length, BIN_TRAILER) >= 0
327 && matchAny(buf, c, BIN_HEADERS)) {
328
329
330 fh.patchType = FileHeader.PatchType.BINARY;
331 return eol;
332 }
333
334
335
336
337 c = eol;
338 }
339
340 if (fh.getHunks().isEmpty()
341 && fh.getPatchType() == FileHeader.PatchType.UNIFIED
342 && !fh.hasMetaDataChanges()) {
343
344
345
346 fh.patchType = FileHeader.PatchType.BINARY;
347 }
348
349 return c;
350 }
351
352 private int parseGitBinary(FileHeader fh, int c, int end) {
353 final BinaryHunktml#BinaryHunk">BinaryHunk postImage = new BinaryHunk(fh, c);
354 final int nEnd = postImage.parseHunk(c, end);
355 if (nEnd < 0) {
356
357
358 error(fh.buf, c, JGitText.get().missingForwardImageInGITBinaryPatch);
359 return c;
360 }
361 c = nEnd;
362 postImage.endOffset = c;
363 fh.forwardBinaryHunk = postImage;
364
365 final BinaryHunkhtml#BinaryHunk">BinaryHunk preImage = new BinaryHunk(fh, c);
366 final int oEnd = preImage.parseHunk(c, end);
367 if (oEnd >= 0) {
368 c = oEnd;
369 preImage.endOffset = c;
370 fh.reverseBinaryHunk = preImage;
371 }
372
373 return c;
374 }
375
376 void warn(byte[] buf, int ptr, String msg) {
377 addError(new FormatError(buf, ptr, FormatError.Severity.WARNING, msg));
378 }
379
380 void error(byte[] buf, int ptr, String msg) {
381 addError(new FormatError(buf, ptr, FormatError.Severity.ERROR, msg));
382 }
383
384 private static boolean matchAny(final byte[] buf, final int c,
385 final byte[][] srcs) {
386 for (byte[] s : srcs) {
387 if (match(buf, c, s) >= 0)
388 return true;
389 }
390 return false;
391 }
392 }