view mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialCommitsInfoBuilderSupport.java @ 1024:c0c4bf1db865

TW-66329 fix using archive command for creating full patch: previous implementation using clone produces very large overhead for big project repositories in case when settings are stored in the project repo since in case of freezing build settings, core system asks plugin to build full patch and cloning repos into temp dir even from local mirror can take tens of minutes
author Maxim Zaytsev <Maxim.Zaytsev@jetbrains.com>
date Mon, 15 Jun 2020 13:07:12 +0300
parents 7bf4d943d5bb
children 10dc26b32c35
line wrap: on
line source
/*
 * Copyright 2000-2018 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package jetbrains.buildServer.buildTriggers.vcs.mercurial;

import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.CommitsAndMountPointsCommand;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.HgVcsRoot;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.LogCommand;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.exception.WrongSubrepoUrlException;
import jetbrains.buildServer.dataStructures.MultiMapToList;
import jetbrains.buildServer.vcs.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Date;
import java.util.Map;

/**
 * Created 30.09.13 13:05
 *
 * @author Eugene Petrenko (eugene.petrenko@jetbrains.com)
 */
public class MercurialCommitsInfoBuilderSupport implements CommitsInfoBuilder, MercurialServerExtension {
  private final MercurialVcsSupport mySupport;
  private final HgVcsRootFactory myHgVcsRootFactory;

  public MercurialCommitsInfoBuilderSupport(@NotNull MercurialVcsSupport vcs,
                                            @NotNull HgVcsRootFactory vcsRootFactory) {
    vcs.addExtension(this);
    mySupport = vcs;
    myHgVcsRootFactory = vcsRootFactory;
  }

  @NotNull
  private static MultiMapToList<String, String> commitToBranchs(@NotNull final Map<String, String> branchToCommit) {
    final MultiMapToList<String, String> map = new MultiMapToList<String, String>();
    for (Map.Entry<String, String> e : branchToCommit.entrySet()) {
      map.add(e.getValue(), e.getKey());
    }
    return map;
  }

  public void collectCommits(@NotNull final VcsRoot root,
                             @NotNull final CheckoutRules rules,
                             @NotNull final CommitsConsumer consumer) throws VcsException {

    final HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(root);
    final ServerHgRepo repo = mySupport.createRepo(hgRoot);
    VcsCallable<MultiMapToList<String, String>> cmd = new VcsCallable<MultiMapToList<String, String>>() {
      public MultiMapToList<String, String> call() throws VcsException {
        return commitToBranchs(mySupport.getCollectChangesPolicy().getHeads(hgRoot));
      }
    };
    final MultiMapToList<String, String> heads = mySupport.syncRepository(hgRoot, new SyncSettings<MultiMapToList<String, String>>(cmd));
    repo.logSubstates().call(new CommitsAndMountPointsCommand.Callback() {
      private final MercurialCommitsInfoBuilderStates subs = new MercurialCommitsInfoBuilderStates();

      public void processHGSubFile(@NotNull String fileId, @NotNull String text) {
        subs.addSubNode(fileId, text);
      }

      public void processHGSubStateFile(@NotNull String fileId, @NotNull String text) {
        subs.addStateNode(fileId, text);
      }

      public void onCommit(@NotNull String commitNum,
                           @NotNull String commitId,
                           @NotNull String[] parents,
                           @NotNull String branch,
                           @NotNull String[] tags,
                           @NotNull String author,
                           @NotNull String message,
                           @NotNull Date timestamp,
                           @Nullable String hgsubNodeId,
                           @Nullable String hgsubstateNodeId) {

        final CommitDataBean bean = new CommitDataBean(
                HgRepo.shortId(commitId),
                HgRepo.shortId(commitId),
                timestamp
                );

        for (String parent : parents) {
          if (LogCommand.ZERO_PARENT_ID.equals(parent)) continue;
          if (LogCommand.ZERO_PARENT_SHORT_ID.equals(parent)) continue;
          bean.addParentRevision(HgRepo.shortId(parent));
        }

        for (String tag : tags) {
          if ("tip".equals(tag)) continue;

          bean.addTag(tag);
        }

        bean.addBranch(branch);
        bean.setCommitAuthor(author);
        bean.setCommitMessage(message);

        for (SubRepo subRepo : subs.mounts(hgsubNodeId, hgsubstateNodeId).values()) {
          try {
            bean.addMountPoint(new CommitMountPointDataBean(
                    subRepo.vcsType().getVcsPluginName(),
                    subRepo.resolveUrl(hgRoot.getRepository()),
                    subRepo.path(),
                    subRepo.revision()
                    ));
          } catch (WrongSubrepoUrlException e) {
            //NOP
          }
        }

        bean.setHeads(heads.getValues(bean.getVersion()));

        consumer.consumeCommit(bean);
      }
    });
  }

}