Eclipseplugins
All Classes Functions Variables Pages
GitUtilsPropertyTester.java
1 package com.proalpha.pds.gitutils;
2 
3 import java.io.FileNotFoundException;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.util.ArrayList;
7 import java.util.Set;
8 
9 import org.eclipse.core.expressions.PropertyTester;
10 import org.eclipse.egit.ui.internal.repository.tree.RepositoryTreeNode;
11 import org.eclipse.jgit.api.CherryPickResult.CherryPickStatus;
12 import org.eclipse.jgit.api.Git;
13 import org.eclipse.jgit.api.errors.GitAPIException;
14 import org.eclipse.jgit.errors.NoWorkTreeException;
15 import org.json.simple.JSONObject;
16 import org.json.simple.parser.JSONParser;
17 import org.json.simple.parser.ParseException;
18 
19 import com.proalpha.git.model.PaCherryPickResult;
20 import com.proalpha.pds.gitutils.cherrypick.CherryPick;
21 import com.proalpha.pds.gitutils.common.Repository;
22 
23 public class GitUtilsPropertyTester extends PropertyTester {
24 
25  @Override
26  public boolean test(Object receiver, String property, Object[] args, Object expectedValue) {
27  if (property.equals("isConflict")) {
28  PaCherryPickResult result = CherryPick.getInstance().getCherryPickResult();
29  if (result == null)
30  return false;
31  return (result.getStatus() == CherryPickStatus.CONFLICTING);
32  }
33 
34  // Returns true if on the selected repository, a conflict is found
35  // and the conflict is marked as mergetool applicable
36  if (property.equals("isMergetoolApplicable")) {
37  if (receiver instanceof RepositoryTreeNode) {
38  RepositoryTreeNode node = (RepositoryTreeNode) receiver;
39  org.eclipse.jgit.lib.Repository repository = node.getRepository();
40 
41  Git git = new Git(repository);
42  try {
43  Set<String> conflictingFiles = git.status().call().getConflicting();
44  Repository.getInstance().setConflictingFiles(new ArrayList<String>(conflictingFiles));
45  return !conflictingFiles.isEmpty();
46  } catch (NoWorkTreeException e) {
47  e.printStackTrace();
48  return false;
49  } catch (GitAPIException e) {
50  e.printStackTrace();
51  return false;
52  }
53  }
54  }
55 
56  // Returns true if on the selected repository, a cherry pick is running
57  // (in particular, if the selected repository is the repository, where a
58  // conflict occurs
59  if (property.equals("isOngoingCherryPick")) {
60 
61  if (CherryPick.getInstance().getCherryPickCommand() != null) {
62  org.eclipse.jgit.lib.Repository cpRepo = CherryPick.getInstance().getCherryPickCommand().getRepository();
63 
64  if (receiver instanceof RepositoryTreeNode) {
65  RepositoryTreeNode node = (RepositoryTreeNode) receiver;
66  org.eclipse.jgit.lib.Repository repository = node.getRepository();
67 
68  if (repository != null)
69  return cpRepo.equals(repository);
70  }
71  }
72  System.out.println("Property tester: " + property + "=" + "false");
73  return false;
74  }
75 
76  // Returns true if a new cherry pick operation can start (i.e., if there is
77  // no ongoing cherry pick operation)
78  if (property.equals("canStartNewCherryPick")) {
79  return (CherryPick.getInstance().getCherryPickCommand() == null);
80  }
81 
82  if (property.equals("isConsRepo")) {
83 
84  if (receiver instanceof RepositoryTreeNode) {
85  RepositoryTreeNode node = (RepositoryTreeNode) receiver;
86  org.eclipse.jgit.lib.Repository repository = node.getRepository();
87  String workspace = repository.getDirectory().getParent();
88 
89  JSONParser jsonParser = new JSONParser();
90  try (FileReader reader = new FileReader(workspace + "/pa_config_project.json"))
91  {
92  JSONObject obj = (JSONObject) jsonParser.parse(reader);
93  if (obj.containsKey("repository_type")) {
94  String repositoryType = (String) obj.get("repository_type");
95  if(repositoryType.equals("cons")) return true;
96  }
97  return false;
98 
99  } catch (FileNotFoundException e) {
100  return false;
101  } catch(ClassCastException e) {
102  return false;
103  } catch (IOException e) {
104  e.printStackTrace();
105  } catch (ParseException e) {
106  e.printStackTrace();
107  }
108  }
109  return false;
110  }
111 
112  return false;
113  }
114 
115 }