Eclipseplugins
FeatureBranchOperation.java
1 package com.proalpha.pds.gitutils.mylyn;
2 
3 import java.lang.reflect.InvocationTargetException;
4 import java.util.ArrayList;
5 import java.util.Arrays;
6 import java.util.List;
7 
8 import org.eclipse.core.resources.IProject;
9 import org.eclipse.core.runtime.IProgressMonitor;
10 import org.eclipse.core.runtime.SubMonitor;
11 import org.eclipse.core.runtime.jobs.ISchedulingRule;
12 import org.eclipse.egit.core.EclipseGitProgressTransformer;
13 import org.eclipse.egit.core.internal.job.RuleUtil;
14 import org.eclipse.egit.core.internal.util.ProjectUtil;
15 import org.eclipse.jgit.api.CheckoutResult;
16 import org.eclipse.jgit.api.Git;
17 import org.eclipse.jgit.lib.Constants;
18 import org.eclipse.jgit.lib.Repository;
19 import org.eclipse.swt.custom.BusyIndicator;
20 import org.eclipse.swt.widgets.Display;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23 
24 import com.proalpha.git.PaGit;
25 import com.proalpha.pds.gitutils.common.CheckoutOperation;
26 import com.proalpha.pds.gitutils.external.DbAndBinaryOperation;
27 import com.proalpha.pds.paconnector.Activator;
28 
37 public class FeatureBranchOperation {
38 
39  private final Logger logger = LoggerFactory.getLogger(FeatureBranchOperation.class);
40 
41  private FeatureBranchSettings settings;
42  private ReconcileActionSettings recoSettings;
43 
44  private IProgressMonitor monitor;
45 
46  private CheckoutOperation coOp;
47 
48  private DbAndBinaryOperation updateDbBinaryOp;
49 
56  this.settings = settings;
57  this.recoSettings = new ReconcileActionSettings(this.settings.getCheckoutAction());
58 
59  PaGit.init(new Git(settings.getRepository()));
60  }
61 
63  this.settings = settings;
64  this.recoSettings = recoSettings;
65  PaGit.init(new Git(settings.getRepository()));
66  }
67 
75  public void execute() {
76  Runnable r = new Runnable() {
77 
78  @Override
79  public void run() {
80  try {
81  SubMonitor subMonitor = SubMonitor.convert(monitor, "pA feature branch creation", 10);
82  PaGit.getInstance().setProgressMonitor(new EclipseGitProgressTransformer(subMonitor.newChild(1)));
83 
84  coOp = new CheckoutOperation(settings.getRepository(), Constants.DEFAULT_REMOTE_NAME,
85  settings.getSourceBranch(), settings.getTargetBranch(), settings.isUpdateSourceBranch(),
86  settings.isUpdateTargetBranch());
87  coOp.setProgressMonitor(subMonitor);
88  coOp.run();
89  subMonitor.worked(1);
90  // only start an operation if the checkout-action is not "NONE"
91  if (!recoSettings.getPaUpdateStages().isEmpty()) {
92  performReconcileJob(settings.getRepository(), recoSettings, subMonitor);
93  }
94  } catch (InvocationTargetException e) {
95  logger.error("An error {0} occured", e);
96  }
97 
98  }
99 
100  public void performReconcileJob(Repository repository, ReconcileActionSettings recoSettings,
101  IProgressMonitor monitor) {
102 
103  // collect all affected projects which include the repository
104  IProject[] projects = ProjectUtil.getProjects(repository);
105  ArrayList<IProject> openedProjects = new ArrayList<>(Arrays.asList(projects));
106  SubMonitor subMonitorArtifacts = SubMonitor.convert(monitor, "database reconcile and artifact update",
107  recoSettings.getPaUpdateStages().size());
108 
109  if (recoSettings.isLoadDf())
110  Activator.getDefault().getProjectManager().closeProjects(openedProjects, subMonitorArtifacts);
111 
112  updateDbBinaryOp = new DbAndBinaryOperation(repository, recoSettings);
113  updateDbBinaryOp.setProgressMonitor(subMonitorArtifacts);
114  updateDbBinaryOp.execute();
115 
116  try {
117  coOp = new CheckoutOperation(settings.getRepository(), Constants.DEFAULT_REMOTE_NAME,
118  settings.getSourceBranch(), settings.getTargetBranch(), settings.isUpdateSourceBranch(),
119  settings.isUpdateTargetBranch());
120  coOp.run();
121  } catch (InvocationTargetException e) {
122  logger.error("An error {0} occured", e);
123  }
124 
125  if (recoSettings.isLoadDf())
126  Activator.getDefault().getProjectManager().openProjects(openedProjects, subMonitorArtifacts);
127  }
128  };
129  BusyIndicator.showWhile(Display.getCurrent(), r);
130  }
131 
132  @SuppressWarnings("restriction")
133  public ISchedulingRule getSchedulingRule() {
134  if (settings != null)
135  return RuleUtil.getRule(settings.getRepository());
136  else
137  return null;
138  }
139 
140  public void setProgressMonitor(IProgressMonitor monitor) {
141  this.monitor = monitor;
142  }
143 
148  public List<ActionResult> getResult() {
149 
150  List<ActionResult> results = new ArrayList<>();
151  if (updateDbBinaryOp != null)
152  results.addAll(updateDbBinaryOp.getActionResults());
153  if (coOp != null) {
154  ActionResult checkoutResult = new ActionResult("checkout");
155  if (coOp.getResult().getStatus() != CheckoutResult.Status.OK)
156  checkoutResult.setStatus("FAIL");
157  results.add(0, checkoutResult);
158  }
159 
160  return results;
161  }
162 
163  public FeatureBranchSettings getSettings() {
164  return settings;
165  }
166 
167  public void setSettings(FeatureBranchSettings settings) {
168  this.settings = settings;
169  }
170 }