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

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