\r
import java.io.Serializable;\r
import java.util.ArrayList;\r
+import java.util.Collections;\r
import java.util.Date;\r
import java.util.HashMap;\r
+import java.util.LinkedHashSet;\r
import java.util.List;\r
import java.util.Map;\r
+import java.util.Set;\r
\r
import org.eclipse.jgit.lib.PersonIdent;\r
import org.eclipse.jgit.revwalk.RevCommit;\r
\r
public final Date endDate;\r
\r
- public final List<RepositoryCommit> commits;\r
+ private final Set<RepositoryCommit> commits;\r
\r
private final Map<String, Metric> authorMetrics;\r
\r
public Activity(Date date, long duration) {\r
startDate = date;\r
endDate = new Date(date.getTime() + duration);\r
- commits = new ArrayList<RepositoryCommit>();\r
+ commits = new LinkedHashSet<RepositoryCommit>();\r
authorMetrics = new HashMap<String, Metric>();\r
repositoryMetrics = new HashMap<String, Metric>();\r
}\r
\r
+ /**\r
+ * Adds a commit to the activity object as long as the commit is not a\r
+ * duplicate.\r
+ * \r
+ * @param repository\r
+ * @param branch\r
+ * @param commit\r
+ * @return a RepositoryCommit, if one was added. Null if this is duplicate\r
+ * commit\r
+ */\r
public RepositoryCommit addCommit(String repository, String branch, RevCommit commit) {\r
RepositoryCommit commitModel = new RepositoryCommit(repository, branch, commit);\r
- commits.add(commitModel);\r
-\r
- if (!repositoryMetrics.containsKey(repository)) {\r
- repositoryMetrics.put(repository, new Metric(repository));\r
- }\r
- repositoryMetrics.get(repository).count++;\r
+ if (commits.add(commitModel)) {\r
+ if (!repositoryMetrics.containsKey(repository)) {\r
+ repositoryMetrics.put(repository, new Metric(repository));\r
+ }\r
+ repositoryMetrics.get(repository).count++;\r
\r
- String author = commit.getAuthorIdent().getEmailAddress().toLowerCase();\r
- if (!authorMetrics.containsKey(author)) {\r
- authorMetrics.put(author, new Metric(author));\r
+ String author = commit.getAuthorIdent().getEmailAddress()\r
+ .toLowerCase();\r
+ if (!authorMetrics.containsKey(author)) {\r
+ authorMetrics.put(author, new Metric(author));\r
+ }\r
+ authorMetrics.get(author).count++;\r
+ return commitModel;\r
}\r
- authorMetrics.get(author).count++;\r
- return commitModel;\r
+ return null;\r
+ }\r
+ \r
+ public int getCommitCount() {\r
+ return commits.size();\r
+ }\r
+ \r
+ public List<RepositoryCommit> getCommits() {\r
+ List<RepositoryCommit> list = new ArrayList<RepositoryCommit>(commits);\r
+ Collections.sort(list);\r
+ return list;\r
}\r
\r
public Map<String, Metric> getAuthorMetrics() {\r
public PersonIdent getAuthorIdent() {\r
return commit.getAuthorIdent();\r
}\r
+ \r
+ @Override\r
+ public boolean equals(Object o) {\r
+ if (o instanceof RepositoryCommit) {\r
+ RepositoryCommit commit = (RepositoryCommit) o;\r
+ return repository.equals(commit.repository) && getName().equals(commit.getName());\r
+ }\r
+ return false;\r
+ }\r
+ \r
+ @Override\r
+ public int hashCode() {\r
+ return (repository + commit).hashCode();\r
+ }\r
\r
@Override\r
public int compareTo(RepositoryCommit o) {\r
import java.text.SimpleDateFormat;\r
import java.util.ArrayList;\r
import java.util.Calendar;\r
-import java.util.Collections;\r
import java.util.Date;\r
import java.util.HashMap;\r
import java.util.List;\r
* @param daysBack\r
* the number of days back from Now to collect\r
* @param objectId\r
- * the branch to retrieve. If this value is null the default\r
- * branch of the repository is used.\r
+ * the branch to retrieve. If this value is null or empty all\r
+ * branches are queried.\r
* @return\r
*/\r
public static List<Activity> getRecentActivity(List<RepositoryModel> models, int daysBack,\r
// Build a map of DailyActivity from the available repositories for the\r
// specified threshold date.\r
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");\r
+ df.setTimeZone(GitBlit.getTimezone());\r
Calendar cal = Calendar.getInstance();\r
+ cal.setTimeZone(GitBlit.getTimezone());\r
\r
Map<String, Activity> activity = new HashMap<String, Activity>();\r
for (RepositoryModel model : models) {\r
if (model.hasCommits && model.lastChange.after(thresholdDate)) {\r
- Repository repository = GitBlit.self().getRepository(model.name);\r
- List<RevCommit> commits = JGitUtils.getRevLog(repository, objectId, thresholdDate);\r
- Map<ObjectId, List<RefModel>> allRefs = JGitUtils.getAllRefs(repository);\r
- repository.close();\r
-\r
- // determine commit branch\r
- String branch = objectId;\r
- if (StringUtils.isEmpty(branch) && !commits.isEmpty()) {\r
- List<RefModel> headRefs = allRefs.get(commits.get(0).getId());\r
- List<String> localBranches = new ArrayList<String>();\r
- for (RefModel ref : headRefs) {\r
- if (ref.getName().startsWith(Constants.R_HEADS)) {\r
- localBranches.add(ref.getName().substring(Constants.R_HEADS.length()));\r
- }\r
- }\r
- // determine branch\r
- if (localBranches.size() == 1) {\r
- // only one branch, choose it\r
- branch = localBranches.get(0);\r
- } else if (localBranches.size() > 1) {\r
- if (localBranches.contains("master")) {\r
- // choose master\r
- branch = "master";\r
- } else {\r
- // choose first branch\r
- branch = localBranches.get(0);\r
- }\r
+ Repository repository = GitBlit.self()\r
+ .getRepository(model.name);\r
+ List<String> branches = new ArrayList<String>();\r
+ if (StringUtils.isEmpty(objectId)) {\r
+ for (RefModel local : JGitUtils.getLocalBranches(\r
+ repository, true, -1)) {\r
+ branches.add(local.getName());\r
}\r
+ } else {\r
+ branches.add(objectId);\r
}\r
+ Map<ObjectId, List<RefModel>> allRefs = JGitUtils\r
+ .getAllRefs(repository);\r
\r
- for (RevCommit commit : commits) {\r
- Date date = JGitUtils.getCommitDate(commit);\r
- String dateStr = df.format(date);\r
- if (!activity.containsKey(dateStr)) {\r
- // Normalize the date to midnight\r
- cal.setTime(date);\r
- cal.set(Calendar.HOUR_OF_DAY, 0);\r
- cal.set(Calendar.MINUTE, 0);\r
- cal.set(Calendar.SECOND, 0);\r
- cal.set(Calendar.MILLISECOND, 0);\r
- activity.put(dateStr, new Activity(cal.getTime()));\r
+ for (String branch : branches) {\r
+ String shortName = branch;\r
+ if (shortName.startsWith(Constants.R_HEADS)) {\r
+ shortName = shortName.substring(Constants.R_HEADS.length());\r
+ }\r
+ List<RevCommit> commits = JGitUtils.getRevLog(repository,\r
+ branch, thresholdDate);\r
+ for (RevCommit commit : commits) {\r
+ Date date = JGitUtils.getCommitDate(commit);\r
+ String dateStr = df.format(date);\r
+ if (!activity.containsKey(dateStr)) {\r
+ // Normalize the date to midnight\r
+ cal.setTime(date);\r
+ cal.set(Calendar.HOUR_OF_DAY, 0);\r
+ cal.set(Calendar.MINUTE, 0);\r
+ cal.set(Calendar.SECOND, 0);\r
+ cal.set(Calendar.MILLISECOND, 0);\r
+ activity.put(dateStr, new Activity(cal.getTime()));\r
+ }\r
+ RepositoryCommit commitModel = activity.get(dateStr)\r
+ .addCommit(model.name, shortName, commit);\r
+ if (commitModel != null) {\r
+ commitModel.setRefs(allRefs.get(commit.getId()));\r
+ }\r
}\r
- RepositoryCommit commitModel = activity.get(dateStr).addCommit(model.name,\r
- branch, commit);\r
- commitModel.setRefs(allRefs.get(commit.getId()));\r
}\r
+ \r
+ // close the repository\r
+ repository.close();\r
}\r
}\r
\r
List<Activity> recentActivity = new ArrayList<Activity>(activity.values());\r
- for (Activity daily : recentActivity) {\r
- Collections.sort(daily.commits);\r
- }\r
return recentActivity;\r
}\r
\r