List of usage examples for org.eclipse.jgit.lib Repository resolve
@Nullable public ObjectId resolve(String revstr) throws AmbiguousObjectException, IncorrectObjectTypeException, RevisionSyntaxException, IOException
From source file:playRepository.GitRepository.java
License:Apache License
public static String deleteFromBranch(PullRequest pullRequest) { if (!canDeleteFromBranch(pullRequest)) { return null; }/*from w w w . j a va 2 s . c om*/ RevWalk revWalk = null; String lastCommitId; Repository repo = null; try { repo = buildGitRepository(pullRequest.fromProject); ObjectId branch = repo.resolve(pullRequest.fromBranch); revWalk = new RevWalk(repo); RevCommit commit = revWalk.parseCommit(branch); lastCommitId = commit.getName(); deleteBranch(repo, pullRequest.fromBranch); return lastCommitId; } catch (Exception e) { throw new RuntimeException(e); } finally { if (revWalk != null) { revWalk.release(); } if (repo != null) { repo.close(); } } }
From source file:playRepository.GitRepository.java
License:Apache License
public static boolean canRestoreBranch(PullRequest pullRequest) { Repository repo = null; try {/*from w ww. j av a2s . c o m*/ repo = buildGitRepository(pullRequest.fromProject); ObjectId resolve = repo.resolve(pullRequest.fromBranch); if (resolve == null && pullRequest.lastCommitId != null) { return true; } } catch (Exception e) { throw new RuntimeException(e); } finally { if (repo != null) { repo.close(); } } return false; }
From source file:playRepository.GitRepository.java
License:Apache License
public static List<GitCommit> diffCommits(PullRequest pullRequest) { List<GitCommit> commits = new ArrayList<>(); if (pullRequest.mergedCommitIdFrom == null || pullRequest.mergedCommitIdTo == null) { return commits; }//from w ww . j av a2 s. c o m Repository repo; try { if (pullRequest.isClosed()) { repo = buildGitRepository(pullRequest.toProject); } else { repo = buildGitRepository(pullRequest.fromProject); } ObjectId untilId = repo.resolve(pullRequest.mergedCommitIdTo); if (untilId == null) { return commits; } ObjectId sinceId = repo.resolve(pullRequest.mergedCommitIdFrom); if (sinceId == null) { return commits; } Iterable<RevCommit> logIterator = new Git(repo).log().addRange(sinceId, untilId).call(); for (RevCommit commit : logIterator) { commits.add(new GitCommit(commit)); } return commits; } catch (Exception e) { throw new RuntimeException(e); } }
From source file:playRepository.GitRepository.java
License:Apache License
public static String getPatch(Repository repository, String fromBranch, String toBranch) { TreeWalk treeWalk = new TreeWalk(repository); RevWalk walk = new RevWalk(repository); try {/*from w w w . j a v a 2 s .c o m*/ ObjectId from = repository.resolve(fromBranch); ObjectId to = repository.resolve(toBranch); RevTree fromTree = walk.parseTree(from); RevTree toTree = walk.parseTree(to); treeWalk.addTree(toTree); treeWalk.addTree(fromTree); ByteArrayOutputStream out = new ByteArrayOutputStream(); DiffFormatter diffFormatter = new DiffFormatter(out); diffFormatter.setRepository(repository); treeWalk.setRecursive(true); diffFormatter.format(DiffEntry.scan(treeWalk)); return out.toString("UTF-8"); } catch (Exception e) { throw new RuntimeException(e); } finally { walk.dispose(); } }
From source file:playRepository.GitRepository.java
License:Apache License
public static String getPatch(PullRequest pullRequest) { if (pullRequest.mergedCommitIdFrom == null || pullRequest.mergedCommitIdTo == null) { return ""; }//from w w w. j av a 2 s .c o m Repository repo; try { repo = buildGitRepository(pullRequest.toProject); ObjectId untilId = repo.resolve(pullRequest.mergedCommitIdTo); if (untilId == null) { return ""; } ObjectId sinceId = repo.resolve(pullRequest.mergedCommitIdFrom); if (sinceId == null) { return ""; } return getPatch(repo, untilId.getName(), sinceId.getName()); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:playRepository.GitRepository.java
License:Apache License
public static List<FileDiff> getDiff(final Repository repositoryA, String revA, Repository repositoryB, String revB) throws IOException { ObjectId commitA = repositoryA.resolve(revA); ObjectId commitB = repositoryB.resolve(revB); return getFileDiffs(repositoryA, repositoryB, commitA, commitB); }
From source file:playRepository.GitRepositoryTest.java
License:Apache License
private byte[] getRawFile(Repository repository, String path) throws IOException { RevTree tree = new RevWalk(repository).parseTree(repository.resolve(Constants.HEAD)); TreeWalk treeWalk = TreeWalk.forPath(repository, path, tree); if (treeWalk.isSubtree()) { return null; } else {/*from w w w . ja va 2 s .co m*/ return repository.open(treeWalk.getObjectId(0)).getBytes(); } }
From source file:uk.ac.cam.cl.dtg.segue.database.GitDb.java
License:Apache License
/** * Will find an object from the git repository if given a sha and a full git path. * /*from w w w . j a va 2 s .c o m*/ * @param sha * - to search for. * @param filename * - of the file in git to locate. * @return ObjectId which will allow you to access information about the node. * @throws IOException * - if we cannot access the repo location. * @throws UnsupportedOperationException * - if git does not support the operation requested. */ private ObjectId findGitObject(final String sha, final String filename) throws IOException, UnsupportedOperationException { if (null == sha || null == filename) { return null; } Repository repository = gitHandle.getRepository(); ObjectId commitId = repository.resolve(sha); RevWalk revWalk = new RevWalk(repository); RevCommit commit = revWalk.parseCommit(commitId); RevTree tree = commit.getTree(); TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create(filename)); int count = 0; ObjectId objectId = null; String path = null; while (treeWalk.next()) { count++; if (null == objectId) { objectId = treeWalk.getObjectId(0); path = treeWalk.getPathString(); } else if (count > 1) { // throw exception if we find that there is more than one that // matches the search. StringBuilder sb = new StringBuilder(); sb.append("Multiple results have been found in the git repository for the following search: "); sb.append(filename + "."); sb.append(" in "); sb.append(sha); sb.append(" Unable to decide which one to return."); throw new UnsupportedOperationException(sb.toString()); } } if (null == objectId) { return null; } revWalk.dispose(); log.debug("Retrieved Commit Id: " + commitId.getName() + " Searching for: " + filename + " found: " + path); return objectId; }
From source file:uk.ac.cam.cl.dtg.segue.database.GitDbTest.java
License:Apache License
@Test public void getTreeWalk_checkThatBlankPathsAreAllowed_noExceptionThrown() throws IOException { Git git = EasyMock.createMock(Git.class); GitDb db = new GitDb(git); try {//from w ww . ja v a2s . com db.getTreeWalk("", ""); fail("Failed to throw required exception on blank sha."); } catch (IllegalArgumentException e) { // Exception correctly thrown. } catch (Exception e) { fail("Wrong type of exception thrown on blank sha"); } try { db.getTreeWalk(null, ""); fail("Failed to throw required exception on null sha."); } catch (NullPointerException e) { // Exception correctly thrown. } catch (Exception e) { fail("Wrong type of exception thrown on null sha"); } try { db.getTreeWalk("sha", null); fail("Failed to throw required exception on null path."); } catch (NullPointerException e) { // Exception correctly thrown. } catch (Exception e) { fail("Wrong type of exception thrown on null path"); } Repository repo = EasyMock.createMock(Repository.class); EasyMock.expect(git.getRepository()).andReturn(repo); EasyMock.expect(repo.resolve("sha")).andReturn(null); EasyMock.replay(git); EasyMock.replay(repo); assertNull(db.getTreeWalk("sha", "")); // Blank path is explicitly allowed. This should not throw an exception. But in this case we've passed an invalid sha, so we should get null back. }
From source file:uk.ac.cam.UROP.twentyfourteen.database.GitDb.java
/** * Will find an object from the git repository if given a sha and a full git * path./*from w w w . j a v a 2s. c o m*/ * * @param sha * @param filename * @return ObjectId which will allow you to access information about the * node. */ private ObjectId findGitObject(String sha, String filename) throws IOException, UnsupportedOperationException { if (null == sha || null == filename) { return null; } Repository repository = gitHandle.getRepository(); ObjectId commitId = repository.resolve(sha); RevWalk revWalk = new RevWalk(repository); RevCommit commit = revWalk.parseCommit(commitId); RevTree tree = commit.getTree(); TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(tree); treeWalk.setRecursive(true); treeWalk.setFilter(PathFilter.create(filename)); int count = 0; ObjectId objectId = null; String path = null; while (treeWalk.next()) { count++; if (null == objectId) { objectId = treeWalk.getObjectId(0); path = treeWalk.getPathString(); } // throw exception if we find that there is more than one that // matches the search. else if (count > 1) { StringBuilder sb = new StringBuilder(); sb.append("Multiple results have been found in the git repository for the following search: "); sb.append(filename + "."); sb.append(" in "); sb.append(sha); sb.append(" Unable to decide which one to return."); throw new UnsupportedOperationException(sb.toString()); } } if (null == objectId) { log.warn("No objects found matching the search criteria (" + sha + "," + filename + ") in Git"); return null; } revWalk.dispose(); log.debug("Retrieved Commit Id: " + commitId.getName() + " Searching for: " + filename + " found: " + path); return objectId; }