From cb285cbfddfc0b633d6b8cdb4dc0d2bd2b8b51ef Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 05 Jan 2012 17:34:05 -0500
Subject: [PATCH] Fixed bug in receive hook for repositories in subfolders

---
 src/com/gitblit/utils/JGitUtils.java |   97 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 97 insertions(+), 0 deletions(-)

diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java
index c61b3d9..d694ee2 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
@@ -925,6 +978,50 @@
 	}
 
 	/**
+	 * 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;
+	}
+
+	/**
 	 * Search the commit history for a case-insensitive match to the value.
 	 * Search results require a specified SearchType of AUTHOR, COMMITTER, or
 	 * COMMIT. Results may be paginated using offset and maxCount. If the

--
Gitblit v1.9.1