Eclipseplugins
GatherCommitsOperation.java
1 package com.proalpha.pds.gitutils.cherrypick;
2 
3 import java.io.IOException;
4 import java.lang.reflect.InvocationTargetException;
5 import java.util.List;
6 
7 import org.eclipse.core.runtime.jobs.ISchedulingRule;
8 import org.eclipse.egit.core.internal.job.RuleUtil;
9 import org.eclipse.jgit.api.errors.GitAPIException;
10 import org.eclipse.jgit.revwalk.RevCommit;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 
14 import com.proalpha.git.PaGit;
15 import com.proalpha.git.util.PaIssueExplorer;
16 import com.proalpha.git.util.PaRepository;
17 import com.proalpha.pds.gitutils.Activator;
18 import com.proalpha.pds.gitutils.PreferencesConstants;
19 
20 public class GatherCommitsOperation {
21 
22  private final Logger logger = LoggerFactory.getLogger(GatherCommitsOperation.class);
23 
24  private List<RevCommit> gatheredCommits;
25  private String repository;
26  private String branch;
27  private String issue;
28 
29  public GatherCommitsOperation(String repository, String branch, String issue) {
30  this.repository = repository;
31  this.branch = branch;
32  this.issue = issue;
33  }
34 
35  public void run() throws InvocationTargetException {
36 
37  String repTemp = null;
38 
39  try {
40  String remoteBranch = "";
41  String gitServerPath = Activator.getDefault().getPreferenceStore()
42  .getString(PreferencesConstants.GIT_SERVER_URL);
43  if (!gitServerPath.endsWith("/"))
44  gitServerPath = gitServerPath + '/';
45 
46  remoteBranch = gitServerPath + "scm/" + this.repository;
47 
48  repTemp = PaRepository.connectRemoteBranch(remoteBranch, this.branch, '_' + this.repository);
49 
50  logger.debug("Remotebranch created as: {}", repTemp);
51  List<RevCommit> commits = PaIssueExplorer.getCommitsOfIssue(repTemp, this.issue);
52 
53  if (logger.isDebugEnabled())
54  for (RevCommit commit : commits) {
55  logger.debug("Found commit: {}, message: {}", commit.getName(), commit.getFullMessage());
56  }
57 
58  this.gatheredCommits = commits;
59 
60  } catch (GitAPIException e) {
61  throw new InvocationTargetException(e.getCause() != null ? e.getCause() : e);
62  } catch (IOException e) {
63  throw new InvocationTargetException(e);
64  } finally {
65  try {
66  PaRepository.disconnectRemote('_' + this.repository);
67  } catch (IOException | GitAPIException e) {
68  logger.error("Exception while disconnecting remote {}", repTemp, e);
69  }
70  }
71 
72  }
73 
74  public List<RevCommit> getOperationResult() {
75  return gatheredCommits;
76  }
77 
78  @SuppressWarnings("restriction")
79  public ISchedulingRule getSchedulingRule() {
80  try {
81  return RuleUtil.getRule(PaGit.getInstance().getGit().getRepository());
82  } catch (NullPointerException npe) {
83  return null;
84  }
85  }
86 }