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

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