|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright 2019 me. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | +package com.factsmission.psps; |
| 25 | + |
| 26 | +import java.io.File; |
| 27 | +import java.io.IOException; |
| 28 | +import java.nio.file.Files; |
| 29 | +import java.nio.file.Path; |
| 30 | +import java.nio.file.Paths; |
| 31 | +import java.util.Comparator; |
| 32 | +import java.util.List; |
| 33 | + |
| 34 | +import org.eclipse.jgit.api.Git; |
| 35 | +import org.eclipse.jgit.api.ListBranchCommand.ListMode; |
| 36 | +import org.eclipse.jgit.api.errors.GitAPIException; |
| 37 | +import org.eclipse.jgit.errors.IncorrectObjectTypeException; |
| 38 | +import org.eclipse.jgit.errors.RevisionSyntaxException; |
| 39 | +import org.eclipse.jgit.lib.Ref; |
| 40 | + |
| 41 | +import org.eclipse.jgit.lib.Constants; |
| 42 | +import org.eclipse.jgit.lib.ObjectId; |
| 43 | +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; |
| 44 | + |
| 45 | +/** |
| 46 | + * |
| 47 | + * @author me |
| 48 | + */ |
| 49 | +public class JGitRepository implements Repository { |
| 50 | + |
| 51 | + private final static Path baseCheckout = Paths.get(System.getProperty("user.dir"), "checkout"); |
| 52 | + private final String repoUri; |
| 53 | + private final String userName; |
| 54 | + private Git git; |
| 55 | + private final Path workingDir; |
| 56 | + private final String repoName; |
| 57 | + |
| 58 | + JGitRepository(String repository, String token) throws IOException { |
| 59 | + this.repoName = repository; |
| 60 | + this.repoUri = "https://github.com/" + repository + ".git"; |
| 61 | + this.userName = token; |
| 62 | + workingDir = baseCheckout.resolve(repository); |
| 63 | + if (Files.exists(workingDir)) { |
| 64 | + Files.walk(workingDir) |
| 65 | + .sorted(Comparator.reverseOrder()) |
| 66 | + .map(Path::toFile) |
| 67 | + .forEach(File::delete); //TODO recycle |
| 68 | + } |
| 69 | + try { |
| 70 | + git = Git.cloneRepository() |
| 71 | + .setURI(repoUri) |
| 72 | + .setDirectory(workingDir.toFile()) |
| 73 | + .setCloneAllBranches(false) |
| 74 | + .setCredentialsProvider(new UsernamePasswordCredentialsProvider(token, "")) |
| 75 | + .call(); |
| 76 | + |
| 77 | + } catch (GitAPIException e) { |
| 78 | + throw new RuntimeException(e); |
| 79 | + } finally { |
| 80 | + git.close(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public String[] getBranches() throws IOException { |
| 86 | + try { |
| 87 | + final List<Ref> branchRefs = git.branchList().setListMode(ListMode.REMOTE).call(); |
| 88 | + final String[] result = new String[branchRefs.size()]; |
| 89 | + int i = 0; |
| 90 | + for (Ref branchRef : branchRefs) { |
| 91 | + String[] parts = branchRef.getName().split("/"); |
| 92 | + result[i++] = parts[parts.length-1]; |
| 93 | + } |
| 94 | + return result; |
| 95 | + } catch (GitAPIException ex) { |
| 96 | + throw new IOException(ex); |
| 97 | + } |
| 98 | + |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public String getCommitURL() throws IOException { |
| 103 | + //something like https://github.com/factsmission/staging-website/commit/4467dc315fd80d4b98ad669e601b614d96e23041 |
| 104 | + try { |
| 105 | + ObjectId id = git.getRepository().resolve(Constants.HEAD); |
| 106 | + String hexString = ObjectId.toString(id); |
| 107 | + return "https://github.com/"+repoName+"/commit/"+hexString; |
| 108 | + } catch (IncorrectObjectTypeException | RevisionSyntaxException ex) { |
| 109 | + throw new IOException(ex); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public byte[] getContent(String repoPath) throws IOException { |
| 115 | + Path path = workingDir.resolve(repoPath); |
| 116 | + if (Files.isDirectory(path)) { |
| 117 | + return null; |
| 118 | + } |
| 119 | + return Files.readAllBytes(path); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public Iterable<String> getPaths() throws IOException { |
| 124 | + return (Iterable<String>)Files.walk(workingDir) |
| 125 | + .sorted(Comparator.reverseOrder()) |
| 126 | + .map(p -> {System.out.println(p); System.out.println(workingDir.relativize(p)); return workingDir.relativize(p);}) |
| 127 | + .map(Path::toString) |
| 128 | + .filter(s -> !s.startsWith(".git"))::iterator; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + public void useBranch(String branch) throws IOException { |
| 133 | + try { |
| 134 | + git.checkout().setName("remotes/origin/"+branch).call(); |
| 135 | + } catch (GitAPIException ex) { |
| 136 | + throw new IOException(ex); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments