From 4fea450fd3edfba6bb9e2c3c0a9231c6d227a09c Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Thu, 23 Feb 2012 19:49:46 -0500
Subject: [PATCH] Fixed nullpointer on pushing to an empty repository (issue 69)

---
 src/com/gitblit/GitBlit.java                  |    2 +-
 docs/04_releases.mkd                          |    1 +
 tests/com/gitblit/tests/JGitUtilsTest.java    |    6 +++---
 src/com/gitblit/wicket/pages/SummaryPage.java |    2 +-
 src/com/gitblit/utils/JGitUtils.java          |   40 ++++++++++++++++++----------------------
 5 files changed, 24 insertions(+), 27 deletions(-)

diff --git a/docs/04_releases.mkd b/docs/04_releases.mkd
index 2208b57..3f5eb39 100644
--- a/docs/04_releases.mkd
+++ b/docs/04_releases.mkd
@@ -30,6 +30,7 @@
 
 #### fixes 
 
+- Fixed (harmless) nullpointer on pushing to an empty repository (issue 69)
 - Fixed possible nullpointer from the servlet container on startup (issue 67)
 - Fixed UTF-8 encoding bug on diff page (issue 66)
 - Fixed timezone bug on the activity page (issue 54)
diff --git a/src/com/gitblit/GitBlit.java b/src/com/gitblit/GitBlit.java
index 7fb325c..a57e605 100644
--- a/src/com/gitblit/GitBlit.java
+++ b/src/com/gitblit/GitBlit.java
@@ -780,7 +780,7 @@
 		RepositoryModel model = new RepositoryModel();
 		model.name = repositoryName;
 		model.hasCommits = JGitUtils.hasCommits(r);
-		model.lastChange = JGitUtils.getLastChange(r, null);
+		model.lastChange = JGitUtils.getLastChange(r);
 		model.isBare = r.isBare();
 		StoredConfig config = JGitUtils.readConfig(r);
 		if (config != null) {
diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java
index a9b99a9..5f193d0 100644
--- a/src/com/gitblit/utils/JGitUtils.java
+++ b/src/com/gitblit/utils/JGitUtils.java
@@ -423,11 +423,9 @@
 	 * last modified date of the repository folder is returned.
 	 * 
 	 * @param repository
-	 * @param branch
-	 *            if unspecified, all branches are checked.
 	 * @return
 	 */
-	public static Date getLastChange(Repository repository, String branch) {
+	public static Date getLastChange(Repository repository) {
 		if (!hasCommits(repository)) {
 			// null repository
 			if (repository == null) {
@@ -436,26 +434,21 @@
 			// fresh repository
 			return new Date(repository.getDirectory().lastModified());
 		}
-		if (StringUtils.isEmpty(branch)) {
-			List<RefModel> branchModels = getLocalBranches(repository, true, -1);
-			if (branchModels.size() > 0) {
-				// find most recent branch update
-				Date lastChange = new Date(0);
-				for (RefModel branchModel : branchModels) {
-					if (branchModel.getDate().after(lastChange)) {
-						lastChange = branchModel.getDate();
-					}
-				}
-				return lastChange;
-			} else {
-				// try to find head
-				branch = Constants.HEAD;
-			}
-		}
 
-		// lookup specified branch
-		RevCommit commit = getCommit(repository, branch);
-		return getCommitDate(commit);
+		List<RefModel> branchModels = getLocalBranches(repository, true, -1);
+		if (branchModels.size() > 0) {
+			// find most recent branch update
+			Date lastChange = new Date(0);
+			for (RefModel branchModel : branchModels) {
+				if (branchModel.getDate().after(lastChange)) {
+					lastChange = branchModel.getDate();
+				}
+			}
+			return lastChange;
+		}
+		
+		// default to the repository folder modification date
+		return new Date(repository.getDirectory().lastModified());
 	}
 
 	/**
@@ -962,6 +955,9 @@
 			} else {
 				branchObject = repository.resolve(objectId);
 			}
+			if (branchObject == null) {
+				return list;
+			}
 
 			RevWalk rw = new RevWalk(repository);
 			rw.markStart(rw.parseCommit(branchObject));
diff --git a/src/com/gitblit/wicket/pages/SummaryPage.java b/src/com/gitblit/wicket/pages/SummaryPage.java
index acb4180..12371f7 100644
--- a/src/com/gitblit/wicket/pages/SummaryPage.java
+++ b/src/com/gitblit/wicket/pages/SummaryPage.java
@@ -84,7 +84,7 @@
 		add(new Label("repositoryOwner", getRepositoryModel().owner));
 
 		add(WicketUtils.createTimestampLabel("repositoryLastChange",
-				JGitUtils.getLastChange(r, null), getTimeZone()));
+				JGitUtils.getLastChange(r), getTimeZone()));
 		if (metricsTotal == null) {
 			add(new Label("branchStats", ""));
 		} else {
diff --git a/tests/com/gitblit/tests/JGitUtilsTest.java b/tests/com/gitblit/tests/JGitUtilsTest.java
index e649769..74531fa 100644
--- a/tests/com/gitblit/tests/JGitUtilsTest.java
+++ b/tests/com/gitblit/tests/JGitUtilsTest.java
@@ -97,11 +97,11 @@
 
 	@Test
 	public void testLastCommit() throws Exception {
-		assertEquals(new Date(0), JGitUtils.getLastChange(null, null));
+		assertEquals(new Date(0), JGitUtils.getLastChange(null));
 
 		Repository repository = GitBlitSuite.getHelloworldRepository();
 		assertTrue(JGitUtils.getCommit(repository, null) != null);
-		Date date = JGitUtils.getLastChange(repository, null);
+		Date date = JGitUtils.getLastChange(repository);
 		repository.close();
 		assertNotNull("Could not get last repository change date!", date);
 	}
@@ -119,7 +119,7 @@
 			assertNull(JGitUtils.getFirstCommit(repository, null));
 			assertEquals(folder.lastModified(), JGitUtils.getFirstChange(repository, null)
 					.getTime());
-			assertEquals(folder.lastModified(), JGitUtils.getLastChange(repository, null).getTime());
+			assertEquals(folder.lastModified(), JGitUtils.getLastChange(repository).getTime());
 			assertNull(JGitUtils.getCommit(repository, null));
 			repository.close();
 			assertTrue(GitBlit.self().deleteRepository(repositoryName));

--
Gitblit v1.9.1