Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

Constants.java 17KB

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