From 13417cf9c6eec555b51da49742e47939d2f5715b Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Fri, 19 Oct 2012 22:47:33 -0400
Subject: [PATCH] Exclude submodules from zip downloads (issue 151)

---
 src/com/gitblit/utils/DiffUtils.java |   36 ++++++++++++++++++++++++++----------
 1 files changed, 26 insertions(+), 10 deletions(-)

diff --git a/src/com/gitblit/utils/DiffUtils.java b/src/com/gitblit/utils/DiffUtils.java
index beeb532..04b5b0b 100644
--- a/src/com/gitblit/utils/DiffUtils.java
+++ b/src/com/gitblit/utils/DiffUtils.java
@@ -25,7 +25,7 @@
 import org.eclipse.jgit.diff.DiffFormatter;
 import org.eclipse.jgit.diff.RawText;
 import org.eclipse.jgit.diff.RawTextComparator;
-import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.revwalk.RevTree;
@@ -48,6 +48,9 @@
 
 	private static final Logger LOGGER = LoggerFactory.getLogger(DiffUtils.class);
 
+	/**
+	 * Enumeration for the diff output types.
+	 */
 	public static enum DiffOutputType {
 		PLAIN, GITWEB, GITBLIT;
 
@@ -144,10 +147,15 @@
 			RevTree commitTree = commit.getTree();
 			RevTree baseTree;
 			if (baseCommit == null) {
-				final RevWalk rw = new RevWalk(repository);
-				RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
-				rw.dispose();
-				baseTree = parent.getTree();
+				if (commit.getParentCount() > 0) {
+					final RevWalk rw = new RevWalk(repository);
+					RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
+					rw.dispose();
+					baseTree = parent.getTree();
+				} else {
+					// FIXME initial commit. no parent?!
+					baseTree = commitTree;
+				}
 			} else {
 				baseTree = baseCommit.getTree();
 			}
@@ -205,9 +213,14 @@
 			RevTree commitTree = commit.getTree();
 			RevTree baseTree;
 			if (baseCommit == null) {
-				final RevWalk rw = new RevWalk(repository);
-				RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
-				baseTree = parent.getTree();
+				if (commit.getParentCount() > 0) {
+					final RevWalk rw = new RevWalk(repository);
+					RevCommit parent = rw.parseCommit(commit.getParent(0).getId());
+					baseTree = parent.getTree();
+				} else {
+					// FIXME initial commit. no parent?!
+					baseTree = commitTree;
+				}
 			} else {
 				baseTree = baseCommit.getTree();
 			}
@@ -243,12 +256,15 @@
 	public static List<AnnotatedLine> blame(Repository repository, String blobPath, String objectId) {
 		List<AnnotatedLine> lines = new ArrayList<AnnotatedLine>();
 		try {
+			ObjectId object;
 			if (StringUtils.isEmpty(objectId)) {
-				objectId = Constants.HEAD;
+				object = JGitUtils.getDefaultBranch(repository);
+			} else {
+				object = repository.resolve(objectId);
 			}
 			BlameCommand blameCommand = new BlameCommand(repository);
 			blameCommand.setFilePath(blobPath);
-			blameCommand.setStartCommit(repository.resolve(objectId));
+			blameCommand.setStartCommit(object);
 			BlameResult blameResult = blameCommand.call();
 			RawText rawText = blameResult.getResultContents();
 			int length = rawText.size();

--
Gitblit v1.9.1