Eclipseplugins
CompileBranchesPage.java
1 package com.proalpha.pds.gitutils.checks;
2 
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.List;
6 
7 import org.eclipse.core.runtime.jobs.Job;
8 import org.eclipse.jface.dialogs.MessageDialog;
9 import org.eclipse.jface.wizard.WizardPage;
10 import org.eclipse.jgit.api.Git;
11 import org.eclipse.jgit.api.errors.GitAPIException;
12 import org.eclipse.jgit.lib.Ref;
13 import org.eclipse.jgit.lib.Repository;
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.widgets.Combo;
16 import org.eclipse.swt.widgets.Composite;
17 import org.eclipse.swt.widgets.Display;
18 import org.eclipse.swt.widgets.Label;
19 
20 public class CompileBranchesPage extends WizardPage {
21  private Repository repository;
22  private RepositoryTools repositoryTools;
23  private Combo cmb_source;
24  private Combo cmb_target;
25  private List<String> localBranches;
26 
27  public CompileBranchesPage(Repository repository) {
28  super("wizardPage");
29  this.repository = repository;
30  this.repositoryTools = new RepositoryTools(repository);
31  setTitle("Select Branches");
32  setDescription("Select the source and target branch for Differeces");
33 
34  }
35 
36  @Override
37  public void createControl(Composite parent) {
38  Composite container = new Composite(parent, SWT.NONE);
39  setControl(container);
40 
41  cmb_source = new Combo(container, SWT.NONE | SWT.READ_ONLY);
42  cmb_source.setBounds(174, 41, 173, 23);
43 
44  Label lblSourceBranch = new Label(container, SWT.NONE);
45  lblSourceBranch.setBounds(27, 49, 141, 15);
46  lblSourceBranch.setText("Source Branch");
47 
48  cmb_target = new Combo(container, SWT.NONE | SWT.READ_ONLY);
49  cmb_target.setBounds(173, 70, 174, 23);
50 
51  Label lblTargetBranch = new Label(container, SWT.NONE);
52  lblTargetBranch.setBounds(27, 78, 141, 15);
53  lblTargetBranch.setText("Target Branch");
54 
55  try {
56  // get current and Master Branch name
57  String curBranch = repositoryTools.extendBanchName(repositoryTools.getBranch());
58  String masterBranch = repositoryTools.extendBanchName(repositoryTools.getMasterBranch());
59  // read local branches
60  localBranches = getBranches();
61  // counter for list entries
62  Integer itemcount = 0;
63 
64  for (String branch : localBranches) {
65  // add branchname in both combo boxes
66  cmb_source.add(branch);
67  cmb_target.add(branch);
68  // if branch is current branch set source combo selection
69  if (curBranch.equals(branch)) {
70  cmb_source.select(itemcount);
71  }
72  // if branch is master branch set target combo selection
73  if (masterBranch.equals(branch)) {
74  cmb_target.select(itemcount);
75  }
76  itemcount += 1;
77 
78  }
79 
80  } catch (IOException | GitAPIException e1) {
81  // do nothing. cmb values not filled
82  }
83 
84  }
85 
86  private List<String> getBranches() throws IOException, GitAPIException {
87  // return a list of all local branch names
88  List<String> allBranches = new ArrayList<String>();
89 
90  try (Git git = new Git(repository)) {
91 
92  List<Ref> call = git.branchList().call();
93 
94  for (Ref ref : call) {
95  allBranches.add(ref.getName());
96  }
97  }
98 
99  return allBranches;
100 
101  }
102 
103  public boolean selection_done() {
104  // is called by Wizzard Mainpage
105 
106  if (cmb_source.getText().length() == 0 || cmb_target.getText().length() == 0) {
107  MessageDialog.openError(Display.getDefault().getActiveShell(), "branch selection error",
108  "not all branches are selected.");
109  return false;
110  } else {
111  // get diffs and start compile
112 
113  // get the single branch name
114  String source_branch = getSingleBranchName(cmb_source.getText());
115  String target_branch = getSingleBranchName(cmb_target.getText());
116  // read temp dir from proALPHA
117  String relativeInputFile = source_branch + '_' + target_branch + ".inp";
118 
119  // if changed files found get dependencies
120  // set job title
121  String titletxt = String.format("get compiler listing between %s and %s", source_branch, target_branch);
122 
123  CompileOptions options = new CompileOptions();
124  options.type = CompileType.DIFF;
125  options.commit1 = source_branch;
126  options.commit2 = target_branch;
127  options.compilerInputFileName = relativeInputFile;
128 
129  Job branchDeptCollectorJob = new CompileJob(titletxt, repositoryTools, options);
130  branchDeptCollectorJob.setUser(true);
131  branchDeptCollectorJob.schedule();
132 
133  return true;
134  }
135  }
136 
137  private String getSingleBranchName(String branch) {
138  // get the branch name without leeding refs
139  if (branch.contains("/")) {
140  String[] refs = branch.split("/");
141  if (refs.length > 1)
142  branch = refs[refs.length - 1];
143  }
144  return branch;
145  }
146 }