Search in sources :

Example 41 with VcsException

use of jetbrains.buildServer.vcs.VcsException in project teamcity-git by JetBrains.

the class CleanCommandImpl method call.

public void call() throws VcsException {
    GitCommandLine cmd = getCmd();
    cmd.addParameters("clean", "-f", "-d");
    switch(myCleanPolicy) {
        case ALL_UNTRACKED:
            cmd.addParameter("-x");
            break;
        case IGNORED_ONLY:
            cmd.addParameter("-X");
            break;
        case NON_IGNORED_ONLY:
            break;
    }
    addExcludes(cmd);
    cmd.withMaxOutputSize(8 * 1024 * 1024);
    try {
        CommandUtil.runCommand(cmd.stdErrExpected(false));
    } catch (VcsException e) {
        Loggers.VCS.warnAndDebugDetails("Failed to clean files", e);
        if (!SystemInfo.isWindows || CommandUtil.isCanceledError(e)) {
            throw e;
        }
        if (CommandUtil.isNoSuchFileOrDirError(e)) {
            throw new VcsException("Some files may be locked: " + e.getMessage(), e);
        }
        final File workingDir = cmd.getWorkingDirectory();
        if (workingDir == null) {
            throw e;
        }
        if (CommandUtil.isFileNameTooLongError(e)) {
            handleLongFileNames(workingDir, e);
        }
    }
}
Also used : GitCommandLine(jetbrains.buildServer.buildTriggers.vcs.git.command.GitCommandLine) VcsException(jetbrains.buildServer.vcs.VcsException) File(java.io.File)

Example 42 with VcsException

use of jetbrains.buildServer.vcs.VcsException in project teamcity-git by JetBrains.

the class CommandLineUtil method getCommandLineError.

@Nullable
public static VcsException getCommandLineError(@NotNull String cmdName, @NotNull String details, @NotNull ExecResult res, boolean includeStdOut, boolean includeStdErr) {
    // noinspection ThrowableResultOfMethodCallIgnored
    Throwable exception = res.getException();
    int exitCode = res.getExitCode();
    if (exitCode != 0 || exception != null) {
        String stderr = res.getStderr();
        String stdout = res.getStdout();
        final String message = "'" + cmdName + "' command failed" + details + "." + (exception != null ? "\nexception: " + exception.toString() : "") + (includeStdErr && !StringUtil.isEmpty(stderr) ? "\nstderr: " + stderr.trim() : "") + (includeStdOut && !StringUtil.isEmpty(stdout) ? "\nstdout: " + stdout.trim() : "") + (res.getElapsedTime() != -1 ? "\nelapsed time: " + TimePrinter.createMillisecondsFormatter().formatTime(res.getElapsedTime()) : "") + (exitCode != 0 ? "\nexit code: " + exitCode : "");
        return exception == null ? new VcsException(message) : new VcsException(message, exception);
    } else {
        return null;
    }
}
Also used : VcsException(jetbrains.buildServer.vcs.VcsException) Nullable(org.jetbrains.annotations.Nullable)

Example 43 with VcsException

use of jetbrains.buildServer.vcs.VcsException in project teamcity-git by JetBrains.

the class GitUtils method getGitDir.

public static File getGitDir(@NotNull File workingTreeDir) throws IOException, VcsException {
    File dotGit = new File(workingTreeDir, ".git");
    if (dotGit.isFile()) {
        List<String> content = FileUtil.readFile(dotGit);
        if (content.isEmpty())
            throw new VcsException("Empty " + dotGit.getAbsolutePath());
        String line = content.get(0);
        if (!line.startsWith("gitdir:"))
            throw new VcsException("Wrong format of " + dotGit.getAbsolutePath() + ": " + content);
        String gitDirPath = line.substring("gitdir:".length()).trim();
        File gitDir = new File(gitDirPath);
        if (gitDir.isAbsolute())
            return gitDir;
        return new File(workingTreeDir, gitDirPath);
    }
    return dotGit;
}
Also used : VcsException(jetbrains.buildServer.vcs.VcsException) File(java.io.File)

Example 44 with VcsException

use of jetbrains.buildServer.vcs.VcsException in project teamcity-git by JetBrains.

the class GitCommandLine method withAskPassScript.

private void withAskPassScript(@Nullable String pass, @NotNull Consumer<String> action) throws VcsException {
    File askPass = null;
    String askPassPath;
    try {
        askPass = myScriptGen.generateAskPass(pass);
        askPassPath = askPass.getAbsolutePath();
        if (askPassPath.contains(" ") && SystemInfo.isWindows) {
            askPassPath = GitUtils.getShortFileName(askPass);
        }
        action.accept(askPassPath);
    } catch (Exception e) {
        if (askPass != null) {
            FileUtil.delete(askPass);
        }
        throw new VcsException(e);
    }
    final File finalAskPass = askPass;
    addPostAction(() -> {
        if (myCtx.isDeleteTempFiles())
            FileUtil.delete(finalAskPass);
    });
}
Also used : VcsException(jetbrains.buildServer.vcs.VcsException) CheckoutCanceledException(jetbrains.buildServer.buildTriggers.vcs.git.command.errors.CheckoutCanceledException) VcsException(jetbrains.buildServer.vcs.VcsException)

Example 45 with VcsException

use of jetbrains.buildServer.vcs.VcsException in project teamcity-git by JetBrains.

the class GitServerUtil method getRepository.

/**
 * Ensures that a bare repository exists at the specified path.
 * If it does not, the directory is attempted to be created.
 *
 * @param dir    the path to the directory to init
 * @param remote the remote URL
 * @return a connection to repository
 * @throws VcsException if the there is a problem with accessing VCS
 */
public static Repository getRepository(@NotNull final File dir, @NotNull final URIish remote) throws VcsException {
    if (dir.exists() && !dir.isDirectory()) {
        throw new VcsException("The specified path is not a directory: " + dir);
    }
    try {
        ensureRepositoryIsValid(dir);
        Repository r = getRepositoryWithDisabledAutoGc(dir);
        String remoteUrl = remote.toString();
        if (remoteUrl.contains("\n") || remoteUrl.contains("\r"))
            throw new VcsException("Newline in url '" + remoteUrl + "'");
        if (!new File(dir, "config").exists()) {
            r.create(true);
            addConfigOptions(r.getConfig(), remoteUrl).save();
        } else {
            final StoredConfig config = r.getConfig();
            final String existingRemote = config.getString("teamcity", null, "remote");
            final String refSpec = config.getString("remote", "origin", "fetch");
            final String mirror = config.getString("remote", "origin", "mirror");
            if (existingRemote != null && !remoteUrl.equals(existingRemote)) {
                throw getWrongUrlError(dir, existingRemote, remote);
            } else if (existingRemote == null || refSpec == null || mirror != null) {
                addConfigOptions(config, remoteUrl).save();
            }
        }
        return r;
    } catch (Exception ex) {
        if (ex instanceof NullPointerException)
            LOG.warn("The repository at directory '" + dir + "' cannot be opened or created", ex);
        throw new VcsException("The repository at directory '" + dir + "' cannot be opened or created, reason: " + ex.toString(), ex);
    }
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) VcsException(jetbrains.buildServer.vcs.VcsException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) TransportException(org.eclipse.jgit.errors.TransportException) VcsException(jetbrains.buildServer.vcs.VcsException) JSchException(com.jcraft.jsch.JSchException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) NotSupportedException(org.eclipse.jgit.errors.NotSupportedException)

Aggregations

VcsException (jetbrains.buildServer.vcs.VcsException)79 File (java.io.File)34 NotNull (org.jetbrains.annotations.NotNull)22 GitTestUtil.dataFile (jetbrains.buildServer.buildTriggers.vcs.git.tests.GitTestUtil.dataFile)13 IOException (java.io.IOException)12 TestFor (jetbrains.buildServer.util.TestFor)11 GeneralCommandLine (com.intellij.execution.configurations.GeneralCommandLine)10 Method (java.lang.reflect.Method)10 ExecResult (jetbrains.buildServer.ExecResult)10 VcsRootImpl (jetbrains.buildServer.vcs.impl.VcsRootImpl)10 AfterMethod (org.testng.annotations.AfterMethod)10 BeforeMethod (org.testng.annotations.BeforeMethod)10 VcsRoot (jetbrains.buildServer.vcs.VcsRoot)9 Repository (org.eclipse.jgit.lib.Repository)9 URIish (org.eclipse.jgit.transport.URIish)9 Nullable (org.jetbrains.annotations.Nullable)8 SimpleCommandLineProcessRunner (jetbrains.buildServer.SimpleCommandLineProcessRunner)5 GitExec (jetbrains.buildServer.buildTriggers.vcs.git.command.GitExec)5 CheckoutRules (jetbrains.buildServer.vcs.CheckoutRules)5 RefSpec (org.eclipse.jgit.transport.RefSpec)5