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

Update copyright
author pavel.sher
date Mon, 22 Jan 2018 11:40:45 +0100
parents 31a1aca3305c
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.HgVcsRoot;
import jetbrains.buildServer.vcs.ListDirectChildrenPolicy;
import jetbrains.buildServer.vcs.VcsException;
import jetbrains.buildServer.vcs.VcsFileData;
import jetbrains.buildServer.vcs.VcsRoot;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

import static com.intellij.openapi.util.text.StringUtil.isEmpty;
import static java.util.regex.Pattern.compile;
import static java.util.regex.Pattern.quote;

/**
 * @author dmitry.neverov
 */
public class ListFilesSupport implements ListDirectChildrenPolicy {

  private final MercurialVcsSupport myVcs;
  private final ServerPluginConfig myConfig;
  private final HgVcsRootFactory myHgVcsRootFactory;
  private long myLastSyncTime = -1;

  public ListFilesSupport(@NotNull MercurialVcsSupport vcs,
                          @NotNull ServerPluginConfig config,
                          @NotNull HgVcsRootFactory hgVcsRootFactory) {
    myVcs = vcs;
    myConfig = config;
    myHgVcsRootFactory = hgVcsRootFactory;
  }

  @NotNull
  public List<VcsFileData> listFiles(@NotNull VcsRoot root, @NotNull String dir) throws VcsException {
    HgVcsRoot hgRoot = myHgVcsRootFactory.createHgRoot(root);
    if (isOutOfDate()) {
      myVcs.syncRepository(hgRoot);
      myLastSyncTime = System.currentTimeMillis();
    }
    String dirPath = isEmpty(dir) || dir.endsWith("/") ? dir : dir + "/";
    return listFilesIn(hgRoot, dirPath);
  }


  @NotNull
  private List<VcsFileData> listFilesIn(@NotNull HgVcsRoot root, @NotNull String dir) throws VcsException {
    HgRepo repo = myVcs.createRepo(root);
    List<VcsFileData> result = new ArrayList<VcsFileData>();
    Pattern p = compile(quote(File.separator));
    for (String file : repo.listFiles(root.getBranchName())) {
      String canonicalFile = p.matcher(file).replaceAll("/");
      if (!canonicalFile.startsWith(dir))
        continue;
      String relativePath = canonicalFile.substring(dir.length());
      int idx = relativePath.indexOf("/");
      if (idx >= 0) {
        result.add(new VcsFileData(relativePath.substring(0, idx), true));
      } else {
        result.add(new VcsFileData(relativePath, false));
      }
    }
    return result;
  }

  private boolean isOutOfDate() {
    if (myLastSyncTime == -1)
      return true;
    return System.currentTimeMillis() - myLastSyncTime > myConfig.listFilesTTLSeconds() * 1000;
  }
}