Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

RefLogUtils.java 23KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /*
  2. * Copyright 2013 gitblit.com.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit.utils;
  17. import java.io.IOException;
  18. import java.text.DateFormat;
  19. import java.text.MessageFormat;
  20. import java.text.SimpleDateFormat;
  21. import java.util.ArrayList;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.Date;
  25. import java.util.HashMap;
  26. import java.util.List;
  27. import java.util.Map;
  28. import java.util.Set;
  29. import java.util.TimeZone;
  30. import java.util.TreeSet;
  31. import org.eclipse.jgit.api.errors.ConcurrentRefUpdateException;
  32. import org.eclipse.jgit.api.errors.JGitInternalException;
  33. import org.eclipse.jgit.dircache.DirCache;
  34. import org.eclipse.jgit.dircache.DirCacheBuilder;
  35. import org.eclipse.jgit.dircache.DirCacheEntry;
  36. import org.eclipse.jgit.internal.JGitText;
  37. import org.eclipse.jgit.lib.CommitBuilder;
  38. import org.eclipse.jgit.lib.FileMode;
  39. import org.eclipse.jgit.lib.ObjectId;
  40. import org.eclipse.jgit.lib.ObjectInserter;
  41. import org.eclipse.jgit.lib.PersonIdent;
  42. import org.eclipse.jgit.lib.RefRename;
  43. import org.eclipse.jgit.lib.RefUpdate;
  44. import org.eclipse.jgit.lib.RefUpdate.Result;
  45. import org.eclipse.jgit.lib.Repository;
  46. import org.eclipse.jgit.revwalk.RevCommit;
  47. import org.eclipse.jgit.revwalk.RevWalk;
  48. import org.eclipse.jgit.transport.ReceiveCommand;
  49. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  50. import org.eclipse.jgit.treewalk.TreeWalk;
  51. import org.slf4j.Logger;
  52. import org.slf4j.LoggerFactory;
  53. import com.gitblit.Constants;
  54. import com.gitblit.models.DailyLogEntry;
  55. import com.gitblit.models.PathModel.PathChangeModel;
  56. import com.gitblit.models.RefLogEntry;
  57. import com.gitblit.models.RefModel;
  58. import com.gitblit.models.RepositoryCommit;
  59. import com.gitblit.models.UserModel;
  60. /**
  61. * Utility class for maintaining a reflog within a git repository on an
  62. * orphan branch.
  63. *
  64. * @author James Moger
  65. *
  66. */
  67. public class RefLogUtils {
  68. private static final String GB_REFLOG = "refs/gitblit/reflog";
  69. private static final Logger LOGGER = LoggerFactory.getLogger(RefLogUtils.class);
  70. /**
  71. * Log an error message and exception.
  72. *
  73. * @param t
  74. * @param repository
  75. * if repository is not null it MUST be the {0} parameter in the
  76. * pattern.
  77. * @param pattern
  78. * @param objects
  79. */
  80. private static void error(Throwable t, Repository repository, String pattern, Object... objects) {
  81. List<Object> parameters = new ArrayList<Object>();
  82. if (objects != null && objects.length > 0) {
  83. for (Object o : objects) {
  84. parameters.add(o);
  85. }
  86. }
  87. if (repository != null) {
  88. parameters.add(0, repository.getDirectory().getAbsolutePath());
  89. }
  90. LOGGER.error(MessageFormat.format(pattern, parameters.toArray()), t);
  91. }
  92. /**
  93. * Returns a RefModel for the reflog branch in the repository. If the
  94. * branch can not be found, null is returned.
  95. *
  96. * @param repository
  97. * @return a refmodel for the reflog branch or null
  98. */
  99. public static RefModel getRefLogBranch(Repository repository) {
  100. List<RefModel> refs = JGitUtils.getRefs(repository, com.gitblit.Constants.R_GITBLIT);
  101. RefModel pushLog = null;
  102. final String GB_PUSHES = "refs/gitblit/pushes";
  103. for (RefModel ref : refs) {
  104. if (ref.reference.getName().equals(GB_REFLOG)) {
  105. return ref;
  106. } else if (ref.reference.getName().equals(GB_PUSHES)) {
  107. pushLog = ref;
  108. }
  109. }
  110. if (pushLog != null) {
  111. // rename refs/gitblit/pushes to refs/gitblit/reflog
  112. RefRename cmd;
  113. try {
  114. cmd = repository.renameRef(GB_PUSHES, GB_REFLOG);
  115. cmd.setRefLogIdent(new PersonIdent("Gitblit", "gitblit@localhost"));
  116. cmd.setRefLogMessage("renamed " + GB_PUSHES + " => " + GB_REFLOG);
  117. Result res = cmd.rename();
  118. switch (res) {
  119. case RENAMED:
  120. return getRefLogBranch(repository);
  121. default:
  122. LOGGER.error("failed to rename " + GB_PUSHES + " => " + GB_REFLOG + " (" + res.name() + ")");
  123. }
  124. } catch (IOException e) {
  125. LOGGER.error("failed to rename pushlog", e);
  126. }
  127. }
  128. return null;
  129. }
  130. private static UserModel newUserModelFrom(PersonIdent ident) {
  131. String name = ident.getName();
  132. String username;
  133. String displayname;
  134. if (name.indexOf('/') > -1) {
  135. int slash = name.indexOf('/');
  136. displayname = name.substring(0, slash);
  137. username = name.substring(slash + 1);
  138. } else {
  139. displayname = name;
  140. username = ident.getEmailAddress();
  141. }
  142. UserModel user = new UserModel(username);
  143. user.displayName = displayname;
  144. user.emailAddress = ident.getEmailAddress();
  145. return user;
  146. }
  147. /**
  148. * Updates the reflog with the received commands.
  149. *
  150. * @param user
  151. * @param repository
  152. * @param commands
  153. * @return true, if the update was successful
  154. */
  155. public static boolean updateRefLog(UserModel user, Repository repository,
  156. Collection<ReceiveCommand> commands) {
  157. RefModel reflogBranch = getRefLogBranch(repository);
  158. if (reflogBranch == null) {
  159. JGitUtils.createOrphanBranch(repository, GB_REFLOG, null);
  160. }
  161. boolean success = false;
  162. String message = "push";
  163. try {
  164. ObjectId headId = repository.resolve(GB_REFLOG + "^{commit}");
  165. ObjectInserter odi = repository.newObjectInserter();
  166. try {
  167. // Create the in-memory index of the push log entry
  168. DirCache index = createIndex(repository, headId, commands);
  169. ObjectId indexTreeId = index.writeTree(odi);
  170. PersonIdent ident;
  171. if (UserModel.ANONYMOUS.equals(user)) {
  172. // anonymous push
  173. ident = new PersonIdent("anonymous", "anonymous");
  174. } else {
  175. // construct real pushing account
  176. ident = new PersonIdent(MessageFormat.format("{0}/{1}", user.getDisplayName(), user.username),
  177. user.emailAddress == null ? user.username : user.emailAddress);
  178. }
  179. // Create a commit object
  180. CommitBuilder commit = new CommitBuilder();
  181. commit.setAuthor(ident);
  182. commit.setCommitter(ident);
  183. commit.setEncoding(Constants.ENCODING);
  184. commit.setMessage(message);
  185. commit.setParentId(headId);
  186. commit.setTreeId(indexTreeId);
  187. // Insert the commit into the repository
  188. ObjectId commitId = odi.insert(commit);
  189. odi.flush();
  190. RevWalk revWalk = new RevWalk(repository);
  191. try {
  192. RevCommit revCommit = revWalk.parseCommit(commitId);
  193. RefUpdate ru = repository.updateRef(GB_REFLOG);
  194. ru.setNewObjectId(commitId);
  195. ru.setExpectedOldObjectId(headId);
  196. ru.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
  197. Result rc = ru.forceUpdate();
  198. switch (rc) {
  199. case NEW:
  200. case FORCED:
  201. case FAST_FORWARD:
  202. success = true;
  203. break;
  204. case REJECTED:
  205. case LOCK_FAILURE:
  206. throw new ConcurrentRefUpdateException(JGitText.get().couldNotLockHEAD,
  207. ru.getRef(), rc);
  208. default:
  209. throw new JGitInternalException(MessageFormat.format(
  210. JGitText.get().updatingRefFailed, GB_REFLOG, commitId.toString(),
  211. rc));
  212. }
  213. } finally {
  214. revWalk.release();
  215. }
  216. } finally {
  217. odi.release();
  218. }
  219. } catch (Throwable t) {
  220. error(t, repository, "Failed to commit reflog entry to {0}");
  221. }
  222. return success;
  223. }
  224. /**
  225. * Creates an in-memory index of the push log entry.
  226. *
  227. * @param repo
  228. * @param headId
  229. * @param commands
  230. * @return an in-memory index
  231. * @throws IOException
  232. */
  233. private static DirCache createIndex(Repository repo, ObjectId headId,
  234. Collection<ReceiveCommand> commands) throws IOException {
  235. DirCache inCoreIndex = DirCache.newInCore();
  236. DirCacheBuilder dcBuilder = inCoreIndex.builder();
  237. ObjectInserter inserter = repo.newObjectInserter();
  238. long now = System.currentTimeMillis();
  239. Set<String> ignorePaths = new TreeSet<String>();
  240. try {
  241. // add receive commands to the temporary index
  242. for (ReceiveCommand command : commands) {
  243. // use the ref names as the path names
  244. String path = command.getRefName();
  245. ignorePaths.add(path);
  246. StringBuilder change = new StringBuilder();
  247. change.append(command.getType().name()).append(' ');
  248. switch (command.getType()) {
  249. case CREATE:
  250. change.append(ObjectId.zeroId().getName());
  251. change.append(' ');
  252. change.append(command.getNewId().getName());
  253. break;
  254. case UPDATE:
  255. case UPDATE_NONFASTFORWARD:
  256. change.append(command.getOldId().getName());
  257. change.append(' ');
  258. change.append(command.getNewId().getName());
  259. break;
  260. case DELETE:
  261. change = null;
  262. break;
  263. }
  264. if (change == null) {
  265. // ref deleted
  266. continue;
  267. }
  268. String content = change.toString();
  269. // create an index entry for this attachment
  270. final DirCacheEntry dcEntry = new DirCacheEntry(path);
  271. dcEntry.setLength(content.length());
  272. dcEntry.setLastModified(now);
  273. dcEntry.setFileMode(FileMode.REGULAR_FILE);
  274. // insert object
  275. dcEntry.setObjectId(inserter.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, content.getBytes("UTF-8")));
  276. // add to temporary in-core index
  277. dcBuilder.add(dcEntry);
  278. }
  279. // Traverse HEAD to add all other paths
  280. TreeWalk treeWalk = new TreeWalk(repo);
  281. int hIdx = -1;
  282. if (headId != null)
  283. hIdx = treeWalk.addTree(new RevWalk(repo).parseTree(headId));
  284. treeWalk.setRecursive(true);
  285. while (treeWalk.next()) {
  286. String path = treeWalk.getPathString();
  287. CanonicalTreeParser hTree = null;
  288. if (hIdx != -1)
  289. hTree = treeWalk.getTree(hIdx, CanonicalTreeParser.class);
  290. if (!ignorePaths.contains(path)) {
  291. // add entries from HEAD for all other paths
  292. if (hTree != null) {
  293. // create a new DirCacheEntry with data retrieved from
  294. // HEAD
  295. final DirCacheEntry dcEntry = new DirCacheEntry(path);
  296. dcEntry.setObjectId(hTree.getEntryObjectId());
  297. dcEntry.setFileMode(hTree.getEntryFileMode());
  298. // add to temporary in-core index
  299. dcBuilder.add(dcEntry);
  300. }
  301. }
  302. }
  303. // release the treewalk
  304. treeWalk.release();
  305. // finish temporary in-core index used for this commit
  306. dcBuilder.finish();
  307. } finally {
  308. inserter.release();
  309. }
  310. return inCoreIndex;
  311. }
  312. public static List<RefLogEntry> getRefLog(String repositoryName, Repository repository) {
  313. return getRefLog(repositoryName, repository, null, 0, -1);
  314. }
  315. public static List<RefLogEntry> getRefLog(String repositoryName, Repository repository, int maxCount) {
  316. return getRefLog(repositoryName, repository, null, 0, maxCount);
  317. }
  318. public static List<RefLogEntry> getRefLog(String repositoryName, Repository repository, int offset, int maxCount) {
  319. return getRefLog(repositoryName, repository, null, offset, maxCount);
  320. }
  321. public static List<RefLogEntry> getRefLog(String repositoryName, Repository repository, Date minimumDate) {
  322. return getRefLog(repositoryName, repository, minimumDate, 0, -1);
  323. }
  324. /**
  325. * Returns the list of reflog entries as they were recorded by Gitblit.
  326. * Each RefLogEntry may represent multiple ref updates.
  327. *
  328. * @param repositoryName
  329. * @param repository
  330. * @param minimumDate
  331. * @param offset
  332. * @param maxCount
  333. * if < 0, all pushes are returned.
  334. * @return a list of push log entries
  335. */
  336. public static List<RefLogEntry> getRefLog(String repositoryName, Repository repository,
  337. Date minimumDate, int offset, int maxCount) {
  338. List<RefLogEntry> list = new ArrayList<RefLogEntry>();
  339. RefModel ref = getRefLogBranch(repository);
  340. if (ref == null) {
  341. return list;
  342. }
  343. if (maxCount == 0) {
  344. return list;
  345. }
  346. Map<ObjectId, List<RefModel>> allRefs = JGitUtils.getAllRefs(repository);
  347. List<RevCommit> pushes;
  348. if (minimumDate == null) {
  349. pushes = JGitUtils.getRevLog(repository, GB_REFLOG, offset, maxCount);
  350. } else {
  351. pushes = JGitUtils.getRevLog(repository, GB_REFLOG, minimumDate);
  352. }
  353. for (RevCommit push : pushes) {
  354. if (push.getAuthorIdent().getName().equalsIgnoreCase("gitblit")) {
  355. // skip gitblit/internal commits
  356. continue;
  357. }
  358. UserModel user = newUserModelFrom(push.getAuthorIdent());
  359. Date date = push.getAuthorIdent().getWhen();
  360. RefLogEntry log = new RefLogEntry(repositoryName, date, user);
  361. list.add(log);
  362. List<PathChangeModel> changedRefs = JGitUtils.getFilesInCommit(repository, push);
  363. for (PathChangeModel change : changedRefs) {
  364. switch (change.changeType) {
  365. case DELETE:
  366. log.updateRef(change.path, ReceiveCommand.Type.DELETE);
  367. break;
  368. case ADD:
  369. log.updateRef(change.path, ReceiveCommand.Type.CREATE);
  370. default:
  371. String content = JGitUtils.getStringContent(repository, push.getTree(), change.path);
  372. String [] fields = content.split(" ");
  373. String oldId = fields[1];
  374. String newId = fields[2];
  375. log.updateRef(change.path, ReceiveCommand.Type.valueOf(fields[0]), oldId, newId);
  376. List<RevCommit> pushedCommits = JGitUtils.getRevLog(repository, oldId, newId);
  377. for (RevCommit pushedCommit : pushedCommits) {
  378. RepositoryCommit repoCommit = log.addCommit(change.path, pushedCommit);
  379. if (repoCommit != null) {
  380. repoCommit.setRefs(allRefs.get(pushedCommit.getId()));
  381. }
  382. }
  383. }
  384. }
  385. }
  386. Collections.sort(list);
  387. return list;
  388. }
  389. /**
  390. * Returns the list of pushes separated by ref (e.g. each ref has it's own
  391. * PushLogEntry object).
  392. *
  393. * @param repositoryName
  394. * @param repository
  395. * @param maxCount
  396. * @return a list of push log entries separated by ref
  397. */
  398. public static List<RefLogEntry> getLogByRef(String repositoryName, Repository repository, int maxCount) {
  399. return getLogByRef(repositoryName, repository, 0, maxCount);
  400. }
  401. /**
  402. * Returns the list of pushes separated by ref (e.g. each ref has it's own
  403. * PushLogEntry object).
  404. *
  405. * @param repositoryName
  406. * @param repository
  407. * @param offset
  408. * @param maxCount
  409. * @return a list of push log entries separated by ref
  410. */
  411. public static List<RefLogEntry> getLogByRef(String repositoryName, Repository repository, int offset,
  412. int maxCount) {
  413. // break the push log into ref push logs and then merge them back into a list
  414. Map<String, List<RefLogEntry>> refMap = new HashMap<String, List<RefLogEntry>>();
  415. List<RefLogEntry> refLog = getRefLog(repositoryName, repository, offset, maxCount);
  416. for (RefLogEntry entry : refLog) {
  417. for (String ref : entry.getChangedRefs()) {
  418. if (!refMap.containsKey(ref)) {
  419. refMap.put(ref, new ArrayList<RefLogEntry>());
  420. }
  421. // construct new ref-specific ref change entry
  422. RefLogEntry refChange;
  423. if (entry instanceof DailyLogEntry) {
  424. // simulated push log from commits grouped by date
  425. refChange = new DailyLogEntry(entry.repository, entry.date);
  426. } else {
  427. // real push log entry
  428. refChange = new RefLogEntry(entry.repository, entry.date, entry.user);
  429. }
  430. refChange.updateRef(ref, entry.getChangeType(ref), entry.getOldId(ref), entry.getNewId(ref));
  431. refChange.addCommits(entry.getCommits(ref));
  432. refMap.get(ref).add(refChange);
  433. }
  434. }
  435. // merge individual ref changes into master list
  436. List<RefLogEntry> mergedRefLog = new ArrayList<RefLogEntry>();
  437. for (List<RefLogEntry> refPush : refMap.values()) {
  438. mergedRefLog.addAll(refPush);
  439. }
  440. // sort ref log
  441. Collections.sort(mergedRefLog);
  442. return mergedRefLog;
  443. }
  444. /**
  445. * Returns the list of ref changes separated by ref (e.g. each ref has it's own
  446. * RefLogEntry object).
  447. *
  448. * @param repositoryName
  449. * @param repository
  450. * @param minimumDate
  451. * @return a list of ref log entries separated by ref
  452. */
  453. public static List<RefLogEntry> getLogByRef(String repositoryName, Repository repository, Date minimumDate) {
  454. // break the push log into ref push logs and then merge them back into a list
  455. Map<String, List<RefLogEntry>> refMap = new HashMap<String, List<RefLogEntry>>();
  456. List<RefLogEntry> pushes = getRefLog(repositoryName, repository, minimumDate);
  457. for (RefLogEntry push : pushes) {
  458. for (String ref : push.getChangedRefs()) {
  459. if (!refMap.containsKey(ref)) {
  460. refMap.put(ref, new ArrayList<RefLogEntry>());
  461. }
  462. // construct new ref-specific push log entry
  463. RefLogEntry refPush = new RefLogEntry(push.repository, push.date, push.user);
  464. refPush.updateRef(ref, push.getChangeType(ref), push.getOldId(ref), push.getNewId(ref));
  465. refPush.addCommits(push.getCommits(ref));
  466. refMap.get(ref).add(refPush);
  467. }
  468. }
  469. // merge individual ref pushes into master list
  470. List<RefLogEntry> refPushLog = new ArrayList<RefLogEntry>();
  471. for (List<RefLogEntry> refPush : refMap.values()) {
  472. refPushLog.addAll(refPush);
  473. }
  474. // sort ref push log
  475. Collections.sort(refPushLog);
  476. return refPushLog;
  477. }
  478. /**
  479. * Returns a commit log grouped by day.
  480. *
  481. * @param repositoryName
  482. * @param repository
  483. * @param minimumDate
  484. * @param offset
  485. * @param maxCount
  486. * if < 0, all pushes are returned.
  487. * @param the timezone to use when aggregating commits by date
  488. * @return a list of grouped commit log entries
  489. */
  490. public static List<DailyLogEntry> getDailyLog(String repositoryName, Repository repository,
  491. Date minimumDate, int offset, int maxCount,
  492. TimeZone timezone) {
  493. DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
  494. df.setTimeZone(timezone);
  495. Map<ObjectId, List<RefModel>> allRefs = JGitUtils.getAllRefs(repository);
  496. Map<String, DailyLogEntry> tags = new HashMap<String, DailyLogEntry>();
  497. Map<String, DailyLogEntry> pulls = new HashMap<String, DailyLogEntry>();
  498. Map<String, DailyLogEntry> dailydigests = new HashMap<String, DailyLogEntry>();
  499. String linearParent = null;
  500. for (RefModel local : JGitUtils.getLocalBranches(repository, true, -1)) {
  501. if (!local.getDate().after(minimumDate)) {
  502. // branch not recently updated
  503. continue;
  504. }
  505. String branch = local.getName();
  506. List<RepositoryCommit> commits = CommitCache.instance().getCommits(repositoryName, repository, branch, minimumDate);
  507. linearParent = null;
  508. for (RepositoryCommit commit : commits) {
  509. if (linearParent != null) {
  510. if (!commit.getName().equals(linearParent)) {
  511. // only follow linear branch commits
  512. continue;
  513. }
  514. }
  515. Date date = commit.getCommitDate();
  516. String dateStr = df.format(date);
  517. if (!dailydigests.containsKey(dateStr)) {
  518. dailydigests.put(dateStr, new DailyLogEntry(repositoryName, date));
  519. }
  520. DailyLogEntry digest = dailydigests.get(dateStr);
  521. if (commit.getParentCount() == 0) {
  522. linearParent = null;
  523. digest.updateRef(branch, ReceiveCommand.Type.CREATE);
  524. } else {
  525. linearParent = commit.getParents()[0].getId().getName();
  526. digest.updateRef(branch, ReceiveCommand.Type.UPDATE, linearParent, commit.getName());
  527. }
  528. RepositoryCommit repoCommit = digest.addCommit(commit);
  529. if (repoCommit != null) {
  530. List<RefModel> matchedRefs = allRefs.get(commit.getId());
  531. repoCommit.setRefs(matchedRefs);
  532. if (!ArrayUtils.isEmpty(matchedRefs)) {
  533. for (RefModel ref : matchedRefs) {
  534. if (ref.getName().startsWith(Constants.R_TAGS)) {
  535. // treat tags as special events in the log
  536. if (!tags.containsKey(dateStr)) {
  537. UserModel tagUser = newUserModelFrom(commit.getAuthorIdent());
  538. Date tagDate = commit.getAuthorIdent().getWhen();
  539. tags.put(dateStr, new DailyLogEntry(repositoryName, tagDate, tagUser));
  540. }
  541. RefLogEntry tagEntry = tags.get(dateStr);
  542. tagEntry.updateRef(ref.getName(), ReceiveCommand.Type.CREATE);
  543. RepositoryCommit rc = repoCommit.clone(ref.getName());
  544. tagEntry.addCommit(rc);
  545. } else if (ref.getName().startsWith(Constants.R_PULL)) {
  546. // treat pull requests as special events in the log
  547. if (!pulls.containsKey(dateStr)) {
  548. UserModel commitUser = newUserModelFrom(commit.getAuthorIdent());
  549. Date commitDate = commit.getAuthorIdent().getWhen();
  550. pulls.put(dateStr, new DailyLogEntry(repositoryName, commitDate, commitUser));
  551. }
  552. RefLogEntry pullEntry = pulls.get(dateStr);
  553. pullEntry.updateRef(ref.getName(), ReceiveCommand.Type.CREATE);
  554. RepositoryCommit rc = repoCommit.clone(ref.getName());
  555. pullEntry.addCommit(rc);
  556. }
  557. }
  558. }
  559. }
  560. }
  561. }
  562. List<DailyLogEntry> list = new ArrayList<DailyLogEntry>(dailydigests.values());
  563. list.addAll(tags.values());
  564. //list.addAll(pulls.values());
  565. Collections.sort(list);
  566. return list;
  567. }
  568. /**
  569. * Returns the list of commits separated by ref (e.g. each ref has it's own
  570. * PushLogEntry object for each day).
  571. *
  572. * @param repositoryName
  573. * @param repository
  574. * @param minimumDate
  575. * @param the timezone to use when aggregating commits by date
  576. * @return a list of push log entries separated by ref and date
  577. */
  578. public static List<DailyLogEntry> getDailyLogByRef(String repositoryName, Repository repository,
  579. Date minimumDate, TimeZone timezone) {
  580. // break the push log into ref push logs and then merge them back into a list
  581. Map<String, List<DailyLogEntry>> refMap = new HashMap<String, List<DailyLogEntry>>();
  582. List<DailyLogEntry> pushes = getDailyLog(repositoryName, repository, minimumDate, 0, -1, timezone);
  583. for (DailyLogEntry push : pushes) {
  584. for (String ref : push.getChangedRefs()) {
  585. if (!refMap.containsKey(ref)) {
  586. refMap.put(ref, new ArrayList<DailyLogEntry>());
  587. }
  588. // construct new ref-specific push log entry
  589. DailyLogEntry refPush = new DailyLogEntry(push.repository, push.date, push.user);
  590. refPush.updateRef(ref, push.getChangeType(ref), push.getOldId(ref), push.getNewId(ref));
  591. refPush.addCommits(push.getCommits(ref));
  592. refMap.get(ref).add(refPush);
  593. }
  594. }
  595. // merge individual ref pushes into master list
  596. List<DailyLogEntry> refPushLog = new ArrayList<DailyLogEntry>();
  597. for (List<DailyLogEntry> refPush : refMap.values()) {
  598. for (DailyLogEntry entry : refPush) {
  599. if (entry.getCommitCount() > 0) {
  600. refPushLog.add(entry);
  601. }
  602. }
  603. }
  604. // sort ref push log
  605. Collections.sort(refPushLog);
  606. return refPushLog;
  607. }
  608. }