From d25c599b9967549fe0ebc7a56ca785c317ba3d4b Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Mon, 09 Jan 2012 21:07:01 -0500 Subject: [PATCH] Documentation. Find bugs. Organized imports. --- src/com/gitblit/utils/JGitUtils.java | 130 +++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 130 insertions(+), 0 deletions(-) diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java index c61b3d9..ae53c94 100644 --- a/src/com/gitblit/utils/JGitUtils.java +++ b/src/com/gitblit/utils/JGitUtils.java @@ -62,6 +62,7 @@ import org.eclipse.jgit.revwalk.RevSort; import org.eclipse.jgit.revwalk.RevTree; import org.eclipse.jgit.revwalk.RevWalk; +import org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter; import org.eclipse.jgit.revwalk.filter.RevFilter; import org.eclipse.jgit.storage.file.FileRepository; import org.eclipse.jgit.transport.CredentialsProvider; @@ -470,6 +471,19 @@ } /** + * Retrieves a Java Date from a Git commit. + * + * @param commit + * @return date of the commit or Date(0) if the commit is null + */ + public static Date getAuthorDate(RevCommit commit) { + if (commit == null) { + return new Date(0); + } + return commit.getAuthorIdent().getWhen(); + } + + /** * Returns the specified commit from the repository. If the repository does * not exist or is empty, null is returned. * @@ -824,6 +838,45 @@ } /** + * Returns a list of commits since the minimum date starting from the + * specified object id. + * + * @param repository + * @param objectId + * if unspecified, HEAD is assumed. + * @param minimumDate + * @return list of commits + */ + public static List<RevCommit> getRevLog(Repository repository, String objectId, Date minimumDate) { + List<RevCommit> list = new ArrayList<RevCommit>(); + if (!hasCommits(repository)) { + return list; + } + try { + // resolve branch + ObjectId branchObject; + if (StringUtils.isEmpty(objectId)) { + branchObject = getDefaultBranch(repository); + } else { + branchObject = repository.resolve(objectId); + } + + RevWalk rw = new RevWalk(repository); + rw.markStart(rw.parseCommit(branchObject)); + rw.setRevFilter(CommitTimeRevFilter.after(minimumDate)); + Iterable<RevCommit> revlog = rw; + for (RevCommit rev : revlog) { + list.add(rev); + } + rw.dispose(); + } catch (Throwable t) { + error(t, repository, "{0} failed to get {1} revlog for minimum date {2}", objectId, + minimumDate); + } + return list; + } + + /** * Returns a list of commits starting from HEAD and working backwards. * * @param repository @@ -920,6 +973,50 @@ rw.dispose(); } catch (Throwable t) { error(t, repository, "{0} failed to get {1} revlog for path {2}", objectId, path); + } + return list; + } + + /** + * Returns a list of commits for the repository within the range specified + * by startRangeId and endRangeId. If the repository does not exist or is + * empty, an empty list is returned. + * + * @param repository + * @param startRangeId + * the first commit (not included in results) + * @param endRangeId + * the end commit (included in results) + * @return a list of commits + */ + public static List<RevCommit> getRevLog(Repository repository, String startRangeId, + String endRangeId) { + List<RevCommit> list = new ArrayList<RevCommit>(); + if (!hasCommits(repository)) { + return list; + } + try { + ObjectId endRange = repository.resolve(endRangeId); + ObjectId startRange = repository.resolve(startRangeId); + + RevWalk rw = new RevWalk(repository); + rw.markStart(rw.parseCommit(endRange)); + if (startRange.equals(ObjectId.zeroId())) { + // maybe this is a tag or an orphan branch + list.add(rw.parseCommit(endRange)); + rw.dispose(); + return list; + } else { + rw.markUninteresting(rw.parseCommit(startRange)); + } + + Iterable<RevCommit> revlog = rw; + for (RevCommit rev : revlog) { + list.add(rev); + } + rw.dispose(); + } catch (Throwable t) { + error(t, repository, "{0} failed to get revlog for {1}..{2}", startRangeId, endRangeId); } return list; } @@ -1187,6 +1284,39 @@ } /** + * Returns a RefModel for the gh-pages branch in the repository. If the + * branch can not be found, null is returned. + * + * @param repository + * @return a refmodel for the gh-pages branch or null + */ + public static RefModel getPagesBranch(Repository repository) { + RefModel ghPages = null; + try { + // search for gh-pages branch in local heads + for (RefModel ref : JGitUtils.getLocalBranches(repository, false, -1)) { + if (ref.displayName.endsWith("gh-pages")) { + ghPages = ref; + break; + } + } + + // search for gh-pages branch in remote heads + if (ghPages == null) { + for (RefModel ref : JGitUtils.getRemoteBranches(repository, false, -1)) { + if (ref.displayName.endsWith("gh-pages")) { + ghPages = ref; + break; + } + } + } + } catch (Throwable t) { + LOGGER.error("Failed to find gh-pages branch!", t); + } + return ghPages; + } + + /** * Returns the list of notes entered about the commit from the refs/notes * namespace. If the repository does not exist or is empty, an empty list is * returned. -- Gitblit v1.9.1