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.

RepositoryModel.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright 2011 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.util.ArrayList;
  19. import java.util.Collection;
  20. import java.util.Date;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Set;
  24. import java.util.TreeSet;
  25. import com.gitblit.Constants.AccessRestrictionType;
  26. import com.gitblit.Constants.AuthorizationControl;
  27. import com.gitblit.Constants.CommitMessageRenderer;
  28. import com.gitblit.Constants.FederationStrategy;
  29. import com.gitblit.Constants.MergeType;
  30. import com.gitblit.utils.ArrayUtils;
  31. import com.gitblit.utils.ModelUtils;
  32. import com.gitblit.utils.StringUtils;
  33. /**
  34. * RepositoryModel is a serializable model class that represents a Gitblit
  35. * repository including its configuration settings and access restriction.
  36. *
  37. * @author James Moger
  38. *
  39. */
  40. public class RepositoryModel implements Serializable, Comparable<RepositoryModel> {
  41. private static final long serialVersionUID = 1L;
  42. // field names are reflectively mapped in EditRepository page
  43. public String name;
  44. public String description;
  45. public List<String> owners;
  46. public Date lastChange;
  47. public String lastChangeAuthor;
  48. public boolean hasCommits;
  49. public boolean showRemoteBranches;
  50. public boolean useIncrementalPushTags;
  51. public String incrementalPushTagPrefix;
  52. public AccessRestrictionType accessRestriction;
  53. public AuthorizationControl authorizationControl;
  54. public boolean allowAuthenticated;
  55. public boolean isFrozen;
  56. public FederationStrategy federationStrategy;
  57. public List<String> federationSets;
  58. public boolean isFederated;
  59. public boolean skipSizeCalculation;
  60. public boolean skipSummaryMetrics;
  61. public String frequency;
  62. public boolean isBare;
  63. public boolean isMirror;
  64. public String origin;
  65. public String HEAD;
  66. public List<String> availableRefs;
  67. public List<String> indexedBranches;
  68. public String size;
  69. public List<String> preReceiveScripts;
  70. public List<String> postReceiveScripts;
  71. public List<String> mailingLists;
  72. public Map<String, String> customFields;
  73. public String projectPath;
  74. private String displayName;
  75. public boolean allowForks;
  76. public Set<String> forks;
  77. public String originRepository;
  78. public boolean verifyCommitter;
  79. public String gcThreshold;
  80. public int gcPeriod;
  81. public int maxActivityCommits;
  82. public List<String> metricAuthorExclusions;
  83. public CommitMessageRenderer commitMessageRenderer;
  84. public boolean acceptNewPatchsets;
  85. public boolean acceptNewTickets;
  86. public boolean requireApproval;
  87. public int requireScore;
  88. public String writeSignoffCommit;
  89. public String mergeTo;
  90. public MergeType mergeType;
  91. public transient boolean isCollectingGarbage;
  92. public Date lastGC;
  93. public String sparkleshareId;
  94. public RepositoryModel() {
  95. this("", "", "", new Date(0));
  96. }
  97. public RepositoryModel(String name, String description, String owner, Date lastchange) {
  98. this.name = name;
  99. this.description = description;
  100. this.lastChange = lastchange;
  101. this.accessRestriction = AccessRestrictionType.NONE;
  102. this.authorizationControl = AuthorizationControl.NAMED;
  103. this.federationSets = new ArrayList<String>();
  104. this.federationStrategy = FederationStrategy.FEDERATE_THIS;
  105. this.projectPath = StringUtils.getFirstPathElement(name);
  106. this.owners = new ArrayList<String>();
  107. this.isBare = true;
  108. this.acceptNewTickets = true;
  109. this.acceptNewPatchsets = true;
  110. this.mergeType = MergeType.DEFAULT_MERGE_TYPE;
  111. addOwner(owner);
  112. }
  113. public List<String> getLocalBranches() {
  114. if (ArrayUtils.isEmpty(availableRefs)) {
  115. return new ArrayList<String>();
  116. }
  117. List<String> localBranches = new ArrayList<String>();
  118. for (String ref : availableRefs) {
  119. if (ref.startsWith("refs/heads")) {
  120. localBranches.add(ref);
  121. }
  122. }
  123. return localBranches;
  124. }
  125. public void addFork(String repository) {
  126. if (forks == null) {
  127. forks = new TreeSet<String>();
  128. }
  129. forks.add(repository);
  130. }
  131. public void removeFork(String repository) {
  132. if (forks == null) {
  133. return;
  134. }
  135. forks.remove(repository);
  136. }
  137. public void resetDisplayName() {
  138. displayName = null;
  139. }
  140. public String getRID() {
  141. return StringUtils.getSHA1(name);
  142. }
  143. @Override
  144. public int hashCode() {
  145. return name.hashCode();
  146. }
  147. @Override
  148. public boolean equals(Object o) {
  149. if (o instanceof RepositoryModel) {
  150. return name.equals(((RepositoryModel) o).name);
  151. }
  152. return false;
  153. }
  154. @Override
  155. public String toString() {
  156. if (displayName == null) {
  157. displayName = StringUtils.stripDotGit(name);
  158. }
  159. return displayName;
  160. }
  161. @Override
  162. public int compareTo(RepositoryModel o) {
  163. return StringUtils.compareRepositoryNames(name, o.name);
  164. }
  165. public boolean isFork() {
  166. return !StringUtils.isEmpty(originRepository);
  167. }
  168. public boolean isOwner(String username) {
  169. if (StringUtils.isEmpty(username) || ArrayUtils.isEmpty(owners)) {
  170. return isUsersPersonalRepository(username);
  171. }
  172. return owners.contains(username.toLowerCase()) || isUsersPersonalRepository(username);
  173. }
  174. public boolean isPersonalRepository() {
  175. return !StringUtils.isEmpty(projectPath) && ModelUtils.isPersonalRepository(projectPath);
  176. }
  177. public boolean isUsersPersonalRepository(String username) {
  178. return !StringUtils.isEmpty(projectPath) && ModelUtils.isUsersPersonalRepository(username, projectPath);
  179. }
  180. public boolean allowAnonymousView() {
  181. return !accessRestriction.atLeast(AccessRestrictionType.VIEW);
  182. }
  183. public boolean isShowActivity() {
  184. return maxActivityCommits > -1;
  185. }
  186. public boolean isSparkleshared() {
  187. return !StringUtils.isEmpty(sparkleshareId);
  188. }
  189. public RepositoryModel cloneAs(String cloneName) {
  190. RepositoryModel clone = new RepositoryModel();
  191. clone.originRepository = name;
  192. clone.name = cloneName;
  193. clone.projectPath = StringUtils.getFirstPathElement(cloneName);
  194. clone.isBare = true;
  195. clone.description = description;
  196. clone.accessRestriction = AccessRestrictionType.PUSH;
  197. clone.authorizationControl = AuthorizationControl.NAMED;
  198. clone.federationStrategy = federationStrategy;
  199. clone.showRemoteBranches = false;
  200. clone.allowForks = false;
  201. clone.acceptNewPatchsets = false;
  202. clone.acceptNewTickets = false;
  203. clone.skipSizeCalculation = skipSizeCalculation;
  204. clone.skipSummaryMetrics = skipSummaryMetrics;
  205. clone.sparkleshareId = sparkleshareId;
  206. return clone;
  207. }
  208. public void addOwner(String username) {
  209. if (!StringUtils.isEmpty(username)) {
  210. String name = username.toLowerCase();
  211. // a set would be more efficient, but this complicates JSON
  212. // deserialization so we enforce uniqueness with an arraylist
  213. if (!owners.contains(name)) {
  214. owners.add(name);
  215. }
  216. }
  217. }
  218. public void removeOwner(String username) {
  219. if (!StringUtils.isEmpty(username)) {
  220. owners.remove(username.toLowerCase());
  221. }
  222. }
  223. public void addOwners(Collection<String> usernames) {
  224. if (!ArrayUtils.isEmpty(usernames)) {
  225. for (String username : usernames) {
  226. addOwner(username);
  227. }
  228. }
  229. }
  230. public void removeOwners(Collection<String> usernames) {
  231. if (!ArrayUtils.isEmpty(owners)) {
  232. for (String username : usernames) {
  233. removeOwner(username);
  234. }
  235. }
  236. }
  237. }