* @param timezone
* @return list of metrics
*/
public static List<Metric> getDateMetrics(Repository repository, String objectId,
boolean includeTotal, String dateFormat, TimeZone timezone) {
Metric total = new Metric("TOTAL");
final Map<String, Metric> metricMap = new HashMap<String, Metric>();
if (JGitUtils.hasCommits(repository)) {
final List<RefModel> tags = JGitUtils.getTags(repository, true, -1);
final Map<ObjectId, RefModel> tagMap = new HashMap<ObjectId, RefModel>();
for (RefModel tag : tags) {
tagMap.put(tag.getReferencedObjectId(), tag);
}
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);
RevCommit lastCommit = revWalk.parseCommit(branchObject);
revWalk.markStart(lastCommit);
DateFormat df;
if (StringUtils.isEmpty(dateFormat)) {
// dynamically determine date format
RevCommit firstCommit = JGitUtils.getFirstCommit(repository,
branchObject.getName());
int diffDays = (lastCommit.getCommitTime() - firstCommit.getCommitTime())
/ (60 * 60 * 24);
total.duration = diffDays;
if (diffDays <= 365) {
// Days
df = new SimpleDateFormat("yyyy-MM-dd");
} else {
// Months
df = new SimpleDateFormat("yyyy-MM");
}
} else {
// use specified date format
df = new SimpleDateFormat(dateFormat);
}
df.setTimeZone(timezone);
Iterable<RevCommit> revlog = revWalk;
for (RevCommit rev : revlog) {
Date d = JGitUtils.getCommitDate(rev);
String p = df.format(d);
if (!metricMap.containsKey(p)) {
metricMap.put(p, new Metric(p));
}
Metric m = metricMap.get(p);
m.count++;
total.count++;
if (tagMap.containsKey(rev.getId())) {
m.tag++;
total.tag++;