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.

Constants.java 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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;
  17. import java.lang.annotation.Documented;
  18. import java.lang.annotation.Retention;
  19. import java.lang.annotation.RetentionPolicy;
  20. import java.net.URL;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. import java.util.jar.Attributes;
  24. import java.util.jar.Manifest;
  25. /**
  26. * Constant values used by Gitblit.
  27. *
  28. * @author James Moger
  29. *
  30. */
  31. public class Constants {
  32. public static final String NAME = "Gitblit";
  33. public static final String FULL_NAME = "Gitblit - a pure Java Git solution";
  34. @Deprecated
  35. public static final String ADMIN_ROLE = "#admin";
  36. @Deprecated
  37. public static final String FORK_ROLE = "#fork";
  38. @Deprecated
  39. public static final String CREATE_ROLE = "#create";
  40. @Deprecated
  41. public static final String NOT_FEDERATED_ROLE = "#notfederated";
  42. @Deprecated
  43. public static final String NO_ROLE = "#none";
  44. public static final String EXTERNAL_ACCOUNT = "#externalAccount";
  45. public static final String PROPERTIES_FILE = "gitblit.properties";
  46. public static final String DEFAULT_USER_REPOSITORY_PREFIX = "~";
  47. public static final String R_PATH = "/r/";
  48. public static final String GIT_PATH = "/git/";
  49. public static final String REGEX_SHA256 = "[a-fA-F0-9]{64}";
  50. /**
  51. * This regular expression is used when searching for "mentions" in tickets
  52. * (when someone writes @thisOtherUser)
  53. */
  54. public static final String REGEX_TICKET_MENTION = "\\B@(?<user>[^\\s]+)\\b";
  55. public static final String ZIP_PATH = "/zip/";
  56. public static final String SYNDICATION_PATH = "/feed/";
  57. public static final String FEDERATION_PATH = "/federation/";
  58. public static final String RPC_PATH = "/rpc/";
  59. public static final String PAGES = "/pages/";
  60. public static final String SPARKLESHARE_INVITE_PATH = "/sparkleshare/";
  61. public static final String RAW_PATH = "/raw/";
  62. public static final String PT_PATH = "/pt";
  63. public static final String BRANCH_GRAPH_PATH = "/graph/";
  64. public static final String BORDER = "*****************************************************************";
  65. public static final String BORDER2 = "#################################################################";
  66. public static final String FEDERATION_USER = "$gitblit";
  67. public static final String PROPOSAL_EXT = ".json";
  68. public static final String ENCODING = "UTF-8";
  69. public static final int LEN_SHORTLOG = 78;
  70. public static final int LEN_SHORTLOG_REFS = 60;
  71. public static final int LEN_FILESTORE_META_MIN = 125;
  72. public static final int LEN_FILESTORE_META_MAX = 146;
  73. public static final String DEFAULT_BRANCH = "default";
  74. public static final String CONFIG_GITBLIT = "gitblit";
  75. public static final String CONFIG_CUSTOM_FIELDS = "customFields";
  76. public static final String ISO8601 = "yyyy-MM-dd'T'HH:mm:ssZ";
  77. public static final String baseFolder = "baseFolder";
  78. public static final String baseFolder$ = "${" + baseFolder + "}";
  79. public static final String contextFolder$ = "${contextFolder}";
  80. public static final String HEAD = "HEAD";
  81. public static final String R_META = "refs/meta/";
  82. public static final String R_HEADS = "refs/heads/";
  83. public static final String R_NOTES = "refs/notes/";
  84. public static final String R_CHANGES = "refs/changes/";
  85. public static final String R_PULL = "refs/pull/";
  86. public static final String R_TAGS = "refs/tags/";
  87. public static final String R_REMOTES = "refs/remotes/";
  88. public static final String R_FOR = "refs/for/";
  89. public static final String R_TICKET = "refs/heads/ticket/";
  90. public static final String R_TICKETS_PATCHSETS = "refs/tickets/";
  91. public static final String R_MASTER = "refs/heads/master";
  92. public static final String MASTER = "master";
  93. public static final String R_DEVELOP = "refs/heads/develop";
  94. public static final String DEVELOP = "develop";
  95. public static final String ATTRIB_AUTHTYPE = NAME + ":authentication-type";
  96. public static final String ATTRIB_AUTHUSER = NAME + ":authenticated-user";
  97. public static final String R_LFS = "info/lfs/";
  98. public static String getVersion() {
  99. String v = Constants.class.getPackage().getImplementationVersion();
  100. if (v == null) {
  101. return "0.0.0-SNAPSHOT";
  102. }
  103. return v;
  104. }
  105. public static String getGitBlitVersion() {
  106. return NAME + " v" + getVersion();
  107. }
  108. public static String getBuildDate() {
  109. return getManifestValue("build-date", "PENDING");
  110. }
  111. public static String getASCIIArt() {
  112. StringBuilder sb = new StringBuilder();
  113. sb.append(" _____ _ _ _ _ _ _").append('\n');
  114. sb.append(" | __ \\(_)| | | | | |(_)| |").append('\n');
  115. sb.append(" | | \\/ _ | |_ | |__ | | _ | |_").append('\n');
  116. sb.append(" | | __ | || __|| '_ \\ | || || __|").append(" ").append("http://gitblit.com").append('\n');
  117. sb.append(" | |_\\ \\| || |_ | |_) || || || |_").append(" ").append("@gitblit").append('\n');
  118. sb.append(" \\____/|_| \\__||_.__/ |_||_| \\__|").append(" ").append(Constants.getVersion()).append('\n');
  119. return sb.toString();
  120. }
  121. private static String getManifestValue(String attrib, String defaultValue) {
  122. Class<?> clazz = Constants.class;
  123. String className = clazz.getSimpleName() + ".class";
  124. String classPath = clazz.getResource(className).toString();
  125. if (!classPath.startsWith("jar")) {
  126. // Class not from JAR
  127. return defaultValue;
  128. }
  129. try {
  130. String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) + "/META-INF/MANIFEST.MF";
  131. Manifest manifest = new Manifest(new URL(manifestPath).openStream());
  132. Attributes attr = manifest.getMainAttributes();
  133. String value = attr.getValue(attrib);
  134. return value;
  135. } catch (Exception e) {
  136. }
  137. return defaultValue;
  138. }
  139. public static enum Role {
  140. NONE, ADMIN, CREATE, FORK, NOT_FEDERATED;
  141. public String getRole() {
  142. return "#" + name().replace("_", "").toLowerCase();
  143. }
  144. @Override
  145. public String toString() {
  146. return getRole();
  147. }
  148. }
  149. /**
  150. * Enumeration representing the four access restriction levels.
  151. */
  152. public static enum AccessRestrictionType {
  153. NONE, PUSH, CLONE, VIEW;
  154. private static final AccessRestrictionType [] AUTH_TYPES = { PUSH, CLONE, VIEW };
  155. public static AccessRestrictionType fromName(String name) {
  156. for (AccessRestrictionType type : values()) {
  157. if (type.name().equalsIgnoreCase(name)) {
  158. return type;
  159. }
  160. }
  161. return NONE;
  162. }
  163. public static List<AccessRestrictionType> choices(boolean allowAnonymousPush) {
  164. if (allowAnonymousPush) {
  165. return Arrays.asList(values());
  166. }
  167. return Arrays.asList(AUTH_TYPES);
  168. }
  169. public boolean exceeds(AccessRestrictionType type) {
  170. return this.ordinal() > type.ordinal();
  171. }
  172. public boolean atLeast(AccessRestrictionType type) {
  173. return this.ordinal() >= type.ordinal();
  174. }
  175. @Override
  176. public String toString() {
  177. return name();
  178. }
  179. public boolean isValidPermission(AccessPermission permission) {
  180. switch (this) {
  181. case VIEW:
  182. // VIEW restriction
  183. // all access permissions are valid
  184. return true;
  185. case CLONE:
  186. // CLONE restriction
  187. // only CLONE or greater access permissions are valid
  188. return permission.atLeast(AccessPermission.CLONE);
  189. case PUSH:
  190. // PUSH restriction
  191. // only PUSH or greater access permissions are valid
  192. return permission.atLeast(AccessPermission.PUSH);
  193. case NONE:
  194. // NO access restriction
  195. // all access permissions are invalid
  196. return false;
  197. }
  198. return false;
  199. }
  200. }
  201. /**
  202. * Enumeration representing the types of authorization control for an
  203. * access restricted resource.
  204. */
  205. public static enum AuthorizationControl {
  206. AUTHENTICATED, NAMED;
  207. public static AuthorizationControl fromName(String name) {
  208. for (AuthorizationControl type : values()) {
  209. if (type.name().equalsIgnoreCase(name)) {
  210. return type;
  211. }
  212. }
  213. return NAMED;
  214. }
  215. @Override
  216. public String toString() {
  217. return name();
  218. }
  219. }
  220. /**
  221. * Enumeration representing the types of federation tokens.
  222. */
  223. public static enum FederationToken {
  224. ALL, USERS_AND_REPOSITORIES, REPOSITORIES;
  225. public static FederationToken fromName(String name) {
  226. for (FederationToken type : values()) {
  227. if (type.name().equalsIgnoreCase(name)) {
  228. return type;
  229. }
  230. }
  231. return REPOSITORIES;
  232. }
  233. @Override
  234. public String toString() {
  235. return name();
  236. }
  237. }
  238. /**
  239. * Enumeration representing the types of federation requests.
  240. */
  241. public static enum FederationRequest {
  242. POKE, PROPOSAL, PULL_REPOSITORIES, PULL_USERS, PULL_TEAMS, PULL_SETTINGS, PULL_SCRIPTS, STATUS;
  243. public static FederationRequest fromName(String name) {
  244. for (FederationRequest type : values()) {
  245. if (type.name().equalsIgnoreCase(name)) {
  246. return type;
  247. }
  248. }
  249. return PULL_REPOSITORIES;
  250. }
  251. @Override
  252. public String toString() {
  253. return name();
  254. }
  255. }
  256. /**
  257. * Enumeration representing the statii of federation requests.
  258. */
  259. public static enum FederationPullStatus {
  260. PENDING, FAILED, SKIPPED, PULLED, MIRRORED, NOCHANGE, EXCLUDED;
  261. public static FederationPullStatus fromName(String name) {
  262. for (FederationPullStatus type : values()) {
  263. if (type.name().equalsIgnoreCase(name)) {
  264. return type;
  265. }
  266. }
  267. return PENDING;
  268. }
  269. @Override
  270. public String toString() {
  271. return name();
  272. }
  273. }
  274. /**
  275. * Enumeration representing the federation types.
  276. */
  277. public static enum FederationStrategy {
  278. EXCLUDE, FEDERATE_THIS, FEDERATE_ORIGIN;
  279. public static FederationStrategy fromName(String name) {
  280. for (FederationStrategy type : values()) {
  281. if (type.name().equalsIgnoreCase(name)) {
  282. return type;
  283. }
  284. }
  285. return FEDERATE_THIS;
  286. }
  287. public boolean exceeds(FederationStrategy type) {
  288. return this.ordinal() > type.ordinal();
  289. }
  290. public boolean atLeast(FederationStrategy type) {
  291. return this.ordinal() >= type.ordinal();
  292. }
  293. @Override
  294. public String toString() {
  295. return name();
  296. }
  297. }
  298. /**
  299. * Enumeration representing the possible results of federation proposal
  300. * requests.
  301. */
  302. public static enum FederationProposalResult {
  303. ERROR, FEDERATION_DISABLED, MISSING_DATA, NO_PROPOSALS, NO_POKE, ACCEPTED;
  304. @Override
  305. public String toString() {
  306. return name();
  307. }
  308. }
  309. /**
  310. * Enumeration representing the possible remote procedure call requests from
  311. * a client.
  312. */
  313. public static enum RpcRequest {
  314. // Order is important here. anything after LIST_SETTINGS requires
  315. // administrator privileges and web.allowRpcManagement.
  316. CLEAR_REPOSITORY_CACHE, REINDEX_TICKETS, GET_PROTOCOL, LIST_REPOSITORIES, LIST_BRANCHES, GET_USER,
  317. FORK_REPOSITORY, LIST_SETTINGS,
  318. CREATE_REPOSITORY, EDIT_REPOSITORY, DELETE_REPOSITORY,
  319. LIST_USERS, CREATE_USER, EDIT_USER, DELETE_USER,
  320. LIST_TEAMS, CREATE_TEAM, EDIT_TEAM, DELETE_TEAM,
  321. LIST_REPOSITORY_MEMBERS, SET_REPOSITORY_MEMBERS, LIST_REPOSITORY_TEAMS, SET_REPOSITORY_TEAMS,
  322. LIST_REPOSITORY_MEMBER_PERMISSIONS, SET_REPOSITORY_MEMBER_PERMISSIONS, LIST_REPOSITORY_TEAM_PERMISSIONS, SET_REPOSITORY_TEAM_PERMISSIONS,
  323. LIST_FEDERATION_REGISTRATIONS, LIST_FEDERATION_RESULTS, LIST_FEDERATION_PROPOSALS, LIST_FEDERATION_SETS,
  324. EDIT_SETTINGS, LIST_STATUS;
  325. public static RpcRequest fromName(String name) {
  326. for (RpcRequest type : values()) {
  327. if (type.name().equalsIgnoreCase(name)) {
  328. return type;
  329. }
  330. }
  331. return null;
  332. }
  333. public boolean exceeds(RpcRequest type) {
  334. return this.ordinal() > type.ordinal();
  335. }
  336. @Override
  337. public String toString() {
  338. return name();
  339. }
  340. }
  341. /**
  342. * Enumeration of the search types.
  343. */
  344. public static enum SearchType {
  345. AUTHOR, COMMITTER, COMMIT;
  346. public static SearchType forName(String name) {
  347. for (SearchType type : values()) {
  348. if (type.name().equalsIgnoreCase(name)) {
  349. return type;
  350. }
  351. }
  352. return COMMIT;
  353. }
  354. @Override
  355. public String toString() {
  356. return name().toLowerCase();
  357. }
  358. }
  359. /**
  360. * Enumeration of the feed content object types.
  361. */
  362. public static enum FeedObjectType {
  363. COMMIT, TAG;
  364. public static FeedObjectType forName(String name) {
  365. for (FeedObjectType type : values()) {
  366. if (type.name().equalsIgnoreCase(name)) {
  367. return type;
  368. }
  369. }
  370. return COMMIT;
  371. }
  372. @Override
  373. public String toString() {
  374. return name().toLowerCase();
  375. }
  376. }
  377. /**
  378. * The types of objects that can be indexed and queried.
  379. */
  380. public static enum SearchObjectType {
  381. commit, blob;
  382. public static SearchObjectType fromName(String name) {
  383. for (SearchObjectType value : values()) {
  384. if (value.name().equals(name)) {
  385. return value;
  386. }
  387. }
  388. return null;
  389. }
  390. }
  391. /**
  392. * The access permissions available for a repository.
  393. */
  394. public static enum AccessPermission {
  395. NONE("N"), EXCLUDE("X"), VIEW("V"), CLONE("R"), PUSH("RW"), CREATE("RWC"), DELETE("RWD"), REWIND("RW+"), OWNER("RW+");
  396. public static final AccessPermission [] NEWPERMISSIONS = { EXCLUDE, VIEW, CLONE, PUSH, CREATE, DELETE, REWIND };
  397. public static final AccessPermission [] SSHPERMISSIONS = { VIEW, CLONE, PUSH };
  398. public static AccessPermission LEGACY = REWIND;
  399. public final String code;
  400. private AccessPermission(String code) {
  401. this.code = code;
  402. }
  403. public boolean atMost(AccessPermission perm) {
  404. return ordinal() <= perm.ordinal();
  405. }
  406. public boolean atLeast(AccessPermission perm) {
  407. return ordinal() >= perm.ordinal();
  408. }
  409. public boolean exceeds(AccessPermission perm) {
  410. return ordinal() > perm.ordinal();
  411. }
  412. public String asRole(String repository) {
  413. return code + ":" + repository;
  414. }
  415. @Override
  416. public String toString() {
  417. return code;
  418. }
  419. public static AccessPermission permissionFromRole(String role) {
  420. String [] fields = role.split(":", 2);
  421. if (fields.length == 1) {
  422. // legacy/undefined assume full permissions
  423. return AccessPermission.LEGACY;
  424. } else {
  425. // code:repository
  426. return AccessPermission.fromCode(fields[0]);
  427. }
  428. }
  429. public static String repositoryFromRole(String role) {
  430. String [] fields = role.split(":", 2);
  431. if (fields.length == 1) {
  432. // legacy/undefined assume full permissions
  433. return role;
  434. } else {
  435. // code:repository
  436. return fields[1];
  437. }
  438. }
  439. public static AccessPermission fromCode(String code) {
  440. for (AccessPermission perm : values()) {
  441. if (perm.code.equalsIgnoreCase(code)) {
  442. return perm;
  443. }
  444. }
  445. return AccessPermission.NONE;
  446. }
  447. }
  448. public static enum RegistrantType {
  449. REPOSITORY, USER, TEAM;
  450. }
  451. public static enum PermissionType {
  452. MISSING, ANONYMOUS, EXPLICIT, TEAM, REGEX, OWNER, ADMINISTRATOR;
  453. }
  454. public static enum GCStatus {
  455. READY, COLLECTING;
  456. public boolean exceeds(GCStatus s) {
  457. return ordinal() > s.ordinal();
  458. }
  459. }
  460. public static enum AuthenticationType {
  461. PUBLIC_KEY, CREDENTIALS, COOKIE, CERTIFICATE, CONTAINER, HTTPHEADER;
  462. public boolean isStandard() {
  463. return ordinal() <= COOKIE.ordinal();
  464. }
  465. }
  466. public static enum AccountType {
  467. LOCAL, CONTAINER, LDAP, REDMINE, SALESFORCE, WINDOWS, PAM, HTPASSWD, HTTPHEADER;
  468. public static AccountType fromString(String value) {
  469. for (AccountType type : AccountType.values()) {
  470. if (type.name().equalsIgnoreCase(value)) {
  471. return type;
  472. }
  473. }
  474. return AccountType.LOCAL;
  475. }
  476. public boolean isLocal() {
  477. return this == LOCAL;
  478. }
  479. }
  480. public static enum CommitMessageRenderer {
  481. PLAIN, MARKDOWN;
  482. public static CommitMessageRenderer fromName(String name) {
  483. for (CommitMessageRenderer renderer : values()) {
  484. if (renderer.name().equalsIgnoreCase(name)) {
  485. return renderer;
  486. }
  487. }
  488. return CommitMessageRenderer.PLAIN;
  489. }
  490. }
  491. public static enum Transport {
  492. // ordered for url advertisements, assuming equal access permissions
  493. SSH, HTTPS, HTTP, GIT;
  494. public static Transport fromString(String value) {
  495. for (Transport t : values()) {
  496. if (t.name().equalsIgnoreCase(value)) {
  497. return t;
  498. }
  499. }
  500. return null;
  501. }
  502. public static Transport fromUrl(String url) {
  503. int delim = url.indexOf("://");
  504. if (delim == -1) {
  505. // if no protocol is specified, SSH is assumed by git clients
  506. return SSH;
  507. }
  508. String scheme = url.substring(0, delim);
  509. return fromString(scheme);
  510. }
  511. }
  512. /**
  513. * The type of merge Gitblit will use when merging a ticket to the integration branch.
  514. * <p>
  515. * The default type is MERGE_ALWAYS.
  516. * <p>
  517. * This is modeled after the Gerrit SubmitType.
  518. */
  519. public static enum MergeType {
  520. /** Allows a merge only if it can be fast-forward merged into the integration branch. */
  521. FAST_FORWARD_ONLY,
  522. /** Uses a fast-forward merge if possible, other wise a merge commit is created. */
  523. MERGE_IF_NECESSARY,
  524. // Future REBASE_IF_NECESSARY,
  525. /** Always merge with a merge commit, even when a fast-forward would be possible. */
  526. MERGE_ALWAYS,
  527. // Future? CHERRY_PICK
  528. ;
  529. public static final MergeType DEFAULT_MERGE_TYPE = MERGE_ALWAYS;
  530. public static MergeType fromName(String name) {
  531. for (MergeType type : values()) {
  532. if (type.name().equalsIgnoreCase(name)) {
  533. return type;
  534. }
  535. }
  536. return DEFAULT_MERGE_TYPE;
  537. }
  538. }
  539. @Documented
  540. @Retention(RetentionPolicy.RUNTIME)
  541. public @interface Unused {
  542. }
  543. }