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