|
| 1 | +package edu.appstate.cs.rascalgit; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.IOException; |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.List; |
| 8 | + |
| 9 | +import org.eclipse.jgit.api.Git; |
| 10 | +import org.eclipse.jgit.api.errors.GitAPIException; |
| 11 | +import org.eclipse.jgit.api.errors.InvalidRemoteException; |
| 12 | +import org.eclipse.jgit.api.errors.TransportException; |
| 13 | +import org.eclipse.jgit.lib.Constants; |
| 14 | +import org.eclipse.jgit.lib.Ref; |
| 15 | +import org.eclipse.jgit.lib.Repository; |
| 16 | +import org.rascalmpl.exceptions.RuntimeExceptionFactory; |
| 17 | + |
| 18 | +import io.usethesource.vallang.IList; |
| 19 | +import io.usethesource.vallang.IListWriter; |
| 20 | +import io.usethesource.vallang.ISourceLocation; |
| 21 | +import io.usethesource.vallang.IString; |
| 22 | +import io.usethesource.vallang.IValueFactory; |
| 23 | + |
| 24 | +public class RascalGit { |
| 25 | + private final IValueFactory values; |
| 26 | + private HashMap<ISourceLocation, Repository> repoMap = new HashMap<>(); |
| 27 | + |
| 28 | + public RascalGit(IValueFactory values) { |
| 29 | + super(); |
| 30 | + this.values = values; |
| 31 | + } |
| 32 | + |
| 33 | + public void cloneRemoteRepository(IString remotePath, ISourceLocation localPath) { |
| 34 | + if (!repoMap.containsKey(localPath)) { |
| 35 | + String origin = remotePath.getValue(); |
| 36 | + File localDir = new File(localPath.getPath()); |
| 37 | + |
| 38 | + try { |
| 39 | + Git git = Git.cloneRepository() |
| 40 | + .setURI(origin) |
| 41 | + .setDirectory(localDir) |
| 42 | + .call(); |
| 43 | + Repository repo = git.getRepository(); |
| 44 | + repoMap.put(localPath, repo); |
| 45 | + } catch (InvalidRemoteException e) { |
| 46 | + throw RuntimeExceptionFactory.javaException(e, null, null); |
| 47 | + } catch (TransportException e) { |
| 48 | + throw RuntimeExceptionFactory.javaException(e, null, null); |
| 49 | + } catch (GitAPIException e) { |
| 50 | + throw RuntimeExceptionFactory.javaException(e, null, null); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + public IList getTags(ISourceLocation localPath) { |
| 56 | + if (repoMap.containsKey(localPath)) { |
| 57 | + Repository repo = repoMap.get(localPath); |
| 58 | + List<Ref> tagRefs; |
| 59 | + try { |
| 60 | + tagRefs = repo.getRefDatabase().getRefsByPrefix(Constants.R_TAGS); |
| 61 | + } catch (IOException e) { |
| 62 | + throw RuntimeExceptionFactory.javaException(e, null, null); |
| 63 | + } |
| 64 | + |
| 65 | + IListWriter lw = values.listWriter(); |
| 66 | + for (Ref r : tagRefs) { |
| 67 | + lw.append(values.string(r.getName().substring(Constants.R_TAGS.length()))); |
| 68 | + } |
| 69 | + return lw.done(); |
| 70 | + } |
| 71 | + return values.listWriter().done(); |
| 72 | + } |
| 73 | + |
| 74 | + public void openLocalRepository(ISourceLocation loc) { |
| 75 | + if (!repoMap.containsKey(loc)) { |
| 76 | + File repoDir = new File(loc.getPath()); |
| 77 | + try { |
| 78 | + Git git = Git.open(repoDir); |
| 79 | + Repository repo = git.getRepository(); |
| 80 | + repoMap.put(loc, repo); |
| 81 | + } catch (IOException ioe) { |
| 82 | + throw RuntimeExceptionFactory.javaException(ioe, null, null); |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | +} |
0 commit comments