BowlerKernel
ScriptingEngine.java
Go to the documentation of this file.
1 package com.neuronrobotics.bowlerstudio.scripting;
2 
3 import com.neuronrobotics.bowlerstudio.IssueReportingExceptionHandler;
4 import com.neuronrobotics.bowlerstudio.util.FileChangeWatcher;
5 import com.neuronrobotics.sdk.common.Log;
6 import com.neuronrobotics.sdk.util.ThreadUtil;
7 import com.neuronrobotics.video.OSUtil;
8 
9 import eu.mihosoft.vrl.v3d.parametrics.CSGDatabase;
10 import org.apache.commons.io.FileUtils;
11 import org.apache.commons.io.IOUtils;
12 import org.eclipse.jgit.api.CloneCommand;
13 import org.eclipse.jgit.api.CreateBranchCommand;
14 import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
15 import org.eclipse.jgit.api.Git;
16 import org.eclipse.jgit.api.ListBranchCommand.ListMode;
17 import org.eclipse.jgit.api.PullCommand;
18 import org.eclipse.jgit.api.Status;
19 import org.eclipse.jgit.api.TransportConfigCallback;
20 import org.eclipse.jgit.api.errors.AbortedByHookException;
21 import org.eclipse.jgit.api.errors.CanceledException;
22 import org.eclipse.jgit.api.errors.CheckoutConflictException;
23 import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
24 import org.eclipse.jgit.api.errors.DetachedHeadException;
25 import org.eclipse.jgit.api.errors.GitAPIException;
26 import org.eclipse.jgit.api.errors.InvalidConfigurationException;
27 import org.eclipse.jgit.api.errors.InvalidRefNameException;
28 import org.eclipse.jgit.api.errors.InvalidRemoteException;
29 import org.eclipse.jgit.api.errors.NoHeadException;
30 import org.eclipse.jgit.api.errors.NoMessageException;
31 import org.eclipse.jgit.api.errors.RefAlreadyExistsException;
32 import org.eclipse.jgit.api.errors.RefNotAdvertisedException;
33 import org.eclipse.jgit.api.errors.RefNotFoundException;
34 import org.eclipse.jgit.api.errors.TransportException;
35 import org.eclipse.jgit.api.errors.UnmergedPathsException;
36 import org.eclipse.jgit.api.errors.WrongRepositoryStateException;
37 import org.eclipse.jgit.internal.storage.file.FileRepository;
38 import org.eclipse.jgit.lib.Config;
39 import org.eclipse.jgit.lib.Constants;
40 import org.eclipse.jgit.lib.ProgressMonitor;
41 import org.eclipse.jgit.lib.Ref;
42 import org.eclipse.jgit.lib.Repository;
43 import org.eclipse.jgit.revwalk.RevCommit;
44 import org.eclipse.jgit.transport.RefSpec;
45 import org.jsoup.Jsoup;
46 import org.jsoup.nodes.Attributes;
47 import org.jsoup.nodes.Document;
48 import org.jsoup.nodes.Element;
49 import org.jsoup.select.Elements;
50 import org.kohsuke.github.GHCreateRepositoryBuilder;
51 import org.kohsuke.github.GHGist;
52 import org.kohsuke.github.GHRepository;
53 import org.kohsuke.github.GitHub;
54 import java.io.BufferedReader;
55 import java.io.File;
56 import java.io.IOException;
57 import java.io.InputStreamReader;
58 import java.io.OutputStream;
59 import java.io.StringWriter;
60 import java.net.URL;
61 import java.net.URLConnection;
62 import java.nio.charset.StandardCharsets;
63 import java.nio.file.Files;
64 import java.nio.file.Paths;
65 import java.text.DecimalFormat;
66 import java.util.ArrayList;
67 import java.util.Collection;
68 import java.util.Collections;
69 import java.util.Comparator;
70 import java.util.Date;
71 import java.util.HashMap;
72 import java.util.Iterator;
73 import java.util.List;
74 import java.util.Map;
75 import java.util.Scanner;
76 import java.util.Set;
77 
78 import javax.swing.filechooser.FileSystemView;
79 import javax.xml.transform.Transformer;
80 import javax.xml.transform.TransformerConfigurationException;
81 import javax.xml.transform.TransformerException;
82 import javax.xml.transform.TransformerFactory;
83 import javax.xml.transform.dom.DOMSource;
84 import javax.xml.transform.stream.StreamResult;
85 
86 public class ScriptingEngine {// this subclasses boarder pane for the widgets
87  // sake, because multiple inheritance is TOO
88  // hard for java...
89  private static final int TIME_TO_WAIT_BETWEEN_GIT_PULL = 100000;
94  private static final ArrayList<GitLogProgressMonitor> logListeners = new ArrayList<>();
95 
96  private static boolean autoupdate = false;
97 
98  private static final String[] imports = new String[] { // "haar",
99  "java.nio.file", "java.util", "java.awt.image", "javafx.scene.text", "javafx.scene", "javafx.scene.control",
100  "eu.mihosoft.vrl.v3d", "eu.mihosoft.vrl.v3d.svg", "eu.mihosoft.vrl.v3d.samples",
101  "eu.mihosoft.vrl.v3d.parametrics", "com.neuronrobotics.imageprovider",
102  "com.neuronrobotics.sdk.addons.kinematics.xml", "com.neuronrobotics.sdk.addons.kinematics",
103  "com.neuronrobotics.sdk.dyio.peripherals", "com.neuronrobotics.sdk.dyio", "com.neuronrobotics.sdk.common",
104  "com.neuronrobotics.sdk.ui", "com.neuronrobotics.sdk.util", "com.neuronrobotics.sdk.serial",
105  "com.neuronrobotics.sdk.addons.kinematics", "com.neuronrobotics.sdk.addons.kinematics.math",
106  "com.neuronrobotics.sdk.addons.kinematics.gui", "com.neuronrobotics.sdk.config",
107  "com.neuronrobotics.bowlerkernel", "com.neuronrobotics.bowlerstudio",
108  "com.neuronrobotics.bowlerstudio.scripting", "com.neuronrobotics.bowlerstudio.tabs",
109  "com.neuronrobotics.bowlerstudio.physics", "com.neuronrobotics.bowlerstudio.physics",
110  "com.neuronrobotics.bowlerstudio.vitamins", "com.neuronrobotics.bowlerstudio.creature",
111  "com.neuronrobotics.bowlerstudio.threed" };
112 
113  private static HashMap<String, File> filesRun = new HashMap<>();
114 
115  // private static GHGist gist;
116 
117  private static File workspace;
118  private static File appdata;
119  private static File lastFile;
120  private static TransportConfigCallback transportConfigCallback = new SshTransportConfigCallback();
121 
122  // UsernamePasswordCredentialsProvider(name,
123  // password);
124  private static ArrayList<IGithubLoginListener> loginListeners = new ArrayList<IGithubLoginListener>();
125 
126  private static HashMap<String, IScriptingLanguage> langauges = new HashMap<>();
127  private static HashMap<String, ArrayList<Runnable>> onCommitEventListeners = new HashMap<>();
128  // static IssueReportingExceptionHandler exp = new
129  // IssueReportingExceptionHandler();
130  static HashMap<Git, GitTimeoutThread> gitOpenTimeout = new HashMap<>();
131  static {
132 
134 
141  // addScriptingLanguage(new KotlinHelper());
145  }
146 
147  public static void setWorkspace(File file) {
148  workspace = file;
149  System.err.println("Workspace: " + workspace.getAbsolutePath());
150  if (!workspace.exists()) {
151  workspace.mkdir();
152  }
153  appdata = new File(file.getAbsolutePath() + "/appdata");
154  if (!appdata.exists()) {
155  appdata.mkdir();
156  }
157  File oldpass = new File(System.getProperty("user.home") + "/.github");
158  if (oldpass.exists())
159  oldpass.delete();
160  try {
162  } catch (Exception e) {
163  // exp.uncaughtException(Thread.currentThread(), e);
164  throw new RuntimeException(e);
165  }
166  }
167 
168  public static void addLogListener(GitLogProgressMonitor l) {
169  if (logListeners.contains(l))
170  return;
171  logListeners.add(l);
172  }
173 
174  public static void removeLogListener(GitLogProgressMonitor l) {
175  if (!logListeners.contains(l))
176  return;
177  logListeners.remove(l);
178  }
179 
180  public static void clearLogListener() {
181 
182  logListeners.clear();
183  }
184 
185  private static Git cloneRepoLocalSelectAuth(String remoteURI, File dir, boolean useSSH)
186  throws InvalidRemoteException, TransportException, GitAPIException {
187  CloneCommand setURI = Git.cloneRepository().setURI(remoteURI);
188 
189  setURI.setProgressMonitor(getProgressMoniter("Cloning ", remoteURI));
190  setURI.setDirectory(dir);
191 
192  if (useSSH) {
193  setURI.setTransportConfigCallback(transportConfigCallback);
194  } else {
195  setURI.setCredentialsProvider(PasswordManager.getCredentialProvider());
196  }
197 
198  Git git = setURI.call();
199  gitOpenTimeout.put(git, makeTimeoutThread(git));
200  return git;
201  }
202 
214  private static Git cloneRepoLocal(String remoteURI, File dir)
215  throws InvalidRemoteException, TransportException, GitAPIException {
216  boolean startsWith = remoteURI.startsWith("git@");
217 
218  try {
219  return cloneRepoLocalSelectAuth(remoteURI, dir, startsWith);
220  } catch (org.eclipse.jgit.api.errors.JGitInternalException ex) {
221  if (ex.getMessage().contains("already exists and is not an empty directory")) {
222  deleteRepo(remoteURI);
223  return cloneRepoLocal(remoteURI, dir);
224  }
225  throw ex;
226  } catch (org.eclipse.jgit.api.errors.TransportException ex) {
227  if (ex.getMessage().contains("Auth fail") && !startsWith) {
228  return cloneRepoLocalSelectAuth(remoteURI, dir, true);
229  }
230  throw ex;
231  }
232  }
233 
234  private static ProgressMonitor getProgressMoniter(String type, String remoteURI) {
235  String reponame = getRepositoryCloneDirectory(remoteURI).getName();
236 
237  RuntimeException e = new RuntimeException();
238  return new ProgressMonitor() {
239  double total = 1;
240  double sum;
241  double tasks = 0;
242  String stage = "done";
243  long timeofLastUpdate = 0;
244 
245  @Override
246  public void update(int completed) {
247  for (Iterator<Git> iterator = gitOpenTimeout.keySet().iterator(); iterator.hasNext();) {
248  Git g = iterator.next();
249  GitTimeoutThread t = gitOpenTimeout.get(g);
250  if (t.ref.toLowerCase().contentEquals(remoteURI.toLowerCase())) {
251  t.resetTimer();
252  break;
253  }
254  }
255 
256  sum += completed;
257  DecimalFormat df = new DecimalFormat("###.#");
258  String format = df.format(total > 0 ? ((sum) / total * 100) : 0);
259  if (!format.contains("."))
260  format = format + ".0";// keep spacing consistant
261  while (format.length() < 5) {
262  format = " " + format;// pad out the string for formatting
263  }
264  String str = format + "% " + stage + " " + reponame + " " + tasks + " of task " + type;
265  if (timeofLastUpdate + 500 < System.currentTimeMillis()) {
266  System.out.println(str);
267  timeofLastUpdate = System.currentTimeMillis();
268  }
269  // System.err.println(str);
270 
272  l.onUpdate(str, e);
273  }
274  }
275 
276  @Override
277  public void start(int totalTasks) {
278 
279  }
280 
281  @Override
282  public boolean isCancelled() {
283  return false;
284  }
285 
286  @Override
287  public void endTask() {
288  String string = "100% " + stage + " " + reponame + " " + type;
289  System.out.println(string);
291  l.onUpdate(string, e);
292  }
293  }
294 
295  @Override
296  public void beginTask(String title, int totalWork) {
297  stage = title;
298  // System.out.println("Setting totalWork to "+totalWork+" for stage "+stage);
299  total = totalWork;
300  sum = 0;
301  tasks += 1;
302  timeofLastUpdate = 0;
303  }
304  };
305  }
306 
313  public static Git openGit(String url) {
314  Repository localRepo;
315  try {
316  localRepo = getRepository(url);
317  return openGit(localRepo);
318  } catch (IOException e) {
319  // TODO Auto-generated catch block
320  e.printStackTrace();
321  }
322  throw new RuntimeException("IOException making repo");
323  }
324 
325  public static boolean isUrlAlreadyOpen(String URL) {
326  if (URL == null)
327  return false;
328  for (Iterator<Git> iterator = gitOpenTimeout.keySet().iterator(); iterator.hasNext();) {
329  Git g = iterator.next();
330  GitTimeoutThread t = gitOpenTimeout.get(g);
331  if (t.ref.toLowerCase().contentEquals(URL.toLowerCase())) {
332  // t.getException().printStackTrace(System.err);
333  return true;
334  }
335  }
336  return false;
337  }
338 
346  public static Git openGit(Repository localRepo) {
347 
348  for (Iterator<Git> iterator = gitOpenTimeout.keySet().iterator(); iterator.hasNext();) {
349  Git g = iterator.next();
350  if (g.getRepository().getDirectory().getAbsolutePath()
351  .contentEquals(localRepo.getDirectory().getAbsolutePath())) {
352  GitTimeoutThread t = gitOpenTimeout.get(g);
353  int i = 0;
354  while (gitOpenTimeout.containsKey(g)) {
355 
356  System.out.println(
357  "Git is locked by other process, blocking " + localRepo.getDirectory().getAbsolutePath());
358  System.out.println("Git locked " + t.ref);
359  if (i > 3) {
360  t.getException().printStackTrace(System.out);
361  System.out.println("Blocking process: ");
362 
363  new Exception().printStackTrace(System.out);
364  }
365  i++;
366  ThreadUtil.wait(1000);
367  }
368  break;
369  }
370  }
371 
372  Git git = new Git(localRepo);
373 
374  gitOpenTimeout.put(git, makeTimeoutThread(git));
375  return git;
376  }
377 
383  public static void closeGit(Git git) {
384  if (git == null)
385  return;
386  if (gitOpenTimeout.containsKey(git)) {
387  Thread thread = gitOpenTimeout.remove(git);
388  if (thread != null) {
389  thread.interrupt();
390  } else {
391  new IssueReportingExceptionHandler().uncaughtException(Thread.currentThread(),
392  new RuntimeException("Closing a git object that was not opened with a timeout!"));
393  }
394  }
395  git.getRepository().close();
396  git.close();
397  }
398 
405  private static GitTimeoutThread makeTimeoutThread(Git git) {
406 
407  GitTimeoutThread thread = new GitTimeoutThread(git);
408  thread.start();
409  return thread;
410  }
411 
412  public static void addOnCommitEventListeners(String url, Runnable event) {
413  synchronized (onCommitEventListeners) {
414  if (!onCommitEventListeners.containsKey(url)) {
415  onCommitEventListeners.put(url, new ArrayList<Runnable>());
416  }
417  if (!onCommitEventListeners.get(url).contains(event)) {
418  onCommitEventListeners.get(url).add(event);
419  }
420  }
421  }
422 
423  public static void removeOnCommitEventListeners(String url, Runnable event) {
424  synchronized (onCommitEventListeners) {
425 
426  if (!onCommitEventListeners.containsKey(url)) {
427  onCommitEventListeners.put(url, new ArrayList<Runnable>());
428  }
429  if (onCommitEventListeners.get(url).contains(event)) {
430  onCommitEventListeners.get(url).remove(event);
431  }
432  }
433  }
434 
442  public static Object inlineScriptRun(File code, ArrayList<Object> args, String shellTypeStorage) throws Exception {
443  if (filesRun.get(code.getName()) == null) {
444  filesRun.put(code.getName(), code);
445  // System.out.println("Loading "+code.getAbsolutePath());
446  }
447 
448  if (langauges.get(shellTypeStorage) != null) {
449  return langauges.get(shellTypeStorage).inlineScriptRun(code, args);
450  }
451  return null;
452  }
453 
461  public static Object inlineScriptStringRun(String line, ArrayList<Object> args, String shellTypeStorage)
462  throws Exception {
463 
464  if (langauges.get(shellTypeStorage) != null) {
465  return langauges.get(shellTypeStorage).inlineScriptRun(line, args);
466  }
467  return null;
468  }
469 
470  public static void addScriptingLanguage(IScriptingLanguage lang) {
471  langauges.put(lang.getShellType(), lang);
472  }
473 
475  if (!loginListeners.contains(l)) {
476  loginListeners.add(l);
477  }
478  }
479 
481  if (loginListeners.contains(l)) {
482  loginListeners.remove(l);
483  }
484  }
485 
486  public static File getWorkspace() {
487  if (workspace == null) {
488  String relative = FileSystemView.getFileSystemView().getDefaultDirectory().getPath();
489  // https://github.com/CommonWealthRobotics/BowlerStudio/issues/378
490  if (OSUtil.isOSX() || OSUtil.isLinux())
491  if (!relative.endsWith("Documents")) {
492  relative = relative + "/Documents";
493  }
494  if (OSUtil.isWindows()) {
495  if (!relative.endsWith("Documents")) {
496  relative = relative + "\\Documents";
497  }
498  }
499 
500  File file = new File(relative + "/bowler-workspace/");
501  file.mkdirs();
502  setWorkspace(file);
503  }
504  return workspace;
505  }
506 
507  public static String getShellType(String name) {
508  for (IScriptingLanguage l : langauges.values()) {
509  if (l.isSupportedFileExtenetion(name))
510  return l.getShellType();
511  }
512 
513  return "Groovy";
514  }
515 
516  public static void login() throws IOException {
518  return;
522  l.onLogin(PasswordManager.getUsername());
523  }
524  }
525 
526  public static void logout() throws IOException {
527 
529  l.onLogout(PasswordManager.getUsername());
530  }
532  }
533 
534  public static GitHub setupAnyonmous() throws IOException {
537  }
538 
539  public static String urlToGist(String in) {
540 
541  if (in.endsWith(".git")) {
542  in = in.substring(0, in.lastIndexOf('.'));
543  }
544  String domain = in.split("//")[1];
545  String[] tokens = domain.split("/");
546  if (tokens[0].toLowerCase().contains("gist.github.com") && tokens.length >= 2) {
547  try {
548  String id = tokens[2].split("#")[0];
549  Log.debug("Gist URL Detected " + id);
550  return id;
551  } catch (ArrayIndexOutOfBoundsException e) {
552  try {
553  String id = tokens[1].split("#")[0];
554  Log.debug("Gist URL Detected " + id);
555  return id;
556  } catch (ArrayIndexOutOfBoundsException ex) {
557  throw new RuntimeException(e);
558  }
559  }
560  }
561 
562  return null;
563  }
564 
565  private static List<String> returnFirstGist(String html) {
566  // Log.debug(html);
567  ArrayList<String> ret = new ArrayList<>();
568  Document doc = Jsoup.parse(html);
569  Elements links = doc.select("script");
570  for (int i = 0; i < links.size(); i++) {
571  Element e = links.get(i);
573  Attributes n = e.attributes();
574  String jSSource = n.get("src");
575  if (jSSource.contains("https://gist.github.com/")) {
576  // System.out.println("Source = "+jSSource);
577  String slug = jSSource;
578  String js = slug.split(".js")[0];
579  String[] id = js.split("/");
580  ret.add(id[id.length - 1]);
581  }
582  }
583  return ret;
584  }
585 
586  public static List<String> getCurrentGist(String addr, Object engine) {
587  if (!javafx.scene.web.WebEngine.class.isInstance(engine))
588  throw new RuntimeException("Engine must be of type javafx.scene.web.WebEngine");
589  String gist = urlToGist(addr);
590 
591  if (gist == null) {
592  try {
593  Log.debug("Non Gist URL Detected");
594  String html;
595  TransformerFactory tf = TransformerFactory.newInstance();
596  Transformer t = tf.newTransformer();
597  StringWriter sw = new StringWriter();
598  t.transform(new DOMSource(((javafx.scene.web.WebEngine) engine).getDocument()), new StreamResult(sw));
599  html = sw.getBuffer().toString();
600  return returnFirstGist(html);
601  } catch (TransformerConfigurationException e) {
602  throw new RuntimeException(e);
603  } catch (TransformerException e) {
604  throw new RuntimeException(e);
605  }
606 
607  }
608  ArrayList<String> ret = new ArrayList<>();
609  ret.add(gist);
610  return ret;
611  }
612 
616  public static void waitForLogin() throws IOException, InvalidRemoteException, TransportException, GitAPIException {
617  if (!PasswordManager.hasNetwork()) {
618  System.err.println("No network, cant log in");
619  return;
620  }
621  try {
624  return;
625  if (PasswordManager.getLoginID() == null) {
626  System.err.println("No login ID found!");
627  return;
628  }
629  if (PasswordManager.getPassword() == null) {
630  System.err.println("No login api key found!");
631  return;
632  }
633  System.err.println("Performing Login");
635 
636  if (!PasswordManager.loggedIn()) {
637  System.err.println("\nERROR: Wrong Password!\n");
638  login();
639  }
640 
641  } catch (Exception e) {
642  throw new RuntimeException(e);
643  }
644 
645  }
646 
647  public static void waitForRepo(String remoteURI, String reason) {
648  while (ScriptingEngine.isUrlAlreadyOpen(remoteURI)) {
649  ThreadUtil.wait(500);
650  for (Iterator<Git> iterator = gitOpenTimeout.keySet().iterator(); iterator.hasNext();) {
651  Git g = iterator.next();
652  GitTimeoutThread t = gitOpenTimeout.get(g);
653  if (t.ref.toLowerCase().contentEquals(remoteURI.toLowerCase())) {
654 
655  System.err.println("\n\n\nPaused " + reason + " by another thread, waiting for repo " + remoteURI);
656  new Exception().printStackTrace(System.err);
657  System.err.println("Paused by:");
658  t.getException().printStackTrace(System.err);
659  System.err.println("\n\n\n");
660 
661  }
662  }
663  }
664  }
665 
666  public static void deleteRepo(String remoteURI) {
667  if (remoteURI == null)
668  return;
669  if (remoteURI.length() < 4)
670  return;
671  waitForRepo(remoteURI, "delete");
672 
673  new Exception("\n\nDelete " + remoteURI + "called Here\n").printStackTrace(System.out);
674  File gitRepoFile = uriToFile(remoteURI);
675  deleteFolder(gitRepoFile.getParentFile());
676  }
677 
678  public static void deleteCache() {
679  deleteFolder(new File(getWorkspace().getAbsolutePath() + "/gitcache/"));
680  }
681 
682  private static void deleteFolder(File folder) {
683 
684  if (!folder.exists() || !folder.isDirectory())
685  return;
686  File[] files = folder.listFiles();
687  if (files != null) { // some JVMs return null for empty dirs
688  for (File f : files) {
689  if (f.isDirectory()) {
690  deleteFolder(f);
691  } else {
692  try {
695  System.out.println("Deleting File " + f.getAbsolutePath());
696  if (!f.delete()) {
697  System.err.println("File failed to delete! " + f);
698  }
699  } catch (Throwable t) {
700  t.printStackTrace();
701  }
702  // System.out.println("Deleting " + f.getAbsolutePath());
703  }
704  }
705  }
706  try {
707  System.out.println("Deleting Folder " + folder.getAbsolutePath());
708  folder.delete();
709  } catch (Throwable t) {
710  t.printStackTrace();
711  }
712  if (folder.exists()) {
713  System.err.println("Folder failed to delete! " + folder);
714  deleteFolder(folder);
715  }
716 
717  }
718 
719  private static void loadFilesToList(ArrayList<String> f, File directory, String extnetion) {
720  loadFilesToList(f, directory, extnetion);
721  }
722 
723  private static void loadFilesToList(ArrayList<String> f, File directory, String extnetion, Git ref) {
724  if (directory == null)
725  return;
726  for (final File fileEntry : directory.listFiles()) {
727 
728  if (fileEntry.getName().endsWith(".git") || fileEntry.getName().startsWith(".git"))
729  continue;// ignore git files
730  if (extnetion != null)
731  if (extnetion.length() > 0)
732  if (!fileEntry.getName().endsWith(extnetion))
733  continue;// skip this file as it fails the filter
734  // from the user
735  if (fileEntry.isDirectory()) {
736  loadFilesToList(f, fileEntry, extnetion, ref);
737  } else {
738 
739  for (IScriptingLanguage l : langauges.values()) {
740  if (l.isSupportedFileExtenetion(fileEntry.getName())) {
741  f.add(findLocalPath(fileEntry, ref));
742  break;
743  }
744  }
745 
746  }
747  }
748  }
749 
750  public static ArrayList<String> filesInGit(String remote, String branch, String extnetion) throws Exception {
751  return filesInGit(remote, branch, extnetion, null);
752  }
753 
754  public static ArrayList<String> filesInGit(String remote, String branch, String extnetion, Git ref)
755  throws Exception {
756  ArrayList<String> f = new ArrayList<>();
757 
758  // waitForLogin();
759  File gistDir = cloneRepo(remote, branch);
760  loadFilesToList(f, gistDir, extnetion, ref);
761 
762  return f;
763 
764  }
765 
766  public static ArrayList<String> filesInGit(String remote) throws Exception {
767  return filesInGit(remote, null, null, null);
768  }
769 
770  public static ArrayList<String> filesInGit(String remote, Git ref) throws Exception {
771  return filesInGit(remote, null, null, ref);
772  }
773 
774  public static String getUserIdOfGist(String id) throws Exception {
775 
776  waitForLogin();
777  Log.debug("Loading Gist: " + id);
778  GHGist gist;
779 
780  gist = PasswordManager.getGithub().getGist(id);
781  return gist.getOwner().getLogin();
782 
783  }
784 
785  public static File createFile(String git, String fileName, String commitMessage) throws Exception {
786  pushCodeToGit(git, ScriptingEngine.getFullBranch(git), fileName, null, commitMessage);
787  return fileFromGit(git, fileName);
788  }
789 
790  public static void pushCodeToGit(String id, String branch, String FileName, String content, String commitMessage)
791  throws Exception {
792  if (PasswordManager.getUsername() == null)
793  login();
794  if (!hasNetwork())
795  return;// No login info means there is no way to publish
796  File gistDir = cloneRepo(id, branch);
797  File desired = new File(gistDir.getAbsoluteFile() + "/" + FileName);
798 
799  boolean flagNewFile = ensureExistance(desired);
800  pushCodeToGit(id, branch, FileName, content, commitMessage, flagNewFile);
801  }
802 
803  private static boolean ensureExistance(File desired) throws IOException {
804  boolean createdFlag = false;
805  File parent = desired.getParentFile();
806  if (!parent.exists()) {
807  parent.mkdirs();
808  System.err.println("Creating " + parent.getAbsolutePath());
809  }
810  if (!desired.exists() && parent.exists()) {
811  System.err.println("Creating " + desired.getAbsolutePath());
812  desired.createNewFile();
813  createdFlag = true;
814  }
815  return createdFlag;
816  }
817 
818  public static void commit(String id, String branch, String FileName, String content, String commitMessage,
819  boolean flagNewFile) throws Exception {
820  commit(id, branch, FileName, content, commitMessage, flagNewFile, null);
821  }
822 
823  @SuppressWarnings("deprecation")
824  public static void commit(String id, String branch, String FileName, String content, String commitMessage,
825  boolean flagNewFile, Git gitRef) throws Exception {
826 
827  if (PasswordManager.getUsername() == null)
828  login();
829  if (!hasNetwork())
830  return;// No login info means there is no way to publish
831  if (gitRef == null)
832  waitForRepo(id, "commit");
833  File gistDir = cloneRepo(id, branch);
834  File desired = new File(gistDir.getAbsoluteFile() + "/" + FileName);
835 
836  String localPath = gistDir.getAbsolutePath();
837  File gitRepoFile = new File(localPath + "/.git");
838 
839  Repository localRepo = new FileRepository(gitRepoFile.getAbsoluteFile());
840  Git git = gitRef;
841  if (git == null)
842  git = openGit(localRepo);
843  try { // latest version
844  if (flagNewFile) {
845  git.add().addFilepattern(FileName).call();
846  }
847  if (gitRef == null)
848  closeGit(git);
849  if (content != null) {
850  OutputStream out = null;
851  try {
852  out = FileUtils.openOutputStream(desired, false);
853  IOUtils.write(content, out);
854  out.close(); // don't swallow close Exception if copy
855  // completes
856  // normally
857  } finally {
858  IOUtils.closeQuietly(out);
859  }
860  }
861 
862  commit(id, branch, commitMessage, gitRef);
863  } catch (Exception ex) {
864  if (gitRef == null)
865  closeGit(git);
866 
867  throw ex;
868  }
869  if (gitRef == null)
870  closeGit(git);
871  try {
872  if (!desired.getName().contentEquals("csgDatabase.json")) {
873  String[] gitID = ScriptingEngine.findGitTagFromFile(desired, gitRef);
874  String remoteURI = gitID[0];
875  ArrayList<String> f = ScriptingEngine.filesInGit(remoteURI, gitRef);
876  for (String s : f) {
877  if (s.contentEquals("csgDatabase.json")) {
878 
879  File dbFile = ScriptingEngine.fileFromGit(gitID[0], s);
880  if (!CSGDatabase.getDbFile().equals(dbFile))
881  CSGDatabase.setDbFile(dbFile);
883  @SuppressWarnings("resource")
884  String c = new Scanner(dbFile).useDelimiter("\\Z").next();
885  ScriptingEngine.commit(remoteURI, branch, s, c, "saving CSG database", false, gitRef);
886  }
887  }
888  }
889  } catch (Exception e) {
890  // ignore CSG database
891  e.printStackTrace();
892  }
893  }
894 
895  @SuppressWarnings("deprecation")
896  public static void pushCodeToGit(String remoteURI, String branch, String FileName, String content,
897  String commitMessage, boolean flagNewFile) throws Exception {
898  waitForRepo(remoteURI, "push");
899  commit(remoteURI, branch, FileName, content, commitMessage, flagNewFile);
900  if (PasswordManager.getUsername() == null)
901  login();
902  if (!hasNetwork())
903  return;// No login info means there is no way to publish
904  File gistDir = cloneRepo(remoteURI, branch);
905  File desired = new File(gistDir.getAbsoluteFile() + "/" + FileName);
906 
907  if (!PasswordManager.hasNetwork() && content != null) {
908  OutputStream out = null;
909  try {
910  out = FileUtils.openOutputStream(desired, false);
911  IOUtils.write(content, out);
912  out.close(); // don't swallow close Exception if copy completes
913  // normally
914  } finally {
915  IOUtils.closeQuietly(out);
916  }
917  return;
918  }
919 
920  waitForLogin();
921  String localPath = gistDir.getAbsolutePath();
922  File gitRepoFile = new File(localPath + "/.git");
923 
924  Repository localRepo = new FileRepository(gitRepoFile.getAbsoluteFile());
925  Git git = null;
926  try {
927  try {
928  pull(remoteURI, branch);
929  } catch (java.lang.RuntimeException exp) {
930  }
931  git = openGit(localRepo);
932  // latest version
933  if (flagNewFile) {
934  git.add().addFilepattern(FileName).call();
935  }
936  if (content != null) {
937  OutputStream out = null;
938  try {
939  out = FileUtils.openOutputStream(desired, false);
940  IOUtils.write(content, out);
941  out.close(); // don't swallow close Exception if copy
942  // completes
943  // normally
944  } finally {
945  IOUtils.closeQuietly(out);
946  }
947  }
948  if (git.getRepository().getConfig().getString("remote", "origin", "url").startsWith("git@"))
949  git.push().setTransportConfigCallback(transportConfigCallback)
950  .setProgressMonitor(getProgressMoniter("Pushing ", remoteURI)).call();
951  else
952  git.push().setCredentialsProvider(PasswordManager.getCredentialProvider())
953  .setProgressMonitor(getProgressMoniter("Pushing ", remoteURI)).call();
954  closeGit(git);
955  System.out.println("PUSH OK! file: " + desired + " on branch " + getBranch(remoteURI));
956  } catch (Exception ex) {
957  ex.printStackTrace();
958  closeGit(git);
959  String[] gitID = ScriptingEngine.findGitTagFromFile(desired);
960  String id = gitID[0];
961 
962  throw ex;
963  }
964 
965  }
966 
967  public static String[] codeFromGit(String id, String FileName) throws Exception {
968 
969  File targetFile = fileFromGit(id, FileName);
970  if (targetFile.exists()) {
971  // System.err.println("Loading file:
972  // "+targetFile.getAbsoluteFile());
973  // Target file is ready to go
974  String text = new String(Files.readAllBytes(Paths.get(targetFile.getAbsolutePath())),
975  StandardCharsets.UTF_8);
976  return new String[] { text, FileName, targetFile.getAbsolutePath() };
977  }
978 
979  return null;
980  }
981 
982  private static String[] codeFromGistID(String id, String FileName) throws Exception {
983  String giturl = "https://gist.github.com/" + id + ".git";
984 
985  File targetFile = fileFromGit(giturl, FileName);
986  if (targetFile.exists()) {
987  System.err.println("Gist at GIT : " + giturl);
988  // Target file is ready to go
989  String text = new String(Files.readAllBytes(Paths.get(targetFile.getAbsolutePath())),
990  StandardCharsets.UTF_8);
991  return new String[] { text, FileName, targetFile.getAbsolutePath() };
992  }
993 
994  return null;
995  }
996 
997  public static Object inlineFileScriptRun(File f, ArrayList<Object> args) throws Exception {
998 
999  return inlineScriptRun(f, args, getShellType(f.getName()));
1000  }
1001 
1002  public static Object inlineGistScriptRun(String gistID, String Filename, ArrayList<Object> args) throws Exception {
1003  String[] gistData = codeFromGistID(gistID, Filename);
1004  return inlineScriptRun(new File(gistData[2]), args, getShellType(gistData[1]));
1005  }
1006 
1007  public static Object gitScriptRun(String gitURL, String Filename) throws Exception {
1008  return gitScriptRun(gitURL, Filename, null);
1009  }
1010 
1011  public static Object gitScriptRun(String gitURL, String Filename, ArrayList<Object> args) throws Exception {
1012  String[] gistData = codeFromGit(gitURL, Filename);
1013  return inlineScriptRun(new File(gistData[2]), args, getShellType(gistData[1]));
1014  }
1015 
1016  public static File fileFromGit(String remoteURI, String fileInRepo)
1017  throws InvalidRemoteException, TransportException, GitAPIException, IOException {
1018  return fileFromGit(remoteURI, null, fileInRepo);
1019  }
1020 
1021  // git@github.com:CommonWealthRobotics/BowlerStudioVitamins.git
1022  // or
1023  // https://github.com/CommonWealthRobotics/BowlerStudioVitamins.git
1024  public static File fileFromGit(String remoteURI, String branch, String fileInRepo)
1025  throws InvalidRemoteException, TransportException, GitAPIException, IOException {
1026  File gitRepoFile = cloneRepo(remoteURI, branch);
1027  return new File(gitRepoFile.getAbsolutePath() + "/" + fileInRepo);
1028  }
1029 
1030  public static File uriToFile(String remoteURI) {
1031  // new Exception().printStackTrace();
1032  String[] colinSplit = remoteURI.split(":");
1033  try {
1034  String gitSplit = colinSplit[1].substring(0, colinSplit[1].lastIndexOf('.'));
1035  File gistDir = new File(getWorkspace().getAbsolutePath() + "/gitcache/" + gitSplit + "/.git");
1036  return gistDir;
1037  } catch (ArrayIndexOutOfBoundsException ex) {
1038  System.err.println("Failed to parse " + remoteURI);
1039  throw ex;
1040  }
1041 
1042  }
1043 
1044  public static String getBranch(String remoteURI)
1045  throws IOException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException,
1046  CheckoutConflictException, InvalidRemoteException, TransportException, GitAPIException {
1047  cloneRepo(remoteURI, null);
1048  File gitRepoFile = uriToFile(remoteURI);
1049  if (!gitRepoFile.exists()) {
1050  gitRepoFile = cloneRepo(remoteURI, null);
1051  }
1052 
1053  Repository localRepo = new FileRepository(gitRepoFile.getAbsoluteFile());
1054  String branch = localRepo.getBranch();
1055  localRepo.close();
1056  if (branch == null)
1057  throw new RuntimeException("FAULT! " + remoteURI + " has no branch!");
1058  return branch;
1059  }
1060 
1061  public static String getFullBranch(String remoteURI)
1062  throws IOException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException,
1063  CheckoutConflictException, InvalidRemoteException, TransportException, GitAPIException {
1064  cloneRepo(remoteURI, null);
1065  File gitRepoFile = uriToFile(remoteURI);
1066  if (!gitRepoFile.exists()) {
1067  gitRepoFile = cloneRepo(remoteURI, null);
1068  }
1069 
1070  Repository localRepo = new FileRepository(gitRepoFile.getAbsoluteFile());
1071  String branch = localRepo.getFullBranch();
1072  localRepo.close();
1073  if (branch == null)
1074  throw new RuntimeException("FAULT! " + remoteURI + " has no branch!");
1075 
1076  return branch;
1077  }
1078 
1079  public static void deleteBranch(String remoteURI, String toDelete) throws Exception {
1080  waitForRepo(remoteURI, "deleteBranch");
1081  boolean found = false;
1082  for (String s : listBranchNames(remoteURI)) {
1083  if (s.contains(toDelete)) {
1084  found = true;
1085  }
1086  }
1087  if (!found)
1088  throw new RuntimeException(toDelete + " can not be deleted because it does not exist");
1089 
1090  File gitRepoFile = uriToFile(remoteURI);
1091  if (!gitRepoFile.exists()) {
1092  gitRepoFile = cloneRepo(remoteURI, null);
1093  }
1094 
1095  Repository localRepo = new FileRepository(gitRepoFile.getAbsoluteFile());
1096  // CreateBranchCommand bcc = null;
1097  // CheckoutCommand checkout;
1098  // String source = getFullBranch(remoteURI);
1099 
1100  Git git;
1101 
1102  git = openGit(localRepo);
1103  if (!toDelete.contains("heads")) {
1104  toDelete = "heads/" + toDelete;
1105  }
1106  if (!toDelete.contains("refs")) {
1107  toDelete = "refs/" + toDelete;
1108  }
1109  Exception ex = null;
1110  try {
1111  // delete branch 'branchToDelete' locally
1112  git.branchDelete().setBranchNames(toDelete).call();
1113 
1114  // delete branch 'branchToDelete' on remote 'origin'
1115  RefSpec refSpec = new RefSpec().setSource(null).setDestination(toDelete);
1116  git.push().setRefSpecs(refSpec).setRemote("origin")
1117  .setCredentialsProvider(PasswordManager.getCredentialProvider())
1118  .setProgressMonitor(getProgressMoniter("Pushing ", remoteURI)).call();
1119  } catch (Exception e) {
1120  ex = e;
1121  }
1122  closeGit(git);
1123  if (ex != null)
1124  throw ex;
1125  }
1126 
1127  public static String newBranch(String remoteURI, String newBranch)
1128  throws IOException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException,
1129  CheckoutConflictException, InvalidRemoteException, TransportException, GitAPIException {
1130  newBranch(remoteURI, newBranch, null);
1131  return getFullBranch(remoteURI);
1132  }
1133 
1134  public static void newBranch(String remoteURI, String newBranch, RevCommit source)
1135  throws IOException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException,
1136  CheckoutConflictException, InvalidRemoteException, TransportException, GitAPIException {
1137  waitForRepo(remoteURI, "newBranch");
1138  Repository localRepo = getRepository(remoteURI);
1139 
1140  Git git = null;
1141  try {
1142  for (String s : listBranchNames(remoteURI)) {
1143  if (s.contains(newBranch)) {
1144  // throw new RuntimeException(newBranch + " can not be created because " + s + "
1145  // is too similar");
1146 
1147  git = openGit(localRepo);
1148  shallowCheckout(remoteURI, newBranch, git);
1149  closeGit(git);
1150  return;
1151 
1152  }
1153  }
1154  } catch (Exception e) {
1155  e.printStackTrace();
1156  closeGit(git);
1157  }
1158 
1159  git = openGit(localRepo);
1160  try {
1161  try {
1162  if (source == null)
1163  source = git.log().setMaxCount(1).call().iterator().next();
1164  newBranchLocal(newBranch, remoteURI, git, source);
1165  } catch (NoHeadException ex) {
1166  newBranchLocal(newBranch, remoteURI, git, null);
1167  }
1168  } catch (Throwable ex) {
1169  closeGit(git);
1170  throw ex;
1171  }
1172 
1173  closeGit(git);
1174 
1175  }
1176 
1177  private static void newBranchLocal(String newBranch, String remoteURI, Git git, RevCommit source)
1178  throws GitAPIException, RefNotFoundException, InvalidRefNameException, CheckoutConflictException,
1179  InvalidRemoteException, TransportException, IOException {
1180  try {
1181  CreateBranchCommand setName = git.branchCreate().setName(newBranch);
1182  if (source != null)
1183  setName = setName.setStartPoint(source);
1184  else {
1185  Ref ref = git.getRepository().findRef(Constants.HEAD);
1186  setName = setName.setStartPoint(ref.getName());
1187  setName.setForce(true);
1188  }
1189  setName.call();
1190  System.out.println("Created new branch " + remoteURI + "\t\t" + newBranch);
1191  } catch (org.eclipse.jgit.api.errors.RefNotFoundException ex) {
1192  System.err.println("ERROR Creating " + newBranch + " in " + remoteURI);
1193  ex.printStackTrace();
1194  } catch (org.eclipse.jgit.api.errors.RefAlreadyExistsException ex) {
1195  // just checkout the existing branch then
1196  }
1197  git.checkout().setName(newBranch).call();
1198  if (PasswordManager.loggedIn())
1199  git.push().setRemote(remoteURI).setRefSpecs(new RefSpec(newBranch + ":" + newBranch))
1200  .setCredentialsProvider(PasswordManager.getCredentialProvider())
1201  .setProgressMonitor(getProgressMoniter("Pushing ", remoteURI)).call();
1202  }
1203 
1204  @SuppressWarnings("deprecation")
1205  private static boolean hasAtLeastOneReference(Git git) throws Exception {
1206  Repository repo = git.getRepository();
1207  Config storedConfig = repo.getConfig();
1208  Set<String> uriList = repo.getConfig().getSubsections("remote");
1209  String remoteURI = null;
1210  for (String remoteName : uriList) {
1211  if (remoteURI == null)
1212  remoteURI = storedConfig.getString("remote", remoteName, "url");
1213  ;
1214  }
1215  long startTime = System.currentTimeMillis();
1216  while (System.currentTimeMillis() < (startTime + 2000)) {
1217  for (Ref ref : repo.getAllRefs().values()) {
1218  if (ref.getObjectId() != null) {
1219  List<Ref> branchList = listBranches(remoteURI, git);
1220  if (branchList.size() > 0) {
1221  // System.out.println("Found "+branchList.size()+"
1222  // branches");
1223  return true;
1224  }
1225  }
1226  }
1227  }
1228 
1229  return true;
1230  }
1231 
1232  public static List<Ref> listBranches(String remoteURI) throws Exception {
1233 
1234  File gitRepoFile = uriToFile(remoteURI);
1235  if (!gitRepoFile.exists()) {
1236  gitRepoFile = cloneRepo(remoteURI, null);
1237  return listBranches(remoteURI);
1238  }
1239 
1240  Repository localRepo = new FileRepository(gitRepoFile.getAbsoluteFile());
1241  // https://gist.github.com/0e6454891a3b3f7c8f28.git
1242  List<Ref> Ret;
1243  Git git = openGit(localRepo);
1244  Ret = listBranches(remoteURI, git);
1245  closeGit(git);
1246  return Ret;
1247  }
1248 
1249  public static List<Ref> listBranches(String remoteURI, Git git) throws Exception {
1250 
1251  // https://gist.github.com/0e6454891a3b3f7c8f28.git
1252  // System.out.println("Listing references from: "+remoteURI);
1253  // System.out.println(" branch: "+getFullBranch(remoteURI));
1254  List<Ref> list = git.branchList().setListMode(ListMode.ALL).call();
1255  // System.out.println(" size : "+list.size());
1256  return list;
1257  }
1258 
1259  public static List<Ref> listLocalBranches(String remoteURI) throws IOException {
1260 
1261  File gitRepoFile = uriToFile(remoteURI);
1262  if (!gitRepoFile.exists()) {
1263  gitRepoFile = cloneRepo(remoteURI, null);
1264  }
1265 
1266  Repository localRepo = new FileRepository(gitRepoFile.getAbsoluteFile());
1267  // https://gist.github.com/0e6454891a3b3f7c8f28.git
1268  Git git = openGit(localRepo);
1269  try {
1270  List<Ref> list = git.branchList().call();
1271  closeGit(git);
1272  return list;
1273  } catch (Exception ex) {
1274 
1275  }
1276  closeGit(git);
1277  return new ArrayList<>();
1278  }
1279 
1280  public static List<String> listLocalBranchNames(String remoteURI) throws Exception {
1281  ArrayList<String> branchNames = new ArrayList<>();
1282 
1283  List<Ref> list = listLocalBranches(remoteURI);
1284  for (Ref ref : list) {
1285  // System.out.println("Branch: " + ref + " " + ref.getName() + " " +
1286  // ref.getObjectId().getName());
1287  branchNames.add(ref.getName());
1288  }
1289  return branchNames;
1290  }
1291 
1292  public static List<String> listBranchNames(String remoteURI) throws Exception {
1293  ArrayList<String> branchNames = new ArrayList<>();
1294 
1295  List<Ref> list = listBranches(remoteURI);
1296  for (Ref ref : list) {
1297  // System.out.println("Branch: " + ref + " " + ref.getName() + " " +
1298  // ref.getObjectId().getName());
1299  branchNames.add(ref.getName());
1300  }
1301  return branchNames;
1302  }
1303 
1304  public static void pull(String remoteURI, String branch) throws IOException, CheckoutConflictException,
1305  NoHeadException, InvalidRemoteException, WrongRepositoryStateException {
1306  waitForRepo(remoteURI, "pull");
1307  // new Exception().printStackTrace();
1308 
1309  if (!hasNetwork())
1310  return;
1311  File gitRepoFile = uriToFile(remoteURI);
1312  if (!gitRepoFile.exists()) {
1313  gitRepoFile = cloneRepo(remoteURI, branch);
1314  }
1315 
1316  Repository localRepo = new FileRepository(gitRepoFile.getAbsoluteFile());
1317  Git git = openGit(localRepo);
1318  try {
1319 
1320  String ref = git.getRepository().getConfig().getString("remote", "origin", "url");
1321  try {
1322 
1323  PullCommand command;
1324  if (ref != null && ref.startsWith("git@")) {
1325  command = git.pull().setTransportConfigCallback(transportConfigCallback);
1326  } else {
1327  command = git.pull().setCredentialsProvider(PasswordManager.getCredentialProvider());
1328  }
1329  command.setProgressMonitor(getProgressMoniter("Pulling ", remoteURI));
1330  try {
1331  command.call();
1332  } catch (org.eclipse.jgit.api.errors.TransportException ex) {
1333  if (ex.getMessage().contains("Auth fail")) {
1334  command = git.pull().setTransportConfigCallback(transportConfigCallback);
1335  command.call();
1336  } else
1337  throw ex;
1338 
1339  }
1340  closeGit(git);
1341  // new Exception(ref).printStackTrace();
1342  } catch (CheckoutConflictException ex) {
1343 // closeGit(git);
1344 // resolveConflict(remoteURI, ex, git);
1345 // pull(remoteURI, branch);
1346  closeGit(git);
1348  throw ex;
1349  } catch (WrongRepositoryStateException e) {
1350  e.printStackTrace();
1351  closeGit(git);
1353  // deleteRepo(remoteURI);
1354  throw e;
1355  } catch (InvalidConfigurationException e) {
1356 
1358  closeGit(git);
1359  throw new RuntimeException("remoteURI " + remoteURI + " branch " + branch + " " + e.getMessage());
1360  } catch (DetachedHeadException e) {
1362  closeGit(git);
1363  throw new RuntimeException("remoteURI " + remoteURI + " branch " + branch + " " + e.getMessage());
1364  } catch (InvalidRemoteException e) {
1366  closeGit(git);
1367  throw new InvalidRemoteException("remoteURI " + remoteURI + " branch " + branch + " " + e.getMessage());
1368  } catch (CanceledException e) {
1370  closeGit(git);
1371  throw new RuntimeException("remoteURI " + remoteURI + " branch " + branch + " " + e.getMessage());
1372  } catch (RefNotFoundException e) {
1374  closeGit(git);
1375  throw new RuntimeException("remoteURI " + remoteURI + " branch " + branch + " " + e.getMessage());
1376  } catch (RefNotAdvertisedException e) {
1378  closeGit(git);
1379  try {
1380  if (branch != null)
1381  newBranch(remoteURI, branch);
1382  else {
1383  git = openGit(remoteURI);
1384  RevCommit source = git.log().setMaxCount(1).call().iterator().next();
1385 
1386  newBranchLocal("main", remoteURI, git, source);
1387  closeGit(git);
1388  }
1389  } catch (Exception ex) {
1390  closeGit(git);
1391  ex.printStackTrace();
1392  throw new RuntimeException("remoteURI " + remoteURI + " branch " + branch + " " + ex.getMessage());
1393  }
1394  } catch (NoHeadException e) {
1395 
1397  closeGit(git);
1398  throw e;
1399 // try {
1400 // closeGit(git);
1401 // newBranch(remoteURI, branch);
1402 // } catch (GitAPIException e1) {
1403 // closeGit(git);
1404 // throw new RuntimeException(e1);
1405 // }
1406 
1407  } catch (TransportException e) {
1408  e.printStackTrace();
1410 
1411  if (git.getRepository().getConfig().getString("remote", "origin", "url").startsWith("git@")) {
1412  try {
1413  git.pull().setTransportConfigCallback(transportConfigCallback)
1414  .setProgressMonitor(getProgressMoniter("Pull ", remoteURI)).call();
1415  closeGit(git);
1416  } catch (Exception ex) {
1417  closeGit(git);
1418  throw new RuntimeException(
1419  "remoteURI " + remoteURI + " branch " + branch + " " + e.getMessage());
1420  }
1421  } else {
1422  closeGit(git);
1423  throw new RuntimeException("remoteURI " + remoteURI + " branch " + branch + " " + e.getMessage());
1424  }
1425 
1426  } catch (GitAPIException e) {
1427  e.printStackTrace();
1429  closeGit(git);
1430  throw new RuntimeException("remoteURI " + remoteURI + " branch " + branch + " " + e.getMessage());
1431  }
1432  } catch (InvalidRemoteException e) {
1434  closeGit(git);
1435  throw new InvalidRemoteException("remoteURI " + remoteURI + " branch " + branch + " " + e.getMessage());
1436  } catch (Throwable t) {
1437  closeGit(git);
1438  }
1439 
1440  }
1441 
1442  public static void pull(String remoteURI) throws IOException, RefAlreadyExistsException, RefNotFoundException,
1443  InvalidRefNameException, InvalidRemoteException, TransportException, GitAPIException {
1444  if (!hasNetwork())
1445  return;// No login info means there is no way to publish
1446  pull(remoteURI, getBranch(remoteURI));
1447 
1448  }
1449 
1450  public static void checkoutCommit(String remoteURI, String branch, String commitHash) throws IOException {
1451  waitForRepo(remoteURI, "checkoutCommit");
1452  File gitRepoFile = ScriptingEngine.uriToFile(remoteURI);
1453  if (!gitRepoFile.exists() || !gitRepoFile.getAbsolutePath().endsWith(".git")) {
1454  System.err.println("Invailid git file!" + gitRepoFile.getAbsolutePath());
1455  throw new RuntimeException("Invailid git file!" + gitRepoFile.getAbsolutePath());
1456  }
1457  Repository localRepo = new FileRepository(gitRepoFile);
1458  Git git = openGit(localRepo);
1459  try {
1460  git.checkout().setName(commitHash).call();
1461  git.checkout().setCreateBranch(true).setName(branch).setStartPoint(commitHash).call();
1462 
1463  } catch (Exception ex) {
1464  ex.printStackTrace();
1465  }
1466 
1467  closeGit(git);
1468 
1469  }
1470 
1471  public static void checkout(String remoteURI, RevCommit commit)
1472  throws IOException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException,
1473  CheckoutConflictException, InvalidRemoteException, TransportException, GitAPIException {
1474  ScriptingEngine.checkout(remoteURI, commit.getName());
1475  }
1476 
1477  public static void checkout(String remoteURI, Ref branch)
1478  throws IOException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException,
1479  CheckoutConflictException, InvalidRemoteException, TransportException, GitAPIException {
1480  String[] name = branch.getName().split("/");
1481  String myName = name[name.length - 1];
1482  ScriptingEngine.checkout(remoteURI, myName);
1483  }
1484 
1485  public static void checkout(String remoteURI, String branch)
1486  throws IOException, RefAlreadyExistsException, RefNotFoundException, InvalidRefNameException,
1487  CheckoutConflictException, InvalidRemoteException, TransportException, GitAPIException {
1488  if (!hasNetwork())
1489  return;
1490  // cloneRepo(remoteURI, branch);
1491  File gitRepoFile = uriToFile(remoteURI);
1492  if (!gitRepoFile.exists() || !gitRepoFile.getAbsolutePath().endsWith(".git")) {
1493  System.err.println("Invailid git file!" + gitRepoFile.getAbsolutePath());
1494  throw new RuntimeException("Invailid git file!" + gitRepoFile.getAbsolutePath());
1495  }
1496 
1497  String currentBranch = getFullBranch(remoteURI);
1498  if (currentBranch != null) {
1499  // String currentBranch=getFullBranch(remoteURI);
1500  Repository localRepo = new FileRepository(gitRepoFile);
1501  if (branch == null)
1502  branch = currentBranch;
1503 
1504  if (currentBranch.length() < branch.length() || !currentBranch.endsWith(branch)) {
1505  System.err.println("Current branch is " + currentBranch + " need " + branch);
1506 
1507  Git git = null;
1508  try {
1509  Collection<Ref> branches = getAllBranches(remoteURI);
1510  git = openGit(localRepo);
1511  for (Ref R : branches) {
1512  if (R.getName().endsWith(branch)) {
1513  System.err.println("\nFound upstream " + R.getName());
1514  shallowCheckout(remoteURI, branch, git);
1515  closeGit(git);
1516  }
1517  }
1518  // The ref does not exist upstream, create
1519  try {
1521  closeGit(git);
1522  newBranch(remoteURI, branch);
1523  } catch (org.eclipse.jgit.api.errors.TransportException ex) {
1524  // Not logged in yet, just return
1526  closeGit(git);
1527  return;
1528  } catch (RefAlreadyExistsException e) {
1530  closeGit(git);
1531  throw new RuntimeException(e);
1532  } catch (RefNotFoundException e) {
1534  closeGit(git);
1535  throw new RuntimeException(e);
1536  } catch (InvalidRefNameException e) {
1538  closeGit(git);
1539  throw new RuntimeException(e);
1540  } catch (CheckoutConflictException e) {
1541  resolveConflict(remoteURI, e, git);
1542  } catch (GitAPIException e) {
1544  closeGit(git);
1545  throw new RuntimeException(e);
1546  } catch (Exception e) {
1548  closeGit(git);
1549  throw new RuntimeException(e);
1550  }
1551  } catch (Exception ex) {
1553  closeGit(git);
1554  throw new RuntimeException(ex);
1555  }
1556  closeGit(git);
1557  }
1558 
1559  }
1560 
1561  }
1562 
1563  private static void shallowCheckout(String remoteURI, String branch, Git git) throws GitAPIException,
1564  RefAlreadyExistsException, InvalidRefNameException, RefNotFoundException, CheckoutConflictException {
1565  try {
1566  git.checkout().setName(branch).setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
1567  .setStartPoint("origin/" + branch).call();
1568  return;
1569  } catch (RefNotFoundException e) {
1570  git.branchCreate().setName(branch).setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
1571  .setStartPoint("origin/" + branch).setForce(true).call();
1572  git.checkout().setName(branch).setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK)
1573  .setStartPoint("origin/" + branch).call();
1574  return;
1575  } catch (CheckoutConflictException con) {
1576 
1577  resolveConflict(remoteURI, con, git);
1578  }
1579  }
1580 
1581  private static boolean resolveConflict(String remoteURI, CheckoutConflictException con, Git git) {
1583  if (git == null)
1584  waitForRepo(remoteURI, "resolveConflict");
1585  try {
1586  Status stat = git.status().call();
1587  Set<String> changed = stat.getModified();
1588  if (changed.size() > 0) {
1589  System.out.println("Modified ");
1590  for (String p : changed) {
1591  System.out.println("Modified Conflict with: " + p);
1592  byte[] bytes;
1593  String content = "";
1594  try {
1595  bytes = Files.readAllBytes(fileFromGit(remoteURI, p).toPath());
1596  content = new String(bytes, "UTF-8");
1597  try {
1598  commit(remoteURI, getBranch(remoteURI), p, content,
1599  "auto-save in ScriptingEngine.resolveConflict", false, git);
1600  } catch (Exception e) {
1601  // TODO Auto-generated catch block
1602  e.printStackTrace();
1603  }
1604  } catch (IOException e1) {
1605  // TODO Auto-generated catch block
1606  e1.printStackTrace();
1607  }
1608 
1609  }
1610  return resolveConflict(remoteURI, con, git);
1611  }
1612  Set<String> untracked = stat.getUntracked();
1613  if (untracked.size() > 0) {
1614  System.out.println("Untracked ");
1615  for (String p : untracked) {
1616  System.out.println("Untracked Conflict with: " + p);
1617  File f = fileFromGit(remoteURI, p);
1618  f.delete();
1619  }
1620  return resolveConflict(remoteURI, con, git);
1621  }
1622  } catch (Exception e) {
1623  // TODO Auto-generated catch block
1624  e.printStackTrace();
1625  }
1626  return true;
1627  }
1628 
1635  public static File cloneRepo(String remoteURI, String branch) {
1636 
1637  File gistDir = getRepositoryCloneDirectory(remoteURI);
1638  String localPath = gistDir.getAbsolutePath();
1639  File gitRepoFile = new File(localPath + "/.git");
1640  File dir = new File(localPath);
1641 
1642  if (!gitRepoFile.exists()) {
1643  if (!hasNetwork())
1644  return null;// No login info means there is no way to publish
1645  waitForRepo(remoteURI, "cloneRepo");
1646  System.out.println("Cloning files from: " + remoteURI);
1647  if (branch != null)
1648  System.out.println(" branch: " + branch);
1649  System.out.println(" to: " + localPath);
1650  Throwable ex = null;
1651  // Clone the repo
1652  Git git = null;
1653  try {
1654  if (branch == null) {
1655  git = cloneRepoLocal(remoteURI, dir);
1657  closeGit(git);
1658  branch = getFullBranch(remoteURI);
1659 
1660  } else {
1661  git = cloneRepoLocal(remoteURI, dir);
1663  closeGit(git);
1664  checkout(remoteURI, branch);
1665  }
1666 
1667  } catch (org.eclipse.jgit.api.errors.JGitInternalException exe) {
1668  closeGit(git);
1669  // deleteRepo(remoteURI);
1670  throw exe;
1671  } catch (Throwable e) {
1672  e.printStackTrace();
1673  closeGit(git);
1675  throw new RuntimeException(e);
1676  }
1677 
1678  }
1679  if (branch != null) {
1680  try {
1681  checkout(remoteURI, branch);
1682  } catch (Exception e) {
1683  throw new RuntimeException(e);
1684  }
1685  }
1686 
1687  return gistDir;
1688 
1689  }
1690 
1691  public static String locateGitUrl(File f) throws IOException {
1692  return locateGitUrl(f, null);
1693  }
1694 
1695  public static String locateGitUrl(File f, Git ref) throws IOException {
1696  File gitRepoFile = new File(f.getAbsolutePath());
1697  while (gitRepoFile != null) {
1698  if (new File(gitRepoFile.getAbsolutePath() + "/.git/config").exists()) {
1699  // System.err.println("Fount git repo for file: "+gitRepoFile);
1700  Repository localRepo = new FileRepository(gitRepoFile.getAbsoluteFile() + "/.git");
1701  Git git = ref;
1702  if (git == null)
1703  git = openGit(localRepo);
1704  String url = git.getRepository().getConfig().getString("remote", "origin", "url");
1705  if (!url.endsWith(".git"))
1706  url += ".git";
1707  if (ref == null)
1708  closeGit(git);
1709  return url;
1710  }
1711  gitRepoFile = gitRepoFile.getParentFile();
1712  }
1713 
1714  return null;
1715  }
1716 
1717  public static Git locateGit(File f) throws IOException {
1718  File gitRepoFile = f;
1719  while (gitRepoFile != null) {
1720  gitRepoFile = gitRepoFile.getParentFile();
1721  if (gitRepoFile != null)
1722  if (new File(gitRepoFile.getAbsolutePath() + "/.git/config").exists()) {
1723  // System.err.println("Fount git repo for file: "+gitRepoFile);
1724  Repository localRepo = new FileRepository(gitRepoFile.getAbsoluteFile() + "/.git");
1725  return openGit(localRepo);
1726 
1727  }
1728  }
1729 
1730  return null;
1731  }
1732 
1733  public static String getText(URL website) throws Exception {
1734 
1735  URLConnection connection = website.openConnection();
1736  BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
1737 
1738  StringBuilder response = new StringBuilder();
1739  String inputLine;
1740 
1741  while ((inputLine = in.readLine()) != null)
1742  response.append(inputLine + "\n");
1743 
1744  in.close();
1745 
1746  return response.toString();
1747  }
1748 
1749  public static File getLastFile() {
1750  if (lastFile == null)
1751  return getWorkspace();
1752  return lastFile;
1753  }
1754 
1755  public static void setLastFile(File lastFile) {
1757  }
1758 
1759  public static File getFileEngineRunByName(String filename) {
1760  return filesRun.get(filename);
1761  }
1762 
1763  public static String[] getImports() {
1764  return imports;
1765  }
1766 
1769  }
1770 
1771  public static void setLoginManager(IGitHubLoginManager lm) {
1773  }
1774 
1775  public static boolean isAutoupdate() {
1776  return autoupdate;
1777  }
1778 
1779  public static boolean setAutoupdate(boolean autoupdate) throws IOException {
1781  ScriptingEngine.autoupdate = true;// prevents recoursion loop from
1782  // PasswordManager.setAutoupdate(autoupdate);
1783  }
1785  return ScriptingEngine.autoupdate;
1786  }
1787 
1788  @SuppressWarnings("unused")
1789  private static File fileFromGistID(String string, String string2)
1790  throws InvalidRemoteException, TransportException, GitAPIException, IOException {
1791  return fileFromGit("https://gist.github.com/" + string + ".git", string2);
1792  }
1793 
1794  public static String findLocalPath(File currentFile, Git git) {
1795  if (git == null)
1796  return findLocalPath(currentFile);
1797  File dir = git.getRepository().getDirectory().getParentFile();
1798 
1799  return dir.toURI().relativize(currentFile.toURI()).getPath();
1800  }
1801 
1802  @SuppressWarnings("unused")
1803  public static String findLocalPath(File currentFile) {
1804  Git git = null;
1805  try {
1806  git = locateGit(currentFile);
1807  String ret = findLocalPath(currentFile, git);
1808  closeGit(git);
1809  return ret;
1810  } catch (IOException e) {
1811  if (git != null)
1812  closeGit(git);
1813  throw new RuntimeException(e);
1814  }
1815 
1816  }
1817 
1818  public static String[] findGitTagFromFile(File currentFile) throws IOException {
1819  return findGitTagFromFile(currentFile, null);
1820  }
1821 
1822  public static String[] findGitTagFromFile(File currentFile, Git ref) throws IOException {
1823  String string = locateGitUrl(currentFile, ref);
1824  Git git = ref;
1825  if (git == null)
1826  git = locateGit(currentFile);
1827  try {
1828  String[] strings = new String[] { string, findLocalPath(currentFile, git) };
1829  if (ref == null)
1830  closeGit(git);
1831  return strings;
1832  } catch (Throwable t) {
1833  t.printStackTrace();
1834  if (ref == null)
1835  closeGit(git);
1836  throw t;
1837  }
1838  }
1839 
1840  public static boolean checkOwner(String url) {
1841  Git git = null;
1842  try {
1843  git = openGit(getRepository(url));
1844  } catch (IOException e1) {
1845  closeGit(git);
1846  throw new RuntimeException(e1);
1847  }
1848  boolean owned = checkOwner(git);
1849  closeGit(git);
1850  return owned;
1851  }
1852 
1853  public static boolean checkOwner(File currentFile) {
1854  try {
1855  Git git;
1856  try {
1857  git = locateGit(currentFile);
1858  } catch (Exception e1) {
1859 
1860  return false;
1861  }
1862  boolean owned;
1863  try {
1864  owned = checkOwner(git);
1865  } catch (Throwable t) {
1866  owned = false;
1867  }
1868  closeGit(git);
1869  return owned;
1870  } catch (Throwable t) {
1871  return false;
1872  }
1873  }
1874 
1875  private static boolean checkOwner(Git git) {
1876  try {
1877  waitForLogin();
1878  git.pull().setCredentialsProvider(PasswordManager.getCredentialProvider()).call();// updates to the
1879  // latest version
1880  git.push().setCredentialsProvider(PasswordManager.getCredentialProvider()).call();
1881  return true;
1882  } catch (Exception e) {
1883  try {
1884  if (git.getRepository().getConfig().getString("remote", "origin", "url").startsWith("git@")) {
1885 
1886  git.pull().setTransportConfigCallback(transportConfigCallback).call();// updates to the
1887  // latest version
1888  git.push().setTransportConfigCallback(transportConfigCallback).call();
1889 
1890  return true;
1891  }
1892  } catch (Exception ex) {
1893  // just return false, the exception is it failing to push
1894  return false;
1895  }
1896 
1897  }
1898  return false;
1899  }
1900 
1901  public static GHGist forkGist(String currentGist) throws Exception {
1902 
1903  if (PasswordManager.getGithub() != null) {
1904 
1905  waitForLogin();
1906  GHGist incoming = PasswordManager.getGithub().getGist(currentGist);
1908  l.onLogin(PasswordManager.getUsername());
1909  }
1910  return incoming.fork();
1911 
1912  }
1913 
1914  return null;
1915  }
1916 
1925  public static String fork(String sourceURL, String newRepoName, String newRepoDescription) throws Exception {
1926  GHRepository repository;
1927  GitHub github = PasswordManager.getGithub();
1928  try {
1929  repository = makeNewRepoNoFailOver(newRepoName, newRepoDescription);
1930  } catch (org.kohsuke.github.HttpException ex) {
1931  if (ex.getMessage().contains("name already exists on this account")) {
1932  repository = github.getRepository(PasswordManager.getLoginID() + "/" + newRepoName);
1933  System.out.println("Repo exists!");
1934  return repository.getHttpTransportUrl();
1935  }
1936  throw ex;
1937  }
1938  String gitRepo = repository.getHttpTransportUrl();
1939 
1940  ArrayList<String> files = filesInGit(sourceURL);
1941  Git git = locateGit(fileFromGit(sourceURL, files.get(0)));
1942  Repository sourceRepoObject = git.getRepository();
1943  try {
1944  sourceRepoObject.getConfig().setString("remote", "origin", "url", gitRepo);
1945  if (git.getRepository().getConfig().getString("remote", "origin", "url").startsWith("git@"))
1946  git.push().setTransportConfigCallback(transportConfigCallback)
1947  .setProgressMonitor(getProgressMoniter("Pushing ", gitRepo)).call();
1948  else
1949  git.push().setCredentialsProvider(PasswordManager.getCredentialProvider())
1950  .setProgressMonitor(getProgressMoniter("Pushing ", gitRepo)).call();
1951  closeGit(git);
1952 
1953  filesInGit(gitRepo);
1954 
1955  return gitRepo;
1956  } catch (org.kohsuke.github.HttpException ex) {
1957  closeGit(git);
1958  if (ex.getMessage().contains("name already exists on this account")) {
1959  return PasswordManager.getGithub().getRepository(PasswordManager.getLoginID() + "/" + newRepoName)
1960  .getHttpTransportUrl();
1961  }
1962  ex.printStackTrace();
1963  } catch (Throwable ex) {
1964  ex.printStackTrace();
1965  }
1966  closeGit(git);
1967  throw new RuntimeException("Repo could not be forked and does not exist");
1968  }
1969 
1970  public static GHRepository makeNewRepoNoFailOver(String newName, String description)
1971  throws IOException, org.kohsuke.github.HttpException {
1972  GitHub github = PasswordManager.getGithub();
1973  try {
1974  GHCreateRepositoryBuilder builder = github.createRepository(newName);
1975  builder.description(description);
1976  GHRepository repo = builder.create();
1977  for (int i = 0; i < 5; i++) {
1978  try {
1979  repo = github.getRepositoryById("" + repo.getId());
1980  return repo;
1981  } catch (Exception ex) {
1982  ex.printStackTrace();
1983  }
1984  try {
1985  Thread.sleep(1000);
1986  } catch (InterruptedException e) {
1987  // TODO Auto-generated catch block
1988  e.printStackTrace();
1989  }
1990  }
1991  return repo;
1992  } catch (org.kohsuke.github.HttpException ex) {
1993  throw ex;
1994  }
1995  }
1996 
1997  public static GHRepository makeNewRepo(String newName, String description) throws IOException {
1998  if (description.length() < 2) {
1999  description = new Date().toString();
2000  }
2001  GitHub github = PasswordManager.getGithub();
2002  GHRepository gist = null;
2003  try {
2004  gist = makeNewRepoNoFailOver(newName, description);
2005  String url = gist.getHttpTransportUrl();
2006  cloneRepo(url, null);
2007  try {
2008 
2009  commit(url, "main", "firstCommit");
2010  newBranch(url, "main");
2011  } catch (IOException | GitAPIException e) {
2012  // TODO Auto-generated catch block
2013  e.printStackTrace();
2014  }
2015  } catch (org.kohsuke.github.HttpException ex) {
2016  if (ex.getMessage().contains("name already exists on this account")) {
2017  gist = github.getRepository(PasswordManager.getLoginID() + "/" + newName);
2018  }
2019  }
2020 
2021  return gist;
2022  }
2023 
2024  public static String locateGitUrlString(File f) {
2025 
2026  try {
2028  Repository repository = locateGit.getRepository();
2029  String string = repository.getConfig().getString("remote", "origin", "url");
2031  return string;
2032 
2033  } catch (IOException e) {
2034  // TODO Auto-generated catch block
2035  e.printStackTrace();
2036  }
2037  return null;
2038  }
2039 
2040  public static String urlToString(URL htmlUrl) {
2041  return htmlUrl.toExternalForm();
2042  }
2043 
2044  public static String urlToGist(URL htmlUrl) {
2045  String externalForm = urlToString(htmlUrl);
2046  System.out.println(externalForm);
2047  return ScriptingEngine.urlToGist(externalForm);
2048  }
2049 
2050  public static List<String> getAllLangauges() {
2051  ArrayList<String> langs = new ArrayList<>();
2052  for (String L : getLangaugesMap().keySet()) {
2053  langs.add(L);
2054  }
2055  return langs;
2056  }
2057 
2058  public static HashMap<String, IScriptingLanguage> getLangaugesMap() {
2059  return langauges;
2060  }
2061 
2062  public static IScriptingLanguage getLangaugeByExtention(String extention) {
2063  for (String L : getLangaugesMap().keySet()) {
2064  if (langauges.get(L).isSupportedFileExtenetion(extention)) {
2065  return langauges.get(L);
2066  }
2067  }
2068  return null;
2069  }
2070 
2071  public static boolean hasNetwork() {
2072 
2073  return PasswordManager.hasNetwork();
2074  }
2075 
2076  public static boolean isLoginSuccess() {
2077  return PasswordManager.loggedIn();
2078  }
2079 
2080  public static String[] copyGitFile(String sourceGit, String targetGit, String filename) {
2081  return copyGitFile(sourceGit, targetGit, filename, filename, false);
2082  }
2083 
2084  public static String[] copyGitFile(String sourceGit, String targetGit, String filename, String outFile,
2085  boolean bailIfExisting) {
2086  String targetFilename = outFile;
2087  String[] WalkingEngine;
2088  if (targetGit.contains("gist.github.com") && filename.contains("/")) {
2089  String[] parts = filename.split("/");
2090  targetFilename = parts[parts.length - 1];
2091  }
2092  try {
2093  WalkingEngine = ScriptingEngine.codeFromGit(sourceGit, filename);
2094  try {
2095  if (null == ScriptingEngine.fileFromGit(targetGit, targetFilename)) {
2096  ScriptingEngine.createFile(targetGit, targetFilename, "copy file");
2097  while (true) {
2098  try {
2099  ScriptingEngine.fileFromGit(targetGit, targetFilename);
2100  break;
2101  } catch (Exception e) {
2102 
2103  }
2104  ThreadUtil.wait(500);
2105  // Log.warn(targetGit +"/"+filename+ " not built yet");
2106  }
2107 
2108  }
2109  } catch (InvalidRemoteException e) {
2110  throw new RuntimeException(e);
2111  } catch (TransportException e) {
2112  throw new RuntimeException(e);
2113  } catch (GitAPIException e) {
2114  throw new RuntimeException(e);
2115  } catch (IOException e) {
2116  throw new RuntimeException(e);
2117  } catch (Exception e) {
2118  throw new RuntimeException(e);
2119  }
2120  String[] newFileCode;
2121  try {
2122  newFileCode = ScriptingEngine.codeFromGit(targetGit, targetFilename);
2123  if (newFileCode == null)
2124  newFileCode = new String[] { "" };
2125  if (newFileCode[0].length() < 10) {
2126  System.out.println("Copy Content to " + targetGit + "/" + targetFilename);
2127  ScriptingEngine.pushCodeToGit(targetGit, ScriptingEngine.getFullBranch(targetGit), targetFilename,
2128  WalkingEngine[0], "copy file content");
2129  }
2130  } catch (Exception e) {
2131  throw new RuntimeException(e);
2132  }
2133  } catch (Exception e1) {
2134  throw new RuntimeException(e1);
2135  }
2136 
2137  return new String[] { targetGit, targetFilename };
2138  }
2139 
2140  public static Ref getBranch(String remoteURI, String branch) throws IOException, GitAPIException {
2141  cloneRepo(remoteURI, null);
2142  Collection<Ref> branches = getAllBranches(remoteURI);
2143  for (Ref r : branches) {
2144  if (r.getName().endsWith(branch)) {
2145  return r;
2146  }
2147  }
2148  return null;
2149 
2150  }
2151 
2152  public static Collection<Ref> getAllBranches(String remoteURI) throws IOException, GitAPIException {
2153  cloneRepo(remoteURI, null);
2154  Git git = openGit(getRepository(remoteURI));
2155  String ref = git.getRepository().getConfig().getString("remote", "origin", "url");
2156  closeGit(git);
2157 
2158  System.out.print("Getting branches " + ref + " ");
2159  if (ref != null && ref.startsWith("git@")) {
2160  return Git.lsRemoteRepository().setHeads(true).setRemote(remoteURI)
2161  .setTransportConfigCallback(transportConfigCallback).call();
2162  } else {
2163  return Git.lsRemoteRepository().setHeads(true).setRemote(remoteURI)
2164  .setCredentialsProvider(PasswordManager.getCredentialProvider()).call();
2165  }
2166  }
2167 
2168  public static Repository getRepository(String remoteURI) throws IOException {
2169  // File gistDir = cloneRepo(url, getFullBranch(url));
2170 
2171  File gistDir = getRepositoryCloneDirectory(remoteURI);
2172  String localPath = gistDir.getAbsolutePath();
2173  File gitRepoFile = new File(localPath + "/.git");
2174  return new FileRepository(gitRepoFile.getAbsoluteFile());
2175  }
2176 
2177  public static File getRepositoryCloneDirectory(String remoteURI) {
2178  if (remoteURI.endsWith("/"))
2179  throw new RuntimeException("URL needs to end in .git, no trailing slash " + remoteURI);
2180  if (!remoteURI.endsWith(".git"))
2181  throw new RuntimeException("URL needs to end in .git " + remoteURI);
2182  String[] colinSplit = remoteURI.split(":");
2183 
2184  String gitSplit = colinSplit[1].substring(0, colinSplit[1].lastIndexOf('.'));
2185  File gistDir = new File(getWorkspace().getAbsolutePath() + "/gitcache/" + gitSplit);
2186  if (!gistDir.exists()) {
2187  gistDir.mkdir();
2188  }
2189  return gistDir;
2190  }
2191 
2192  public static void setCommitContentsAsCurrent(String url, String branch, RevCommit commit)
2193  throws IOException, GitAPIException {
2194  checkout(url, commit);
2195  Collection<Ref> branches = getAllBranches(url);
2196  String newBranch = branch;
2197  for (Ref iterableBranchInstance : branches) {
2198  String[] name = iterableBranchInstance.getName().split("/");
2199  String myName = name[name.length - 1];
2200  if (myName.contains(newBranch)) {
2201  newBranch = newBranch + "-1";
2202  }
2203  }
2204 //
2205  newBranch(url, newBranch, commit);
2206  commit(url, branch, "New branch " + branch + " created here");
2207  }
2208 
2209  private static void commit(String url, String branch, String message)
2210  throws IOException, GitAPIException, NoHeadException, NoMessageException, UnmergedPathsException,
2211  ConcurrentRefUpdateException, WrongRepositoryStateException, AbortedByHookException {
2212  commit(url, branch, message, null);
2213  }
2214 
2215  private static void commit(String url, String branch, String message, Git passedRef)
2216  throws IOException, GitAPIException, NoHeadException, NoMessageException, UnmergedPathsException,
2217  ConcurrentRefUpdateException, WrongRepositoryStateException, AbortedByHookException {
2218  Git git = passedRef;
2219  if (git == null)
2220  git = openGit(getRepository(url));
2221  try {
2222  git.commit().setAll(true).setMessage(message).call();
2223  ArrayList<Runnable> arrayList = onCommitEventListeners.get(url);
2224  if (arrayList != null) {
2225  for (int i = 0; i < arrayList.size(); i++) {
2226  Runnable r = arrayList.get(i);
2227  try {
2228  r.run();
2229  } catch (Throwable t) {
2230  t.printStackTrace();
2231  }
2232  }
2233  }
2234  } catch (Throwable t) {
2235  if (passedRef == null)
2236  closeGit(git);
2237  throw t;
2238  }
2239  if (passedRef == null)
2240  closeGit(git);
2241  }
2242 
2243  public static File getAppData() {
2244  return appdata;
2245  }
2246 
2247  // Method to compare two versions.
2248  // Returns 1 if v2 is
2249  // smaller, -1 if v1 is smaller, 0 if equal
2250  public static int versionCompare(String v1, String v2) {
2251  // vnum stores each numeric part of version
2252  int vnum1 = 0, vnum2 = 0;
2253 
2254  // loop until both String are processed
2255  for (int i = 0, j = 0; (i < v1.length() || j < v2.length());) {
2256  // Storing numeric part of
2257  // version 1 in vnum1
2258  while (i < v1.length() && v1.charAt(i) != '.') {
2259  vnum1 = vnum1 * 10 + (v1.charAt(i) - '0');
2260  i++;
2261  }
2262 
2263  // storing numeric part
2264  // of version 2 in vnum2
2265  while (j < v2.length() && v2.charAt(j) != '.') {
2266  vnum2 = vnum2 * 10 + (v2.charAt(j) - '0');
2267  j++;
2268  }
2269 
2270  if (vnum1 > vnum2)
2271  return -1;
2272  if (vnum2 > vnum1)
2273  return 1;
2274 
2275  // if equal, reset variables and
2276  // go for next numeric part
2277  vnum1 = vnum2 = 0;
2278  i++;
2279  j++;
2280  }
2281  return 0;
2282  }
2283 
2284  public static List<String> getAllTags(String gitRepo) {
2285  ArrayList<String> tags = new ArrayList<>();
2286  Git jGit = openGit(gitRepo);
2287  List<Ref> call;
2288  try {
2289  call = jGit.tagList().call();
2290  for (Ref ref : call) {
2291  String string = ref.getName().split("/")[2];
2292  tags.add(string);
2293  }
2294  } catch (Throwable e) {
2295  // TODO Auto-generated catch block
2296  e.printStackTrace();
2297 
2298  }
2299  Collections.sort(tags, new Comparator<String>() {
2300  public int compare(String object1, String object2) {
2301  return versionCompare(object1, object2);
2302  }
2303  });
2304  closeGit(jGit);
2305  return tags;
2306  }
2307 
2308  public static boolean tagExists(String remoteURI, String newTag) {
2309  List<String> tags = getAllTags(remoteURI);
2310  for (String s : tags) {
2311  System.out.println("Checking " + newTag + " against " + s);
2312  if (s.contentEquals(newTag)) {
2313  return true;
2314  }
2315  }
2316  return false;
2317  }
2318 
2319  public static void tagRepo(String remoteURI, String newTag) {
2320  System.out.println("Tagging " + remoteURI + " at " + newTag);
2321  if (tagExists(remoteURI, newTag)) {
2322  System.out.println("ERROR! Tag exists " + remoteURI + "@" + newTag);
2323  return;
2324  }
2325  Git git = openGit(remoteURI);
2326  // Creating tag
2327  try {
2328  try {
2329  git.tag().setName(newTag).setForceUpdate(true).call();
2330  } catch (Throwable t) {
2331  t.printStackTrace();
2332  }
2333  if (git.getRepository().getConfig().getString("remote", "origin", "url").startsWith("git@"))
2334  git.push().setPushTags().setTransportConfigCallback(transportConfigCallback)
2335  .setProgressMonitor(getProgressMoniter("Pushing ", remoteURI)).call();
2336  else
2337  git.push().setPushTags().setCredentialsProvider(PasswordManager.getCredentialProvider())
2338  .setProgressMonitor(getProgressMoniter("Pushing ", remoteURI)).call();
2339  } catch (GitAPIException e) {
2340  // TODO Auto-generated catch block
2341  e.printStackTrace();
2342  }
2343  closeGit(git);
2344  }
2345 
2346 }
static String[] copyGitFile(String sourceGit, String targetGit, String filename)
static boolean resolveConflict(String remoteURI, CheckoutConflictException con, Git git)
static void pushCodeToGit(String id, String branch, String FileName, String content, String commitMessage)
static String[] codeFromGit(String id, String FileName)
static void checkoutCommit(String remoteURI, String branch, String commitHash)
static List< String > getCurrentGist(String addr, Object engine)
static ArrayList< IGithubLoginListener > loginListeners
static Object inlineScriptRun(File code, ArrayList< Object > args, String shellTypeStorage)
static File fileFromGistID(String string, String string2)
static void checkout(String remoteURI, String branch)
static File cloneRepo(String remoteURI, String branch)
static Git cloneRepoLocalSelectAuth(String remoteURI, File dir, boolean useSSH)
static void checkout(String remoteURI, RevCommit commit)
static File createFile(String git, String fileName, String commitMessage)
static List< String > listBranchNames(String remoteURI)
static void commit(String id, String branch, String FileName, String content, String commitMessage, boolean flagNewFile)
static Ref getBranch(String remoteURI, String branch)
static ArrayList< String > filesInGit(String remote, String branch, String extnetion)
static File fileFromGit(String remoteURI, String branch, String fileInRepo)
static void deleteBranch(String remoteURI, String toDelete)
static ArrayList< String > filesInGit(String remote)
static void waitForRepo(String remoteURI, String reason)
static void shallowCheckout(String remoteURI, String branch, Git git)
static List< String > listLocalBranchNames(String remoteURI)
static GHRepository makeNewRepoNoFailOver(String newName, String description)
static String fork(String sourceURL, String newRepoName, String newRepoDescription)
static GHRepository makeNewRepo(String newName, String description)
static void removeIGithubLoginListener(IGithubLoginListener l)
static boolean tagExists(String remoteURI, String newTag)
static List< Ref > listBranches(String remoteURI, Git git)
static ArrayList< String > filesInGit(String remote, String branch, String extnetion, Git ref)
static void loadFilesToList(ArrayList< String > f, File directory, String extnetion)
static HashMap< String, IScriptingLanguage > langauges
static Object inlineGistScriptRun(String gistID, String Filename, ArrayList< Object > args)
static String[] copyGitFile(String sourceGit, String targetGit, String filename, String outFile, boolean bailIfExisting)
static void newBranchLocal(String newBranch, String remoteURI, Git git, RevCommit source)
static HashMap< String, ArrayList< Runnable > > onCommitEventListeners
static Object gitScriptRun(String gitURL, String Filename)
static Object gitScriptRun(String gitURL, String Filename, ArrayList< Object > args)
static Object inlineFileScriptRun(File f, ArrayList< Object > args)
static ArrayList< String > filesInGit(String remote, Git ref)
static String findLocalPath(File currentFile, Git git)
static Object inlineScriptStringRun(String line, ArrayList< Object > args, String shellTypeStorage)
static void commit(String url, String branch, String message, Git passedRef)
static IScriptingLanguage getLangaugeByExtention(String extention)
static String newBranch(String remoteURI, String newBranch)
static String[] codeFromGistID(String id, String FileName)
static void removeOnCommitEventListeners(String url, Runnable event)
static String[] findGitTagFromFile(File currentFile, Git ref)
static void newBranch(String remoteURI, String newBranch, RevCommit source)
static void loadFilesToList(ArrayList< String > f, File directory, String extnetion, Git ref)
static final ArrayList< GitLogProgressMonitor > logListeners
static ProgressMonitor getProgressMoniter(String type, String remoteURI)
static HashMap< String, IScriptingLanguage > getLangaugesMap()
static File fileFromGit(String remoteURI, String fileInRepo)
static void removeLogListener(GitLogProgressMonitor l)
static Collection< Ref > getAllBranches(String remoteURI)
static void addIGithubLoginListener(IGithubLoginListener l)
static void setCommitContentsAsCurrent(String url, String branch, RevCommit commit)
static void addScriptingLanguage(IScriptingLanguage lang)
static Git cloneRepoLocal(String remoteURI, File dir)
static void addOnCommitEventListeners(String url, Runnable event)
static void commit(String url, String branch, String message)
static void pull(String remoteURI, String branch)
static void tagRepo(String remoteURI, String newTag)
static void checkout(String remoteURI, Ref branch)
static void debug(String message)
Definition: Log.java:128