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:util.DifferenceComputer.java
License:Apache License
public static void main(String[] args) throws IOException, GitAPIException { ///// w w w . ja v a2 s.c om String coreRepoPath = "/Users/Onekin/Desktop/VODPlayer-CoreAssets-2";//args[0]; String branchToLookInto = "develop.coreAssets"; try { Git git = Git.open(new File(coreRepoPath)); Repository repository = git.getRepository(); Ref ref = repository.findRef(branchToLookInto); if (ref == null) { ref = git.checkout().setCreateBranch(true).setName(branchToLookInto) .setUpstreamMode(SetupUpstreamMode.TRACK).setStartPoint("origin/" + branchToLookInto) .call(); } ObjectId parentCommit = repository .resolve(repository.findRef(branchToLookInto).getObjectId().getName() + "^^{commit}");//http://download.eclipse.org/jgit/site/4.2.0.201601211800-r/apidocs/org/eclipse/jgit/lib/Repository.html#resolve(java.lang.String) ObjectId currentCommit = repository .resolve(repository.findRef(branchToLookInto).getObjectId().getName() + "^{commit}"); List<DiffEntry> listOfChangedFiles = getChangedFilesBetweenTwoCommits(coreRepoPath, currentCommit, parentCommit); } catch (Exception e) { e.getMessage(); e.printStackTrace(); } }
From source file:util.DifferenceComputer.java
License:Apache License
public static List<DiffEntry> getChangedFilesBetweenTwoCommits(String repoPath, ObjectId ancestor, ObjectId commit) {/* ww w . jav a 2 s. c o m*/ List<DiffEntry> diffs = null; try { // The {tree} will return the underlying tree-id instead of the commit-id itself! // For a description of what the carets do see e.g. http://www.paulboxley.com/blog/2011/06/git-caret-and-tilde // This means we are selecting the parent of the parent of the parent of the parent of current HEAD and // take the tree-ish of it Git git = Git.open(new File(repoPath)); Repository repository = git.getRepository(); //ObjectId oldHead = repository.resolve(repository.findRef(branchToLookInto).getObjectId().getName()+"^^{tree}");//http://download.eclipse.org/jgit/site/4.2.0.201601211800-r/apidocs/org/eclipse/jgit/lib/Repository.html#resolve(java.lang.String) //ObjectId head = repository.resolve(repository.findRef(branchToLookInto).getObjectId().getName()+"^{tree}"); ObjectId oldHead = repository.resolve(ancestor.getName() + "^{tree}");//http://download.eclipse.org/jgit/site/4.2.0.201601211800-r/apidocs/org/eclipse/jgit/lib/Repository.html#resolve(java.lang.String) ObjectId head = repository.resolve(commit.getName() + "^{tree}"); System.out.println("Printing diff between tree: " + oldHead + "and" + head); // prepare the two iterators to compute the diff between ObjectReader reader = repository.newObjectReader(); CanonicalTreeParser oldTreeIter = new CanonicalTreeParser(); oldTreeIter.reset(reader, oldHead); CanonicalTreeParser newTreeIter = new CanonicalTreeParser(); newTreeIter.reset(reader, head); // finally get the list of changed files git = new Git(repository); diffs = git.diff().setNewTree(newTreeIter).setOldTree(oldTreeIter).call(); int changedFiles = 0; for (DiffEntry diff : diffs) { System.out.println("Entry: " + diff); System.out.println("getNewPath: " + diff.getNewPath()); System.out.println("getScore: " + diff.getScore()); System.out.println("getChangeType: " + diff.getChangeType()); System.out.println("getNewMode: " + diff.getNewMode()); System.out.println("getPath: " + diff.getPath(null)); changedFiles++; } System.out.println("Changed Files: " + changedFiles); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Done"); return diffs; }
From source file:wherehows.common.utils.GitUtil.java
License:Open Source License
/** * Fetch all commit metadata from the repo * @param repoDir repository directory/*from www .jav a 2 s . c om*/ * @return list of commit metadata * @throws IOException * @throws GitAPIException */ public static List<CommitMetadata> getRepoMetadata(String repoDir) throws IOException, GitAPIException { List<CommitMetadata> metadataList = new ArrayList<>(); FileRepositoryBuilder builder = new FileRepositoryBuilder(); Repository repository = builder.setGitDir(new File(repoDir, ".git")).readEnvironment().findGitDir().build(); // Current branch may not be master. Instead of hard coding determine the current branch String currentBranch = repository.getBranch(); Ref head = repository.getRef("refs/heads/" + currentBranch); // current branch may not be "master" if (head == null) { return metadataList; } Git git = new Git(repository); RevWalk walk = new RevWalk(repository); RevCommit commit = walk.parseCommit(head.getObjectId()); TreeWalk treeWalk = new TreeWalk(repository); treeWalk.addTree(commit.getTree()); treeWalk.setRecursive(true); while (treeWalk.next()) { String filePath = treeWalk.getPathString(); Iterable<RevCommit> commitLog = git.log().add(repository.resolve(Constants.HEAD)).addPath(filePath) .call(); for (RevCommit r : commitLog) { CommitMetadata metadata = new CommitMetadata(r.getName()); metadata.setFilePath(filePath); metadata.setFileName(FilenameUtils.getName(filePath)); metadata.setMessage(r.getShortMessage().trim()); // Difference between committer and author // refer to: http://git-scm.com/book/ch2-3.html PersonIdent committer = r.getCommitterIdent(); PersonIdent author = r.getAuthorIdent(); metadata.setAuthor(author.getName()); metadata.setAuthorEmail(author.getEmailAddress()); metadata.setCommitter(committer.getName()); metadata.setCommitterEmail(committer.getEmailAddress()); metadata.setCommitTime(committer.getWhen()); metadataList.add(metadata); } } git.close(); return metadataList; }