Search in sources :

Example 1 with GitFacadeImpl

use of jetbrains.buildServer.buildTriggers.vcs.git.command.impl.GitFacadeImpl 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 2 with GitFacadeImpl

use of jetbrains.buildServer.buildTriggers.vcs.git.command.impl.GitFacadeImpl 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 3 with GitFacadeImpl

use of jetbrains.buildServer.buildTriggers.vcs.git.command.impl.GitFacadeImpl 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 4 with GitFacadeImpl

use of jetbrains.buildServer.buildTriggers.vcs.git.command.impl.GitFacadeImpl in project teamcity-git by JetBrains.

the class NativeGitCommands method lsRemote.

@NotNull
@Override
public Map<String, Ref> lsRemote(@NotNull Repository db, @NotNull GitVcsRoot gitRoot, @NotNull FetchSettings settings) throws VcsException {
    final Context ctx = new ContextImpl(gitRoot, myConfig, myGitDetector.detectGit(), settings.getProgress());
    final GitFacadeImpl gitFacade = new GitFacadeImpl(db.getDirectory(), ctx);
    gitFacade.setSshKeyManager(mySshKeyManager);
    final jetbrains.buildServer.buildTriggers.vcs.git.command.LsRemoteCommand lsRemote = gitFacade.lsRemote().peelRefs().setAuthSettings(gitRoot.getAuthSettings()).setUseNativeSsh(true).setTimeout(myConfig.getRepositoryStateTimeoutSeconds()).setRetryAttempts(myConfig.getConnectionRetryAttempts()).trace(myConfig.getGitTraceEnv());
    return executeCommand(ctx, "ls-remote", LogUtil.describe(gitRoot), () -> {
        return lsRemote.call().stream().collect(Collectors.toMap(Ref::getName, ref -> ref));
    });
}
Also used : CommitResult(jetbrains.buildServer.vcs.CommitResult) jetbrains.buildServer.buildTriggers.vcs.git(jetbrains.buildServer.buildTriggers.vcs.git) Loggers(jetbrains.buildServer.log.Loggers) RefSpec(org.eclipse.jgit.transport.RefSpec) Collection(java.util.Collection) TagCommand(jetbrains.buildServer.buildTriggers.vcs.git.TagCommand) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) LsRemoteCommand(jetbrains.buildServer.buildTriggers.vcs.git.LsRemoteCommand) GitFacadeImpl(jetbrains.buildServer.buildTriggers.vcs.git.command.impl.GitFacadeImpl) PersonIdent(org.eclipse.jgit.lib.PersonIdent) FetchCommand(jetbrains.buildServer.buildTriggers.vcs.git.FetchCommand) NamedThreadFactory(jetbrains.buildServer.util.NamedThreadFactory) Map(java.util.Map) Ref(org.eclipse.jgit.lib.Ref) URIish(org.eclipse.jgit.transport.URIish) VcsException(jetbrains.buildServer.vcs.VcsException) VcsRootSshKeyManager(jetbrains.buildServer.ssh.VcsRootSshKeyManager) Logger(com.intellij.openapi.diagnostic.Logger) PushCommand(jetbrains.buildServer.buildTriggers.vcs.git.PushCommand) NotNull(org.jetbrains.annotations.NotNull) FuncThrow(jetbrains.buildServer.util.FuncThrow) Repository(org.eclipse.jgit.lib.Repository) GitFacadeImpl(jetbrains.buildServer.buildTriggers.vcs.git.command.impl.GitFacadeImpl) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

GitFacadeImpl (jetbrains.buildServer.buildTriggers.vcs.git.command.impl.GitFacadeImpl)4 VcsException (jetbrains.buildServer.vcs.VcsException)4 NotNull (org.jetbrains.annotations.NotNull)3 HashSet (java.util.HashSet)2 PersonIdent (org.eclipse.jgit.lib.PersonIdent)2 Repository (org.eclipse.jgit.lib.Repository)2 RefSpec (org.eclipse.jgit.transport.RefSpec)2 Logger (com.intellij.openapi.diagnostic.Logger)1 IOException (java.io.IOException)1 Collection (java.util.Collection)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 jetbrains.buildServer.buildTriggers.vcs.git (jetbrains.buildServer.buildTriggers.vcs.git)1 FetchCommand (jetbrains.buildServer.buildTriggers.vcs.git.FetchCommand)1 LsRemoteCommand (jetbrains.buildServer.buildTriggers.vcs.git.LsRemoteCommand)1 PushCommand (jetbrains.buildServer.buildTriggers.vcs.git.PushCommand)1 TagCommand (jetbrains.buildServer.buildTriggers.vcs.git.TagCommand)1 Loggers (jetbrains.buildServer.log.Loggers)1 VcsRootSshKeyManager (jetbrains.buildServer.ssh.VcsRootSshKeyManager)1 FuncThrow (jetbrains.buildServer.util.FuncThrow)1