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

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