Mercurial > hg > mercurial
view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CatCommand.java @ 804:578eab848c5d
@NotNull
author | eugene.petrenko@jetbrains.com |
---|---|
date | Fri, 30 May 2014 10:29:34 +0200 |
parents | 005ea6b2dc0f |
children | 25482087e9b5 |
line wrap: on
line source
/* * Copyright 2000-2014 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.command; import jetbrains.buildServer.buildTriggers.vcs.mercurial.HgFileUtil; import jetbrains.buildServer.buildTriggers.vcs.mercurial.OS; import jetbrains.buildServer.log.Loggers; import jetbrains.buildServer.vcs.VcsException; import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; import static java.util.Collections.singletonList; import static jetbrains.buildServer.buildTriggers.vcs.mercurial.HgFileUtil.deleteDir; public class CatCommand extends AuthCommand { private String myRevId; private List<String> myRelativePaths = new ArrayList<String>(); private boolean myCheckForFailure = true; public CatCommand(@NotNull CommandSettings commandSettings, @NotNull String hgPath, @NotNull File workingDir, @NotNull AuthSettings authSettings) { super(commandSettings, hgPath, workingDir, authSettings); } @NotNull public CatCommand files(@NotNull String relativePath) { myRelativePaths = singletonList(relativePath); return this; } @NotNull public CatCommand files(@NotNull List<String> relativePaths) { myRelativePaths = relativePaths; return this; } @NotNull public CatCommand setRevId(final String revId) { myRevId = revId; return this; } @NotNull public CatCommand atRevision(@NotNull ChangeSet cset) { myRevId = cset.getId(); return this; } @NotNull public CatCommand checkForFailure(boolean doCheckForFailure) { myCheckForFailure = doCheckForFailure; return this; } @NotNull public File call() throws VcsException { return execute(myRelativePaths, myCheckForFailure); } @NotNull public File execute(List<String> relPaths) throws VcsException { return execute(relPaths, true); } @NotNull public File execute(@NotNull final List<String> relPaths, final boolean checkFailure) throws VcsException { File tempDir = null; try { tempDir = createTmpDir(); createDirectories(relPaths, tempDir); catFiles(relPaths, checkFailure, tempDir); return tempDir; } catch (VcsException e) { deleteDir(tempDir, Loggers.VCS); throw e; } } @NotNull private File createTmpDir() throws VcsException { try { return HgFileUtil.createTempDir(); } catch (IOException e) { throw new VcsException("Unable to create temporary directory", e); } } private void createDirectories(@NotNull final List<String> relPaths, @NotNull final File tempDir) throws VcsException { for (String path: relPaths) { File parentFile = new File(tempDir, path).getParentFile(); if (!parentFile.isDirectory() && !parentFile.mkdirs()) throw new VcsException("Failed to create directory: " + parentFile.getAbsolutePath()); } } private void catFiles(@NotNull final List<String> relPaths, final boolean checkFailure, @NotNull final File tempDir) throws VcsException { final Queue<String> paths = new LinkedList<String>(relPaths); while (!paths.isEmpty()) { MercurialCommandLine cli = createCommandLine(tempDir); int cmdSize = cli.getCommandLineString().length() + 42; do { String path = paths.poll(); cli.addParameter(path); cmdSize += path.length() + 3; //quotes + space } while (cmdSize < OS.getMaxCommandLineSize() && !paths.isEmpty()); runCommand(cli, myCommandSettings.setCheckForFailure(checkFailure)); } } @NotNull private MercurialCommandLine createCommandLine(@NotNull final File tempDir) { final MercurialCommandLine cli = createCommandLine(); addHttpAuthParams(cli); cli.addParameter("cat"); cli.addParameter("-o"); cli.addParameter(tempDir.getAbsolutePath() + File.separator + "%p"); if (myRevId != null) { cli.addParameter("-r"); cli.addParameter(myRevId); } return cli; } }