Use try-with-resource to avoid leaks with RevWalk and TreeWalk
[egit/eclipse.git] / org.eclipse.egit.core / src / org / eclipse / egit / core / internal / CompareCoreUtils.java
blobc3f2b62305e301e870fc403074264afb8c5c640d
1 /*******************************************************************************
2 * Copyright (c) 2010, 2013 SAP AG and others.
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
9 * Contributors:
10 * Stefan Lay (SAP AG) - initial implementation
11 * Benjamin Muskalla (Tasktop Technologies) - moved into Core for reusability
12 *******************************************************************************/
13 package org.eclipse.egit.core.internal;
15 import java.io.IOException;
16 import java.util.List;
18 import org.eclipse.core.resources.IEncodedStorage;
19 import org.eclipse.core.resources.IFile;
20 import org.eclipse.core.resources.IResource;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.egit.core.internal.util.ResourceUtil;
23 import org.eclipse.jgit.diff.DiffEntry;
24 import org.eclipse.jgit.diff.RenameDetector;
25 import org.eclipse.jgit.diff.DiffEntry.ChangeType;
26 import org.eclipse.jgit.lib.Constants;
27 import org.eclipse.jgit.lib.NullProgressMonitor;
28 import org.eclipse.jgit.lib.ObjectReader;
29 import org.eclipse.jgit.lib.Repository;
30 import org.eclipse.jgit.revwalk.RevCommit;
31 import org.eclipse.jgit.treewalk.TreeWalk;
33 /**
34 * Utility class for compare-related functionality
36 public class CompareCoreUtils {
38 /**
39 * Determine the encoding used by Eclipse for the resource which belongs to
40 * repoPath in the eclipse workspace or null if no resource is found
42 * @param db
43 * the repository
44 * @param repoPath
45 * the path in the git repository
46 * @return the encoding used in eclipse for the resource or null if
49 public static String getResourceEncoding(Repository db, String repoPath) {
50 if (db.isBare())
51 return null;
52 IFile resource = ResourceUtil.getFileForLocation(db, repoPath);
53 if (resource == null)
54 return null;
56 return getResourceEncoding(resource);
59 /**
60 * Determine the encoding used by eclipse for the resource.
62 * @param resource
63 * must be an instance of IEncodedStorage
64 * @return the encoding used in Eclipse for the resource if found or null
66 public static String getResourceEncoding(IResource resource) {
67 // Get the encoding for the current version. As a matter of
68 // principle one might want to use the eclipse settings for the
69 // version we are retrieving as that may be defined by the
70 // project settings, but there is no historic API for this.
71 String charset;
72 IEncodedStorage encodedStorage = ((IEncodedStorage) resource);
73 try {
74 charset = encodedStorage.getCharset();
75 if (charset == null)
76 charset = resource.getParent().getDefaultCharset();
77 } catch (CoreException e) {
78 charset = Constants.CHARACTER_ENCODING;
80 return charset;
83 /**
84 * Get the {@link DiffEntry} corresponding to a change in file path. If the
85 * file was renamed, the resulting {@link DiffEntry} will contain the old
86 * path and blob ID. If the file was only added, null will be returned.
88 * @param repository
89 * @param newPath
90 * path of the file in new commit
91 * @param newCommit
92 * new commit
93 * @param oldCommit
94 * old commit, e.g. parent commit of newCommit
95 * @param objectReader
96 * reader for the repository
97 * @return the diff entry corresponding to the change for path, or null if
98 * none could be found
99 * @throws IOException
101 public static DiffEntry getChangeDiffEntry(Repository repository, String newPath,
102 RevCommit newCommit, RevCommit oldCommit, ObjectReader objectReader)
103 throws IOException {
104 try (TreeWalk walk = new TreeWalk(objectReader)) {
105 walk.setRecursive(true);
106 walk.addTree(oldCommit.getTree());
107 walk.addTree(newCommit.getTree());
109 List<DiffEntry> entries = DiffEntry.scan(walk);
111 for (DiffEntry diff : entries) {
112 if (diff.getChangeType() == ChangeType.MODIFY
113 && newPath.equals(diff.getNewPath()))
114 return diff;
117 if (entries.size() < 2)
118 return null;
120 RenameDetector detector = new RenameDetector(repository);
121 detector.addAll(entries);
122 List<DiffEntry> renames = detector.compute(walk.getObjectReader(),
123 NullProgressMonitor.INSTANCE);
124 for (DiffEntry diff : renames) {
125 if (diff.getChangeType() == ChangeType.RENAME
126 && newPath.equals(diff.getNewPath()))
127 return diff;
130 return null;