Search in sources :

Example 16 with VcsException

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

the class GitPatchBuilderDispatcher method buildPatchInSeparateProcess.

private void buildPatchInSeparateProcess() throws Exception {
    final String rootStr = LogUtil.describe(myGitRoot);
    final ProcessXmxProvider xmxProvider = new ProcessXmxProvider(new RepositoryXmxStorage(myContext.getRepository(), "patch"), myConfig, "patch", "(root: " + rootStr + ")");
    Integer xmx = xmxProvider.getNextXmx();
    while (xmx != null) {
        final GeneralCommandLine patchCmd = createPatchCommandLine(xmx);
        final File patchFile = FileUtil.createTempFile("git", "patch");
        final File internalProperties = getPatchPropertiesFile();
        try {
            final ByteArrayOutputStream stdout = new LineAwareByteArrayOutputStream(Charset.forName("UTF-8"), new NoOpLineListener(), false);
            final ByteArrayOutputStream stderr = new ByteArrayOutputStream();
            final GitProcessExecutor.GitExecResult gitResult = new GitProcessExecutor(patchCmd).runProcess(getInput(patchFile, internalProperties), myConfig.getPatchProcessIdleTimeoutSeconds(), stdout, stderr, new GitProcessExecutor.ProcessExecutorAdapter());
            VcsException patchError = CommandLineUtil.getCommandLineError("build patch", gitResult.getExecResult());
            if (patchError != null) {
                if (gitResult.isOutOfMemoryError()) {
                    final Integer nextXmx = xmxProvider.getNextXmx();
                    if (nextXmx != null) {
                        xmx = nextXmx;
                        FileUtil.delete(patchFile);
                        continue;
                    }
                    throw new VcsException("There is not enough memory for git patch (last attempted -Xmx" + xmx + "M). Please contact your system administrator", patchError);
                } else if (gitResult.isTimeout()) {
                    throw new VcsException("git patch for root " + rootStr + " was idle for more than " + myConfig.getFetchTimeout() + " second(s), try increasing the timeout using the " + PluginConfigImpl.TEAMCITY_GIT_IDLE_TIMEOUT_SECONDS + " property", patchError);
                }
                throw patchError;
            }
            new LowLevelPatcher(new FileInputStream(patchFile)).applyPatch(new NoExitLowLevelPatchTranslator(((PatchBuilderEx) myBuilder).getLowLevelBuilder()));
            break;
        } finally {
            FileUtil.delete(patchFile);
            FileUtil.delete(internalProperties);
        }
    }
}
Also used : RepositoryXmxStorage(jetbrains.buildServer.buildTriggers.vcs.git.process.RepositoryXmxStorage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LineAwareByteArrayOutputStream(jetbrains.buildServer.LineAwareByteArrayOutputStream) FileInputStream(java.io.FileInputStream) GeneralCommandLine(com.intellij.execution.configurations.GeneralCommandLine) LineAwareByteArrayOutputStream(jetbrains.buildServer.LineAwareByteArrayOutputStream) VcsException(jetbrains.buildServer.vcs.VcsException) GitProcessExecutor(jetbrains.buildServer.buildTriggers.vcs.git.process.GitProcessExecutor) File(java.io.File)

Example 17 with VcsException

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

the class NativeGitCommands method push.

@NotNull
@Override
public CommitResult push(@NotNull Repository db, @NotNull GitVcsRoot gitRoot, @NotNull String ref, @NotNull String commit, @NotNull String lastCommit) throws VcsException {
    final String fullRef = GitUtils.expandRef(ref);
    final Context ctx = new ContextImpl(gitRoot, myConfig, myGitDetector.detectGit());
    final GitFacadeImpl gitFacade = new GitFacadeImpl(db.getDirectory(), ctx);
    gitFacade.setSshKeyManager(mySshKeyManager);
    gitFacade.updateRef().setRef(fullRef).setRevision(commit).setOldValue(lastCommit).call();
    final String debugInfo = LogUtil.describe(gitRoot);
    try {
        return executeCommand(ctx, "push", debugInfo, () -> {
            gitFacade.push().setRemote(gitRoot.getRepositoryPushURL().toString()).setRefspec(fullRef).setAuthSettings(gitRoot.getAuthSettings()).setUseNativeSsh(true).setTimeout(myConfig.getPushTimeoutSeconds()).trace(myConfig.getGitTraceEnv()).call();
            return CommitResult.createSuccessResult(commit);
        });
    } catch (VcsException e) {
        // restore local ref
        try {
            gitFacade.updateRef().setRef(fullRef).setRevision(lastCommit).setOldValue(commit).call();
        } catch (VcsException v) {
            Loggers.VCS.warn("Failed to restore initial revision " + lastCommit + " of " + fullRef + " after unssuccessful push of revision " + commit + " for " + debugInfo, v);
        }
        throw e;
    }
}
Also used : GitFacadeImpl(jetbrains.buildServer.buildTriggers.vcs.git.command.impl.GitFacadeImpl) VcsException(jetbrains.buildServer.vcs.VcsException) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with VcsException

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

the class NativeGitCommands method fetch.

@Override
public void fetch(@NotNull Repository db, @NotNull URIish fetchURI, @NotNull FetchSettings settings) throws IOException, VcsException {
    final GitExec gitExec = myGitDetector.detectGit();
    final Context ctx = new ContextImpl(null, myConfig, gitExec, settings.getProgress());
    final GitFacadeImpl gitFacade = new GitFacadeImpl(db.getDirectory(), ctx);
    gitFacade.setSshKeyManager(mySshKeyManager);
    // otherwise git fails to update local branches which were e.g. renamed.
    try {
        gitFacade.remote().setCommand("prune").setRemote("origin").setAuthSettings(settings.getAuthSettings()).setUseNativeSsh(true).trace(myConfig.getGitTraceEnv()).call();
    } catch (VcsException e) {
        Loggers.VCS.warnAndDebugDetails("Error while pruning removed branches in " + db, e);
    }
    final jetbrains.buildServer.buildTriggers.vcs.git.command.FetchCommand fetch = gitFacade.fetch().setRemote(fetchURI.toString()).setFetchTags(false).setAuthSettings(settings.getAuthSettings()).setUseNativeSsh(true).setTimeout(myConfig.getFetchTimeout()).setRetryAttempts(myConfig.getConnectionRetryAttempts()).trace(myConfig.getGitTraceEnv()).addPreAction(() -> GitServerUtil.removeRefLocks(db.getDirectory()));
    final Collection<String> resultRefSpecs = new HashSet<>();
    switch(settings.getFetchMode()) {
        case FETCH_ALL_REFS:
            fetch.setRefspec(ALL_REF_SPEC);
            resultRefSpecs.add(ALL_REF_SPEC);
            break;
        case FETCH_ALL_REFS_EXCEPT_TAGS:
            fetch.setRefspec(ALL_REF_SPEC);
            resultRefSpecs.add(ALL_REF_SPEC);
            fetch.setRefspec(EXCLUDE_TAGS_REF_SPEC);
            resultRefSpecs.add(EXCLUDE_TAGS_REF_SPEC);
            break;
        case FETCH_REF_SPECS:
        default:
            for (RefSpec refSpec : settings.getRefSpecs()) {
                final String refSpecStr = refSpec.toString();
                fetch.setRefspec(refSpecStr);
                resultRefSpecs.add(refSpecStr);
            }
            break;
    }
    if (isSilentFetch(ctx))
        fetch.setQuite(true);
    else
        fetch.setShowProgress(true);
    executeCommand(ctx, "fetch", getDebugInfo(db, fetchURI, resultRefSpecs), () -> {
        fetch.call();
        return true;
    });
}
Also used : GitFacadeImpl(jetbrains.buildServer.buildTriggers.vcs.git.command.impl.GitFacadeImpl) RefSpec(org.eclipse.jgit.transport.RefSpec) VcsException(jetbrains.buildServer.vcs.VcsException) HashSet(java.util.HashSet)

Example 19 with VcsException

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

the class NativeGitCommands method tag.

@Override
@NotNull
public String tag(@NotNull OperationContext context, @NotNull String tag, @NotNull String commit) throws VcsException {
    final Context ctx = new ContextImpl(context.getGitRoot(), myConfig, myGitDetector.detectGit());
    final Repository db = context.getRepository();
    final GitFacadeImpl gitFacade = new GitFacadeImpl(db.getDirectory(), ctx);
    gitFacade.setSshKeyManager(mySshKeyManager);
    final GitVcsRoot gitRoot = context.getGitRoot();
    final PersonIdent tagger = PersonIdentFactory.getTagger(gitRoot, db);
    gitFacade.tag().setName(tag).setCommit(commit).force(true).annotate(true).setTagger(tagger.getName(), tagger.getEmailAddress()).setMessage("automatically created by TeamCity VCS labeling build feature").call();
    final String debugInfo = LogUtil.describe(gitRoot);
    try {
        return executeCommand(ctx, "push", debugInfo, () -> {
            gitFacade.push().setRemote(gitRoot.getRepositoryPushURL().toString()).setRefspec(tag).setAuthSettings(gitRoot.getAuthSettings()).setUseNativeSsh(true).setTimeout(myConfig.getPushTimeoutSeconds()).trace(myConfig.getGitTraceEnv()).call();
            Loggers.VCS.info("Tag '" + tag + "' was successfully pushed for " + debugInfo);
            return tag;
        });
    } catch (VcsException e) {
        // remove local tag
        try {
            gitFacade.tag().delete(true).setName(tag).call();
        } catch (VcsException v) {
            Loggers.VCS.warn("Failed to delete local tag " + tag + " of " + commit + " after unssuccessful push for " + debugInfo, v);
        }
        throw e;
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) GitFacadeImpl(jetbrains.buildServer.buildTriggers.vcs.git.command.impl.GitFacadeImpl) PersonIdent(org.eclipse.jgit.lib.PersonIdent) VcsException(jetbrains.buildServer.vcs.VcsException) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with VcsException

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

the class TransportFactoryImpl method createTransport.

public Transport createTransport(@NotNull final Repository r, @NotNull final URIish url, @NotNull final AuthSettings authSettings, final int timeoutSeconds) throws NotSupportedException, VcsException {
    try {
        checkUrl(url);
        URIish preparedURI = prepareURI(url);
        final Transport t = Transport.open(r, preparedURI);
        t.setCredentialsProvider(new AuthCredentialsProvider(authSettings));
        if (t instanceof SshTransport) {
            SshTransport ssh = (SshTransport) t;
            ssh.setSshSessionFactory(getSshSessionFactory(url, authSettings));
        }
        t.setTimeout(timeoutSeconds);
        return t;
    } catch (TransportException e) {
        throw new VcsException("Cannot create transport: " + e.getMessage(), e);
    }
}
Also used : VcsException(jetbrains.buildServer.vcs.VcsException) TransportException(org.eclipse.jgit.errors.TransportException)

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