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.

RenameDetector.java 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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. import static org.eclipse.jgit.diff.DiffEntry.Side.NEW;
  45. import static org.eclipse.jgit.diff.DiffEntry.Side.OLD;
  46. import java.io.IOException;
  47. import java.util.ArrayList;
  48. import java.util.Arrays;
  49. import java.util.Collection;
  50. import java.util.Collections;
  51. import java.util.Comparator;
  52. import java.util.HashMap;
  53. import java.util.List;
  54. import org.eclipse.jgit.diff.DiffEntry.ChangeType;
  55. import org.eclipse.jgit.diff.SimilarityIndex.TableFullException;
  56. import org.eclipse.jgit.internal.JGitText;
  57. import org.eclipse.jgit.lib.AbbreviatedObjectId;
  58. import org.eclipse.jgit.lib.FileMode;
  59. import org.eclipse.jgit.lib.NullProgressMonitor;
  60. import org.eclipse.jgit.lib.ObjectReader;
  61. import org.eclipse.jgit.lib.ProgressMonitor;
  62. import org.eclipse.jgit.lib.Repository;
  63. /** Detect and resolve object renames. */
  64. public class RenameDetector {
  65. private static final int EXACT_RENAME_SCORE = 100;
  66. private static final Comparator<DiffEntry> DIFF_COMPARATOR = new Comparator<DiffEntry>() {
  67. @Override
  68. public int compare(DiffEntry a, DiffEntry b) {
  69. int cmp = nameOf(a).compareTo(nameOf(b));
  70. if (cmp == 0)
  71. cmp = sortOf(a.getChangeType()) - sortOf(b.getChangeType());
  72. return cmp;
  73. }
  74. private String nameOf(DiffEntry ent) {
  75. // Sort by the new name, unless the change is a delete. On
  76. // deletes the new name is /dev/null, so we sort instead by
  77. // the old name.
  78. //
  79. if (ent.changeType == ChangeType.DELETE)
  80. return ent.oldPath;
  81. return ent.newPath;
  82. }
  83. private int sortOf(ChangeType changeType) {
  84. // Sort deletes before adds so that a major type change for
  85. // a file path (such as symlink to regular file) will first
  86. // remove the path, then add it back with the new type.
  87. //
  88. switch (changeType) {
  89. case DELETE:
  90. return 1;
  91. case ADD:
  92. return 2;
  93. default:
  94. return 10;
  95. }
  96. }
  97. };
  98. private List<DiffEntry> entries;
  99. private List<DiffEntry> deleted;
  100. private List<DiffEntry> added;
  101. private boolean done;
  102. private final ObjectReader objectReader;
  103. /** Similarity score required to pair an add/delete as a rename. */
  104. private int renameScore = 60;
  105. /**
  106. * Similarity score required to keep modified file pairs together. Any
  107. * modified file pairs with a similarity score below this will be broken
  108. * apart.
  109. */
  110. private int breakScore = -1;
  111. /** Limit in the number of files to consider for renames. */
  112. private int renameLimit;
  113. /** Set if the number of adds or deletes was over the limit. */
  114. private boolean overRenameLimit;
  115. /**
  116. * Create a new rename detector for the given repository
  117. *
  118. * @param repo
  119. * the repository to use for rename detection
  120. */
  121. public RenameDetector(Repository repo) {
  122. this(repo.newObjectReader(), repo.getConfig().get(DiffConfig.KEY));
  123. }
  124. /**
  125. * Create a new rename detector with a specified reader and diff config.
  126. *
  127. * @param reader
  128. * reader to obtain objects from the repository with.
  129. * @param cfg
  130. * diff config specifying rename detection options.
  131. * @since 3.0
  132. */
  133. public RenameDetector(ObjectReader reader, DiffConfig cfg) {
  134. objectReader = reader.newReader();
  135. renameLimit = cfg.getRenameLimit();
  136. reset();
  137. }
  138. /**
  139. * @return minimum score required to pair an add/delete as a rename. The
  140. * score ranges are within the bounds of (0, 100).
  141. */
  142. public int getRenameScore() {
  143. return renameScore;
  144. }
  145. /**
  146. * Set the minimum score required to pair an add/delete as a rename.
  147. * <p>
  148. * When comparing two files together their score must be greater than or
  149. * equal to the rename score for them to be considered a rename match. The
  150. * score is computed based on content similarity, so a score of 60 implies
  151. * that approximately 60% of the bytes in the files are identical.
  152. *
  153. * @param score
  154. * new rename score, must be within [0, 100].
  155. * @throws IllegalArgumentException
  156. * the score was not within [0, 100].
  157. */
  158. public void setRenameScore(int score) {
  159. if (score < 0 || score > 100)
  160. throw new IllegalArgumentException(
  161. JGitText.get().similarityScoreMustBeWithinBounds);
  162. renameScore = score;
  163. }
  164. /**
  165. * @return the similarity score required to keep modified file pairs
  166. * together. Any modify pairs that score below this will be broken
  167. * apart into separate add/deletes. Values less than or equal to
  168. * zero indicate that no modifies will be broken apart. Values over
  169. * 100 cause all modify pairs to be broken.
  170. */
  171. public int getBreakScore() {
  172. return breakScore;
  173. }
  174. /**
  175. * @param breakScore
  176. * the similarity score required to keep modified file pairs
  177. * together. Any modify pairs that score below this will be
  178. * broken apart into separate add/deletes. Values less than or
  179. * equal to zero indicate that no modifies will be broken apart.
  180. * Values over 100 cause all modify pairs to be broken.
  181. */
  182. public void setBreakScore(int breakScore) {
  183. this.breakScore = breakScore;
  184. }
  185. /** @return limit on number of paths to perform inexact rename detection. */
  186. public int getRenameLimit() {
  187. return renameLimit;
  188. }
  189. /**
  190. * Set the limit on the number of files to perform inexact rename detection.
  191. * <p>
  192. * The rename detector has to build a square matrix of the rename limit on
  193. * each side, then perform that many file compares to determine similarity.
  194. * If 1000 files are added, and 1000 files are deleted, a 1000*1000 matrix
  195. * must be allocated, and 1,000,000 file compares may need to be performed.
  196. *
  197. * @param limit
  198. * new file limit. 0 means no limit; a negative number means no
  199. * inexact rename detection will be performed, only exact rename
  200. * detection.
  201. */
  202. public void setRenameLimit(int limit) {
  203. renameLimit = limit;
  204. }
  205. /**
  206. * Check if the detector is over the rename limit.
  207. * <p>
  208. * This method can be invoked either before or after {@code getEntries} has
  209. * been used to perform rename detection.
  210. *
  211. * @return true if the detector has more file additions or removals than the
  212. * rename limit is currently set to. In such configurations the
  213. * detector will skip expensive computation.
  214. */
  215. public boolean isOverRenameLimit() {
  216. if (done)
  217. return overRenameLimit;
  218. int cnt = Math.max(added.size(), deleted.size());
  219. return getRenameLimit() != 0 && getRenameLimit() < cnt;
  220. }
  221. /**
  222. * Add entries to be considered for rename detection.
  223. *
  224. * @param entriesToAdd
  225. * one or more entries to add.
  226. * @throws IllegalStateException
  227. * if {@code getEntries} was already invoked.
  228. */
  229. public void addAll(Collection<DiffEntry> entriesToAdd) {
  230. if (done)
  231. throw new IllegalStateException(JGitText.get().renamesAlreadyFound);
  232. for (DiffEntry entry : entriesToAdd) {
  233. switch (entry.getChangeType()) {
  234. case ADD:
  235. added.add(entry);
  236. break;
  237. case DELETE:
  238. deleted.add(entry);
  239. break;
  240. case MODIFY:
  241. if (sameType(entry.getOldMode(), entry.getNewMode())) {
  242. entries.add(entry);
  243. } else {
  244. List<DiffEntry> tmp = DiffEntry.breakModify(entry);
  245. deleted.add(tmp.get(0));
  246. added.add(tmp.get(1));
  247. }
  248. break;
  249. case COPY:
  250. case RENAME:
  251. default:
  252. entries.add(entry);
  253. }
  254. }
  255. }
  256. /**
  257. * Add an entry to be considered for rename detection.
  258. *
  259. * @param entry
  260. * to add.
  261. * @throws IllegalStateException
  262. * if {@code getEntries} was already invoked.
  263. */
  264. public void add(DiffEntry entry) {
  265. addAll(Collections.singletonList(entry));
  266. }
  267. /**
  268. * Detect renames in the current file set.
  269. * <p>
  270. * This convenience function runs without a progress monitor.
  271. *
  272. * @return an unmodifiable list of {@link DiffEntry}s representing all files
  273. * that have been changed.
  274. * @throws IOException
  275. * file contents cannot be read from the repository.
  276. */
  277. public List<DiffEntry> compute() throws IOException {
  278. return compute(NullProgressMonitor.INSTANCE);
  279. }
  280. /**
  281. * Detect renames in the current file set.
  282. *
  283. * @param pm
  284. * report progress during the detection phases.
  285. * @return an unmodifiable list of {@link DiffEntry}s representing all files
  286. * that have been changed.
  287. * @throws IOException
  288. * file contents cannot be read from the repository.
  289. */
  290. public List<DiffEntry> compute(ProgressMonitor pm) throws IOException {
  291. if (!done) {
  292. try {
  293. return compute(objectReader, pm);
  294. } finally {
  295. objectReader.close();
  296. }
  297. }
  298. return Collections.unmodifiableList(entries);
  299. }
  300. /**
  301. * Detect renames in the current file set.
  302. *
  303. * @param reader
  304. * reader to obtain objects from the repository with.
  305. * @param pm
  306. * report progress during the detection phases.
  307. * @return an unmodifiable list of {@link DiffEntry}s representing all files
  308. * that have been changed.
  309. * @throws IOException
  310. * file contents cannot be read from the repository.
  311. */
  312. public List<DiffEntry> compute(ObjectReader reader, ProgressMonitor pm)
  313. throws IOException {
  314. final ContentSource cs = ContentSource.create(reader);
  315. return compute(new ContentSource.Pair(cs, cs), pm);
  316. }
  317. /**
  318. * Detect renames in the current file set.
  319. *
  320. * @param reader
  321. * reader to obtain objects from the repository with.
  322. * @param pm
  323. * report progress during the detection phases.
  324. * @return an unmodifiable list of {@link DiffEntry}s representing all files
  325. * that have been changed.
  326. * @throws IOException
  327. * file contents cannot be read from the repository.
  328. */
  329. public List<DiffEntry> compute(ContentSource.Pair reader, ProgressMonitor pm)
  330. throws IOException {
  331. if (!done) {
  332. done = true;
  333. if (pm == null)
  334. pm = NullProgressMonitor.INSTANCE;
  335. if (0 < breakScore)
  336. breakModifies(reader, pm);
  337. if (!added.isEmpty() && !deleted.isEmpty())
  338. findExactRenames(pm);
  339. if (!added.isEmpty() && !deleted.isEmpty())
  340. findContentRenames(reader, pm);
  341. if (0 < breakScore && !added.isEmpty() && !deleted.isEmpty())
  342. rejoinModifies(pm);
  343. entries.addAll(added);
  344. added = null;
  345. entries.addAll(deleted);
  346. deleted = null;
  347. Collections.sort(entries, DIFF_COMPARATOR);
  348. }
  349. return Collections.unmodifiableList(entries);
  350. }
  351. /** Reset this rename detector for another rename detection pass. */
  352. public void reset() {
  353. entries = new ArrayList<>();
  354. deleted = new ArrayList<>();
  355. added = new ArrayList<>();
  356. done = false;
  357. }
  358. private void breakModifies(ContentSource.Pair reader, ProgressMonitor pm)
  359. throws IOException {
  360. ArrayList<DiffEntry> newEntries = new ArrayList<>(entries.size());
  361. pm.beginTask(JGitText.get().renamesBreakingModifies, entries.size());
  362. for (int i = 0; i < entries.size(); i++) {
  363. DiffEntry e = entries.get(i);
  364. if (e.getChangeType() == ChangeType.MODIFY) {
  365. int score = calculateModifyScore(reader, e);
  366. if (score < breakScore) {
  367. List<DiffEntry> tmp = DiffEntry.breakModify(e);
  368. DiffEntry del = tmp.get(0);
  369. del.score = score;
  370. deleted.add(del);
  371. added.add(tmp.get(1));
  372. } else {
  373. newEntries.add(e);
  374. }
  375. } else {
  376. newEntries.add(e);
  377. }
  378. pm.update(1);
  379. }
  380. entries = newEntries;
  381. }
  382. private void rejoinModifies(ProgressMonitor pm) {
  383. HashMap<String, DiffEntry> nameMap = new HashMap<>();
  384. ArrayList<DiffEntry> newAdded = new ArrayList<>(added.size());
  385. pm.beginTask(JGitText.get().renamesRejoiningModifies, added.size()
  386. + deleted.size());
  387. for (DiffEntry src : deleted) {
  388. nameMap.put(src.oldPath, src);
  389. pm.update(1);
  390. }
  391. for (DiffEntry dst : added) {
  392. DiffEntry src = nameMap.remove(dst.newPath);
  393. if (src != null) {
  394. if (sameType(src.oldMode, dst.newMode)) {
  395. entries.add(DiffEntry.pair(ChangeType.MODIFY, src, dst,
  396. src.score));
  397. } else {
  398. nameMap.put(src.oldPath, src);
  399. newAdded.add(dst);
  400. }
  401. } else {
  402. newAdded.add(dst);
  403. }
  404. pm.update(1);
  405. }
  406. added = newAdded;
  407. deleted = new ArrayList<>(nameMap.values());
  408. }
  409. private int calculateModifyScore(ContentSource.Pair reader, DiffEntry d)
  410. throws IOException {
  411. try {
  412. SimilarityIndex src = new SimilarityIndex();
  413. src.hash(reader.open(OLD, d));
  414. src.sort();
  415. SimilarityIndex dst = new SimilarityIndex();
  416. dst.hash(reader.open(NEW, d));
  417. dst.sort();
  418. return src.score(dst, 100);
  419. } catch (TableFullException tableFull) {
  420. // If either table overflowed while being constructed, don't allow
  421. // the pair to be broken. Returning 1 higher than breakScore will
  422. // ensure its not similar, but not quite dissimilar enough to break.
  423. //
  424. overRenameLimit = true;
  425. return breakScore + 1;
  426. }
  427. }
  428. private void findContentRenames(ContentSource.Pair reader,
  429. ProgressMonitor pm)
  430. throws IOException {
  431. int cnt = Math.max(added.size(), deleted.size());
  432. if (getRenameLimit() == 0 || cnt <= getRenameLimit()) {
  433. SimilarityRenameDetector d;
  434. d = new SimilarityRenameDetector(reader, deleted, added);
  435. d.setRenameScore(getRenameScore());
  436. d.compute(pm);
  437. overRenameLimit |= d.isTableOverflow();
  438. deleted = d.getLeftOverSources();
  439. added = d.getLeftOverDestinations();
  440. entries.addAll(d.getMatches());
  441. } else {
  442. overRenameLimit = true;
  443. }
  444. }
  445. @SuppressWarnings("unchecked")
  446. private void findExactRenames(ProgressMonitor pm) {
  447. pm.beginTask(JGitText.get().renamesFindingExact, //
  448. added.size() + added.size() + deleted.size()
  449. + added.size() * deleted.size());
  450. HashMap<AbbreviatedObjectId, Object> deletedMap = populateMap(deleted, pm);
  451. HashMap<AbbreviatedObjectId, Object> addedMap = populateMap(added, pm);
  452. ArrayList<DiffEntry> uniqueAdds = new ArrayList<>(added.size());
  453. ArrayList<List<DiffEntry>> nonUniqueAdds = new ArrayList<>();
  454. for (Object o : addedMap.values()) {
  455. if (o instanceof DiffEntry)
  456. uniqueAdds.add((DiffEntry) o);
  457. else
  458. nonUniqueAdds.add((List<DiffEntry>) o);
  459. }
  460. ArrayList<DiffEntry> left = new ArrayList<>(added.size());
  461. for (DiffEntry a : uniqueAdds) {
  462. Object del = deletedMap.get(a.newId);
  463. if (del instanceof DiffEntry) {
  464. // We have one add to one delete: pair them if they are the same
  465. // type
  466. DiffEntry e = (DiffEntry) del;
  467. if (sameType(e.oldMode, a.newMode)) {
  468. e.changeType = ChangeType.RENAME;
  469. entries.add(exactRename(e, a));
  470. } else {
  471. left.add(a);
  472. }
  473. } else if (del != null) {
  474. // We have one add to many deletes: find the delete with the
  475. // same type and closest name to the add, then pair them
  476. List<DiffEntry> list = (List<DiffEntry>) del;
  477. DiffEntry best = bestPathMatch(a, list);
  478. if (best != null) {
  479. best.changeType = ChangeType.RENAME;
  480. entries.add(exactRename(best, a));
  481. } else {
  482. left.add(a);
  483. }
  484. } else {
  485. left.add(a);
  486. }
  487. pm.update(1);
  488. }
  489. for (List<DiffEntry> adds : nonUniqueAdds) {
  490. Object o = deletedMap.get(adds.get(0).newId);
  491. if (o instanceof DiffEntry) {
  492. // We have many adds to one delete: find the add with the same
  493. // type and closest name to the delete, then pair them. Mark the
  494. // rest as copies of the delete.
  495. DiffEntry d = (DiffEntry) o;
  496. DiffEntry best = bestPathMatch(d, adds);
  497. if (best != null) {
  498. d.changeType = ChangeType.RENAME;
  499. entries.add(exactRename(d, best));
  500. for (DiffEntry a : adds) {
  501. if (a != best) {
  502. if (sameType(d.oldMode, a.newMode)) {
  503. entries.add(exactCopy(d, a));
  504. } else {
  505. left.add(a);
  506. }
  507. }
  508. }
  509. } else {
  510. left.addAll(adds);
  511. }
  512. } else if (o != null) {
  513. // We have many adds to many deletes: score all the adds against
  514. // all the deletes by path name, take the best matches, pair
  515. // them as renames, then call the rest copies
  516. List<DiffEntry> dels = (List<DiffEntry>) o;
  517. long[] matrix = new long[dels.size() * adds.size()];
  518. int mNext = 0;
  519. for (int delIdx = 0; delIdx < dels.size(); delIdx++) {
  520. String deletedName = dels.get(delIdx).oldPath;
  521. for (int addIdx = 0; addIdx < adds.size(); addIdx++) {
  522. String addedName = adds.get(addIdx).newPath;
  523. int score = SimilarityRenameDetector.nameScore(addedName, deletedName);
  524. matrix[mNext] = SimilarityRenameDetector.encode(score, delIdx, addIdx);
  525. mNext++;
  526. }
  527. }
  528. Arrays.sort(matrix);
  529. for (--mNext; mNext >= 0; mNext--) {
  530. long ent = matrix[mNext];
  531. int delIdx = SimilarityRenameDetector.srcFile(ent);
  532. int addIdx = SimilarityRenameDetector.dstFile(ent);
  533. DiffEntry d = dels.get(delIdx);
  534. DiffEntry a = adds.get(addIdx);
  535. if (a == null) {
  536. pm.update(1);
  537. continue; // was already matched earlier
  538. }
  539. ChangeType type;
  540. if (d.changeType == ChangeType.DELETE) {
  541. // First use of this source file. Tag it as a rename so we
  542. // later know it is already been used as a rename, other
  543. // matches (if any) will claim themselves as copies instead.
  544. //
  545. d.changeType = ChangeType.RENAME;
  546. type = ChangeType.RENAME;
  547. } else {
  548. type = ChangeType.COPY;
  549. }
  550. entries.add(DiffEntry.pair(type, d, a, 100));
  551. adds.set(addIdx, null); // Claim the destination was matched.
  552. pm.update(1);
  553. }
  554. } else {
  555. left.addAll(adds);
  556. }
  557. }
  558. added = left;
  559. deleted = new ArrayList<>(deletedMap.size());
  560. for (Object o : deletedMap.values()) {
  561. if (o instanceof DiffEntry) {
  562. DiffEntry e = (DiffEntry) o;
  563. if (e.changeType == ChangeType.DELETE)
  564. deleted.add(e);
  565. } else {
  566. List<DiffEntry> list = (List<DiffEntry>) o;
  567. for (DiffEntry e : list) {
  568. if (e.changeType == ChangeType.DELETE)
  569. deleted.add(e);
  570. }
  571. }
  572. }
  573. pm.endTask();
  574. }
  575. /**
  576. * Find the best match by file path for a given DiffEntry from a list of
  577. * DiffEntrys. The returned DiffEntry will be of the same type as <src>. If
  578. * no DiffEntry can be found that has the same type, this method will return
  579. * null.
  580. *
  581. * @param src
  582. * the DiffEntry to try to find a match for
  583. * @param list
  584. * a list of DiffEntrys to search through
  585. * @return the DiffEntry from <list> who's file path best matches <src>
  586. */
  587. private static DiffEntry bestPathMatch(DiffEntry src, List<DiffEntry> list) {
  588. DiffEntry best = null;
  589. int score = -1;
  590. for (DiffEntry d : list) {
  591. if (sameType(mode(d), mode(src))) {
  592. int tmp = SimilarityRenameDetector
  593. .nameScore(path(d), path(src));
  594. if (tmp > score) {
  595. best = d;
  596. score = tmp;
  597. }
  598. }
  599. }
  600. return best;
  601. }
  602. @SuppressWarnings("unchecked")
  603. private HashMap<AbbreviatedObjectId, Object> populateMap(
  604. List<DiffEntry> diffEntries, ProgressMonitor pm) {
  605. HashMap<AbbreviatedObjectId, Object> map = new HashMap<>();
  606. for (DiffEntry de : diffEntries) {
  607. Object old = map.put(id(de), de);
  608. if (old instanceof DiffEntry) {
  609. ArrayList<DiffEntry> list = new ArrayList<>(2);
  610. list.add((DiffEntry) old);
  611. list.add(de);
  612. map.put(id(de), list);
  613. } else if (old != null) {
  614. // Must be a list of DiffEntries
  615. ((List<DiffEntry>) old).add(de);
  616. map.put(id(de), old);
  617. }
  618. pm.update(1);
  619. }
  620. return map;
  621. }
  622. private static String path(DiffEntry de) {
  623. return de.changeType == ChangeType.DELETE ? de.oldPath : de.newPath;
  624. }
  625. private static FileMode mode(DiffEntry de) {
  626. return de.changeType == ChangeType.DELETE ? de.oldMode : de.newMode;
  627. }
  628. private static AbbreviatedObjectId id(DiffEntry de) {
  629. return de.changeType == ChangeType.DELETE ? de.oldId : de.newId;
  630. }
  631. static boolean sameType(FileMode a, FileMode b) {
  632. // Files have to be of the same type in order to rename them.
  633. // We would never want to rename a file to a gitlink, or a
  634. // symlink to a file.
  635. //
  636. int aType = a.getBits() & FileMode.TYPE_MASK;
  637. int bType = b.getBits() & FileMode.TYPE_MASK;
  638. return aType == bType;
  639. }
  640. private static DiffEntry exactRename(DiffEntry src, DiffEntry dst) {
  641. return DiffEntry.pair(ChangeType.RENAME, src, dst, EXACT_RENAME_SCORE);
  642. }
  643. private static DiffEntry exactCopy(DiffEntry src, DiffEntry dst) {
  644. return DiffEntry.pair(ChangeType.COPY, src, dst, EXACT_RENAME_SCORE);
  645. }
  646. }