private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
@Override
public ExecutionResults execute(CommandContext commandContext) throws Exception {
ExecutionResults results = new ExecutionResults();
String gitRepo = (String) getParameter(commandContext, "GitRepository");
String branchName = (String) getParameter(commandContext, "BranchName");
String compareToBranchName = (String) getParameter(commandContext, "CompareToBranchName");
String fromDate = (String) getParameter(commandContext, "FromDate");
Date startCommitDate = null;
if (fromDate != null) {
startCommitDate = dateFormat.parse(fromDate);
}
Set<String> existingCommits = new LinkedHashSet<String>();
BeanManager beanManager = CDIUtils.lookUpBeanManager(commandContext);
logger.debug("BeanManager " + beanManager);
IOService ioService = CDIUtils.createBean(IOService.class, beanManager, new NamedLiteral("ioStrategy"));
logger.debug("IoService " + ioService);
Path branchPath = ioService.get(URI.create("git://" + branchName + "@" + gitRepo));
if (compareToBranchName != null) {
Path compareToBranchPath = ioService.get(URI.create("git://" + compareToBranchName + "@" + gitRepo));
VersionAttributeView compareView = ioService.getFileAttributeView( compareToBranchPath, VersionAttributeView.class );
List<VersionRecord> compareLogs = compareView.readAttributes().history().records();
for (VersionRecord ccommit : compareLogs) {
if (startCommitDate != null && startCommitDate.after(ccommit.date())) {
break;
}
existingCommits.add(ccommit.id());
}
}
VersionAttributeView vinit = ioService.getFileAttributeView( branchPath, VersionAttributeView.class );
List<VersionRecord> logs = vinit.readAttributes().history().records();
List<CommitInfo> commits = new ArrayList<CommitInfo>();
JGitFileSystem fs = (JGitFileSystem)ioService.getFileSystem(branchPath.toUri());
Collections.reverse(logs);
for (VersionRecord commit : logs) {
// check if there are already commits in compare to branch
if (existingCommits.contains(commit.id())) {
continue;
}
String shortMessage = commit.comment();
Date commitDate = commit.date();
if (startCommitDate != null && startCommitDate.after(commitDate)) {
break;
}
List<String> files = getFilesInCommit(fs.gitRepo().getRepository(), JGitUtil.resolveObjectId(fs.gitRepo(), commit.id()));
CommitInfo commitInfo = new CommitInfo(commit.id(), shortMessage, commit.author(), commitDate, files);
commits.add(commitInfo);
logger.debug("Found commit {}", commitInfo);
}
String commitsString = dumpToStringCommit(commits);
Map<String, List<String>> commitsPerFileMap = sortByFileName(commits);
results.setData("Commits", commits);
results.setData("CommitsPerFile", commitsPerFileMap);
results.setData("CommitsPerFileString", dumpToStringFiles(commitsPerFileMap.keySet()));
results.setData("CommitsPerFileMap", commitsPerFileMap);
results.setData("CommitsString", commitsString);
return results;
}