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