From fbe265fa787e4be9cb63c6bae2ef30b9400d9afc Mon Sep 17 00:00:00 2001
From: Simon Harrer <simon.harrer@gmail.com>
Date: Thu, 18 Jul 2013 10:11:04 -0400
Subject: [PATCH] Fixes findbugs warning - dereferencing null in exception case

---
 src/main/java/com/gitblit/utils/ActivityUtils.java |   60 ++++++++++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 44 insertions(+), 16 deletions(-)

diff --git a/src/main/java/com/gitblit/utils/ActivityUtils.java b/src/main/java/com/gitblit/utils/ActivityUtils.java
index 732fdeb..c4e9587 100644
--- a/src/main/java/com/gitblit/utils/ActivityUtils.java
+++ b/src/main/java/com/gitblit/utils/ActivityUtils.java
@@ -27,14 +27,15 @@
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.TimeZone;
+import java.util.TreeSet;
 
 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 com.gitblit.GitBlit;
+import com.gitblit.Keys;
 import com.gitblit.models.Activity;
 import com.gitblit.models.GravatarProfile;
 import com.gitblit.models.RefModel;
@@ -78,10 +79,19 @@
 		df.setTimeZone(timezone);
 		Calendar cal = Calendar.getInstance();
 		cal.setTimeZone(timezone);
+		
+		// aggregate author exclusions
+		Set<String> authorExclusions = new TreeSet<String>();
+		authorExclusions.addAll(GitBlit.getStrings(Keys.web.metricAuthorExclusions));
+		for (RepositoryModel model : models) {
+			if (!ArrayUtils.isEmpty(model.metricAuthorExclusions)) {
+				authorExclusions.addAll(model.metricAuthorExclusions);
+			}
+		}
 
 		Map<String, Activity> activity = new HashMap<String, Activity>();
 		for (RepositoryModel model : models) {
-			if (model.maxActivityCommits == -1) {
+			if (!model.isShowActivity()) {
 				// skip this repository
 				continue;
 			}
@@ -95,27 +105,28 @@
 				if (StringUtils.isEmpty(objectId)) {
 					for (RefModel local : JGitUtils.getLocalBranches(
 							repository, true, -1)) {
+			        	if (!local.getDate().after(thresholdDate)) {
+							// branch not recently updated
+			        		continue;
+			        	}
 						branches.add(local.getName());
 					}
 				} else {
 					branches.add(objectId);
 				}
-				Map<ObjectId, List<RefModel>> allRefs = JGitUtils
-						.getAllRefs(repository, model.showRemoteBranches);
 
 				for (String branch : branches) {
 					String shortName = branch;
 					if (shortName.startsWith(Constants.R_HEADS)) {
 						shortName = shortName.substring(Constants.R_HEADS.length());
 					}
-					List<RevCommit> commits = JGitUtils.getRevLog(repository,
-							branch, thresholdDate);
+					List<RepositoryCommit> commits = CommitCache.instance().getCommits(model.name, repository, branch, thresholdDate);
 					if (model.maxActivityCommits > 0 && commits.size() > model.maxActivityCommits) {
 						// trim commits to maximum count
 						commits = commits.subList(0,  model.maxActivityCommits);
 					}
-					for (RevCommit commit : commits) {						
-						Date date = JGitUtils.getCommitDate(commit);
+					for (RepositoryCommit commit : commits) {						
+						Date date = commit.getCommitDate();
 						String dateStr = df.format(date);
 						if (!activity.containsKey(dateStr)) {
 							// Normalize the date to midnight
@@ -124,13 +135,11 @@
 							cal.set(Calendar.MINUTE, 0);
 							cal.set(Calendar.SECOND, 0);
 							cal.set(Calendar.MILLISECOND, 0);
-							activity.put(dateStr, new Activity(cal.getTime()));
+							Activity a = new Activity(cal.getTime());
+							a.excludeAuthors(authorExclusions);
+							activity.put(dateStr, a);
 						}
-						RepositoryCommit commitModel = activity.get(dateStr)
-								.addCommit(model.name, shortName, commit);
-						if (commitModel != null) {
-							commitModel.setRefs(allRefs.get(commit.getId()));
-						}
+						activity.get(dateStr).addCommit(commit);
 					}
 				}
 				
@@ -165,7 +174,7 @@
 	 *            size of thumbnail. if width <= 0, the default of 50 is used.
 	 * @return
 	 */
-	public static String getGravatarThumbnailUrl(String email, int width) {
+	public static String getGravatarIdenticonUrl(String email, int width) {
 		if (width <= 0) {
 			width = 50;
 		}
@@ -174,6 +183,25 @@
 				"https://www.gravatar.com/avatar/{0}?s={1,number,0}&d=identicon", emailHash, width);
 		return url;
 	}
+	
+	/**
+	 * Creates a Gravatar thumbnail url from the specified email address.
+	 * 
+	 * @param email
+	 *            address to query Gravatar
+	 * @param width
+	 *            size of thumbnail. if width <= 0, the default of 50 is used.
+	 * @return
+	 */
+	public static String getGravatarThumbnailUrl(String email, int width) {
+		if (width <= 0) {
+			width = 50;
+		}
+		String emailHash = StringUtils.getMD5(email);
+		String url = MessageFormat.format(
+				"https://www.gravatar.com/avatar/{0}?s={1,number,0}&d=mm", emailHash, width);
+		return url;
+	}
 
 	/**
 	 * Returns the Gravatar profile, if available, for the specified hashcode.

--
Gitblit v1.9.1