1 package com.proalpha.pds.gitutils.checks;
3 import java.io.IOException;
4 import java.nio.file.Paths;
5 import java.util.ArrayList;
6 import java.util.Collections;
8 import java.util.regex.Matcher;
10 import org.eclipse.core.commands.ExecutionEvent;
11 import org.eclipse.core.commands.ExecutionException;
12 import org.eclipse.core.resources.IProject;
13 import org.eclipse.core.resources.ResourcesPlugin;
14 import org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode;
15 import org.eclipse.jface.viewers.ISelection;
16 import org.eclipse.jface.viewers.IStructuredSelection;
17 import org.eclipse.jgit.api.Git;
18 import org.eclipse.jgit.api.Status;
19 import org.eclipse.jgit.api.errors.GitAPIException;
20 import org.eclipse.jgit.diff.DiffEntry;
21 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
22 import org.eclipse.jgit.errors.MissingObjectException;
23 import org.eclipse.jgit.errors.NoWorkTreeException;
24 import org.eclipse.jgit.lib.ObjectReader;
25 import org.eclipse.jgit.lib.Ref;
26 import org.eclipse.jgit.lib.Repository;
27 import org.eclipse.jgit.revwalk.RevCommit;
28 import org.eclipse.jgit.revwalk.RevTree;
29 import org.eclipse.jgit.revwalk.RevWalk;
30 import org.eclipse.jgit.treewalk.AbstractTreeIterator;
31 import org.eclipse.jgit.treewalk.CanonicalTreeParser;
32 import org.eclipse.ui.handlers.HandlerUtil;
34 import com.proalpha.git.util.PaRepository;
35 import com.proalpha.pds.gitutils.Activator;
36 import com.proalpha.pds.paconnector.PaProject;
38 @SuppressWarnings(
"restriction")
41 private Repository repository =
null;
44 this.repository = repository;
48 RepositoryTreeNode<?> treenode =
null;
50 treenode = (RepositoryTreeNode<?>) getSelectedNodes(event).get(0);
51 }
catch (ExecutionException e) {
52 Activator.logToConsole(String.format(
"Error in Constructor of class RepositoryTools: %s", e.getMessage()));
54 if (treenode !=
null) {
55 repository = treenode.getRepository();
59 public List<?> getSelectedNodes(ExecutionEvent event)
throws ExecutionException {
60 ISelection selection = HandlerUtil.getCurrentSelectionChecked(event);
61 if (selection instanceof IStructuredSelection)
62 return ((IStructuredSelection) selection).toList();
65 "Event %s is no instance of IStructuredSelection. Returning empty list.", event.toString()));
66 return Collections.emptyList();
70 public List<String> getStagedFiles() {
73 List<String> stagesFiles =
new ArrayList<>();
75 try (Git git =
new Git(repository)) {
76 status = git.status().call();
78 for (String file : status.getChanged()) {
79 if (file.contains(
"/")) {
80 stagesFiles.add(file.substring(file.lastIndexOf(
'/') + 1));
82 stagesFiles.add(file);
86 for (String file : status.getAdded()) {
87 if (file.contains(
"/")) {
88 stagesFiles.add(file.substring(file.lastIndexOf(
'/') + 1));
90 stagesFiles.add(file);
94 for (String file : status.getRemoved()) {
96 if (file.contains(
"/")) {
97 stagesFiles.add(file.substring(file.lastIndexOf(
'/') + 1));
99 stagesFiles.add(file);
102 }
catch (NoWorkTreeException | GitAPIException e) {
103 Activator.logToConsole(String.format(
"Error in Method getStagedFiles: %s", e.getMessage()));
104 return Collections.emptyList();
110 public List<String> getUnstagedFiles() {
112 Status status =
null;
113 List<String> unStagesFiles =
new ArrayList<>();
115 try (Git git =
new Git(repository)) {
116 status = git.status().call();
118 for (String file : status.getUntracked()) {
119 if (file.contains(
"/")) {
120 unStagesFiles.add(file.substring(file.lastIndexOf(
'/') + 1));
122 unStagesFiles.add(file);
126 for (String file : status.getModified()) {
127 if (file.contains(
"/")) {
128 unStagesFiles.add(file.substring(file.lastIndexOf(
'/') + 1));
130 unStagesFiles.add(file);
134 for (String file : status.getConflicting()) {
135 if (file.contains(
"/")) {
136 unStagesFiles.add(file.substring(file.lastIndexOf(
'/') + 1));
138 unStagesFiles.add(file);
142 for (String file : status.getMissing()) {
143 if (file.contains(
"/")) {
144 unStagesFiles.add(file.substring(file.lastIndexOf(
'/') + 1));
146 unStagesFiles.add(file);
149 }
catch (NoWorkTreeException | GitAPIException e) {
150 Activator.logToConsole(String.format(
"Error in Method getUnstagedFiles: %s", e.getMessage()));
151 return Collections.emptyList();
154 return unStagesFiles;
157 public List<String> getChangedFilesWithPath() {
159 Status status =
null;
160 List<String> changedFiles =
new ArrayList<>();
162 try (Git git =
new Git(repository)) {
163 status = git.status().call();
165 for (String file : status.getModified()) {
167 if (file.contains(
"src/")) {
168 changedFiles.add(file.substring(file.lastIndexOf(
"src/") + 4));
170 changedFiles.add(file);
174 for (String file : status.getConflicting()) {
176 if (file.contains(
"src/")) {
177 changedFiles.add(file.substring(file.lastIndexOf(
"src/") + 4));
179 changedFiles.add(file);
182 for (String file : status.getAdded()) {
184 if (file.contains(
"src/")) {
185 changedFiles.add(file.substring(file.lastIndexOf(
"src/") + 4));
187 changedFiles.add(file);
190 }
catch (NoWorkTreeException | GitAPIException e) {
191 Activator.logToConsole(String.format(
"Error in Method getChangedFilesWithPath: %s", e.getMessage()));
192 return Collections.emptyList();
198 public String getDirectory() {
199 return this.repository.getDirectory().getPath();
202 public String getBranch() {
204 return this.repository.getBranch();
205 }
catch (IOException e) {
206 Activator.logToConsole(String.format(
"Error in Method getBranch: %s", e.getMessage()));
213 long countSeparator = repository.getDirectory().getAbsolutePath().codePoints()
214 .filter(ch -> ch == System.getProperty(
"file.separator").toCharArray()[0]).count();
215 String[] filePathArray = repository.getDirectory().getAbsolutePath()
216 .split(Matcher.quoteReplacement(System.getProperty(
"file.separator")));
217 IProject project =
null;
219 if (countSeparator > 3) {
220 project = ResourcesPlugin.getWorkspace().getRoot().getProject(
221 filePathArray[filePathArray.length - 4] +
"-" + filePathArray[filePathArray.length - 3]);
223 project = ResourcesPlugin.getWorkspace().getRoot().getProject(
224 filePathArray[filePathArray.length - 2] +
"-" + filePathArray[filePathArray.length - 1]);
227 return com.proalpha.pds.paconnector.Activator.getDefault().getProjectManager().getPaProject(project);
231 public List<String> getBranchDiffFiles() {
237 String branch = extendBanchName(getBranch());
239 String master_branch = extendBanchName(this.getMasterBranch());
241 AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, branch);
242 AbstractTreeIterator newTreeParser = prepareTreeParser(repository, master_branch);
244 return getTreeDiffs(oldTreeParser, newTreeParser);
246 }
catch (IOException e) {
247 Activator.logToConsole(String.format(
"Error in Method getBranchDiffFiles: %s", e.getMessage()));
249 return Collections.emptyList();
253 public List<String> getBranchesDiffs(String branch1, String branch2) {
256 AbstractTreeIterator oldTreeParser = prepareTreeParser(repository, branch1);
257 AbstractTreeIterator newTreeParser = prepareTreeParser(repository, branch2);
259 return getTreeDiffs(oldTreeParser, newTreeParser);
261 }
catch (IOException e) {
262 Activator.logToConsole(String.format(
"Error in Method getBranchesDiffs: %s", e.getMessage()));
264 return Collections.emptyList();
268 private List<String> getTreeDiffs(AbstractTreeIterator oldTreeParser, AbstractTreeIterator newTreeParser) {
270 if (oldTreeParser ==
null || newTreeParser ==
null)
271 return Collections.emptyList();
273 List<DiffEntry> diff;
275 try (Git git =
new Git(repository)) {
277 diff = git.diff().setOldTree(oldTreeParser).setNewTree(newTreeParser).call();
279 }
catch (GitAPIException e) {
280 Activator.logToConsole(String.format(
"Error in Method getTreeDiffs: %s" + e.getMessage()));
281 return Collections.emptyList();
283 List<String> changedFiles =
new ArrayList<>();
286 for (DiffEntry entry : diff) {
289 String change_type = entry.getChangeType().toString();
292 if (change_type.equals(
"ADD") || change_type.equals(
"MODIFY"))
293 changedFiles.add(Paths.get(entry.getNewPath()).getFileName().toString());
296 changedFiles.add(Paths.get(entry.getOldPath()).getFileName().toString());
302 private static AbstractTreeIterator prepareTreeParser(Repository repository, String branch)
throws IOException {
307 Ref branchref = repository.exactRef(branch);
310 if (branchref !=
null) {
312 try (RevWalk walk =
new RevWalk(repository)) {
313 RevCommit commit = walk.parseCommit(branchref.getObjectId());
314 RevTree tree = walk.parseTree(commit.getTree().getId());
316 CanonicalTreeParser cronTreeParser =
new CanonicalTreeParser();
317 try (ObjectReader reader = repository.newObjectReader()) {
318 cronTreeParser.reset(reader, tree.getId());
323 return cronTreeParser;
326 Activator.logToConsole(String.format(
"Branch not found: %s", branch));
332 private AbstractTreeIterator getTreeParserbyCommit(RevCommit commit)
333 throws MissingObjectException, IncorrectObjectTypeException, IOException {
335 if (commit !=
null) {
336 try (RevWalk walk =
new RevWalk(repository)) {
337 RevTree tree = walk.parseTree(commit.getTree().getId());
339 CanonicalTreeParser cronTreeParser =
new CanonicalTreeParser();
340 try (ObjectReader reader = repository.newObjectReader()) {
341 cronTreeParser.reset(reader, tree.getId());
346 return cronTreeParser;
349 Activator.logToConsole(
"commit is null. can not init Tree parser");
355 public String extendBanchName(String branch) {
356 if (!branch.startsWith(
"refs/heads/"))
357 branch =
"refs/heads/" + branch;
361 private RevCommit getCurrentBranchCommit(String branchName)
throws IOException {
363 Ref branchref = repository.exactRef(branchName);
366 if (branchref !=
null) {
368 try (RevWalk walk =
new RevWalk(repository)) {
369 return walk.parseCommit(branchref.getObjectId());
377 public String getMasterBranch() {
378 List<String> branches = PaRepository.getMasterBranches(this.repository);
381 String mainBranch =
null;
383 if (branches !=
null && !branches.isEmpty())
384 mainBranch = branches.get(0);
386 branches = PaRepository.getCodeFreezeBranches(this.repository);
387 if (branches !=
null && !branches.isEmpty())
388 mainBranch = branches.get(0);
395 public Repository getRepository() {
397 return this.repository;