選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ForkPage.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright 2012 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.text.MessageFormat;
  18. import org.apache.wicket.PageParameters;
  19. import org.apache.wicket.markup.html.basic.Label;
  20. import org.slf4j.LoggerFactory;
  21. import com.gitblit.models.RepositoryModel;
  22. import com.gitblit.models.UserModel;
  23. import com.gitblit.wicket.GitBlitWebApp;
  24. import com.gitblit.wicket.GitBlitWebSession;
  25. import com.gitblit.wicket.GitblitRedirectException;
  26. import com.gitblit.wicket.WicketUtils;
  27. public class ForkPage extends RepositoryPage {
  28. public ForkPage(PageParameters params) {
  29. super(params);
  30. setVersioned(false);
  31. GitBlitWebSession session = GitBlitWebSession.get();
  32. RepositoryModel repository = getRepositoryModel();
  33. UserModel user = session.getUser();
  34. boolean canFork = user.canFork(repository);
  35. if (!canFork) {
  36. // redirect to the summary page if this repository is not empty
  37. GitBlitWebSession.get().cacheErrorMessage(
  38. MessageFormat.format(getString("gb.forkNotAuthorized"), repository.name));
  39. throw new GitblitRedirectException(SummaryPage.class, WicketUtils.newRepositoryParameter(repository.name));
  40. }
  41. String fork = app().repositories().getFork(user.username, repository.name);
  42. if (fork != null) {
  43. // redirect to user's fork
  44. throw new GitblitRedirectException(SummaryPage.class, WicketUtils.newRepositoryParameter(fork));
  45. }
  46. add(new Label("forkText", getString("gb.preparingFork")));
  47. if (!session.isForking()) {
  48. // prepare session
  49. session.isForking(true);
  50. // fork it
  51. ForkThread forker = new ForkThread(app(), repository, session);
  52. forker.start();
  53. }
  54. }
  55. @Override
  56. protected boolean allowForkControls() {
  57. return false;
  58. }
  59. @Override
  60. protected String getPageName() {
  61. return "fork";
  62. }
  63. /**
  64. * ForkThread does the work of working the repository in a background
  65. * thread. The completion status is tracked through a session variable and
  66. * monitored by this page.
  67. */
  68. private static class ForkThread extends Thread {
  69. private final GitBlitWebApp app;
  70. private final RepositoryModel repository;
  71. private final GitBlitWebSession session;
  72. public ForkThread(GitBlitWebApp app, RepositoryModel repository, GitBlitWebSession session) {
  73. this.app = app;
  74. this.repository = repository;
  75. this.session = session;
  76. }
  77. @Override
  78. public void run() {
  79. UserModel user = session.getUser();
  80. try {
  81. app.gitblit().fork(repository, user);
  82. } catch (Exception e) {
  83. LoggerFactory.getLogger(ForkPage.class).error(MessageFormat.format("Failed to fork {0} for {1}", repository.name, user.username), e);
  84. } finally {
  85. session.isForking(false);
  86. }
  87. }
  88. }
  89. }