Mercurial > hg > mercurial
view mercurial-common/src/jetbrains/buildServer/buildTriggers/vcs/mercurial/command/MercurialCommandLine.java @ 1124:a14a2f7e74d8 development/2024.03.x tip
2024.03.x branch is created
author | Nadia Burnasheva <nadezhda.burnasheva@jetbrains.com> |
---|---|
date | Thu, 15 Feb 2024 11:33:35 +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.command; import com.intellij.execution.configurations.GeneralCommandLine; import jetbrains.buildServer.buildTriggers.vcs.mercurial.MercurialProgress; import jetbrains.buildServer.util.StringUtil; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; import java.nio.charset.Charset; import java.util.*; import static com.intellij.openapi.util.text.StringUtil.isEmpty; public class MercurialCommandLine { private String myExePath; private final List<String> myArguments = new ArrayList<>(); private final Map<String, String> myEnvPatch = new TreeMap<>(); private String myWorkingDirectory; private final Set<String> myPrivateData; private Charset myCharset; private String myDescription = ""; private boolean myHasProgress = false; public MercurialCommandLine(@NotNull final Set<String> privateData) { myPrivateData = privateData; } @NotNull public MercurialCommandLine forkWithoutCommandlineArguments() { final MercurialCommandLine fork = fork(); fork.myArguments.clear(); return fork; } @NotNull public MercurialCommandLine fork() { final MercurialCommandLine line = new MercurialCommandLine(myPrivateData); line.myExePath = myExePath; line.myArguments.addAll(myArguments); line.myEnvPatch.putAll(myEnvPatch); line.myWorkingDirectory = myWorkingDirectory; line.myCharset = myCharset; return line; } public int getCommandLineLength() { return getCommandLineString().length() + 42; } @NotNull public String getCommandLineString() { return toGeneralCommandLine().getCommandLineString(); } public void setExePath(@NotNull final String hgPath) { myExePath = hgPath; } @NotNull public List<String> getArguments() { return new ArrayList<>(myArguments); } public void addParameter(@NotNull final String parameter) { String escaped = escape(parameter); myArguments.add(escaped); } public void addParameters(@NotNull final Collection<String> argz) { for (String s : argz) { addParameter(s); } } public void addParameters(@NotNull final String... argz) { for (String s : argz) { addParameter(s); } } public void addParameters(@NotNull final String a, @NotNull final String b) { addParameter(a); addParameter(b); } public void setEnvParams(@NotNull final Map<String, String> hgEnv) { myEnvPatch.clear(); myEnvPatch.putAll(hgEnv); } public void addEnvParam(@NotNull final String key, @NotNull final String value) { myEnvPatch.put(key, value); } public void setCharset(@NotNull Charset charset) { myCharset = charset; } public void setWorkDirectory(@NonNls String path) { myWorkingDirectory = path; } public String getWorkingDirectory() { return myWorkingDirectory; } private String escape(String s) { return StringUtil.escapeQuotesIfWindows(s); } @NotNull public GeneralCommandLine toGeneralCommandLine() { final Charset charset = myCharset; final GeneralCommandLine cmd = new GeneralCommandLine() { @Override public String getCommandLineParams() { return CommandUtil.removePrivateData(super.getCommandLineParams(), myPrivateData); } @Override public String getCommandLineString() { return CommandUtil.removePrivateData(super.getCommandLineString(), myPrivateData); } @Override public Charset getCharset() { if (charset != null) return charset; return super.getCharset(); } }; if (myExePath != null) { cmd.setExePath(myExePath); } if (myWorkingDirectory != null) { cmd.setWorkDirectory(myWorkingDirectory); } for (String argument : myArguments) { cmd.addParameter(argument); } if (!myEnvPatch.isEmpty()) { cmd.setPassParentEnvs(true); cmd.setEnvParams(new TreeMap<>(myEnvPatch)); } return cmd; } @NotNull public String getDescription() { return myDescription; } public void setDescription(@NotNull String description) { myDescription = description; } public void setHasProgress(boolean hasProgress) { myHasProgress = hasProgress; } public boolean hasProgress() { return myHasProgress; } public void logCommandStarted(@NotNull MercurialProgress progress) { if (!isEmpty(myDescription)) { if (myHasProgress) { progress.progressStarted(getProgressMessage()); } else { progress.reportProgress(getProgressMessage()); } } } public void logCommandFinished(@NotNull MercurialProgress progress) { if (!isEmpty(myDescription) && myHasProgress) progress.progressFinished(getProgressMessage()); } @NotNull private String getProgressMessage() { String result = myDescription != null ? "[" + myWorkingDirectory + "] " : ""; result += myDescription; return result; } }