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

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