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 35KB

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