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.

NewRepositoryPage.java 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*
  2. * Copyright 2014 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.wicket.pages;
  17. import java.io.File;
  18. import java.io.IOException;
  19. import java.io.Serializable;
  20. import java.io.UnsupportedEncodingException;
  21. import java.text.MessageFormat;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import org.apache.wicket.ajax.AjaxRequestTarget;
  26. import org.apache.wicket.ajax.form.AjaxFormComponentUpdatingBehavior;
  27. import org.apache.wicket.behavior.SimpleAttributeModifier;
  28. import org.apache.wicket.markup.html.basic.Label;
  29. import org.apache.wicket.markup.html.form.Button;
  30. import org.apache.wicket.markup.html.form.CheckBox;
  31. import org.apache.wicket.markup.html.form.DropDownChoice;
  32. import org.apache.wicket.markup.html.form.Form;
  33. import org.apache.wicket.markup.html.form.Radio;
  34. import org.apache.wicket.markup.html.form.RadioGroup;
  35. import org.apache.wicket.markup.html.form.TextField;
  36. import org.apache.wicket.markup.html.list.ListItem;
  37. import org.apache.wicket.markup.html.list.ListView;
  38. import org.apache.wicket.model.CompoundPropertyModel;
  39. import org.apache.wicket.model.IModel;
  40. import org.apache.wicket.model.Model;
  41. import org.eclipse.jgit.dircache.DirCache;
  42. import org.eclipse.jgit.dircache.DirCacheBuilder;
  43. import org.eclipse.jgit.dircache.DirCacheEntry;
  44. import org.eclipse.jgit.lib.CommitBuilder;
  45. import org.eclipse.jgit.lib.Config;
  46. import org.eclipse.jgit.lib.FileMode;
  47. import org.eclipse.jgit.lib.ObjectId;
  48. import org.eclipse.jgit.lib.ObjectInserter;
  49. import org.eclipse.jgit.lib.PersonIdent;
  50. import org.eclipse.jgit.lib.RefUpdate;
  51. import org.eclipse.jgit.lib.RefUpdate.Result;
  52. import org.eclipse.jgit.lib.Repository;
  53. import org.eclipse.jgit.revwalk.RevCommit;
  54. import org.eclipse.jgit.revwalk.RevWalk;
  55. import com.gitblit.Constants;
  56. import com.gitblit.Constants.AccessRestrictionType;
  57. import com.gitblit.Constants.AuthorizationControl;
  58. import com.gitblit.GitBlitException;
  59. import com.gitblit.Keys;
  60. import com.gitblit.models.RepositoryModel;
  61. import com.gitblit.models.UserModel;
  62. import com.gitblit.utils.ArrayUtils;
  63. import com.gitblit.utils.FileUtils;
  64. import com.gitblit.utils.StringUtils;
  65. import com.gitblit.wicket.GitBlitWebSession;
  66. import com.gitblit.wicket.WicketUtils;
  67. public class NewRepositoryPage extends RootSubPage {
  68. private final RepositoryModel repositoryModel;
  69. private RadioGroup<Permission> permissionGroup;
  70. private IModel<Boolean> addReadmeModel;
  71. private Model<String> gitignoreModel;
  72. private IModel<Boolean> addGitflowModel;
  73. private IModel<Boolean> addGitignoreModel;
  74. public NewRepositoryPage() {
  75. // create constructor
  76. super();
  77. repositoryModel = new RepositoryModel();
  78. setupPage(getString("gb.newRepository"), "");
  79. setStatelessHint(false);
  80. setOutputMarkupId(true);
  81. }
  82. @Override
  83. protected boolean requiresPageMap() {
  84. return true;
  85. }
  86. @Override
  87. protected Class<? extends BasePage> getRootNavPageClass() {
  88. return RepositoriesPage.class;
  89. }
  90. @Override
  91. protected void onInitialize() {
  92. super.onInitialize();
  93. CompoundPropertyModel<RepositoryModel> rModel = new CompoundPropertyModel<>(repositoryModel);
  94. Form<RepositoryModel> form = new Form<RepositoryModel>("editForm", rModel) {
  95. private static final long serialVersionUID = 1L;
  96. @Override
  97. protected void onSubmit() {
  98. // confirm a repository name was entered
  99. if (StringUtils.isEmpty(repositoryModel.name)) {
  100. error(getString("gb.pleaseSetRepositoryName"));
  101. return;
  102. }
  103. String project = repositoryModel.projectPath;
  104. String fullName = (project + "/" + repositoryModel.name).trim();
  105. fullName = fullName.replace('\\', '/');
  106. fullName = fullName.replace("//", "/");
  107. if (fullName.charAt(0) == '/') {
  108. fullName = fullName.substring(1);
  109. }
  110. if (fullName.endsWith("/")) {
  111. fullName = fullName.substring(0, fullName.length() - 1);
  112. }
  113. try {
  114. if (fullName.contains("../")) {
  115. error(getString("gb.illegalRelativeSlash"));
  116. return;
  117. }
  118. if (fullName.contains("/../")) {
  119. error(getString("gb.illegalRelativeSlash"));
  120. return;
  121. }
  122. // confirm valid characters in repository name
  123. Character c = StringUtils.findInvalidCharacter(fullName);
  124. if (c != null) {
  125. error(MessageFormat.format(getString("gb.illegalCharacterRepositoryName"),
  126. c));
  127. return;
  128. }
  129. repositoryModel.name = fullName;
  130. repositoryModel.projectPath = null;
  131. Permission permisison = permissionGroup.getModelObject();
  132. repositoryModel.accessRestriction = permisison.type;
  133. repositoryModel.authorizationControl = AuthorizationControl.NAMED;
  134. repositoryModel.owners = new ArrayList<String>();
  135. repositoryModel.owners.add(GitBlitWebSession.get().getUsername());
  136. // setup branch defaults
  137. boolean useGitFlow = addGitflowModel.getObject();
  138. repositoryModel.HEAD = Constants.R_MASTER;
  139. repositoryModel.mergeTo = Constants.MASTER;
  140. if (useGitFlow) {
  141. // tickets normally merge to develop unless they are hotfixes
  142. repositoryModel.mergeTo = Constants.DEVELOP;
  143. }
  144. repositoryModel.allowForks = app().settings().getBoolean(Keys.web.allowForking, true);
  145. // optionally generate an initial commit
  146. boolean addReadme = addReadmeModel.getObject();
  147. String gitignore = null;
  148. boolean addGitignore = addGitignoreModel.getObject();
  149. if (addGitignore) {
  150. gitignore = gitignoreModel.getObject();
  151. if (StringUtils.isEmpty(gitignore)) {
  152. throw new GitBlitException("Please select a .gitignore file");
  153. }
  154. }
  155. // init the repository
  156. app().gitblit().updateRepositoryModel(repositoryModel.name, repositoryModel, true);
  157. // optionally create an initial commit
  158. initialCommit(repositoryModel, addReadme, gitignore, useGitFlow);
  159. } catch (GitBlitException e) {
  160. error(e.getMessage());
  161. // restore project and name fields on error condition
  162. repositoryModel.projectPath = StringUtils.getFirstPathElement(fullName);
  163. if (!StringUtils.isEmpty(repositoryModel.projectPath)) {
  164. repositoryModel.name = fullName.substring(repositoryModel.projectPath.length() + 1);
  165. }
  166. return;
  167. }
  168. setRedirect(true);
  169. setResponsePage(SummaryPage.class, WicketUtils.newRepositoryParameter(fullName));
  170. }
  171. };
  172. GitBlitWebSession session = GitBlitWebSession.get();
  173. UserModel user = session.getUser();
  174. // build project list for repository destination
  175. String defaultProject = null;
  176. List<String> projects = new ArrayList<String>();
  177. if (user.canAdmin()) {
  178. String main = app().settings().getString(Keys.web.repositoryRootGroupName, "main");
  179. projects.add(main);
  180. defaultProject = main;
  181. }
  182. if (user.canCreate()) {
  183. projects.add(user.getPersonalPath());
  184. if (defaultProject == null) {
  185. // only prefer personal namespace if default is not already set
  186. defaultProject = user.getPersonalPath();
  187. }
  188. }
  189. repositoryModel.projectPath = defaultProject;
  190. // do not let the browser pre-populate these fields
  191. form.add(new SimpleAttributeModifier("autocomplete", "off"));
  192. form.add(new DropDownChoice<String>("projectPath", projects));
  193. form.add(new TextField<String>("name"));
  194. form.add(new TextField<String>("description"));
  195. Permission anonymousPermission = new Permission(getString("gb.anonymous"), getString("gb.anonymousRepoDescription"), "blank.png", AccessRestrictionType.NONE);
  196. Permission publicPermission = new Permission(getString("gb.public"), getString("gb.publicRepoDescription"), "lock_go_16x16.png", AccessRestrictionType.PUSH);
  197. Permission protectedPermission = new Permission(getString("gb.protected"), getString("gb.protectedRepoDescription"), "lock_pull_16x16.png", AccessRestrictionType.CLONE);
  198. Permission privatePermission = new Permission(getString("gb.private"), getString("gb.privateRepoDescription"), "shield_16x16.png", AccessRestrictionType.VIEW);
  199. List<Permission> permissions = new ArrayList<Permission>();
  200. if (app().settings().getBoolean(Keys.git.allowAnonymousPushes, false)) {
  201. permissions.add(anonymousPermission);
  202. }
  203. permissions.add(publicPermission);
  204. permissions.add(protectedPermission);
  205. permissions.add(privatePermission);
  206. // determine default permission selection
  207. AccessRestrictionType defaultRestriction = AccessRestrictionType.fromName(
  208. app().settings().getString(Keys.git.defaultAccessRestriction, AccessRestrictionType.PUSH.name()));
  209. if (AccessRestrictionType.NONE == defaultRestriction) {
  210. defaultRestriction = AccessRestrictionType.PUSH;
  211. }
  212. Permission defaultPermission = publicPermission;
  213. for (Permission permission : permissions) {
  214. if (permission.type == defaultRestriction) {
  215. defaultPermission = permission;
  216. }
  217. }
  218. permissionGroup = new RadioGroup<>("permissionsGroup", new Model<Permission>(defaultPermission));
  219. form.add(permissionGroup);
  220. ListView<Permission> permissionsList = new ListView<Permission>("permissions", permissions) {
  221. private static final long serialVersionUID = 1L;
  222. @Override
  223. protected void populateItem(ListItem<Permission> item) {
  224. Permission p = item.getModelObject();
  225. item.add(new Radio<Permission>("radio", item.getModel()));
  226. item.add(WicketUtils.newImage("image", p.image));
  227. item.add(new Label("name", p.name));
  228. item.add(new Label("description", p.description));
  229. }
  230. };
  231. permissionGroup.add(permissionsList);
  232. //
  233. // initial commit options
  234. //
  235. // add README
  236. addReadmeModel = Model.of(false);
  237. form.add(new CheckBox("addReadme", addReadmeModel));
  238. // add .gitignore
  239. File gitignoreDir = app().runtime().getFileOrFolder(Keys.git.gitignoreFolder, "${baseFolder}/gitignore");
  240. File [] files = gitignoreDir.listFiles();
  241. if (files == null) {
  242. files = new File[0];
  243. }
  244. List<String> gitignores = new ArrayList<String>();
  245. for (File file : files) {
  246. if (file.isFile() && file.getName().endsWith(".gitignore")) {
  247. gitignores.add(StringUtils.stripFileExtension(file.getName()));
  248. }
  249. }
  250. Collections.sort(gitignores);
  251. gitignoreModel = Model.of("");
  252. final DropDownChoice<String> gitignoreChoice = new DropDownChoice<String>("gitignore", gitignoreModel, gitignores);
  253. gitignoreChoice.setOutputMarkupId(true);
  254. form.add(gitignoreChoice.setEnabled(false));
  255. addGitignoreModel = Model.of(false);
  256. final CheckBox gitignoreCheckbox = new CheckBox("addGitignore", addGitignoreModel);
  257. form.add(gitignoreCheckbox);
  258. gitignoreCheckbox.add(new AjaxFormComponentUpdatingBehavior("onchange") {
  259. private static final long serialVersionUID = 1L;
  260. @Override
  261. protected void onUpdate(AjaxRequestTarget target) {
  262. gitignoreChoice.setEnabled(addGitignoreModel.getObject());
  263. target.addComponent(gitignoreChoice);
  264. }
  265. });
  266. // TODO add .gitflow
  267. addGitflowModel = Model.of(false);
  268. form.add(new CheckBox("addGitflow", addGitflowModel));
  269. form.add(new Button("create"));
  270. add(form);
  271. }
  272. /**
  273. * Prepare the initial commit for the repository.
  274. *
  275. * @param repository
  276. * @param addReadme
  277. * @param gitignore
  278. * @param addGitFlow
  279. * @return true if an initial commit was created
  280. */
  281. protected boolean initialCommit(RepositoryModel repository, boolean addReadme, String gitignore,
  282. boolean addGitFlow) {
  283. boolean initialCommit = addReadme || !StringUtils.isEmpty(gitignore) || addGitFlow;
  284. if (!initialCommit) {
  285. return false;
  286. }
  287. // build an initial commit
  288. boolean success = false;
  289. Repository db = app().repositories().getRepository(repositoryModel.name);
  290. ObjectInserter odi = db.newObjectInserter();
  291. try {
  292. UserModel user = GitBlitWebSession.get().getUser();
  293. PersonIdent author = new PersonIdent(user.getDisplayName(), user.emailAddress);
  294. DirCache newIndex = DirCache.newInCore();
  295. DirCacheBuilder indexBuilder = newIndex.builder();
  296. if (addReadme) {
  297. // insert a README
  298. String title = StringUtils.stripDotGit(StringUtils.getLastPathElement(repositoryModel.name));
  299. String description = repositoryModel.description == null ? "" : repositoryModel.description;
  300. String readme = String.format("## %s\n\n%s\n\n", title, description);
  301. byte [] bytes = readme.getBytes(Constants.ENCODING);
  302. DirCacheEntry entry = new DirCacheEntry("README.md");
  303. entry.setLength(bytes.length);
  304. entry.setLastModified(System.currentTimeMillis());
  305. entry.setFileMode(FileMode.REGULAR_FILE);
  306. entry.setObjectId(odi.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, bytes));
  307. indexBuilder.add(entry);
  308. }
  309. if (!StringUtils.isEmpty(gitignore)) {
  310. // insert a .gitignore file
  311. File dir = app().runtime().getFileOrFolder(Keys.git.gitignoreFolder, "${baseFolder}/gitignore");
  312. File file = new File(dir, gitignore + ".gitignore");
  313. if (file.exists() && file.length() > 0) {
  314. byte [] bytes = FileUtils.readContent(file);
  315. if (!ArrayUtils.isEmpty(bytes)) {
  316. DirCacheEntry entry = new DirCacheEntry(".gitignore");
  317. entry.setLength(bytes.length);
  318. entry.setLastModified(System.currentTimeMillis());
  319. entry.setFileMode(FileMode.REGULAR_FILE);
  320. entry.setObjectId(odi.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, bytes));
  321. indexBuilder.add(entry);
  322. }
  323. }
  324. }
  325. if (addGitFlow) {
  326. // insert a .gitflow file
  327. Config config = new Config();
  328. config.setString("gitflow", null, "masterBranch", Constants.MASTER);
  329. config.setString("gitflow", null, "developBranch", Constants.DEVELOP);
  330. config.setString("gitflow", null, "featureBranchPrefix", "feature/");
  331. config.setString("gitflow", null, "releaseBranchPrefix", "release/");
  332. config.setString("gitflow", null, "hotfixBranchPrefix", "hotfix/");
  333. config.setString("gitflow", null, "supportBranchPrefix", "support/");
  334. config.setString("gitflow", null, "versionTagPrefix", "");
  335. byte [] bytes = config.toText().getBytes(Constants.ENCODING);
  336. DirCacheEntry entry = new DirCacheEntry(".gitflow");
  337. entry.setLength(bytes.length);
  338. entry.setLastModified(System.currentTimeMillis());
  339. entry.setFileMode(FileMode.REGULAR_FILE);
  340. entry.setObjectId(odi.insert(org.eclipse.jgit.lib.Constants.OBJ_BLOB, bytes));
  341. indexBuilder.add(entry);
  342. }
  343. indexBuilder.finish();
  344. if (newIndex.getEntryCount() == 0) {
  345. // nothing to commit
  346. return false;
  347. }
  348. ObjectId treeId = newIndex.writeTree(odi);
  349. // Create a commit object
  350. CommitBuilder commit = new CommitBuilder();
  351. commit.setAuthor(author);
  352. commit.setCommitter(author);
  353. commit.setEncoding(Constants.ENCODING);
  354. commit.setMessage("Initial commit");
  355. commit.setTreeId(treeId);
  356. // Insert the commit into the repository
  357. ObjectId commitId = odi.insert(commit);
  358. odi.flush();
  359. // set the branch refs
  360. RevWalk revWalk = new RevWalk(db);
  361. try {
  362. // set the master branch
  363. RevCommit revCommit = revWalk.parseCommit(commitId);
  364. RefUpdate masterRef = db.updateRef(Constants.R_MASTER);
  365. masterRef.setNewObjectId(commitId);
  366. masterRef.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
  367. Result masterRC = masterRef.update();
  368. switch (masterRC) {
  369. case NEW:
  370. success = true;
  371. break;
  372. default:
  373. success = false;
  374. }
  375. if (addGitFlow) {
  376. // set the develop branch for git-flow
  377. RefUpdate developRef = db.updateRef(Constants.R_DEVELOP);
  378. developRef.setNewObjectId(commitId);
  379. developRef.setRefLogMessage("commit: " + revCommit.getShortMessage(), false);
  380. Result developRC = developRef.update();
  381. switch (developRC) {
  382. case NEW:
  383. success = true;
  384. break;
  385. default:
  386. success = false;
  387. }
  388. }
  389. } finally {
  390. revWalk.release();
  391. }
  392. } catch (UnsupportedEncodingException e) {
  393. logger().error(null, e);
  394. } catch (IOException e) {
  395. logger().error(null, e);
  396. } finally {
  397. odi.release();
  398. db.close();
  399. }
  400. return success;
  401. }
  402. private static class Permission implements Serializable {
  403. private static final long serialVersionUID = 1L;
  404. final String name;
  405. final String description;
  406. final String image;
  407. final AccessRestrictionType type;
  408. Permission(String name, String description, String img, AccessRestrictionType type) {
  409. this.name = name;
  410. this.description = description;
  411. this.image = img;
  412. this.type = type;
  413. }
  414. }
  415. }