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
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
;
34 * Utility class for compare-related functionality
36 public class CompareCoreUtils
{
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
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
) {
52 IFile resource
= ResourceUtil
.getFileForLocation(db
, repoPath
);
56 return getResourceEncoding(resource
);
60 * Determine the encoding used by eclipse for the 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.
72 IEncodedStorage encodedStorage
= ((IEncodedStorage
) resource
);
74 charset
= encodedStorage
.getCharset();
76 charset
= resource
.getParent().getDefaultCharset();
77 } catch (CoreException e
) {
78 charset
= Constants
.CHARACTER_ENCODING
;
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.
90 * path of the file in new commit
94 * old commit, e.g. parent commit of newCommit
96 * reader for the repository
97 * @return the diff entry corresponding to the change for path, or null if
101 public static DiffEntry
getChangeDiffEntry(Repository repository
, String newPath
,
102 RevCommit newCommit
, RevCommit oldCommit
, ObjectReader objectReader
)
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()))
117 if (entries
.size() < 2)
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()))