Eclipseplugins
CherryPick.java
1 package com.proalpha.pds.gitutils.cherrypick;
2 
3 import java.io.IOException;
4 
5 import org.eclipse.jgit.api.errors.GitAPIException;
6 import org.eclipse.jgit.lib.Constants;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9 
10 import com.proalpha.git.PaGit;
11 import com.proalpha.git.commands.PaIssueConveyanceCommand;
12 import com.proalpha.git.model.PaCherryPickResult;
13 import com.proalpha.git.util.PaRepository;
14 import com.proalpha.pds.gitutils.Activator;
15 import com.proalpha.pds.gitutils.PreferencesConstants;
16 
17 public class CherryPick {
18 
19  private final Logger logger = LoggerFactory.getLogger(CherryPick.class);
20 
21  private static final class CherryPickInstanceHolder {
22  static final CherryPick INSTANCE = new CherryPick();
23  }
24 
25  private CherryPick() {
26  }
27 
28  public static CherryPick getInstance() {
29  return CherryPickInstanceHolder.INSTANCE;
30  }
31 
32  private CherryPickSettings cherryPickSettings;
33  private PaCherryPickResult cherryPickResult;
34  private PaIssueConveyanceCommand cherryPickCommand;
35 
36  public CherryPickSettings getCherryPickSettings() {
37  return cherryPickSettings;
38  }
39 
40  public PaCherryPickResult getCherryPickResult() {
41  return cherryPickResult;
42  }
43 
44  public void setCherryPickResult(PaCherryPickResult cherryPickResult) {
45  this.cherryPickResult = cherryPickResult;
46  }
47 
48  public PaIssueConveyanceCommand getCherryPickCommand() {
49  return cherryPickCommand;
50  }
51 
52  public void createCherryPickCommand(CherryPickSettings settings) throws IOException, GitAPIException {
53 
54  cherryPickSettings = settings;
55  String remoteBranch = "";
56  String gitServerPath = Activator.getDefault().getPreferenceStore()
57  .getString(PreferencesConstants.GIT_SERVER_URL);
58  if (!gitServerPath.endsWith("/"))
59  gitServerPath = gitServerPath + '/';
60 
61 
62  remoteBranch = gitServerPath + "scm/" + cherryPickSettings.getSourceRepoString();
63 
64  String repTemp = PaRepository.connectRemoteBranch(remoteBranch, cherryPickSettings.getSourceBranch(), '_' + cherryPickSettings.getSourceRepoString());
65 
66  cherryPickCommand = PaGit.getInstance().paIssueConveyenceCommand()
67  .setIssue(cherryPickSettings.getSourceIssue())
68  .setTargetIssue(cherryPickSettings.getTargetIssue())
69  .setSourceRef(repTemp)
70  .setTargetRef(Constants.R_HEADS + cherryPickSettings.getTargetBranch());
71  }
72 
77  public void cancelCherryPick(boolean rollback) {
78 
79  try {
80  if (rollback) {
81  cherryPickCommand.rollback();
82  }
83  PaRepository.disconnectRemote('_' + cherryPickSettings.getSourceRepoString());
84  } catch (IOException | GitAPIException e) {
85  logger.error("Exception while disconnecting remote {}", cherryPickCommand.getSourceRef(), e);
86  }
87 
88  this.reinitialize();
89 
90  }
91 
96  public void reinitialize() {
97  cherryPickSettings = null;
98  cherryPickCommand = null;
99  cherryPickResult = null;
100  }
101 
102 }