From a2709dd91e5d6b18f573016882ccc70799573ad3 Mon Sep 17 00:00:00 2001 From: James Moger <james.moger@gitblit.com> Date: Fri, 22 Jul 2011 17:47:53 -0400 Subject: [PATCH] Centralize default branch/HEAD resolution (issue 14) --- src/com/gitblit/utils/MetricUtils.java | 60 +++++++++++++++++++++++++++++++++++++++++++++++------------- 1 files changed, 47 insertions(+), 13 deletions(-) diff --git a/src/com/gitblit/utils/MetricUtils.java b/src/com/gitblit/utils/MetricUtils.java index acdacc4..bf63a95 100644 --- a/src/com/gitblit/utils/MetricUtils.java +++ b/src/com/gitblit/utils/MetricUtils.java @@ -16,6 +16,7 @@ package com.gitblit.utils; import java.text.DateFormat; +import java.text.MessageFormat; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; @@ -47,6 +48,29 @@ private static final Logger LOGGER = LoggerFactory.getLogger(MetricUtils.class); /** + * Log an error message and exception. + * + * @param t + * @param repository + * if repository is not null it MUST be the {0} parameter in the + * pattern. + * @param pattern + * @param objects + */ + private static void error(Throwable t, Repository repository, String pattern, Object... objects) { + List<Object> parameters = new ArrayList<Object>(); + if (objects != null && objects.length > 0) { + for (Object o : objects) { + parameters.add(o); + } + } + if (repository != null) { + parameters.add(0, repository.getDirectory().getAbsolutePath()); + } + LOGGER.error(MessageFormat.format(pattern, parameters.toArray()), t); + } + + /** * Returns the list of metrics for the specified commit reference, branch, * or tag within the repository. If includeTotal is true, the total of all * the metrics will be included as the first element in the returned list. @@ -67,9 +91,7 @@ boolean includeTotal, String dateFormat) { Metric total = new Metric("TOTAL"); final Map<String, Metric> metricMap = new HashMap<String, Metric>(); - if (StringUtils.isEmpty(objectId)) { - objectId = Constants.HEAD; - } + if (JGitUtils.hasCommits(repository)) { final List<RefModel> tags = JGitUtils.getTags(repository, true, -1); final Map<ObjectId, RefModel> tagMap = new HashMap<ObjectId, RefModel>(); @@ -78,15 +100,22 @@ } RevWalk revWalk = null; try { + // resolve branch + ObjectId branchObject; + if (StringUtils.isEmpty(objectId)) { + branchObject = JGitUtils.getDefaultBranch(repository); + } else { + branchObject = repository.resolve(objectId); + } + revWalk = new RevWalk(repository); - ObjectId object = repository.resolve(objectId); - RevCommit lastCommit = revWalk.parseCommit(object); + RevCommit lastCommit = revWalk.parseCommit(branchObject); revWalk.markStart(lastCommit); DateFormat df; if (StringUtils.isEmpty(dateFormat)) { // dynamically determine date format - RevCommit firstCommit = JGitUtils.getFirstCommit(repository, Constants.HEAD); + RevCommit firstCommit = JGitUtils.getFirstCommit(repository, branchObject.getName()); int diffDays = (lastCommit.getCommitTime() - firstCommit.getCommitTime()) / (60 * 60 * 24); total.duration = diffDays; @@ -118,7 +147,8 @@ } } } catch (Throwable t) { - LOGGER.error("Failed to mine log history for date metrics", t); + error(t, repository, "{0} failed to mine log history for date metrics of {1}", + objectId); } finally { if (revWalk != null) { revWalk.dispose(); @@ -150,14 +180,17 @@ public static List<Metric> getAuthorMetrics(Repository repository, String objectId, boolean byEmailAddress) { final Map<String, Metric> metricMap = new HashMap<String, Metric>(); - if (StringUtils.isEmpty(objectId)) { - objectId = Constants.HEAD; - } if (JGitUtils.hasCommits(repository)) { try { RevWalk walk = new RevWalk(repository); - ObjectId object = repository.resolve(objectId); - RevCommit lastCommit = walk.parseCommit(object); + // resolve branch + ObjectId branchObject; + if (StringUtils.isEmpty(objectId)) { + branchObject = JGitUtils.getDefaultBranch(repository); + } else { + branchObject = repository.resolve(objectId); + } + RevCommit lastCommit = walk.parseCommit(branchObject); walk.markStart(lastCommit); Iterable<RevCommit> revlog = walk; @@ -181,7 +214,8 @@ m.count++; } } catch (Throwable t) { - LOGGER.error("Failed to mine log history for author metrics", t); + error(t, repository, "{0} failed to mine log history for author metrics of {1}", + objectId); } } List<String> keys = new ArrayList<String>(metricMap.keySet()); -- Gitblit v1.9.1