view mercurial-server/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/ModificationDataFactory.java @ 977:38adef4f1b8f Indore-2017.2.x

Update copyright
author pavel.sher
date Mon, 22 Jan 2018 11:40:45 +0100
parents ed49cbc93aa3
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.ChangeSet;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.ChangeSetRevision;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.FileStatus;
import jetbrains.buildServer.buildTriggers.vcs.mercurial.command.Status;
import jetbrains.buildServer.log.Loggers;
import jetbrains.buildServer.serverSide.TeamCityProperties;
import jetbrains.buildServer.vcs.*;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import static java.util.Collections.emptyList;

/**
 * Created 19.12.13 19:05
 *
 * @author Eugene Petrenko (eugene.petrenko@jetbrains.com)
 */
public class ModificationDataFactory {
  @NotNull
  public static ModificationData createModificationData(@NotNull final CollectChangesContext ctx,
                                                        @NotNull final ChangeSet cset,
                                                        @NotNull final VcsRoot root,
                                                        @NotNull final CheckoutRules checkoutRules) throws VcsException {
    final List<ChangeSetRevision> parents = cset.getParents();
    if (parents.isEmpty()) throw new IllegalStateException("Commit " + cset.getId() + " has no parents");

    final String version = ctx.getStringFromPool(cset.getId());
    final List<VcsChange> files = toVcsChanges(ctx, cset.getModifiedFiles(), ctx.getStringFromPool(parents.get(0).getId()), version, checkoutRules);
    final ModificationData result = new ModificationData(cset.getTimestamp(), files, cset.getDescription(), ctx.getStringFromPool(cset.getUser()), root, version, version);

    for (ChangeSetRevision parent : parents) {
      result.addParentRevision(ctx.getStringFromPool(parent.getId()));
    }
    setCanBeIgnored(result, cset);
    return result;
  }

  private static void setCanBeIgnored(@NotNull final ModificationData md,
                                      @NotNull final ChangeSet cset) {
    if (md.getParentRevisions().size() > 1) {
      boolean canIgnoreMerges = TeamCityProperties.getBooleanOrTrue("teamcity.hg.allowIgnoringMergeCommits");
      if (!canIgnoreMerges)
        md.setCanBeIgnored(false);
    } else if (cset.getModifiedFiles().isEmpty()) {
      boolean canIgnoreEmptyCommits = TeamCityProperties.getBooleanOrTrue("teamcity.hg.allowIgnoringEmptyCommits");
      if (!canIgnoreEmptyCommits)
        md.setCanBeIgnored(false);
    }
  }


  @NotNull
  public static List<VcsChange> toVcsChanges(@NotNull CollectChangesContext ctx,
                                             @NotNull List<FileStatus> modifiedFiles,
                                             @NotNull String prevVer,
                                             @NotNull String curVer,
                                             @NotNull CheckoutRules rules) {
    final List<VcsChange> files = new ArrayList<VcsChange>(0);
    for (FileStatus mf : modifiedFiles) {
      if (rules.map(mf.getPath()) == null) continue;

      final String normalizedPath = PathUtil.normalizeSeparator(mf.getPath());
      final String path = ctx.getStringFromPool(normalizedPath);
      files.add(new VcsChange(getChangeType(mf.getStatus()), mf.getStatus().getName(), path, path, prevVer, curVer));
    }

    if (files.isEmpty()) return emptyList();
    return files;
  }

  @NotNull
  private static VcsChangeInfo.Type getChangeType(final Status status) {
    switch (status) {
      case ADDED:
        return VcsChangeInfo.Type.ADDED;
      case MODIFIED:
        return VcsChangeInfo.Type.CHANGED;
      case REMOVED:
        return VcsChangeInfo.Type.REMOVED;
      default:
        Loggers.VCS.warn("Unable to convert status: " + status + " to VCS change type");
        return VcsChangeInfo.Type.NOT_CHANGED;
    }
  }
}