You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FileHeader.java 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.patch;
  44. import static java.nio.charset.StandardCharsets.UTF_8;
  45. import static org.eclipse.jgit.lib.Constants.encodeASCII;
  46. import static org.eclipse.jgit.util.RawParseUtils.decode;
  47. import static org.eclipse.jgit.util.RawParseUtils.decodeNoFallback;
  48. import static org.eclipse.jgit.util.RawParseUtils.extractBinaryString;
  49. import static org.eclipse.jgit.util.RawParseUtils.match;
  50. import static org.eclipse.jgit.util.RawParseUtils.nextLF;
  51. import static org.eclipse.jgit.util.RawParseUtils.parseBase10;
  52. import java.io.IOException;
  53. import java.nio.charset.CharacterCodingException;
  54. import java.nio.charset.Charset;
  55. import java.text.MessageFormat;
  56. import java.util.ArrayList;
  57. import java.util.Collections;
  58. import java.util.List;
  59. import org.eclipse.jgit.diff.DiffEntry;
  60. import org.eclipse.jgit.diff.EditList;
  61. import org.eclipse.jgit.internal.JGitText;
  62. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  63. import org.eclipse.jgit.lib.FileMode;
  64. import org.eclipse.jgit.util.QuotedString;
  65. import org.eclipse.jgit.util.RawParseUtils;
  66. import org.eclipse.jgit.util.TemporaryBuffer;
  67. /**
  68. * Patch header describing an action for a single file path.
  69. */
  70. public class FileHeader extends DiffEntry {
  71. private static final byte[] OLD_MODE = encodeASCII("old mode "); //$NON-NLS-1$
  72. private static final byte[] NEW_MODE = encodeASCII("new mode "); //$NON-NLS-1$
  73. static final byte[] DELETED_FILE_MODE = encodeASCII("deleted file mode "); //$NON-NLS-1$
  74. static final byte[] NEW_FILE_MODE = encodeASCII("new file mode "); //$NON-NLS-1$
  75. private static final byte[] COPY_FROM = encodeASCII("copy from "); //$NON-NLS-1$
  76. private static final byte[] COPY_TO = encodeASCII("copy to "); //$NON-NLS-1$
  77. private static final byte[] RENAME_OLD = encodeASCII("rename old "); //$NON-NLS-1$
  78. private static final byte[] RENAME_NEW = encodeASCII("rename new "); //$NON-NLS-1$
  79. private static final byte[] RENAME_FROM = encodeASCII("rename from "); //$NON-NLS-1$
  80. private static final byte[] RENAME_TO = encodeASCII("rename to "); //$NON-NLS-1$
  81. private static final byte[] SIMILARITY_INDEX = encodeASCII("similarity index "); //$NON-NLS-1$
  82. private static final byte[] DISSIMILARITY_INDEX = encodeASCII("dissimilarity index "); //$NON-NLS-1$
  83. static final byte[] INDEX = encodeASCII("index "); //$NON-NLS-1$
  84. static final byte[] OLD_NAME = encodeASCII("--- "); //$NON-NLS-1$
  85. static final byte[] NEW_NAME = encodeASCII("+++ "); //$NON-NLS-1$
  86. /** Type of patch used by this file. */
  87. public static enum PatchType {
  88. /** A traditional unified diff style patch of a text file. */
  89. UNIFIED,
  90. /** An empty patch with a message "Binary files ... differ" */
  91. BINARY,
  92. /** A Git binary patch, holding pre and post image deltas */
  93. GIT_BINARY;
  94. }
  95. /** Buffer holding the patch data for this file. */
  96. final byte[] buf;
  97. /** Offset within {@link #buf} to the "diff ..." line. */
  98. final int startOffset;
  99. /** Position 1 past the end of this file within {@link #buf}. */
  100. int endOffset;
  101. /** Type of patch used to modify this file */
  102. PatchType patchType;
  103. /** The hunks of this file */
  104. private List<HunkHeader> hunks;
  105. /** If {@link #patchType} is {@link PatchType#GIT_BINARY}, the new image */
  106. BinaryHunk forwardBinaryHunk;
  107. /** If {@link #patchType} is {@link PatchType#GIT_BINARY}, the old image */
  108. BinaryHunk reverseBinaryHunk;
  109. /**
  110. * Constructs a new FileHeader
  111. *
  112. * @param headerLines
  113. * buffer holding the diff header for this file
  114. * @param edits
  115. * the edits for this file
  116. * @param type
  117. * the type of patch used to modify this file
  118. */
  119. public FileHeader(byte[] headerLines, EditList edits, PatchType type) {
  120. this(headerLines, 0);
  121. endOffset = headerLines.length;
  122. int ptr = parseGitFileName(Patch.DIFF_GIT.length, headerLines.length);
  123. parseGitHeaders(ptr, headerLines.length);
  124. this.patchType = type;
  125. addHunk(new HunkHeader(this, edits));
  126. }
  127. FileHeader(byte[] b, int offset) {
  128. buf = b;
  129. startOffset = offset;
  130. changeType = ChangeType.MODIFY; // unless otherwise designated
  131. patchType = PatchType.UNIFIED;
  132. }
  133. int getParentCount() {
  134. return 1;
  135. }
  136. /**
  137. * Get the byte array holding this file's patch script.
  138. *
  139. * @return the byte array holding this file's patch script.
  140. */
  141. public byte[] getBuffer() {
  142. return buf;
  143. }
  144. /**
  145. * Get offset of the start of this file's script in {@link #getBuffer()}.
  146. *
  147. * @return offset of the start of this file's script in
  148. * {@link #getBuffer()}.
  149. */
  150. public int getStartOffset() {
  151. return startOffset;
  152. }
  153. /**
  154. * Get offset one past the end of the file script.
  155. *
  156. * @return offset one past the end of the file script.
  157. */
  158. public int getEndOffset() {
  159. return endOffset;
  160. }
  161. /**
  162. * Convert the patch script for this file into a string.
  163. * <p>
  164. * The default character encoding
  165. * ({@link java.nio.charset.StandardCharsets#UTF_8}) is assumed for both the
  166. * old and new files.
  167. *
  168. * @return the patch script, as a Unicode string.
  169. */
  170. public String getScriptText() {
  171. return getScriptText(null, null);
  172. }
  173. /**
  174. * Convert the patch script for this file into a string.
  175. *
  176. * @param oldCharset
  177. * hint character set to decode the old lines with.
  178. * @param newCharset
  179. * hint character set to decode the new lines with.
  180. * @return the patch script, as a Unicode string.
  181. */
  182. public String getScriptText(Charset oldCharset, Charset newCharset) {
  183. return getScriptText(new Charset[] { oldCharset, newCharset });
  184. }
  185. String getScriptText(Charset[] charsetGuess) {
  186. if (getHunks().isEmpty()) {
  187. // If we have no hunks then we can safely assume the entire
  188. // patch is a binary style patch, or a meta-data only style
  189. // patch. Either way the encoding of the headers should be
  190. // strictly 7-bit US-ASCII and the body is either 7-bit ASCII
  191. // (due to the base 85 encoding used for a BinaryHunk) or is
  192. // arbitrary noise we have chosen to ignore and not understand
  193. // (e.g. the message "Binary files ... differ").
  194. //
  195. return extractBinaryString(buf, startOffset, endOffset);
  196. }
  197. if (charsetGuess != null && charsetGuess.length != getParentCount() + 1)
  198. throw new IllegalArgumentException(MessageFormat.format(
  199. JGitText.get().expectedCharacterEncodingGuesses,
  200. Integer.valueOf(getParentCount() + 1)));
  201. if (trySimpleConversion(charsetGuess)) {
  202. Charset cs = charsetGuess != null ? charsetGuess[0] : null;
  203. if (cs == null) {
  204. cs = UTF_8;
  205. }
  206. try {
  207. return decodeNoFallback(cs, buf, startOffset, endOffset);
  208. } catch (CharacterCodingException cee) {
  209. // Try the much slower, more-memory intensive version which
  210. // can handle a character set conversion patch.
  211. }
  212. }
  213. final StringBuilder r = new StringBuilder(endOffset - startOffset);
  214. // Always treat the headers as US-ASCII; Git file names are encoded
  215. // in a C style escape if any character has the high-bit set.
  216. //
  217. final int hdrEnd = getHunks().get(0).getStartOffset();
  218. for (int ptr = startOffset; ptr < hdrEnd;) {
  219. final int eol = Math.min(hdrEnd, nextLF(buf, ptr));
  220. r.append(extractBinaryString(buf, ptr, eol));
  221. ptr = eol;
  222. }
  223. final String[] files = extractFileLines(charsetGuess);
  224. final int[] offsets = new int[files.length];
  225. for (HunkHeader h : getHunks())
  226. h.extractFileLines(r, files, offsets);
  227. return r.toString();
  228. }
  229. private static boolean trySimpleConversion(Charset[] charsetGuess) {
  230. if (charsetGuess == null)
  231. return true;
  232. for (int i = 1; i < charsetGuess.length; i++) {
  233. if (charsetGuess[i] != charsetGuess[0])
  234. return false;
  235. }
  236. return true;
  237. }
  238. private String[] extractFileLines(Charset[] csGuess) {
  239. final TemporaryBuffer[] tmp = new TemporaryBuffer[getParentCount() + 1];
  240. try {
  241. for (int i = 0; i < tmp.length; i++)
  242. tmp[i] = new TemporaryBuffer.Heap(Integer.MAX_VALUE);
  243. for (HunkHeader h : getHunks())
  244. h.extractFileLines(tmp);
  245. final String[] r = new String[tmp.length];
  246. for (int i = 0; i < tmp.length; i++) {
  247. Charset cs = csGuess != null ? csGuess[i] : null;
  248. if (cs == null) {
  249. cs = UTF_8;
  250. }
  251. r[i] = RawParseUtils.decode(cs, tmp[i].toByteArray());
  252. }
  253. return r;
  254. } catch (IOException ioe) {
  255. throw new RuntimeException(JGitText.get().cannotConvertScriptToText, ioe);
  256. }
  257. }
  258. /**
  259. * Get style of patch used to modify this file.
  260. *
  261. * @return style of patch used to modify this file.
  262. */
  263. public PatchType getPatchType() {
  264. return patchType;
  265. }
  266. /**
  267. * Whether this patch modifies metadata about a file
  268. *
  269. * @return {@code true} if this patch modifies metadata about a file .
  270. */
  271. public boolean hasMetaDataChanges() {
  272. return changeType != ChangeType.MODIFY || newMode != oldMode;
  273. }
  274. /**
  275. * Get hunks altering this file; in order of appearance in patch
  276. *
  277. * @return hunks altering this file; in order of appearance in patch.
  278. */
  279. public List<? extends HunkHeader> getHunks() {
  280. if (hunks == null)
  281. return Collections.emptyList();
  282. return hunks;
  283. }
  284. void addHunk(HunkHeader h) {
  285. if (h.getFileHeader() != this)
  286. throw new IllegalArgumentException(JGitText.get().hunkBelongsToAnotherFile);
  287. if (hunks == null)
  288. hunks = new ArrayList<>();
  289. hunks.add(h);
  290. }
  291. HunkHeader newHunkHeader(int offset) {
  292. return new HunkHeader(this, offset);
  293. }
  294. /**
  295. * Get the new-image delta/literal if this is a
  296. * {@link PatchType#GIT_BINARY}.
  297. *
  298. * @return the new-image delta/literal if this is a
  299. * {@link PatchType#GIT_BINARY}.
  300. */
  301. public BinaryHunk getForwardBinaryHunk() {
  302. return forwardBinaryHunk;
  303. }
  304. /**
  305. * Get the old-image delta/literal if this is a
  306. * {@link PatchType#GIT_BINARY}.
  307. *
  308. * @return the old-image delta/literal if this is a
  309. * {@link PatchType#GIT_BINARY}.
  310. */
  311. public BinaryHunk getReverseBinaryHunk() {
  312. return reverseBinaryHunk;
  313. }
  314. /**
  315. * Convert to a list describing the content edits performed on this file.
  316. *
  317. * @return a list describing the content edits performed on this file.
  318. */
  319. public EditList toEditList() {
  320. final EditList r = new EditList();
  321. for (HunkHeader hunk : hunks)
  322. r.addAll(hunk.toEditList());
  323. return r;
  324. }
  325. /**
  326. * Parse a "diff --git" or "diff --cc" line.
  327. *
  328. * @param ptr
  329. * first character after the "diff --git " or "diff --cc " part.
  330. * @param end
  331. * one past the last position to parse.
  332. * @return first character after the LF at the end of the line; -1 on error.
  333. */
  334. int parseGitFileName(int ptr, int end) {
  335. final int eol = nextLF(buf, ptr);
  336. final int bol = ptr;
  337. if (eol >= end) {
  338. return -1;
  339. }
  340. // buffer[ptr..eol] looks like "a/foo b/foo\n". After the first
  341. // A regex to match this is "^[^/]+/(.*?) [^/+]+/\1\n$". There
  342. // is only one way to split the line such that text to the left
  343. // of the space matches the text to the right, excluding the part
  344. // before the first slash.
  345. //
  346. final int aStart = nextLF(buf, ptr, '/');
  347. if (aStart >= eol)
  348. return eol;
  349. while (ptr < eol) {
  350. final int sp = nextLF(buf, ptr, ' ');
  351. if (sp >= eol) {
  352. // We can't split the header, it isn't valid.
  353. // This may be OK if this is a rename patch.
  354. //
  355. return eol;
  356. }
  357. final int bStart = nextLF(buf, sp, '/');
  358. if (bStart >= eol)
  359. return eol;
  360. // If buffer[aStart..sp - 1] = buffer[bStart..eol - 1]
  361. // we have a valid split.
  362. //
  363. if (eq(aStart, sp - 1, bStart, eol - 1)) {
  364. if (buf[bol] == '"') {
  365. // We're a double quoted name. The region better end
  366. // in a double quote too, and we need to decode the
  367. // characters before reading the name.
  368. //
  369. if (buf[sp - 2] != '"') {
  370. return eol;
  371. }
  372. oldPath = QuotedString.GIT_PATH.dequote(buf, bol, sp - 1);
  373. oldPath = p1(oldPath);
  374. } else {
  375. oldPath = decode(UTF_8, buf, aStart, sp - 1);
  376. }
  377. newPath = oldPath;
  378. return eol;
  379. }
  380. // This split wasn't correct. Move past the space and try
  381. // another split as the space must be part of the file name.
  382. //
  383. ptr = sp;
  384. }
  385. return eol;
  386. }
  387. int parseGitHeaders(int ptr, int end) {
  388. while (ptr < end) {
  389. final int eol = nextLF(buf, ptr);
  390. if (isHunkHdr(buf, ptr, eol) >= 1) {
  391. // First hunk header; break out and parse them later.
  392. break;
  393. } else if (match(buf, ptr, OLD_NAME) >= 0) {
  394. parseOldName(ptr, eol);
  395. } else if (match(buf, ptr, NEW_NAME) >= 0) {
  396. parseNewName(ptr, eol);
  397. } else if (match(buf, ptr, OLD_MODE) >= 0) {
  398. oldMode = parseFileMode(ptr + OLD_MODE.length, eol);
  399. } else if (match(buf, ptr, NEW_MODE) >= 0) {
  400. newMode = parseFileMode(ptr + NEW_MODE.length, eol);
  401. } else if (match(buf, ptr, DELETED_FILE_MODE) >= 0) {
  402. oldMode = parseFileMode(ptr + DELETED_FILE_MODE.length, eol);
  403. newMode = FileMode.MISSING;
  404. changeType = ChangeType.DELETE;
  405. } else if (match(buf, ptr, NEW_FILE_MODE) >= 0) {
  406. parseNewFileMode(ptr, eol);
  407. } else if (match(buf, ptr, COPY_FROM) >= 0) {
  408. oldPath = parseName(oldPath, ptr + COPY_FROM.length, eol);
  409. changeType = ChangeType.COPY;
  410. } else if (match(buf, ptr, COPY_TO) >= 0) {
  411. newPath = parseName(newPath, ptr + COPY_TO.length, eol);
  412. changeType = ChangeType.COPY;
  413. } else if (match(buf, ptr, RENAME_OLD) >= 0) {
  414. oldPath = parseName(oldPath, ptr + RENAME_OLD.length, eol);
  415. changeType = ChangeType.RENAME;
  416. } else if (match(buf, ptr, RENAME_NEW) >= 0) {
  417. newPath = parseName(newPath, ptr + RENAME_NEW.length, eol);
  418. changeType = ChangeType.RENAME;
  419. } else if (match(buf, ptr, RENAME_FROM) >= 0) {
  420. oldPath = parseName(oldPath, ptr + RENAME_FROM.length, eol);
  421. changeType = ChangeType.RENAME;
  422. } else if (match(buf, ptr, RENAME_TO) >= 0) {
  423. newPath = parseName(newPath, ptr + RENAME_TO.length, eol);
  424. changeType = ChangeType.RENAME;
  425. } else if (match(buf, ptr, SIMILARITY_INDEX) >= 0) {
  426. score = parseBase10(buf, ptr + SIMILARITY_INDEX.length, null);
  427. } else if (match(buf, ptr, DISSIMILARITY_INDEX) >= 0) {
  428. score = parseBase10(buf, ptr + DISSIMILARITY_INDEX.length, null);
  429. } else if (match(buf, ptr, INDEX) >= 0) {
  430. parseIndexLine(ptr + INDEX.length, eol);
  431. } else {
  432. // Probably an empty patch (stat dirty).
  433. break;
  434. }
  435. ptr = eol;
  436. }
  437. return ptr;
  438. }
  439. void parseOldName(int ptr, int eol) {
  440. oldPath = p1(parseName(oldPath, ptr + OLD_NAME.length, eol));
  441. if (oldPath == DEV_NULL)
  442. changeType = ChangeType.ADD;
  443. }
  444. void parseNewName(int ptr, int eol) {
  445. newPath = p1(parseName(newPath, ptr + NEW_NAME.length, eol));
  446. if (newPath == DEV_NULL)
  447. changeType = ChangeType.DELETE;
  448. }
  449. void parseNewFileMode(int ptr, int eol) {
  450. oldMode = FileMode.MISSING;
  451. newMode = parseFileMode(ptr + NEW_FILE_MODE.length, eol);
  452. changeType = ChangeType.ADD;
  453. }
  454. int parseTraditionalHeaders(int ptr, int end) {
  455. while (ptr < end) {
  456. final int eol = nextLF(buf, ptr);
  457. if (isHunkHdr(buf, ptr, eol) >= 1) {
  458. // First hunk header; break out and parse them later.
  459. break;
  460. } else if (match(buf, ptr, OLD_NAME) >= 0) {
  461. parseOldName(ptr, eol);
  462. } else if (match(buf, ptr, NEW_NAME) >= 0) {
  463. parseNewName(ptr, eol);
  464. } else {
  465. // Possibly an empty patch.
  466. break;
  467. }
  468. ptr = eol;
  469. }
  470. return ptr;
  471. }
  472. private String parseName(String expect, int ptr, int end) {
  473. if (ptr == end)
  474. return expect;
  475. String r;
  476. if (buf[ptr] == '"') {
  477. // New style GNU diff format
  478. //
  479. r = QuotedString.GIT_PATH.dequote(buf, ptr, end - 1);
  480. } else {
  481. // Older style GNU diff format, an optional tab ends the name.
  482. //
  483. int tab = end;
  484. while (ptr < tab && buf[tab - 1] != '\t')
  485. tab--;
  486. if (ptr == tab)
  487. tab = end;
  488. r = decode(UTF_8, buf, ptr, tab - 1);
  489. }
  490. if (r.equals(DEV_NULL))
  491. r = DEV_NULL;
  492. return r;
  493. }
  494. private static String p1(final String r) {
  495. final int s = r.indexOf('/');
  496. return s > 0 ? r.substring(s + 1) : r;
  497. }
  498. FileMode parseFileMode(int ptr, int end) {
  499. int tmp = 0;
  500. while (ptr < end - 1) {
  501. tmp <<= 3;
  502. tmp += buf[ptr++] - '0';
  503. }
  504. return FileMode.fromBits(tmp);
  505. }
  506. void parseIndexLine(int ptr, int end) {
  507. // "index $asha1..$bsha1[ $mode]" where $asha1 and $bsha1
  508. // can be unique abbreviations
  509. //
  510. final int dot2 = nextLF(buf, ptr, '.');
  511. final int mode = nextLF(buf, dot2, ' ');
  512. oldId = AbbreviatedObjectId.fromString(buf, ptr, dot2 - 1);
  513. newId = AbbreviatedObjectId.fromString(buf, dot2 + 1, mode - 1);
  514. if (mode < end)
  515. newMode = oldMode = parseFileMode(mode, end);
  516. }
  517. private boolean eq(int aPtr, int aEnd, int bPtr, int bEnd) {
  518. if (aEnd - aPtr != bEnd - bPtr) {
  519. return false;
  520. }
  521. while (aPtr < aEnd) {
  522. if (buf[aPtr++] != buf[bPtr++])
  523. return false;
  524. }
  525. return true;
  526. }
  527. /**
  528. * Determine if this is a patch hunk header.
  529. *
  530. * @param buf
  531. * the buffer to scan
  532. * @param start
  533. * first position in the buffer to evaluate
  534. * @param end
  535. * last position to consider; usually the end of the buffer (
  536. * <code>buf.length</code>) or the first position on the next
  537. * line. This is only used to avoid very long runs of '@' from
  538. * killing the scan loop.
  539. * @return the number of "ancestor revisions" in the hunk header. A
  540. * traditional two-way diff ("@@ -...") returns 1; a combined diff
  541. * for a 3 way-merge returns 3. If this is not a hunk header, 0 is
  542. * returned instead.
  543. */
  544. static int isHunkHdr(byte[] buf, int start, int end) {
  545. int ptr = start;
  546. while (ptr < end && buf[ptr] == '@')
  547. ptr++;
  548. if (ptr - start < 2)
  549. return 0;
  550. if (ptr == end || buf[ptr++] != ' ')
  551. return 0;
  552. if (ptr == end || buf[ptr++] != '-')
  553. return 0;
  554. return (ptr - 3) - start;
  555. }
  556. }