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.

DiffFormatter.java 36KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2008-2009, Johannes E. Schindelin <johannes.schindelin@gmx.de>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.diff;
  45. import static org.eclipse.jgit.diff.DiffEntry.ChangeType.ADD;
  46. import static org.eclipse.jgit.diff.DiffEntry.ChangeType.COPY;
  47. import static org.eclipse.jgit.diff.DiffEntry.ChangeType.DELETE;
  48. import static org.eclipse.jgit.diff.DiffEntry.ChangeType.MODIFY;
  49. import static org.eclipse.jgit.diff.DiffEntry.ChangeType.RENAME;
  50. import static org.eclipse.jgit.diff.DiffEntry.Side.NEW;
  51. import static org.eclipse.jgit.diff.DiffEntry.Side.OLD;
  52. import static org.eclipse.jgit.lib.Constants.encode;
  53. import static org.eclipse.jgit.lib.Constants.encodeASCII;
  54. import static org.eclipse.jgit.lib.FileMode.GITLINK;
  55. import java.io.ByteArrayOutputStream;
  56. import java.io.IOException;
  57. import java.io.OutputStream;
  58. import java.util.Collection;
  59. import java.util.Collections;
  60. import java.util.List;
  61. import org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm;
  62. import org.eclipse.jgit.diff.DiffEntry.ChangeType;
  63. import org.eclipse.jgit.dircache.DirCacheIterator;
  64. import org.eclipse.jgit.errors.AmbiguousObjectException;
  65. import org.eclipse.jgit.errors.CorruptObjectException;
  66. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  67. import org.eclipse.jgit.errors.LargeObjectException;
  68. import org.eclipse.jgit.errors.MissingObjectException;
  69. import org.eclipse.jgit.internal.JGitText;
  70. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  71. import org.eclipse.jgit.lib.AnyObjectId;
  72. import org.eclipse.jgit.lib.ConfigConstants;
  73. import org.eclipse.jgit.lib.Constants;
  74. import org.eclipse.jgit.lib.FileMode;
  75. import org.eclipse.jgit.lib.ObjectId;
  76. import org.eclipse.jgit.lib.ObjectLoader;
  77. import org.eclipse.jgit.lib.ObjectReader;
  78. import org.eclipse.jgit.lib.ProgressMonitor;
  79. import org.eclipse.jgit.lib.Repository;
  80. import org.eclipse.jgit.patch.FileHeader;
  81. import org.eclipse.jgit.patch.FileHeader.PatchType;
  82. import org.eclipse.jgit.patch.HunkHeader;
  83. import org.eclipse.jgit.revwalk.FollowFilter;
  84. import org.eclipse.jgit.revwalk.RevTree;
  85. import org.eclipse.jgit.revwalk.RevWalk;
  86. import org.eclipse.jgit.storage.pack.PackConfig;
  87. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  88. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  89. import org.eclipse.jgit.treewalk.EmptyTreeIterator;
  90. import org.eclipse.jgit.treewalk.TreeWalk;
  91. import org.eclipse.jgit.treewalk.WorkingTreeIterator;
  92. import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
  93. import org.eclipse.jgit.treewalk.filter.IndexDiffFilter;
  94. import org.eclipse.jgit.treewalk.filter.NotIgnoredFilter;
  95. import org.eclipse.jgit.treewalk.filter.PathFilter;
  96. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  97. import org.eclipse.jgit.util.QuotedString;
  98. import org.eclipse.jgit.util.io.DisabledOutputStream;
  99. /**
  100. * Format a Git style patch script.
  101. */
  102. public class DiffFormatter implements AutoCloseable {
  103. private static final int DEFAULT_BINARY_FILE_THRESHOLD = PackConfig.DEFAULT_BIG_FILE_THRESHOLD;
  104. private static final byte[] noNewLine = encodeASCII("\\ No newline at end of file\n"); //$NON-NLS-1$
  105. /** Magic return content indicating it is empty or no content present. */
  106. private static final byte[] EMPTY = new byte[] {};
  107. /** Magic return indicating the content is binary. */
  108. private static final byte[] BINARY = new byte[] {};
  109. private final OutputStream out;
  110. private Repository db;
  111. private ObjectReader reader;
  112. private DiffConfig diffCfg;
  113. private int context = 3;
  114. private int abbreviationLength = 7;
  115. private DiffAlgorithm diffAlgorithm;
  116. private RawTextComparator comparator = RawTextComparator.DEFAULT;
  117. private int binaryFileThreshold = DEFAULT_BINARY_FILE_THRESHOLD;
  118. private String oldPrefix = "a/"; //$NON-NLS-1$
  119. private String newPrefix = "b/"; //$NON-NLS-1$
  120. private TreeFilter pathFilter = TreeFilter.ALL;
  121. private RenameDetector renameDetector;
  122. private ProgressMonitor progressMonitor;
  123. private ContentSource.Pair source;
  124. /**
  125. * Create a new formatter with a default level of context.
  126. *
  127. * @param out
  128. * the stream the formatter will write line data to. This stream
  129. * should have buffering arranged by the caller, as many small
  130. * writes are performed to it.
  131. */
  132. public DiffFormatter(OutputStream out) {
  133. this.out = out;
  134. }
  135. /** @return the stream we are outputting data to. */
  136. protected OutputStream getOutputStream() {
  137. return out;
  138. }
  139. /**
  140. * Set the repository the formatter can load object contents from.
  141. *
  142. * Once a repository has been set, the formatter must be released to ensure
  143. * the internal ObjectReader is able to release its resources.
  144. *
  145. * @param repository
  146. * source repository holding referenced objects.
  147. */
  148. public void setRepository(Repository repository) {
  149. if (reader != null)
  150. reader.close();
  151. db = repository;
  152. reader = db.newObjectReader();
  153. diffCfg = db.getConfig().get(DiffConfig.KEY);
  154. ContentSource cs = ContentSource.create(reader);
  155. source = new ContentSource.Pair(cs, cs);
  156. DiffConfig dc = db.getConfig().get(DiffConfig.KEY);
  157. if (dc.isNoPrefix()) {
  158. setOldPrefix(""); //$NON-NLS-1$
  159. setNewPrefix(""); //$NON-NLS-1$
  160. }
  161. setDetectRenames(dc.isRenameDetectionEnabled());
  162. diffAlgorithm = DiffAlgorithm.getAlgorithm(db.getConfig().getEnum(
  163. ConfigConstants.CONFIG_DIFF_SECTION, null,
  164. ConfigConstants.CONFIG_KEY_ALGORITHM,
  165. SupportedAlgorithm.HISTOGRAM));
  166. }
  167. /**
  168. * Change the number of lines of context to display.
  169. *
  170. * @param lineCount
  171. * number of lines of context to see before the first
  172. * modification and after the last modification within a hunk of
  173. * the modified file.
  174. */
  175. public void setContext(final int lineCount) {
  176. if (lineCount < 0)
  177. throw new IllegalArgumentException(
  178. JGitText.get().contextMustBeNonNegative);
  179. context = lineCount;
  180. }
  181. /**
  182. * Change the number of digits to show in an ObjectId.
  183. *
  184. * @param count
  185. * number of digits to show in an ObjectId.
  186. */
  187. public void setAbbreviationLength(final int count) {
  188. if (count < 0)
  189. throw new IllegalArgumentException(
  190. JGitText.get().abbreviationLengthMustBeNonNegative);
  191. abbreviationLength = count;
  192. }
  193. /**
  194. * Set the algorithm that constructs difference output.
  195. *
  196. * @param alg
  197. * the algorithm to produce text file differences.
  198. * @see HistogramDiff
  199. */
  200. public void setDiffAlgorithm(DiffAlgorithm alg) {
  201. diffAlgorithm = alg;
  202. }
  203. /**
  204. * Set the line equivalence function for text file differences.
  205. *
  206. * @param cmp
  207. * The equivalence function used to determine if two lines of
  208. * text are identical. The function can be changed to ignore
  209. * various types of whitespace.
  210. * @see RawTextComparator#DEFAULT
  211. * @see RawTextComparator#WS_IGNORE_ALL
  212. * @see RawTextComparator#WS_IGNORE_CHANGE
  213. * @see RawTextComparator#WS_IGNORE_LEADING
  214. * @see RawTextComparator#WS_IGNORE_TRAILING
  215. */
  216. public void setDiffComparator(RawTextComparator cmp) {
  217. comparator = cmp;
  218. }
  219. /**
  220. * Set maximum file size for text files.
  221. *
  222. * Files larger than this size will be treated as though they are binary and
  223. * not text. Default is {@value #DEFAULT_BINARY_FILE_THRESHOLD} .
  224. *
  225. * @param threshold
  226. * the limit, in bytes. Files larger than this size will be
  227. * assumed to be binary, even if they aren't.
  228. */
  229. public void setBinaryFileThreshold(int threshold) {
  230. this.binaryFileThreshold = threshold;
  231. }
  232. /**
  233. * Set the prefix applied in front of old file paths.
  234. *
  235. * @param prefix
  236. * the prefix in front of old paths. Typically this is the
  237. * standard string {@code "a/"}, but may be any prefix desired by
  238. * the caller. Must not be null. Use the empty string to have no
  239. * prefix at all.
  240. */
  241. public void setOldPrefix(String prefix) {
  242. oldPrefix = prefix;
  243. }
  244. /**
  245. * Get the prefix applied in front of old file paths.
  246. *
  247. * @return the prefix
  248. * @since 2.0
  249. */
  250. public String getOldPrefix() {
  251. return this.oldPrefix;
  252. }
  253. /**
  254. * Set the prefix applied in front of new file paths.
  255. *
  256. * @param prefix
  257. * the prefix in front of new paths. Typically this is the
  258. * standard string {@code "b/"}, but may be any prefix desired by
  259. * the caller. Must not be null. Use the empty string to have no
  260. * prefix at all.
  261. */
  262. public void setNewPrefix(String prefix) {
  263. newPrefix = prefix;
  264. }
  265. /**
  266. * Get the prefix applied in front of new file paths.
  267. *
  268. * @return the prefix
  269. * @since 2.0
  270. */
  271. public String getNewPrefix() {
  272. return this.newPrefix;
  273. }
  274. /** @return true if rename detection is enabled. */
  275. public boolean isDetectRenames() {
  276. return renameDetector != null;
  277. }
  278. /**
  279. * Enable or disable rename detection.
  280. *
  281. * Before enabling rename detection the repository must be set with
  282. * {@link #setRepository(Repository)}. Once enabled the detector can be
  283. * configured away from its defaults by obtaining the instance directly from
  284. * {@link #getRenameDetector()} and invoking configuration.
  285. *
  286. * @param on
  287. * if rename detection should be enabled.
  288. */
  289. public void setDetectRenames(boolean on) {
  290. if (on && renameDetector == null) {
  291. assertHaveRepository();
  292. renameDetector = new RenameDetector(db);
  293. } else if (!on)
  294. renameDetector = null;
  295. }
  296. /** @return the rename detector if rename detection is enabled. */
  297. public RenameDetector getRenameDetector() {
  298. return renameDetector;
  299. }
  300. /**
  301. * Set the progress monitor for long running rename detection.
  302. *
  303. * @param pm
  304. * progress monitor to receive rename detection status through.
  305. */
  306. public void setProgressMonitor(ProgressMonitor pm) {
  307. progressMonitor = pm;
  308. }
  309. /**
  310. * Set the filter to produce only specific paths.
  311. *
  312. * If the filter is an instance of {@link FollowFilter}, the filter path
  313. * will be updated during successive scan or format invocations. The updated
  314. * path can be obtained from {@link #getPathFilter()}.
  315. *
  316. * @param filter
  317. * the tree filter to apply.
  318. */
  319. public void setPathFilter(TreeFilter filter) {
  320. pathFilter = filter != null ? filter : TreeFilter.ALL;
  321. }
  322. /** @return the current path filter. */
  323. public TreeFilter getPathFilter() {
  324. return pathFilter;
  325. }
  326. /**
  327. * Flush the underlying output stream of this formatter.
  328. *
  329. * @throws IOException
  330. * the stream's own flush method threw an exception.
  331. */
  332. public void flush() throws IOException {
  333. out.flush();
  334. }
  335. /**
  336. * Release the internal ObjectReader state.
  337. *
  338. * @since 4.0
  339. */
  340. @Override
  341. public void close() {
  342. if (reader != null)
  343. reader.close();
  344. }
  345. /**
  346. * Determine the differences between two trees.
  347. *
  348. * No output is created, instead only the file paths that are different are
  349. * returned. Callers may choose to format these paths themselves, or convert
  350. * them into {@link FileHeader} instances with a complete edit list by
  351. * calling {@link #toFileHeader(DiffEntry)}.
  352. * <p>
  353. * Either side may be null to indicate that the tree has beed added or
  354. * removed. The diff will be computed against nothing.
  355. *
  356. * @param a
  357. * the old (or previous) side or null
  358. * @param b
  359. * the new (or updated) side or null
  360. * @return the paths that are different.
  361. * @throws IOException
  362. * trees cannot be read or file contents cannot be read.
  363. */
  364. public List<DiffEntry> scan(AnyObjectId a, AnyObjectId b)
  365. throws IOException {
  366. assertHaveRepository();
  367. try (RevWalk rw = new RevWalk(reader)) {
  368. RevTree aTree = a != null ? rw.parseTree(a) : null;
  369. RevTree bTree = b != null ? rw.parseTree(b) : null;
  370. return scan(aTree, bTree);
  371. }
  372. }
  373. /**
  374. * Determine the differences between two trees.
  375. *
  376. * No output is created, instead only the file paths that are different are
  377. * returned. Callers may choose to format these paths themselves, or convert
  378. * them into {@link FileHeader} instances with a complete edit list by
  379. * calling {@link #toFileHeader(DiffEntry)}.
  380. * <p>
  381. * Either side may be null to indicate that the tree has beed added or
  382. * removed. The diff will be computed against nothing.
  383. *
  384. * @param a
  385. * the old (or previous) side or null
  386. * @param b
  387. * the new (or updated) side or null
  388. * @return the paths that are different.
  389. * @throws IOException
  390. * trees cannot be read or file contents cannot be read.
  391. */
  392. public List<DiffEntry> scan(RevTree a, RevTree b) throws IOException {
  393. assertHaveRepository();
  394. AbstractTreeIterator aIterator = makeIteratorFromTreeOrNull(a);
  395. AbstractTreeIterator bIterator = makeIteratorFromTreeOrNull(b);
  396. return scan(aIterator, bIterator);
  397. }
  398. private AbstractTreeIterator makeIteratorFromTreeOrNull(RevTree tree)
  399. throws IncorrectObjectTypeException, IOException {
  400. if (tree != null) {
  401. CanonicalTreeParser parser = new CanonicalTreeParser();
  402. parser.reset(reader, tree);
  403. return parser;
  404. } else
  405. return new EmptyTreeIterator();
  406. }
  407. /**
  408. * Determine the differences between two trees.
  409. *
  410. * No output is created, instead only the file paths that are different are
  411. * returned. Callers may choose to format these paths themselves, or convert
  412. * them into {@link FileHeader} instances with a complete edit list by
  413. * calling {@link #toFileHeader(DiffEntry)}.
  414. *
  415. * @param a
  416. * the old (or previous) side.
  417. * @param b
  418. * the new (or updated) side.
  419. * @return the paths that are different.
  420. * @throws IOException
  421. * trees cannot be read or file contents cannot be read.
  422. */
  423. public List<DiffEntry> scan(AbstractTreeIterator a, AbstractTreeIterator b)
  424. throws IOException {
  425. assertHaveRepository();
  426. TreeWalk walk = new TreeWalk(reader);
  427. walk.addTree(a);
  428. walk.addTree(b);
  429. walk.setRecursive(true);
  430. TreeFilter filter = getDiffTreeFilterFor(a, b);
  431. if (pathFilter instanceof FollowFilter) {
  432. walk.setFilter(AndTreeFilter.create(
  433. PathFilter.create(((FollowFilter) pathFilter).getPath()),
  434. filter));
  435. } else {
  436. walk.setFilter(AndTreeFilter.create(pathFilter, filter));
  437. }
  438. source = new ContentSource.Pair(source(a), source(b));
  439. List<DiffEntry> files = DiffEntry.scan(walk);
  440. if (pathFilter instanceof FollowFilter && isAdd(files)) {
  441. // The file we are following was added here, find where it
  442. // came from so we can properly show the rename or copy,
  443. // then continue digging backwards.
  444. //
  445. a.reset();
  446. b.reset();
  447. walk.reset();
  448. walk.addTree(a);
  449. walk.addTree(b);
  450. walk.setFilter(filter);
  451. if (renameDetector == null)
  452. setDetectRenames(true);
  453. files = updateFollowFilter(detectRenames(DiffEntry.scan(walk)));
  454. } else if (renameDetector != null)
  455. files = detectRenames(files);
  456. return files;
  457. }
  458. private static TreeFilter getDiffTreeFilterFor(AbstractTreeIterator a,
  459. AbstractTreeIterator b) {
  460. if (a instanceof DirCacheIterator && b instanceof WorkingTreeIterator)
  461. return new IndexDiffFilter(0, 1);
  462. if (a instanceof WorkingTreeIterator && b instanceof DirCacheIterator)
  463. return new IndexDiffFilter(1, 0);
  464. TreeFilter filter = TreeFilter.ANY_DIFF;
  465. if (a instanceof WorkingTreeIterator)
  466. filter = AndTreeFilter.create(new NotIgnoredFilter(0), filter);
  467. if (b instanceof WorkingTreeIterator)
  468. filter = AndTreeFilter.create(new NotIgnoredFilter(1), filter);
  469. return filter;
  470. }
  471. private ContentSource source(AbstractTreeIterator iterator) {
  472. if (iterator instanceof WorkingTreeIterator)
  473. return ContentSource.create((WorkingTreeIterator) iterator);
  474. return ContentSource.create(reader);
  475. }
  476. private List<DiffEntry> detectRenames(List<DiffEntry> files)
  477. throws IOException {
  478. renameDetector.reset();
  479. renameDetector.addAll(files);
  480. return renameDetector.compute(reader, progressMonitor);
  481. }
  482. private boolean isAdd(List<DiffEntry> files) {
  483. String oldPath = ((FollowFilter) pathFilter).getPath();
  484. for (DiffEntry ent : files) {
  485. if (ent.getChangeType() == ADD && ent.getNewPath().equals(oldPath))
  486. return true;
  487. }
  488. return false;
  489. }
  490. private List<DiffEntry> updateFollowFilter(List<DiffEntry> files) {
  491. String oldPath = ((FollowFilter) pathFilter).getPath();
  492. for (DiffEntry ent : files) {
  493. if (isRename(ent) && ent.getNewPath().equals(oldPath)) {
  494. pathFilter = FollowFilter.create(ent.getOldPath(), diffCfg);
  495. return Collections.singletonList(ent);
  496. }
  497. }
  498. return Collections.emptyList();
  499. }
  500. private static boolean isRename(DiffEntry ent) {
  501. return ent.getChangeType() == RENAME || ent.getChangeType() == COPY;
  502. }
  503. /**
  504. * Format the differences between two trees.
  505. *
  506. * The patch is expressed as instructions to modify {@code a} to make it
  507. * {@code b}.
  508. * <p>
  509. * Either side may be null to indicate that the tree has beed added or
  510. * removed. The diff will be computed against nothing.
  511. *
  512. * @param a
  513. * the old (or previous) side or null
  514. * @param b
  515. * the new (or updated) side or null
  516. * @throws IOException
  517. * trees cannot be read, file contents cannot be read, or the
  518. * patch cannot be output.
  519. */
  520. public void format(AnyObjectId a, AnyObjectId b) throws IOException {
  521. format(scan(a, b));
  522. }
  523. /**
  524. * Format the differences between two trees.
  525. *
  526. * The patch is expressed as instructions to modify {@code a} to make it
  527. * {@code b}.
  528. *
  529. * <p>
  530. * Either side may be null to indicate that the tree has beed added or
  531. * removed. The diff will be computed against nothing.
  532. *
  533. * @param a
  534. * the old (or previous) side or null
  535. * @param b
  536. * the new (or updated) side or null
  537. * @throws IOException
  538. * trees cannot be read, file contents cannot be read, or the
  539. * patch cannot be output.
  540. */
  541. public void format(RevTree a, RevTree b) throws IOException {
  542. format(scan(a, b));
  543. }
  544. /**
  545. * Format the differences between two trees.
  546. *
  547. * The patch is expressed as instructions to modify {@code a} to make it
  548. * {@code b}.
  549. * <p>
  550. * Either side may be null to indicate that the tree has beed added or
  551. * removed. The diff will be computed against nothing.
  552. *
  553. * @param a
  554. * the old (or previous) side or null
  555. * @param b
  556. * the new (or updated) side or null
  557. * @throws IOException
  558. * trees cannot be read, file contents cannot be read, or the
  559. * patch cannot be output.
  560. */
  561. public void format(AbstractTreeIterator a, AbstractTreeIterator b)
  562. throws IOException {
  563. format(scan(a, b));
  564. }
  565. /**
  566. * Format a patch script from a list of difference entries. Requires
  567. * {@link #scan(AbstractTreeIterator, AbstractTreeIterator)} to have been
  568. * called first.
  569. *
  570. * @param entries
  571. * entries describing the affected files.
  572. * @throws IOException
  573. * a file's content cannot be read, or the output stream cannot
  574. * be written to.
  575. */
  576. public void format(List<? extends DiffEntry> entries) throws IOException {
  577. for (DiffEntry ent : entries)
  578. format(ent);
  579. }
  580. /**
  581. * Format a patch script for one file entry.
  582. *
  583. * @param ent
  584. * the entry to be formatted.
  585. * @throws IOException
  586. * a file's content cannot be read, or the output stream cannot
  587. * be written to.
  588. */
  589. public void format(DiffEntry ent) throws IOException {
  590. FormatResult res = createFormatResult(ent);
  591. format(res.header, res.a, res.b);
  592. }
  593. private static void writeGitLinkDiffText(OutputStream o, DiffEntry ent)
  594. throws IOException {
  595. if (ent.getOldMode() == GITLINK) {
  596. o.write(encodeASCII("-Subproject commit " + ent.getOldId().name() //$NON-NLS-1$
  597. + "\n")); //$NON-NLS-1$
  598. }
  599. if (ent.getNewMode() == GITLINK) {
  600. o.write(encodeASCII("+Subproject commit " + ent.getNewId().name() //$NON-NLS-1$
  601. + "\n")); //$NON-NLS-1$
  602. }
  603. }
  604. private String format(AbbreviatedObjectId id) {
  605. if (id.isComplete() && db != null) {
  606. try {
  607. id = reader.abbreviate(id.toObjectId(), abbreviationLength);
  608. } catch (IOException cannotAbbreviate) {
  609. // Ignore this. We'll report the full identity.
  610. }
  611. }
  612. return id.name();
  613. }
  614. private static String quotePath(String name) {
  615. return QuotedString.GIT_PATH.quote(name);
  616. }
  617. /**
  618. * Format a patch script, reusing a previously parsed FileHeader.
  619. * <p>
  620. * This formatter is primarily useful for editing an existing patch script
  621. * to increase or reduce the number of lines of context within the script.
  622. * All header lines are reused as-is from the supplied FileHeader.
  623. *
  624. * @param head
  625. * existing file header containing the header lines to copy.
  626. * @param a
  627. * text source for the pre-image version of the content. This
  628. * must match the content of {@link FileHeader#getOldId()}.
  629. * @param b
  630. * text source for the post-image version of the content. This
  631. * must match the content of {@link FileHeader#getNewId()}.
  632. * @throws IOException
  633. * writing to the supplied stream failed.
  634. */
  635. public void format(final FileHeader head, final RawText a, final RawText b)
  636. throws IOException {
  637. // Reuse the existing FileHeader as-is by blindly copying its
  638. // header lines, but avoiding its hunks. Instead we recreate
  639. // the hunks from the text instances we have been supplied.
  640. //
  641. final int start = head.getStartOffset();
  642. int end = head.getEndOffset();
  643. if (!head.getHunks().isEmpty())
  644. end = head.getHunks().get(0).getStartOffset();
  645. out.write(head.getBuffer(), start, end - start);
  646. if (head.getPatchType() == PatchType.UNIFIED)
  647. format(head.toEditList(), a, b);
  648. }
  649. /**
  650. * Formats a list of edits in unified diff format
  651. *
  652. * @param edits
  653. * some differences which have been calculated between A and B
  654. * @param a
  655. * the text A which was compared
  656. * @param b
  657. * the text B which was compared
  658. * @throws IOException
  659. */
  660. public void format(final EditList edits, final RawText a, final RawText b)
  661. throws IOException {
  662. for (int curIdx = 0; curIdx < edits.size();) {
  663. Edit curEdit = edits.get(curIdx);
  664. final int endIdx = findCombinedEnd(edits, curIdx);
  665. final Edit endEdit = edits.get(endIdx);
  666. int aCur = (int) Math.max(0, (long) curEdit.getBeginA() - context);
  667. int bCur = (int) Math.max(0, (long) curEdit.getBeginB() - context);
  668. final int aEnd = (int) Math.min(a.size(), (long) endEdit.getEndA() + context);
  669. final int bEnd = (int) Math.min(b.size(), (long) endEdit.getEndB() + context);
  670. writeHunkHeader(aCur, aEnd, bCur, bEnd);
  671. while (aCur < aEnd || bCur < bEnd) {
  672. if (aCur < curEdit.getBeginA() || endIdx + 1 < curIdx) {
  673. writeContextLine(a, aCur);
  674. if (isEndOfLineMissing(a, aCur))
  675. out.write(noNewLine);
  676. aCur++;
  677. bCur++;
  678. } else if (aCur < curEdit.getEndA()) {
  679. writeRemovedLine(a, aCur);
  680. if (isEndOfLineMissing(a, aCur))
  681. out.write(noNewLine);
  682. aCur++;
  683. } else if (bCur < curEdit.getEndB()) {
  684. writeAddedLine(b, bCur);
  685. if (isEndOfLineMissing(b, bCur))
  686. out.write(noNewLine);
  687. bCur++;
  688. }
  689. if (end(curEdit, aCur, bCur) && ++curIdx < edits.size())
  690. curEdit = edits.get(curIdx);
  691. }
  692. }
  693. }
  694. /**
  695. * Output a line of context (unmodified line).
  696. *
  697. * @param text
  698. * RawText for accessing raw data
  699. * @param line
  700. * the line number within text
  701. * @throws IOException
  702. */
  703. protected void writeContextLine(final RawText text, final int line)
  704. throws IOException {
  705. writeLine(' ', text, line);
  706. }
  707. private static boolean isEndOfLineMissing(final RawText text, final int line) {
  708. return line + 1 == text.size() && text.isMissingNewlineAtEnd();
  709. }
  710. /**
  711. * Output an added line.
  712. *
  713. * @param text
  714. * RawText for accessing raw data
  715. * @param line
  716. * the line number within text
  717. * @throws IOException
  718. */
  719. protected void writeAddedLine(final RawText text, final int line)
  720. throws IOException {
  721. writeLine('+', text, line);
  722. }
  723. /**
  724. * Output a removed line
  725. *
  726. * @param text
  727. * RawText for accessing raw data
  728. * @param line
  729. * the line number within text
  730. * @throws IOException
  731. */
  732. protected void writeRemovedLine(final RawText text, final int line)
  733. throws IOException {
  734. writeLine('-', text, line);
  735. }
  736. /**
  737. * Output a hunk header
  738. *
  739. * @param aStartLine
  740. * within first source
  741. * @param aEndLine
  742. * within first source
  743. * @param bStartLine
  744. * within second source
  745. * @param bEndLine
  746. * within second source
  747. * @throws IOException
  748. */
  749. protected void writeHunkHeader(int aStartLine, int aEndLine,
  750. int bStartLine, int bEndLine) throws IOException {
  751. out.write('@');
  752. out.write('@');
  753. writeRange('-', aStartLine + 1, aEndLine - aStartLine);
  754. writeRange('+', bStartLine + 1, bEndLine - bStartLine);
  755. out.write(' ');
  756. out.write('@');
  757. out.write('@');
  758. out.write('\n');
  759. }
  760. private void writeRange(final char prefix, final int begin, final int cnt)
  761. throws IOException {
  762. out.write(' ');
  763. out.write(prefix);
  764. switch (cnt) {
  765. case 0:
  766. // If the range is empty, its beginning number must be the
  767. // line just before the range, or 0 if the range is at the
  768. // start of the file stream. Here, begin is always 1 based,
  769. // so an empty file would produce "0,0".
  770. //
  771. out.write(encodeASCII(begin - 1));
  772. out.write(',');
  773. out.write('0');
  774. break;
  775. case 1:
  776. // If the range is exactly one line, produce only the number.
  777. //
  778. out.write(encodeASCII(begin));
  779. break;
  780. default:
  781. out.write(encodeASCII(begin));
  782. out.write(',');
  783. out.write(encodeASCII(cnt));
  784. break;
  785. }
  786. }
  787. /**
  788. * Write a standard patch script line.
  789. *
  790. * @param prefix
  791. * prefix before the line, typically '-', '+', ' '.
  792. * @param text
  793. * the text object to obtain the line from.
  794. * @param cur
  795. * line number to output.
  796. * @throws IOException
  797. * the stream threw an exception while writing to it.
  798. */
  799. protected void writeLine(final char prefix, final RawText text,
  800. final int cur) throws IOException {
  801. out.write(prefix);
  802. text.writeLine(out, cur);
  803. out.write('\n');
  804. }
  805. /**
  806. * Creates a {@link FileHeader} representing the given {@link DiffEntry}
  807. * <p>
  808. * This method does not use the OutputStream associated with this
  809. * DiffFormatter instance. It is therefore safe to instantiate this
  810. * DiffFormatter instance with a {@link DisabledOutputStream} if this method
  811. * is the only one that will be used.
  812. *
  813. * @param ent
  814. * the DiffEntry to create the FileHeader for
  815. * @return a FileHeader representing the DiffEntry. The FileHeader's buffer
  816. * will contain only the header of the diff output. It will also
  817. * contain one {@link HunkHeader}.
  818. * @throws IOException
  819. * the stream threw an exception while writing to it, or one of
  820. * the blobs referenced by the DiffEntry could not be read.
  821. * @throws CorruptObjectException
  822. * one of the blobs referenced by the DiffEntry is corrupt.
  823. * @throws MissingObjectException
  824. * one of the blobs referenced by the DiffEntry is missing.
  825. */
  826. public FileHeader toFileHeader(DiffEntry ent) throws IOException,
  827. CorruptObjectException, MissingObjectException {
  828. return createFormatResult(ent).header;
  829. }
  830. private static class FormatResult {
  831. FileHeader header;
  832. RawText a;
  833. RawText b;
  834. }
  835. private FormatResult createFormatResult(DiffEntry ent) throws IOException,
  836. CorruptObjectException, MissingObjectException {
  837. final FormatResult res = new FormatResult();
  838. ByteArrayOutputStream buf = new ByteArrayOutputStream();
  839. final EditList editList;
  840. final FileHeader.PatchType type;
  841. formatHeader(buf, ent);
  842. if (ent.getOldMode() == GITLINK || ent.getNewMode() == GITLINK) {
  843. formatOldNewPaths(buf, ent);
  844. writeGitLinkDiffText(buf, ent);
  845. editList = new EditList();
  846. type = PatchType.UNIFIED;
  847. } else if (ent.getOldId() == null || ent.getNewId() == null) {
  848. // Content not changed (e.g. only mode, pure rename)
  849. editList = new EditList();
  850. type = PatchType.UNIFIED;
  851. } else {
  852. assertHaveRepository();
  853. byte[] aRaw = open(OLD, ent);
  854. byte[] bRaw = open(NEW, ent);
  855. if (aRaw == BINARY || bRaw == BINARY //
  856. || RawText.isBinary(aRaw) || RawText.isBinary(bRaw)) {
  857. formatOldNewPaths(buf, ent);
  858. buf.write(encodeASCII("Binary files differ\n")); //$NON-NLS-1$
  859. editList = new EditList();
  860. type = PatchType.BINARY;
  861. } else {
  862. res.a = new RawText(aRaw);
  863. res.b = new RawText(bRaw);
  864. editList = diff(res.a, res.b);
  865. type = PatchType.UNIFIED;
  866. switch (ent.getChangeType()) {
  867. case RENAME:
  868. case COPY:
  869. if (!editList.isEmpty())
  870. formatOldNewPaths(buf, ent);
  871. break;
  872. default:
  873. formatOldNewPaths(buf, ent);
  874. break;
  875. }
  876. }
  877. }
  878. res.header = new FileHeader(buf.toByteArray(), editList, type);
  879. return res;
  880. }
  881. private EditList diff(RawText a, RawText b) {
  882. return diffAlgorithm.diff(comparator, a, b);
  883. }
  884. private void assertHaveRepository() {
  885. if (db == null)
  886. throw new IllegalStateException(JGitText.get().repositoryIsRequired);
  887. }
  888. private byte[] open(DiffEntry.Side side, DiffEntry entry)
  889. throws IOException {
  890. if (entry.getMode(side) == FileMode.MISSING)
  891. return EMPTY;
  892. if (entry.getMode(side).getObjectType() != Constants.OBJ_BLOB)
  893. return EMPTY;
  894. AbbreviatedObjectId id = entry.getId(side);
  895. if (!id.isComplete()) {
  896. Collection<ObjectId> ids = reader.resolve(id);
  897. if (ids.size() == 1) {
  898. id = AbbreviatedObjectId.fromObjectId(ids.iterator().next());
  899. switch (side) {
  900. case OLD:
  901. entry.oldId = id;
  902. break;
  903. case NEW:
  904. entry.newId = id;
  905. break;
  906. }
  907. } else if (ids.size() == 0)
  908. throw new MissingObjectException(id, Constants.OBJ_BLOB);
  909. else
  910. throw new AmbiguousObjectException(id, ids);
  911. }
  912. try {
  913. ObjectLoader ldr = source.open(side, entry);
  914. return ldr.getBytes(binaryFileThreshold);
  915. } catch (LargeObjectException.ExceedsLimit overLimit) {
  916. return BINARY;
  917. } catch (LargeObjectException.ExceedsByteArrayLimit overLimit) {
  918. return BINARY;
  919. } catch (LargeObjectException.OutOfMemory tooBig) {
  920. return BINARY;
  921. } catch (LargeObjectException tooBig) {
  922. tooBig.setObjectId(id.toObjectId());
  923. throw tooBig;
  924. }
  925. }
  926. /**
  927. * Output the first header line
  928. *
  929. * @param o
  930. * The stream the formatter will write the first header line to
  931. * @param type
  932. * The {@link ChangeType}
  933. * @param oldPath
  934. * old path to the file
  935. * @param newPath
  936. * new path to the file
  937. * @throws IOException
  938. * the stream threw an exception while writing to it.
  939. */
  940. protected void formatGitDiffFirstHeaderLine(ByteArrayOutputStream o,
  941. final ChangeType type, final String oldPath, final String newPath)
  942. throws IOException {
  943. o.write(encodeASCII("diff --git ")); //$NON-NLS-1$
  944. o.write(encode(quotePath(oldPrefix + (type == ADD ? newPath : oldPath))));
  945. o.write(' ');
  946. o.write(encode(quotePath(newPrefix
  947. + (type == DELETE ? oldPath : newPath))));
  948. o.write('\n');
  949. }
  950. private void formatHeader(ByteArrayOutputStream o, DiffEntry ent)
  951. throws IOException {
  952. final ChangeType type = ent.getChangeType();
  953. final String oldp = ent.getOldPath();
  954. final String newp = ent.getNewPath();
  955. final FileMode oldMode = ent.getOldMode();
  956. final FileMode newMode = ent.getNewMode();
  957. formatGitDiffFirstHeaderLine(o, type, oldp, newp);
  958. if ((type == MODIFY || type == COPY || type == RENAME)
  959. && !oldMode.equals(newMode)) {
  960. o.write(encodeASCII("old mode ")); //$NON-NLS-1$
  961. oldMode.copyTo(o);
  962. o.write('\n');
  963. o.write(encodeASCII("new mode ")); //$NON-NLS-1$
  964. newMode.copyTo(o);
  965. o.write('\n');
  966. }
  967. switch (type) {
  968. case ADD:
  969. o.write(encodeASCII("new file mode ")); //$NON-NLS-1$
  970. newMode.copyTo(o);
  971. o.write('\n');
  972. break;
  973. case DELETE:
  974. o.write(encodeASCII("deleted file mode ")); //$NON-NLS-1$
  975. oldMode.copyTo(o);
  976. o.write('\n');
  977. break;
  978. case RENAME:
  979. o.write(encodeASCII("similarity index " + ent.getScore() + "%")); //$NON-NLS-1$ //$NON-NLS-2$
  980. o.write('\n');
  981. o.write(encode("rename from " + quotePath(oldp))); //$NON-NLS-1$
  982. o.write('\n');
  983. o.write(encode("rename to " + quotePath(newp))); //$NON-NLS-1$
  984. o.write('\n');
  985. break;
  986. case COPY:
  987. o.write(encodeASCII("similarity index " + ent.getScore() + "%")); //$NON-NLS-1$ //$NON-NLS-2$
  988. o.write('\n');
  989. o.write(encode("copy from " + quotePath(oldp))); //$NON-NLS-1$
  990. o.write('\n');
  991. o.write(encode("copy to " + quotePath(newp))); //$NON-NLS-1$
  992. o.write('\n');
  993. break;
  994. case MODIFY:
  995. if (0 < ent.getScore()) {
  996. o.write(encodeASCII("dissimilarity index " //$NON-NLS-1$
  997. + (100 - ent.getScore()) + "%")); //$NON-NLS-1$
  998. o.write('\n');
  999. }
  1000. break;
  1001. }
  1002. if (ent.getOldId() != null && !ent.getOldId().equals(ent.getNewId())) {
  1003. formatIndexLine(o, ent);
  1004. }
  1005. }
  1006. /**
  1007. * @param o
  1008. * the stream the formatter will write line data to
  1009. * @param ent
  1010. * the DiffEntry to create the FileHeader for
  1011. * @throws IOException
  1012. * writing to the supplied stream failed.
  1013. */
  1014. protected void formatIndexLine(OutputStream o, DiffEntry ent)
  1015. throws IOException {
  1016. o.write(encodeASCII("index " // //$NON-NLS-1$
  1017. + format(ent.getOldId()) //
  1018. + ".." // //$NON-NLS-1$
  1019. + format(ent.getNewId())));
  1020. if (ent.getOldMode().equals(ent.getNewMode())) {
  1021. o.write(' ');
  1022. ent.getNewMode().copyTo(o);
  1023. }
  1024. o.write('\n');
  1025. }
  1026. private void formatOldNewPaths(ByteArrayOutputStream o, DiffEntry ent)
  1027. throws IOException {
  1028. if (ent.oldId.equals(ent.newId))
  1029. return;
  1030. final String oldp;
  1031. final String newp;
  1032. switch (ent.getChangeType()) {
  1033. case ADD:
  1034. oldp = DiffEntry.DEV_NULL;
  1035. newp = quotePath(newPrefix + ent.getNewPath());
  1036. break;
  1037. case DELETE:
  1038. oldp = quotePath(oldPrefix + ent.getOldPath());
  1039. newp = DiffEntry.DEV_NULL;
  1040. break;
  1041. default:
  1042. oldp = quotePath(oldPrefix + ent.getOldPath());
  1043. newp = quotePath(newPrefix + ent.getNewPath());
  1044. break;
  1045. }
  1046. o.write(encode("--- " + oldp + "\n")); //$NON-NLS-1$ //$NON-NLS-2$
  1047. o.write(encode("+++ " + newp + "\n")); //$NON-NLS-1$ //$NON-NLS-2$
  1048. }
  1049. private int findCombinedEnd(final List<Edit> edits, final int i) {
  1050. int end = i + 1;
  1051. while (end < edits.size()
  1052. && (combineA(edits, end) || combineB(edits, end)))
  1053. end++;
  1054. return end - 1;
  1055. }
  1056. private boolean combineA(final List<Edit> e, final int i) {
  1057. return e.get(i).getBeginA() - e.get(i - 1).getEndA() <= 2 * context;
  1058. }
  1059. private boolean combineB(final List<Edit> e, final int i) {
  1060. return e.get(i).getBeginB() - e.get(i - 1).getEndB() <= 2 * context;
  1061. }
  1062. private static boolean end(final Edit edit, final int a, final int b) {
  1063. return edit.getEndA() <= a && edit.getEndB() <= b;
  1064. }
  1065. }