Search in sources :

Example 21 with VcsException

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

the class GitHubRawFileContentProvider method getContent.

@NotNull
public byte[] getContent(@NotNull String filePath, @NotNull VcsRoot root, @NotNull String version) throws VcsException {
    OperationContext ctx = myVcs.createContext(root, "retrieving content");
    InputStream is = null;
    try {
        URLConnection conn = getConnection(ctx.getGitRoot(), filePath, version);
        is = conn.getInputStream();
        return readStream(is);
    } catch (Exception e) {
        // LOG
        return myGenericProvider.getContent(filePath, root, version);
    } finally {
        ctx.close();
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : InputStream(java.io.InputStream) IOException(java.io.IOException) URLConnection(java.net.URLConnection) VcsException(jetbrains.buildServer.vcs.VcsException) IOException(java.io.IOException) NotNull(org.jetbrains.annotations.NotNull)

Example 22 with VcsException

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

the class UpdaterWithMirror method updateLocalMirror.

protected void updateLocalMirror(File bareRepositoryDir, CommonURIish fetchUrl, Ref... revisions) throws VcsException {
    final boolean isSubmodule = !isRootRepositoryDir(bareRepositoryDir);
    String mirrorDescription = (isSubmodule ? "submodule " : "") + "local mirror of root " + myRoot.getName() + " at " + bareRepositoryDir;
    LOG.info("Update " + mirrorDescription);
    if (isValidGitRepo(bareRepositoryDir)) {
        removeOrphanedIdxFiles(bareRepositoryDir);
    } else {
        FileUtil.delete(bareRepositoryDir);
    }
    final AgentGitFacade git = myGitFactory.create(bareRepositoryDir);
    final SSLInvestigator sslInvestigator = getSSLInvestigator(fetchUrl);
    boolean fetchRequired;
    if (!bareRepositoryDir.exists()) {
        LOG.info("Init " + mirrorDescription);
        bareRepositoryDir.mkdirs();
        git.init().setBare(true).call();
        configureRemoteUrl(bareRepositoryDir, fetchUrl);
        sslInvestigator.setCertificateOptions(git);
        fetchRequired = true;
    } else {
        configureRemoteUrl(bareRepositoryDir, fetchUrl);
        sslInvestigator.setCertificateOptions(git);
        fetchRequired = removeOutdatedRefs(bareRepositoryDir);
    }
    final AgentCommitLoader commitLoader = isSubmodule ? AgentCommitLoaderFactory.getCommitLoaderForSubmodule(myRoot, myBuild, bareRepositoryDir, myGitFactory, myPluginConfig, myLogger) : AgentCommitLoaderFactory.getCommitLoaderForMirror(myRoot, myBuild, bareRepositoryDir, myGitFactory, myPluginConfig, myLogger);
    try {
        loadCommits(fetchRequired, commitLoader, revisions);
    } catch (VcsException e) {
        if (myPluginConfig.isFailOnCleanCheckout() || !CommandUtil.shouldFetchFromScratch(e)) {
            throw e;
        }
        LOG.warnAndDebugDetails("Failed to fetch mirror, will try removing it and cloning from scratch", e);
        if (cleanDir(bareRepositoryDir)) {
            git.init().setBare(true).call();
            configureRemoteUrl(bareRepositoryDir, fetchUrl);
            sslInvestigator.setCertificateOptions(git);
            loadCommits(true, commitLoader, revisions);
        } else {
            LOG.info("Failed to delete repository " + bareRepositoryDir + " after failed checkout, clone repository in another directory");
            myMirrorManager.invalidate(bareRepositoryDir);
            updateLocalMirror(myMirrorManager.getMirrorDir(fetchUrl.toString()), fetchUrl, revisions);
        }
    }
}
Also used : VcsException(jetbrains.buildServer.vcs.VcsException) SSLInvestigator(jetbrains.buildServer.buildTriggers.vcs.git.agent.ssl.SSLInvestigator)

Example 23 with VcsException

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

the class CheckoutDirectoryCleaner method runGitCommand.

// protected for tests
protected void runGitCommand(@NotNull File repo, @NotNull String pathToGit, @NotNull String cmdName, int timeout, @NotNull String... params) {
    final String cmd = " '" + cmdName + "' in repo " + repo.getAbsolutePath();
    try {
        final GeneralCommandLine cl = new GeneralCommandLine();
        cl.setWorkingDirectory(repo);
        cl.setExePath(pathToGit);
        cl.addParameters(params);
        final long start = System.currentTimeMillis();
        ExecResult result = SimpleCommandLineProcessRunner.runCommand(cl, null, new SimpleCommandLineProcessRunner.ProcessRunCallback() {

            @Override
            public void onProcessStarted(@NotNull Process ps) {
                LOG.debug("Start" + cmd);
            }

            @Override
            public void onProcessFinished(@NotNull Process ps) {
                final long finish = System.currentTimeMillis();
                LOG.debug("Finish" + cmd + ", duration: " + (finish - start) + "ms");
            }

            @Override
            public Integer getOutputIdleSecondsTimeout() {
                return timeout;
            }

            @Override
            public Integer getMaxAcceptedOutputSize() {
                return COMMAND_OUTPUT_THRESHOLD;
            }

            @Override
            public boolean terminateEntireProcessTree() {
                return false;
            }
        });
        final VcsException commandError = CommandLineUtil.getCommandLineError(cmd, result);
        if (commandError != null) {
            LOG.warnAndDebugDetails("Error while running" + cmd, commandError);
        }
        if (result.getStderr().length() > 0) {
            LOG.debug("Output produced by" + cmd);
            LOG.debug(result.getStderr());
        }
    } catch (Exception e) {
        LOG.debug("Error while running" + cmd, e);
    }
}
Also used : SimpleCommandLineProcessRunner(jetbrains.buildServer.SimpleCommandLineProcessRunner) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) VcsException(jetbrains.buildServer.vcs.VcsException) VcsException(jetbrains.buildServer.vcs.VcsException) ExecResult(jetbrains.buildServer.ExecResult)

Example 24 with VcsException

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

the class AgentMirrorCleaner method getRunningBuildRepositories.

private Set<String> getRunningBuildRepositories(@NotNull DirectoryCleanersProviderContext context) {
    Set<String> repositories = new HashSet<String>();
    for (VcsRootEntry entry : context.getRunningBuild().getVcsRootEntries()) {
        VcsRoot root = entry.getVcsRoot();
        if (!Constants.VCS_NAME.equals(root.getVcsName()))
            continue;
        try {
            GitVcsRoot gitRoot = new AgentGitVcsRoot(myMirrorManager, root, myTokenStorage);
            String repositoryUrl = gitRoot.getRepositoryFetchURL().toString();
            LOG.debug("Repository " + repositoryUrl + " is used in the build, its mirror won't be cleaned");
            addRepositoryWithSubmodules(repositories, gitRoot.getRepositoryFetchURL().toString());
        } catch (VcsException e) {
            LOG.warn("Error while creating git root " + root.getName() + ". If the root has a mirror on agent, the mirror might be cleaned", e);
        }
    }
    return repositories;
}
Also used : VcsException(jetbrains.buildServer.vcs.VcsException) VcsRootEntry(jetbrains.buildServer.vcs.VcsRootEntry) GitVcsRoot(jetbrains.buildServer.buildTriggers.vcs.git.GitVcsRoot) VcsRoot(jetbrains.buildServer.vcs.VcsRoot) GitVcsRoot(jetbrains.buildServer.buildTriggers.vcs.git.GitVcsRoot)

Example 25 with VcsException

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

the class RemoteRepositoryConfigurator method configure.

/**
 * Configures and save the remote repository for specified VCS root
 * @param fetchUrl fetchUrl remote repository URL
 * @throws VcsException in case of any error
 */
public void configure(@NotNull CommonURIish fetchUrl) throws VcsException {
    File gitDir = getGitDir();
    Repository repository = null;
    try {
        repository = new RepositoryBuilder().setGitDir(gitDir).build();
        StoredConfig config = repository.getConfig();
        String scheme = fetchUrl.getScheme();
        String user = fetchUrl.getUser();
        if (myExcludeUsernameFromHttpUrls && isHttp(scheme) && !StringUtil.isEmpty(user)) {
            URIish fetchUrlNoUser = ((URIish) fetchUrl.get()).setUser(null);
            config.setString("remote", "origin", "url", fetchUrlNoUser.toString());
            config.setString("credential", null, "username", user);
        } else {
            config.setString("remote", "origin", "url", fetchUrl.toString());
            config.unset("credential", null, "username");
        }
        config.setString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*");
        config.save();
    } catch (Exception e) {
        throw new VcsException("Error while configuring remote repository at " + gitDir + ": " + e.getMessage(), e);
    } finally {
        if (repository != null)
            repository.close();
    }
}
Also used : StoredConfig(org.eclipse.jgit.lib.StoredConfig) CommonURIish(jetbrains.buildServer.buildTriggers.vcs.git.CommonURIish) URIish(org.eclipse.jgit.transport.URIish) Repository(org.eclipse.jgit.lib.Repository) RepositoryBuilder(org.eclipse.jgit.lib.RepositoryBuilder) VcsException(jetbrains.buildServer.vcs.VcsException) File(java.io.File) VcsException(jetbrains.buildServer.vcs.VcsException)

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