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.4KB

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