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

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