view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/HgSubs.java @ 1045:71d765469b2d

fixed function name
author victory.bedrosova
date Tue, 19 Jan 2021 22:53:01 +0100
parents 10dc26b32c35
children
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.util.FileUtil;
import jetbrains.buildServer.util.StringUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.util.*;

import static java.util.Collections.emptyMap;

/**
 * Created 13.01.14 19:51
 *
 * @author Eugene Petrenko (eugene.petrenko@jetbrains.com)
 */
public class HgSubs {

  @NotNull
  public static Map<String, SubRepo> readSubrepositories(@NotNull final File hgsub,
                                                         @NotNull final File hgsubstate) {
    if (!hgsub.exists() || !hgsubstate.exists()) {
      return emptyMap();
    }

    final Map<String, String> path2repo;
    final Map<String, String> path2revision;
    try {
      path2repo = readHgsub(hgsub);
      path2revision = readHgsubstate(hgsubstate);
    } catch (IOException e) {
      return emptyMap();
    }

    return readSubrepositories(path2repo, path2revision);
  }

  @NotNull
  public static Map<String, SubRepo> readSubrepositories(@Nullable final String hgsubText,
                                                         @Nullable final String hgsubstateText) {

    if (hgsubstateText == null || hgsubText == null) return emptyMap();

    return readSubrepositories(
            readHgsub(Arrays.asList(StringUtil.splitByLines(hgsubText))),
            readHgsubstate(Arrays.asList(StringUtil.splitByLines(hgsubstateText)))
    );
  }

  @NotNull
  private static Map<String, SubRepo> readSubrepositories(@NotNull final Map<String, String> path2repo,
                                                          @NotNull final Map<String, String> path2revision) {
    final Map<String, SubRepo> result = new HashMap<>();
    for (Map.Entry<String, String> entry : path2repo.entrySet()) {
      final String path = entry.getKey();
      final String url = entry.getValue();
      final String revision = path2revision.get(path);
      if (revision != null)
        result.put(path, new SubRepo(path, url, revision));
    }
    return result;
  }

  @NotNull
  /*returns map: relative path -> repository url */
  private static Map<String, String> readHgsub(@NotNull final File hgsub) throws IOException {
    return readHgsub(FileUtil.readFile(hgsub));
  }

  @NotNull
  /*returns map: relative path -> repository url */
  private static Map<String, String> readHgsub(@NotNull final Collection<String> lines) {
    Map<String, String> result = new HashMap<>();
    for (String line : lines) {
      String[] parts = line.split(" = ");
      if (parts.length == 2)
        result.put(parts[0], parts[1]);
    }
    return result;
  }


  @NotNull
  /*returns map: relative path -> revision */
  private static Map<String, String> readHgsubstate(@NotNull final File hgsubstate) throws IOException {
    return readHgsubstate(FileUtil.readFile(hgsubstate));
  }

  @NotNull
  /*returns map: relative path -> revision */
  private static Map<String, String> readHgsubstate(@NotNull final Collection<String> lines) {
    final Map<String, String> result = new HashMap<>();
    for (String line : lines) {
      String[] parts = line.split(" ");
      if (parts.length == 2)
        result.put(parts[1], parts[0]);
    }
    return result;
  }
}