Mercurial > hg > mercurial
view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/CommandUtil.java @ 557:2b8299ba321d
Speed up changes collecting
author | Dmitry Neverov <dmitry.neverov@jetbrains.com> |
---|---|
date | Fri, 01 Mar 2013 16:03:00 +0400 |
parents | 493f78cb64dd |
children | 319955174f96 |
line wrap: on
line source
/* * Copyright 2000-2011 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.ExecResult; import jetbrains.buildServer.SimpleCommandLineProcessRunner; import jetbrains.buildServer.log.Loggers; import jetbrains.buildServer.vcs.VcsException; import org.jetbrains.annotations.NotNull; import java.io.File; import java.util.Set; public class CommandUtil { public static CommandResult runCommand(@NotNull MercurialCommandLine cli, @NotNull CommandSettings settings) throws VcsException { final String command = removePrivateData(cli.getCommandLineString(), settings.getPrivateData()); logRunCommand(cli, command, settings); CommandResult res = run(cli, settings.getTimeout(), command, settings.getPrivateData()); if (settings.isCheckForFailure() || settings.isFailWithNonEmptyStderr()) res.checkFailure(settings.isFailWithNonEmptyStderr()); logCommandOutput(command, res); return res; } private static CommandResult run(@NotNull final MercurialCommandLine cli, final int executionTimeout, @NotNull final String command, @NotNull final Set<String> privateData) { final long start = System.currentTimeMillis(); ExecResult res = SimpleCommandLineProcessRunner.runCommand(cli, null, new SimpleCommandLineProcessRunner.ProcessRunCallbackAdapter() { @Override public Integer getOutputIdleSecondsTimeout() { return executionTimeout; } @Override public void onProcessFinished(Process ps) { long duration = System.currentTimeMillis() - start; System.out.println("Command " + command + " took " + duration + "ms"); Loggers.VCS.debug("Command " + command + " took " + duration + "ms"); } }); return new CommandResult(Loggers.VCS, command, res, privateData); } public static String removePrivateData(final String str, final Set<String> privateData) { String result = str; for (String data: privateData) { if (data == null || data.length() == 0) continue; result = result.replace(data, "******"); } return result; } private static void logRunCommand(@NotNull MercurialCommandLine cmd, @NotNull String command, @NotNull CommandSettings settings) { String workingDir = cmd.getWorkingDirectory(); String dir = workingDir != null ? workingDir : new File(".").getAbsolutePath(); String message = "[" + dir + "] " + command; if ("debug".equals(settings.getLogLevel())) { Loggers.VCS.debug(message); } else { Loggers.VCS.info(message); } } private static void logCommandOutput(@NotNull String command, @NotNull CommandResult result) { Loggers.VCS.debug("Command " + command + " output:\n" + result.getStdout()); // System.out.println("Command " + command + " output:\n " + result.getStdout()); } }