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.

DiffAlgorithm.java 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.diff;
  44. /**
  45. * Compares two {@link Sequence}s to create an {@link EditList} of changes.
  46. * <p>
  47. * An algorithm's {@code diff} method must be callable from concurrent threads
  48. * without data collisions. This permits some algorithms to use a singleton
  49. * pattern, with concurrent invocations using the same singleton. Other
  50. * algorithms may support parameterization, in which case the caller can create
  51. * a unique instance per thread.
  52. */
  53. public abstract class DiffAlgorithm {
  54. /**
  55. * Supported diff algorithm
  56. */
  57. public enum SupportedAlgorithm {
  58. /**
  59. * Myers diff algorithm
  60. */
  61. MYERS,
  62. /**
  63. * Histogram diff algorithm
  64. */
  65. HISTOGRAM
  66. }
  67. /**
  68. * @param alg
  69. * the diff algorithm for which an implementation should be
  70. * returned
  71. * @return an implementation of the specified diff algorithm
  72. */
  73. public static DiffAlgorithm getAlgorithm(SupportedAlgorithm alg) {
  74. switch (alg) {
  75. case MYERS:
  76. return MyersDiff.INSTANCE;
  77. case HISTOGRAM:
  78. return new HistogramDiff();
  79. default:
  80. throw new IllegalArgumentException();
  81. }
  82. }
  83. /**
  84. * Compare two sequences and identify a list of edits between them.
  85. *
  86. * @param <S>
  87. * type of sequence being compared.
  88. * @param cmp
  89. * the comparator supplying the element equivalence function.
  90. * @param a
  91. * the first (also known as old or pre-image) sequence. Edits
  92. * returned by this algorithm will reference indexes using the
  93. * 'A' side: {@link Edit#getBeginA()}, {@link Edit#getEndA()}.
  94. * @param b
  95. * the second (also known as new or post-image) sequence. Edits
  96. * returned by this algorithm will reference indexes using the
  97. * 'B' side: {@link Edit#getBeginB()}, {@link Edit#getEndB()}.
  98. * @return a modifiable edit list comparing the two sequences. If empty, the
  99. * sequences are identical according to {@code cmp}'s rules. The
  100. * result list is never null.
  101. */
  102. public <S extends Sequence> EditList diff(
  103. SequenceComparator<? super S> cmp, S a, S b) {
  104. Edit region = cmp.reduceCommonStartEnd(a, b, coverEdit(a, b));
  105. switch (region.getType()) {
  106. case INSERT:
  107. case DELETE:
  108. return EditList.singleton(region);
  109. case REPLACE: {
  110. if (region.getLengthA() == 1 && region.getLengthB() == 1)
  111. return EditList.singleton(region);
  112. SubsequenceComparator<S> cs = new SubsequenceComparator<>(cmp);
  113. Subsequence<S> as = Subsequence.a(a, region);
  114. Subsequence<S> bs = Subsequence.b(b, region);
  115. EditList e = Subsequence.toBase(diffNonCommon(cs, as, bs), as, bs);
  116. return normalize(cmp, e, a, b);
  117. }
  118. case EMPTY:
  119. return new EditList(0);
  120. default:
  121. throw new IllegalStateException();
  122. }
  123. }
  124. private static <S extends Sequence> Edit coverEdit(S a, S b) {
  125. return new Edit(0, a.size(), 0, b.size());
  126. }
  127. /**
  128. * Reorganize an {@link EditList} for better diff consistency.
  129. * <p>
  130. * {@code DiffAlgorithms} may return {@link Edit.Type#INSERT} or
  131. * {@link Edit.Type#DELETE} edits that can be "shifted". For
  132. * example, the deleted section
  133. * <pre>
  134. * -a
  135. * -b
  136. * -c
  137. * a
  138. * b
  139. * c
  140. * </pre>
  141. * can be shifted down by 1, 2 or 3 locations.
  142. * <p>
  143. * To avoid later merge issues, we shift such edits to a
  144. * consistent location. {@code normalize} uses a simple strategy of
  145. * shifting such edits to their latest possible location.
  146. * <p>
  147. * This strategy may not always produce an aesthetically pleasing
  148. * diff. For instance, it works well with
  149. * <pre>
  150. * function1 {
  151. * ...
  152. * }
  153. *
  154. * +function2 {
  155. * + ...
  156. * +}
  157. * +
  158. * function3 {
  159. * ...
  160. * }
  161. * </pre>
  162. * but less so for
  163. * <pre>
  164. * #
  165. * # comment1
  166. * #
  167. * function1() {
  168. * }
  169. *
  170. * #
  171. * +# comment3
  172. * +#
  173. * +function3() {
  174. * +}
  175. * +
  176. * +#
  177. * # comment2
  178. * #
  179. * function2() {
  180. * }
  181. * </pre>
  182. * <a href="https://github.com/mhagger/diff-slider-tools">More
  183. * sophisticated strategies</a> are possible, say by calculating a
  184. * suitable "aesthetic cost" for each possible position and using
  185. * the lowest cost, but {@code normalize} just shifts edits
  186. * to the end as much as possible.
  187. *
  188. * @param <S>
  189. * type of sequence being compared.
  190. * @param cmp
  191. * the comparator supplying the element equivalence function.
  192. * @param e
  193. * a modifiable edit list comparing the provided sequences.
  194. * @param a
  195. * the first (also known as old or pre-image) sequence.
  196. * @param b
  197. * the second (also known as new or post-image) sequence.
  198. * @return a modifiable edit list with edit regions shifted to their
  199. * latest possible location. The result list is never null.
  200. * @since 4.7
  201. */
  202. private static <S extends Sequence> EditList normalize(
  203. SequenceComparator<? super S> cmp, EditList e, S a, S b) {
  204. Edit prev = null;
  205. for (int i = e.size() - 1; i >= 0; i--) {
  206. Edit cur = e.get(i);
  207. Edit.Type curType = cur.getType();
  208. int maxA = (prev == null) ? a.size() : prev.beginA;
  209. int maxB = (prev == null) ? b.size() : prev.beginB;
  210. if (curType == Edit.Type.INSERT) {
  211. while (cur.endA < maxA && cur.endB < maxB
  212. && cmp.equals(b, cur.beginB, b, cur.endB)) {
  213. cur.shift(1);
  214. }
  215. } else if (curType == Edit.Type.DELETE) {
  216. while (cur.endA < maxA && cur.endB < maxB
  217. && cmp.equals(a, cur.beginA, a, cur.endA)) {
  218. cur.shift(1);
  219. }
  220. }
  221. prev = cur;
  222. }
  223. return e;
  224. }
  225. /**
  226. * Compare two sequences and identify a list of edits between them.
  227. *
  228. * This method should be invoked only after the two sequences have been
  229. * proven to have no common starting or ending elements. The expected
  230. * elimination of common starting and ending elements is automatically
  231. * performed by the {@link #diff(SequenceComparator, Sequence, Sequence)}
  232. * method, which invokes this method using {@link Subsequence}s.
  233. *
  234. * @param <S>
  235. * type of sequence being compared.
  236. * @param cmp
  237. * the comparator supplying the element equivalence function.
  238. * @param a
  239. * the first (also known as old or pre-image) sequence. Edits
  240. * returned by this algorithm will reference indexes using the
  241. * 'A' side: {@link Edit#getBeginA()}, {@link Edit#getEndA()}.
  242. * @param b
  243. * the second (also known as new or post-image) sequence. Edits
  244. * returned by this algorithm will reference indexes using the
  245. * 'B' side: {@link Edit#getBeginB()}, {@link Edit#getEndB()}.
  246. * @return a modifiable edit list comparing the two sequences.
  247. */
  248. public abstract <S extends Sequence> EditList diffNonCommon(
  249. SequenceComparator<? super S> cmp, S a, S b);
  250. }