You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PushLogEntry.java 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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.models;
  17. import java.io.Serializable;
  18. import java.text.MessageFormat;
  19. import java.util.ArrayList;
  20. import java.util.Collections;
  21. import java.util.Date;
  22. import java.util.HashMap;
  23. import java.util.HashSet;
  24. import java.util.LinkedHashSet;
  25. import java.util.List;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import org.eclipse.jgit.lib.Constants;
  29. import org.eclipse.jgit.lib.PersonIdent;
  30. import org.eclipse.jgit.revwalk.RevCommit;
  31. import org.eclipse.jgit.transport.ReceiveCommand;
  32. import com.gitblit.utils.StringUtils;
  33. /**
  34. * Model class to represent a push into a repository.
  35. *
  36. * @author James Moger
  37. */
  38. public class PushLogEntry implements Serializable, Comparable<PushLogEntry> {
  39. private static final long serialVersionUID = 1L;
  40. public final String repository;
  41. public final Date date;
  42. public final UserModel user;
  43. private final Set<RepositoryCommit> commits;
  44. private final Map<String, ReceiveCommand.Type> refUpdates;
  45. private final Map<String, String> refIdChanges;
  46. private int authorCount;
  47. /**
  48. * Constructor for specified duration of push from start date.
  49. *
  50. * @param repository
  51. * the repository that received the push
  52. * @param date
  53. * the date of the push
  54. * @param user
  55. * the user who pushed
  56. */
  57. public PushLogEntry(String repository, Date date, UserModel user) {
  58. this.repository = repository;
  59. this.date = date;
  60. this.user = user;
  61. this.commits = new LinkedHashSet<RepositoryCommit>();
  62. this.refUpdates = new HashMap<String, ReceiveCommand.Type>();
  63. this.refIdChanges = new HashMap<String, String>();
  64. this.authorCount = -1;
  65. }
  66. /**
  67. * Tracks the change type for the specified ref.
  68. *
  69. * @param ref
  70. * @param type
  71. */
  72. public void updateRef(String ref, ReceiveCommand.Type type) {
  73. if (!refUpdates.containsKey(ref)) {
  74. refUpdates.put(ref, type);
  75. }
  76. }
  77. /**
  78. * Tracks the change type for the specified ref.
  79. *
  80. * @param ref
  81. * @param type
  82. * @param oldId
  83. * @param newId
  84. */
  85. public void updateRef(String ref, ReceiveCommand.Type type, String oldId, String newId) {
  86. if (!refUpdates.containsKey(ref)) {
  87. refUpdates.put(ref, type);
  88. refIdChanges.put(ref, oldId + "-" + newId);
  89. }
  90. }
  91. /**
  92. * Returns the old id of a ref.
  93. *
  94. * @param ref
  95. * @return the old id
  96. */
  97. public String getOldId(String ref) {
  98. String change = refIdChanges.get(ref);
  99. if (StringUtils.isEmpty(change)) {
  100. return null;
  101. }
  102. return change.split("-")[0];
  103. }
  104. /**
  105. * Returns the new id of a ref
  106. *
  107. * @param ref
  108. * @return the new id
  109. */
  110. public String getNewId(String ref) {
  111. String change = refIdChanges.get(ref);
  112. if (StringUtils.isEmpty(change)) {
  113. return null;
  114. }
  115. return change.split("-")[1];
  116. }
  117. /**
  118. * Returns the change type of the ref change.
  119. *
  120. * @param ref
  121. * @return the change type for the ref
  122. */
  123. public ReceiveCommand.Type getChangeType(String ref) {
  124. ReceiveCommand.Type type = refUpdates.get(ref);
  125. return type;
  126. }
  127. /**
  128. * Adds a commit to the push entry object as long as the commit is not a
  129. * duplicate.
  130. *
  131. * @param branch
  132. * @param commit
  133. * @return a RepositoryCommit, if one was added. Null if this is duplicate
  134. * commit
  135. */
  136. public RepositoryCommit addCommit(String branch, RevCommit commit) {
  137. RepositoryCommit commitModel = new RepositoryCommit(repository, branch, commit);
  138. if (commits.add(commitModel)) {
  139. authorCount = -1;
  140. return commitModel;
  141. }
  142. return null;
  143. }
  144. /**
  145. * Adds a a list of repository commits. This is used to construct discrete
  146. * ref push log entries
  147. *
  148. * @param commits
  149. */
  150. public void addCommits(List<RepositoryCommit> list) {
  151. commits.addAll(list);
  152. authorCount = -1;
  153. }
  154. /**
  155. * Returns true if this push contains a non-fastforward ref update.
  156. *
  157. * @return true if this is a non-fastforward push
  158. */
  159. public boolean isNonFastForward() {
  160. for (Map.Entry<String, ReceiveCommand.Type> entry : refUpdates.entrySet()) {
  161. if (ReceiveCommand.Type.UPDATE_NONFASTFORWARD.equals(entry.getValue())) {
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167. /**
  168. * Returns true if this ref has been rewound.
  169. *
  170. * @param ref
  171. * @return true if this is a non-fastforward ref update
  172. */
  173. public boolean isNonFastForward(String ref) {
  174. ReceiveCommand.Type type = refUpdates.get(ref);
  175. if (type == null) {
  176. return false;
  177. }
  178. return ReceiveCommand.Type.UPDATE_NONFASTFORWARD.equals(type);
  179. }
  180. /**
  181. * Returns true if this ref has been deleted.
  182. *
  183. * @param ref
  184. * @return true if this is a delete ref update
  185. */
  186. public boolean isDelete(String ref) {
  187. ReceiveCommand.Type type = refUpdates.get(ref);
  188. if (type == null) {
  189. return false;
  190. }
  191. return ReceiveCommand.Type.DELETE.equals(type);
  192. }
  193. /**
  194. * Returns the list of refs changed by the push.
  195. *
  196. * @return a list of refs
  197. */
  198. public List<String> getChangedRefs() {
  199. return new ArrayList<String>(refUpdates.keySet());
  200. }
  201. /**
  202. * Returns the list of branches changed by the push.
  203. *
  204. * @return a list of branches
  205. */
  206. public List<String> getChangedBranches() {
  207. return getChangedRefs(Constants.R_HEADS);
  208. }
  209. /**
  210. * Returns the list of tags changed by the push.
  211. *
  212. * @return a list of tags
  213. */
  214. public List<String> getChangedTags() {
  215. return getChangedRefs(Constants.R_TAGS);
  216. }
  217. /**
  218. * Gets the changed refs in the push.
  219. *
  220. * @param baseRef
  221. * @return the changed refs
  222. */
  223. protected List<String> getChangedRefs(String baseRef) {
  224. Set<String> refs = new HashSet<String>();
  225. for (String ref : refUpdates.keySet()) {
  226. if (baseRef == null || ref.startsWith(baseRef)) {
  227. refs.add(ref);
  228. }
  229. }
  230. List<String> list = new ArrayList<String>(refs);
  231. Collections.sort(list);
  232. return list;
  233. }
  234. public int getAuthorCount() {
  235. if (authorCount == -1) {
  236. Set<String> authors = new HashSet<String>();
  237. for (RepositoryCommit commit : commits) {
  238. String name = commit.getAuthorIdent().getName();
  239. authors.add(name);
  240. }
  241. authorCount = authors.size();
  242. }
  243. return authorCount;
  244. }
  245. /**
  246. * The total number of commits in the push.
  247. *
  248. * @return the number of commits in the push
  249. */
  250. public int getCommitCount() {
  251. return commits.size();
  252. }
  253. /**
  254. * Returns all commits in the push.
  255. *
  256. * @return a list of commits
  257. */
  258. public List<RepositoryCommit> getCommits() {
  259. List<RepositoryCommit> list = new ArrayList<RepositoryCommit>(commits);
  260. Collections.sort(list);
  261. return list;
  262. }
  263. /**
  264. * Returns all commits that belong to a particular ref
  265. *
  266. * @param ref
  267. * @return a list of commits
  268. */
  269. public List<RepositoryCommit> getCommits(String ref) {
  270. List<RepositoryCommit> list = new ArrayList<RepositoryCommit>();
  271. for (RepositoryCommit commit : commits) {
  272. if (commit.branch.equals(ref)) {
  273. list.add(commit);
  274. }
  275. }
  276. Collections.sort(list);
  277. return list;
  278. }
  279. public PersonIdent getCommitterIdent() {
  280. return new PersonIdent(user.getDisplayName(), user.emailAddress == null ? user.username : user.emailAddress);
  281. }
  282. public PersonIdent getAuthorIdent() {
  283. return getCommitterIdent();
  284. }
  285. @Override
  286. public int compareTo(PushLogEntry o) {
  287. // reverse chronological order
  288. return o.date.compareTo(date);
  289. }
  290. @Override
  291. public String toString() {
  292. StringBuilder sb = new StringBuilder();
  293. sb.append(MessageFormat.format("{0,date,yyyy-MM-dd HH:mm}: {1} pushed {2,number,0} commit{3} to {4} ",
  294. date, user.getDisplayName(), commits.size(), commits.size() == 1 ? "":"s", repository));
  295. for (Map.Entry<String, ReceiveCommand.Type> entry : refUpdates.entrySet()) {
  296. String ref = entry.getKey();
  297. ReceiveCommand.Type type = entry.getValue();
  298. sb.append("\n ").append(ref).append(' ').append(type.name()).append('\n');
  299. for (RepositoryCommit commit : getCommits(ref)) {
  300. sb.append(" ").append(commit.toString()).append('\n');
  301. }
  302. }
  303. return sb.toString();
  304. }
  305. }