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

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.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 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 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 transient boolean isCollectingGarbage;
  85. public Date lastGC;
  86. public String sparkleshareId;
  87. public RepositoryModel() {
  88. this("", "", "", new Date(0));
  89. }
  90. public RepositoryModel(String name, String description, String owner, Date lastchange) {
  91. this.name = name;
  92. this.description = description;
  93. this.lastChange = lastchange;
  94. this.accessRestriction = AccessRestrictionType.NONE;
  95. this.authorizationControl = AuthorizationControl.NAMED;
  96. this.federationSets = new ArrayList<String>();
  97. this.federationStrategy = FederationStrategy.FEDERATE_THIS;
  98. this.projectPath = StringUtils.getFirstPathElement(name);
  99. this.owners = new ArrayList<String>();
  100. this.isBare = true;
  101. addOwner(owner);
  102. }
  103. public List<String> getLocalBranches() {
  104. if (ArrayUtils.isEmpty(availableRefs)) {
  105. return new ArrayList<String>();
  106. }
  107. List<String> localBranches = new ArrayList<String>();
  108. for (String ref : availableRefs) {
  109. if (ref.startsWith("refs/heads")) {
  110. localBranches.add(ref);
  111. }
  112. }
  113. return localBranches;
  114. }
  115. public void addFork(String repository) {
  116. if (forks == null) {
  117. forks = new TreeSet<String>();
  118. }
  119. forks.add(repository);
  120. }
  121. public void removeFork(String repository) {
  122. if (forks == null) {
  123. return;
  124. }
  125. forks.remove(repository);
  126. }
  127. public void resetDisplayName() {
  128. displayName = null;
  129. }
  130. @Override
  131. public int hashCode() {
  132. return name.hashCode();
  133. }
  134. @Override
  135. public boolean equals(Object o) {
  136. if (o instanceof RepositoryModel) {
  137. return name.equals(((RepositoryModel) o).name);
  138. }
  139. return false;
  140. }
  141. @Override
  142. public String toString() {
  143. if (displayName == null) {
  144. displayName = StringUtils.stripDotGit(name);
  145. }
  146. return displayName;
  147. }
  148. @Override
  149. public int compareTo(RepositoryModel o) {
  150. return StringUtils.compareRepositoryNames(name, o.name);
  151. }
  152. public boolean isFork() {
  153. return !StringUtils.isEmpty(originRepository);
  154. }
  155. public boolean isOwner(String username) {
  156. if (StringUtils.isEmpty(username) || ArrayUtils.isEmpty(owners)) {
  157. return false;
  158. }
  159. return owners.contains(username.toLowerCase());
  160. }
  161. public boolean isPersonalRepository() {
  162. return !StringUtils.isEmpty(projectPath) && ModelUtils.isPersonalRepository(projectPath);
  163. }
  164. public boolean isUsersPersonalRepository(String username) {
  165. return !StringUtils.isEmpty(projectPath) && ModelUtils.isUsersPersonalRepository(username, projectPath);
  166. }
  167. public boolean allowAnonymousView() {
  168. return !accessRestriction.atLeast(AccessRestrictionType.VIEW);
  169. }
  170. public boolean isShowActivity() {
  171. return maxActivityCommits > -1;
  172. }
  173. public boolean isSparkleshared() {
  174. return !StringUtils.isEmpty(sparkleshareId);
  175. }
  176. public RepositoryModel cloneAs(String cloneName) {
  177. RepositoryModel clone = new RepositoryModel();
  178. clone.originRepository = name;
  179. clone.name = cloneName;
  180. clone.projectPath = StringUtils.getFirstPathElement(cloneName);
  181. clone.isBare = true;
  182. clone.description = description;
  183. clone.accessRestriction = AccessRestrictionType.PUSH;
  184. clone.authorizationControl = AuthorizationControl.NAMED;
  185. clone.federationStrategy = federationStrategy;
  186. clone.showRemoteBranches = false;
  187. clone.allowForks = false;
  188. clone.useDocs = useDocs;
  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. }