示例#1
0
  public ComparePage(PageParameters params) {
    super(params);
    Repository r = getRepository();
    RepositoryModel repository = getRepositoryModel();

    if (StringUtils.isEmpty(objectId)) {
      // seleciton form
      add(new Label("comparison").setVisible(false));
    } else {
      // active comparison
      Fragment comparison = new Fragment("comparison", "comparisonFragment", this);
      add(comparison);

      RevCommit fromCommit;
      RevCommit toCommit;

      String[] parts = objectId.split("\\.\\.");
      if (parts[0].startsWith("refs/") && parts[1].startsWith("refs/")) {
        // set the ref models
        fromRefId.setObject(parts[0]);
        toRefId.setObject(parts[1]);

        fromCommit = getCommit(r, fromRefId.getObject());
        toCommit = getCommit(r, toRefId.getObject());
      } else {
        // set the id models
        fromCommitId.setObject(parts[0]);
        toCommitId.setObject(parts[1]);

        fromCommit = getCommit(r, fromCommitId.getObject());
        toCommit = getCommit(r, toCommitId.getObject());
      }

      // prepare submodules
      getSubmodules(toCommit);

      final String startId = fromCommit.getId().getName();
      final String endId = toCommit.getId().getName();

      // commit ids
      fromCommitId.setObject(startId);
      toCommitId.setObject(endId);

      final DiffOutput diff = DiffUtils.getDiff(r, fromCommit, toCommit, DiffOutputType.HTML);

      // add compare diffstat
      int insertions = 0;
      int deletions = 0;
      for (PathChangeModel pcm : diff.stat.paths) {
        insertions += pcm.insertions;
        deletions += pcm.deletions;
      }
      comparison.add(new DiffStatPanel("diffStat", insertions, deletions));

      // compare page links
      //			comparison.add(new BookmarkablePageLink<Void>("patchLink", PatchPage.class,
      //					WicketUtils.newRangeParameter(repositoryName, fromCommitId.toString(),
      // toCommitId.getObject())));

      // display list of commits
      comparison.add(
          new LogPanel(
              "commitList", repositoryName, objectId, r, 0, 0, repository.showRemoteBranches));

      // changed paths list
      comparison.add(new CommitLegendPanel("commitLegend", diff.stat.paths));
      ListDataProvider<PathChangeModel> pathsDp =
          new ListDataProvider<PathChangeModel>(diff.stat.paths);
      DataView<PathChangeModel> pathsView =
          new DataView<PathChangeModel>("changedPath", pathsDp) {
            private static final long serialVersionUID = 1L;
            int counter;

            @Override
            public void populateItem(final Item<PathChangeModel> item) {
              final PathChangeModel entry = item.getModelObject();
              Label changeType = new Label("changeType", "");
              WicketUtils.setChangeTypeCssClass(changeType, entry.changeType);
              setChangeTypeTooltip(changeType, entry.changeType);
              item.add(changeType);
              item.add(new DiffStatPanel("diffStat", entry.insertions, entry.deletions, true));

              boolean hasSubmodule = false;
              String submodulePath = null;
              if (entry.isTree()) {
                // tree
                item.add(
                    new LinkPanel(
                        "pathName",
                        null,
                        entry.path,
                        TreePage.class,
                        WicketUtils.newPathParameter(repositoryName, endId, entry.path)));
              } else if (entry.isSubmodule()) {
                // submodule
                String submoduleId = entry.objectId;
                SubmoduleModel submodule = getSubmodule(entry.path);
                submodulePath = submodule.gitblitPath;
                hasSubmodule = submodule.hasSubmodule;

                // add relative link
                item.add(
                    new LinkPanel(
                        "pathName",
                        "list",
                        entry.path + " @ " + getShortObjectId(submoduleId),
                        "#" + entry.path));
              } else {
                // add relative link
                item.add(new LinkPanel("pathName", "list", entry.path, "#" + entry.path));
              }

              // quick links
              if (entry.isSubmodule()) {
                // submodule
                item.add(new ExternalLink("patch", "").setEnabled(false));
                item.add(
                    new BookmarkablePageLink<Void>(
                            "view",
                            CommitPage.class,
                            WicketUtils.newObjectParameter(submodulePath, entry.objectId))
                        .setEnabled(hasSubmodule));
                item.add(new ExternalLink("blame", "").setEnabled(false));
                item.add(
                    new BookmarkablePageLink<Void>(
                            "history",
                            HistoryPage.class,
                            WicketUtils.newPathParameter(repositoryName, endId, entry.path))
                        .setEnabled(!entry.changeType.equals(ChangeType.ADD)));
              } else {
                // tree or blob
                item.add(
                    new BookmarkablePageLink<Void>(
                            "patch",
                            PatchPage.class,
                            WicketUtils.newBlobDiffParameter(
                                repositoryName, startId, endId, entry.path))
                        .setEnabled(!entry.changeType.equals(ChangeType.DELETE)));
                item.add(
                    new BookmarkablePageLink<Void>(
                            "view",
                            BlobPage.class,
                            WicketUtils.newPathParameter(repositoryName, endId, entry.path))
                        .setEnabled(!entry.changeType.equals(ChangeType.DELETE)));
                item.add(
                    new BookmarkablePageLink<Void>(
                            "raw",
                            RawPage.class,
                            WicketUtils.newPathParameter(repositoryName, endId, entry.path))
                        .setEnabled(!entry.changeType.equals(ChangeType.DELETE)));
                item.add(
                    new BookmarkablePageLink<Void>(
                            "blame",
                            BlamePage.class,
                            WicketUtils.newPathParameter(repositoryName, endId, entry.path))
                        .setEnabled(
                            !entry.changeType.equals(ChangeType.ADD)
                                && !entry.changeType.equals(ChangeType.DELETE)));
                item.add(
                    new BookmarkablePageLink<Void>(
                            "history",
                            HistoryPage.class,
                            WicketUtils.newPathParameter(repositoryName, endId, entry.path))
                        .setEnabled(!entry.changeType.equals(ChangeType.ADD)));
              }
              WicketUtils.setAlternatingBackground(item, counter);
              counter++;
            }
          };
      comparison.add(pathsView);
      comparison.add(new Label("diffText", diff.content).setEscapeModelStrings(false));
    }

    //
    // ref selection form
    //
    SessionlessForm<Void> refsForm =
        new SessionlessForm<Void>("compareRefsForm", getClass(), getPageParameters()) {

          private static final long serialVersionUID = 1L;

          @Override
          public void onSubmit() {
            String from = ComparePage.this.fromRefId.getObject();
            String to = ComparePage.this.toRefId.getObject();

            PageParameters params = WicketUtils.newRangeParameter(repositoryName, from, to);
            String relativeUrl = urlFor(ComparePage.class, params).toString();
            String absoluteUrl = RequestUtils.toAbsolutePath(relativeUrl);
            getRequestCycle().setRequestTarget(new RedirectRequestTarget(absoluteUrl));
          }
        };

    List<String> refs = new ArrayList<String>();
    for (RefModel ref : JGitUtils.getLocalBranches(r, true, -1)) {
      refs.add(ref.getName());
    }
    if (repository.showRemoteBranches) {
      for (RefModel ref : JGitUtils.getRemoteBranches(r, true, -1)) {
        refs.add(ref.getName());
      }
    }
    for (RefModel ref : JGitUtils.getTags(r, true, -1)) {
      refs.add(ref.getName());
    }
    refsForm.add(
        new DropDownChoice<String>("fromRef", fromRefId, refs).setEnabled(refs.size() > 0));
    refsForm.add(new DropDownChoice<String>("toRef", toRefId, refs).setEnabled(refs.size() > 0));
    add(refsForm);

    //
    // manual ids form
    //
    SessionlessForm<Void> idsForm =
        new SessionlessForm<Void>("compareIdsForm", getClass(), getPageParameters()) {

          private static final long serialVersionUID = 1L;

          @Override
          public void onSubmit() {
            String from = ComparePage.this.fromCommitId.getObject();
            String to = ComparePage.this.toCommitId.getObject();

            PageParameters params = WicketUtils.newRangeParameter(repositoryName, from, to);
            String relativeUrl = urlFor(ComparePage.class, params).toString();
            String absoluteUrl = RequestUtils.toAbsolutePath(relativeUrl);
            getRequestCycle().setRequestTarget(new RedirectRequestTarget(absoluteUrl));
          }
        };

    TextField<String> fromIdField = new TextField<String>("fromId", fromCommitId);
    WicketUtils.setInputPlaceholder(fromIdField, getString("gb.from") + "...");
    idsForm.add(fromIdField);

    TextField<String> toIdField = new TextField<String>("toId", toCommitId);
    WicketUtils.setInputPlaceholder(toIdField, getString("gb.to") + "...");
    idsForm.add(toIdField);
    add(idsForm);

    r.close();
  }
示例#2
0
  private void setup(PageParameters params) {
    setupPage("", "");

    // default values
    ArrayList<String> repositories = new ArrayList<String>();
    String query = "";
    int page = 1;
    int pageSize = app().settings().getInteger(Keys.web.itemsPerPage, 50);

    // display user-accessible selections
    UserModel user = GitBlitWebSession.get().getUser();
    List<String> availableRepositories = new ArrayList<String>();
    for (RepositoryModel model : app().repositories().getRepositoryModels(user)) {
      if (model.hasCommits && !ArrayUtils.isEmpty(model.indexedBranches)) {
        availableRepositories.add(model.name);
      }
    }

    if (params != null) {
      String repository = WicketUtils.getRepositoryName(params);
      if (!StringUtils.isEmpty(repository)) {
        repositories.add(repository);
      }

      page = WicketUtils.getPage(params);

      if (params.containsKey("repositories")) {
        String value = params.getString("repositories", "");
        List<String> list = StringUtils.getStringsFromValue(value);
        repositories.addAll(list);
      }

      if (params.containsKey("allrepos")) {
        repositories.addAll(availableRepositories);
      }

      if (params.containsKey("query")) {
        query = params.getString("query", "");
      } else {
        String value = WicketUtils.getSearchString(params);
        String type = WicketUtils.getSearchType(params);
        com.gitblit.Constants.SearchType searchType =
            com.gitblit.Constants.SearchType.forName(type);
        if (!StringUtils.isEmpty(value)) {
          if (searchType == SearchType.COMMIT) {
            query = "type:" + searchType.name().toLowerCase() + " AND \"" + value + "\"";
          } else {
            query = searchType.name().toLowerCase() + ":\"" + value + "\"";
          }
        }
      }
    }

    boolean luceneEnabled = app().settings().getBoolean(Keys.web.allowLuceneIndexing, true);
    if (luceneEnabled) {
      if (availableRepositories.size() == 0) {
        info(getString("gb.noIndexedRepositoriesWarning"));
      }
    } else {
      error(getString("gb.luceneDisabled"));
    }

    // enforce user-accessible repository selections
    Set<String> uniqueRepositories = new LinkedHashSet<String>();
    for (String selectedRepository : repositories) {
      if (availableRepositories.contains(selectedRepository)) {
        uniqueRepositories.add(selectedRepository);
      }
    }
    ArrayList<String> searchRepositories = new ArrayList<String>(uniqueRepositories);

    // search form
    final Model<String> queryModel = new Model<String>(query);
    final Model<ArrayList<String>> repositoriesModel =
        new Model<ArrayList<String>>(searchRepositories);
    final Model<Boolean> allreposModel =
        new Model<Boolean>(params != null && params.containsKey("allrepos"));
    SessionlessForm<Void> form =
        new SessionlessForm<Void>("searchForm", getClass()) {

          private static final long serialVersionUID = 1L;

          @Override
          public void onSubmit() {
            String q = queryModel.getObject();
            if (StringUtils.isEmpty(q)) {
              error(getString("gb.undefinedQueryWarning"));
              return;
            }
            if (repositoriesModel.getObject().size() == 0 && !allreposModel.getObject()) {
              error(getString("gb.noSelectedRepositoriesWarning"));
              return;
            }
            PageParameters params = new PageParameters();
            params.put("repositories", StringUtils.flattenStrings(repositoriesModel.getObject()));
            params.put("query", queryModel.getObject());
            params.put("allrepos", allreposModel.getObject());
            LuceneSearchPage page = new LuceneSearchPage(params);
            setResponsePage(page);
          }
        };

    ListMultipleChoice<String> selections =
        new ListMultipleChoice<String>(
            "repositories", repositoriesModel, availableRepositories, new StringChoiceRenderer());
    selections.setMaxRows(8);
    form.add(selections.setEnabled(luceneEnabled));
    form.add(new TextField<String>("query", queryModel).setEnabled(luceneEnabled));
    form.add(new CheckBox("allrepos", allreposModel));
    add(form.setEnabled(luceneEnabled));

    // execute search
    final List<SearchResult> results = new ArrayList<SearchResult>();
    if (!ArrayUtils.isEmpty(searchRepositories) && !StringUtils.isEmpty(query)) {
      results.addAll(app().repositories().search(query, page, pageSize, searchRepositories));
    }

    // results header
    if (results.size() == 0) {
      if (!ArrayUtils.isEmpty(searchRepositories) && !StringUtils.isEmpty(query)) {
        add(new Label("resultsHeader", query).setRenderBodyOnly(true));
        add(new Label("resultsCount", getString("gb.noHits")).setRenderBodyOnly(true));
      } else {
        add(new Label("resultsHeader").setVisible(false));
        add(new Label("resultsCount").setVisible(false));
      }
    } else {
      add(new Label("resultsHeader", query).setRenderBodyOnly(true));
      add(
          new Label(
                  "resultsCount",
                  MessageFormat.format(
                      getString("gb.queryResults"),
                      results.get(0).hitId,
                      results.get(results.size() - 1).hitId,
                      results.get(0).totalHits))
              .setRenderBodyOnly(true));
    }

    // search results view
    ListDataProvider<SearchResult> resultsDp = new ListDataProvider<SearchResult>(results);
    final DataView<SearchResult> resultsView =
        new DataView<SearchResult>("searchResults", resultsDp) {
          private static final long serialVersionUID = 1L;

          @Override
          public void populateItem(final Item<SearchResult> item) {
            final SearchResult sr = item.getModelObject();
            switch (sr.type) {
              case commit:
                {
                  Label icon = WicketUtils.newIcon("type", "icon-refresh");
                  WicketUtils.setHtmlTooltip(icon, "commit");
                  item.add(icon);
                  item.add(
                      new LinkPanel(
                          "summary",
                          null,
                          sr.summary,
                          CommitPage.class,
                          WicketUtils.newObjectParameter(sr.repository, sr.commitId)));
                  // show tags
                  Fragment fragment = new Fragment("tags", "tagsPanel", LuceneSearchPage.this);
                  List<String> tags = sr.tags;
                  if (tags == null) {
                    tags = new ArrayList<String>();
                  }
                  ListDataProvider<String> tagsDp = new ListDataProvider<String>(tags);
                  final DataView<String> tagsView =
                      new DataView<String>("tag", tagsDp) {
                        private static final long serialVersionUID = 1L;

                        @Override
                        public void populateItem(final Item<String> item) {
                          String tag = item.getModelObject();
                          Component c =
                              new LinkPanel(
                                  "tagLink",
                                  null,
                                  tag,
                                  TagPage.class,
                                  WicketUtils.newObjectParameter(
                                      sr.repository, Constants.R_TAGS + tag));
                          WicketUtils.setCssClass(c, "tagRef");
                          item.add(c);
                        }
                      };
                  fragment.add(tagsView);
                  item.add(fragment);
                  break;
                }
              case blob:
                {
                  Label icon = WicketUtils.newIcon("type", "icon-file");
                  WicketUtils.setHtmlTooltip(icon, "blob");
                  item.add(icon);
                  item.add(
                      new LinkPanel(
                          "summary",
                          null,
                          sr.path,
                          BlobPage.class,
                          WicketUtils.newPathParameter(sr.repository, sr.branch, sr.path)));
                  item.add(new Label("tags").setVisible(false));
                  break;
                }
            }
            item.add(
                new Label("fragment", sr.fragment)
                    .setEscapeModelStrings(false)
                    .setVisible(!StringUtils.isEmpty(sr.fragment)));
            item.add(
                new LinkPanel(
                    "repository",
                    null,
                    sr.repository,
                    SummaryPage.class,
                    WicketUtils.newRepositoryParameter(sr.repository)));
            if (StringUtils.isEmpty(sr.branch)) {
              item.add(new Label("branch", "null"));
            } else {
              item.add(
                  new LinkPanel(
                      "branch",
                      "branch",
                      StringUtils.getRelativePath(Constants.R_HEADS, sr.branch),
                      LogPage.class,
                      WicketUtils.newObjectParameter(sr.repository, sr.branch)));
            }
            item.add(new Label("author", sr.author));
            item.add(
                WicketUtils.createDatestampLabel("date", sr.date, getTimeZone(), getTimeUtils()));
          }
        };
    add(resultsView.setVisible(results.size() > 0));

    PageParameters pagerParams = new PageParameters();
    pagerParams.put("repositories", StringUtils.flattenStrings(repositoriesModel.getObject()));
    pagerParams.put("query", queryModel.getObject());

    boolean showPager = false;
    int totalPages = 0;
    if (results.size() > 0) {
      totalPages =
          (results.get(0).totalHits / pageSize) + (results.get(0).totalHits % pageSize > 0 ? 1 : 0);
      showPager = results.get(0).totalHits > pageSize;
    }

    add(
        new PagerPanel("topPager", page, totalPages, LuceneSearchPage.class, pagerParams)
            .setVisible(showPager));
    add(
        new PagerPanel("bottomPager", page, totalPages, LuceneSearchPage.class, pagerParams)
            .setVisible(showPager));
  }