protected List<CommitInfo> doHistory( Git git, String branch, String objectId, String path, int limit) { Repository r = git.getRepository(); CommitFinder finder = new CommitFinder(r); CommitListFilter block = new CommitListFilter(); if (Strings.isNotBlank(path)) { finder.setFilter(PathFilterUtils.and(path)); } finder.setFilter(block); if (limit > 0) { finder.setFilter(new CommitLimitFilter(100).setStop(true)); } if (Strings.isNotBlank(objectId)) { finder.findFrom(objectId); } else { if (Strings.isNotBlank(branch)) { RevCommit base = CommitUtils.getBase(r, branch); finder.findFrom(base); } else { finder.find(); } } List<RevCommit> commits = block.getCommits(); List<CommitInfo> results = new ArrayList<CommitInfo>(); for (RevCommit entry : commits) { CommitInfo commitInfo = createCommitInfo(entry); results.add(commitInfo); } return results; }
@Test public void getRepositoryAndFindAuthors() throws Exception { GitScriptService service = this.componentManager.getInstance(ScriptService.class, "git"); Repository repository = service.getRepository(this.testRepository.getAbsolutePath(), TEST_REPO_CLONED); Assert.assertEquals(true, new Git(repository).pull().call().isSuccessful()); CommitFinder finder = new CommitFinder(repository); CommitCountFilter count = new CommitCountFilter(); finder.setMatcher(count); finder.find(); Assert.assertEquals(1, count.getCount()); Set<PersonIdent> authors = service.findAuthors(repository); Assert.assertEquals(1, authors.size()); Assert.assertEquals("test author", authors.iterator().next().getName()); }
public String getProjectVersion(File repoDir) throws IOException, GitAPIException { Git git = Git.open(repoDir); Repository repo = git.getRepository(); // Find base commit between current branch and "master": String branch = repo.getBranch(); RevCommit base = CommitUtils.getBase(repo, "master", branch); CommitCountFilter count = new CommitCountFilter(); CommitFinder finder = new CommitFinder(repo).setFilter(count); finder.findBetween(branch, base); long commitsSinceBase = count.getCount(); // Find tags in "master" before base commit: RevWalk rw = new RevWalk(repo); rw.markStart(base); rw.setRetainBody(false); Ref master = repo.getRef("master"); List<Ref> masterAsList = Arrays.asList(master); List<Ref> tags = git.tagList().call(); Map<RevCommit, Ref> masterTags = new HashMap<RevCommit, Ref>(); for (Ref tag : tags) { tag = repo.peel(tag); ObjectId commitID = tag.getPeeledObjectId(); if (commitID == null) continue; RevCommit commit = rw.parseCommit(commitID); // Only remember tags reachable from "master": if (!RevWalkUtils.findBranchesReachableFrom(commit, rw, masterAsList).isEmpty()) { masterTags.put(commit, tag); } } // Find the shortest distance in commits between base tag in "master": long commitsBetweenBaseAndTag = Long.MAX_VALUE; String tagName = ""; for (RevCommit tagCommit : masterTags.keySet()) { count.reset(); finder.findBetween(base, tagCommit); if (count.getCount() < commitsBetweenBaseAndTag) { commitsBetweenBaseAndTag = count.getCount(); tagName = masterTags.get(tagCommit).getName(); } } if (commitsBetweenBaseAndTag == Long.MAX_VALUE) { // If no tag, get total number of commits: commitsBetweenBaseAndTag = repo.getRefDatabase().getRefs("").size(); } long commitsSinceLastMasterTag = commitsSinceBase + commitsBetweenBaseAndTag; // Construct version string: String version = branch.equals("master") ? "" : (branch + "-"); if (tagName.startsWith("refs/tags/")) { tagName = tagName.substring("refs/tags/".length()); } // v1.1 -> 1.1 if (tagName.matches("v\\d+.*")) { tagName = tagName.substring(1); } if (tagName.isEmpty()) { version = "0"; } version += tagName + ((!tagonly) ? "." + commitsSinceLastMasterTag : ""); return version; }