view mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/MercurialCommitSupport.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 com.intellij.openapi.diagnostic.Logger;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.HgVcsRoot;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.PushCommand;
import jetbrains.buildServer.util.FileUtil;
import jetbrains.buildServer.util.StringUtil;
import jetbrains.buildServer.vcs.*;
import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class MercurialCommitSupport implements CommitSupport, MercurialServerExtension {

  private final static Logger LOG = Logger.getInstance(MercurialCommitSupport.class.getName());

  private final MercurialVcsSupport myVcs;
  private final MirrorManager myMirrorManager;
  private final ServerPluginConfig myConfig;
  private final HgVcsRootFactory myHgVcsRootFactory;
  private final HgRepoFactory myHgRepoFactory;


  public MercurialCommitSupport(@NotNull MercurialVcsSupport vcs,
                                @NotNull MirrorManager mirrorManager,
                                @NotNull ServerPluginConfig config,
                                @NotNull HgVcsRootFactory vcsRootFactory,
                                @NotNull HgRepoFactory hgRepoFactory) {
    myVcs = vcs;
    myMirrorManager = mirrorManager;
    myConfig = config;
    myHgVcsRootFactory = vcsRootFactory;
    myHgRepoFactory = hgRepoFactory;
    myVcs.addExtension(this);
  }

  @NotNull
  public CommitPatchBuilder getCommitPatchBuilder(@NotNull VcsRoot root) throws VcsException {
    File patchDir;
    try {
      patchDir = HgFileUtil.createTempDir();
    } catch (IOException e) {
      throw new VcsException("Cannot create temp directory for commit", e);
    }
    File checkoutDir;
    try {
      checkoutDir = HgFileUtil.createTempDir();
    } catch (IOException e) {
      throw new VcsException("Cannot create checkout directory for commit", e);
    }

    HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(root);
    return new MercurialCommitPatchBuilder(root, hgRoot, patchDir, checkoutDir);
  }


  private class MercurialCommitPatchBuilder implements CommitPatchBuilder {
    private final VcsRoot myRoot;
    private final HgVcsRoot myHgRoot;
    private final File myPatchDir;
    private final File myCheckoutDir;
    private final Set<String> myCreatedFiles = new HashSet<String>();
    private final Set<String> myDeletedFiles = new HashSet<String>();
    private final Set<String> myDeletedDirs = new HashSet<String>();

    public MercurialCommitPatchBuilder(@NotNull VcsRoot root, @NotNull HgVcsRoot hgRoot, @NotNull File patchDir, @NotNull File checkoutDir) {
      myRoot = root;
      myHgRoot = hgRoot;
      myPatchDir = patchDir;
      myCheckoutDir = checkoutDir;
    }

    public void createFile(@NotNull String path, @NotNull InputStream content) throws VcsException {
      OutputStream out = null;
      try {
        File f = new File(myPatchDir, path);
        f.getParentFile().mkdirs();
        out = new BufferedOutputStream(new FileOutputStream(f));
        FileUtil.copy(content, out);
        myCreatedFiles.add(path);
      } catch (IOException e) {
        LOG.error("Error while creating file " + path, e);
        throw new VcsException("Error while creating file " + path, e);
      } finally {
        if (out != null) {
          try {
            out.close();
          } catch (IOException e) {
            LOG.error(e);
          }
        }
      }
    }

    public void createDirectory(@NotNull String path) {
      //ignore
    }

    public void deleteFile(@NotNull String path) {
      myDeletedFiles.add(path);
    }

    public void deleteDirectory(@NotNull String path) {
      myDeletedDirs.add(path);
    }

    public void renameFile(@NotNull String oldPath, @NotNull String newPath, @NotNull InputStream content) throws VcsException {
      myDeletedFiles.add(oldPath);
      createFile(newPath, content);
    }

    @NotNull
    public CommitResult commit(@NotNull CommitSettings commitSettings) throws VcsException {
      try {
        RepositoryStateData state = getCurrentState();
        String defaultBranch = state.getDefaultBranchName();
        String defaultBranchRevision = state.getBranchRevisions().get(defaultBranch);
        if (!state.getBranchRevisions().isEmpty() && defaultBranchRevision == null)
          throw new VcsException("The '" + defaultBranch + "' destination branch doesn't exist");

        new CheckoutRepository(myMirrorManager, myHgRepoFactory, myConfig.getPullTimeout(), myConfig.useTagsAsBranches(),
                myHgRoot, myCheckoutDir).setRevision(defaultBranchRevision).checkout();

        HgRepo repo = myHgRepoFactory.createRepo(myHgRoot, myCheckoutDir);
        boolean bookmark = repo.isBookmark(defaultBranch);
        if (!bookmark)
          repo.branch().name(defaultBranch).call();

        for (String dir : myDeletedDirs) {
          FileUtil.delete(new File(myCheckoutDir, dir));
        }
        for (String f : myDeletedFiles) {
          FileUtil.delete(new File(myCheckoutDir, f));
        }
        for (String f : myCreatedFiles) {
          FileUtil.copy(new File(myPatchDir, f), new File(myCheckoutDir, f));
        }

        repo.addRemove().call();
        int exitCode = repo.commit().by(commitSettings.getUserName()).message(nonEmptyMessage(commitSettings)).call();
        if (exitCode == 1)
          return CommitResult.createRepositoryUpToDateResult(defaultBranchRevision);

        if (bookmark)
          repo.updateBookmark().name(defaultBranch).call();

        PushCommand push = repo.push().toRepository(myHgRoot.getRepository());
        if (bookmark)
          push.bookmark(defaultBranch);
        push.call();

        return CommitResult.createSuccessResult(repo.getWorkingDirRevision());
      } catch (Exception e) {
        if (e instanceof VcsException)
          throw (VcsException) e;
        throw new VcsException(e);
      }
    }

    @NotNull
    private RepositoryStateData getCurrentState() throws VcsException {
      Map<String, String> props = new HashMap<String, String>(myHgRoot.getProperties());
      props.put(Constants.IGNORE_MISSING_DEFAULT_BRANCH, "true");
      return myVcs.getCollectChangesPolicy().getCurrentState(new HgVcsRoot(props));
    }

    private String nonEmptyMessage(@NotNull CommitSettings commitSettings) {
      String msg = commitSettings.getDescription();
      if (!StringUtil.isEmpty(msg))
        return msg;
      return "no comments";
    }

    public void dispose() {
      FileUtil.delete(myPatchDir);
      FileUtil.delete(myCheckoutDir);
    }
  }
}