in git-server/src/main/java/jetbrains/buildServer/buildTriggers/vcs/git/command/NativeGitCommands.java [437:511]
public String tag(@NotNull OperationContext context, @NotNull String tag, @Nullable String message, @NotNull String commit) throws VcsException {
final Context ctx = new ContextImpl(context.getGitRoot(), myConfig, myGitDetector.detectGit(), myKnownHostsManager);
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(message != null ? message : "").call();
final String debugInfo = LogUtil.describe(gitRoot);
List<Ref> currentTags;
try {
currentTags = executeCommand(ctx, "ls-remote", debugInfo, () -> gitFacade.lsRemote()
.setTags()
.setBranches(tag)
.setAuthSettings(gitRoot.getAuthSettings()).setUseNativeSsh(true)
.setTimeout(myConfig.getPushTimeoutSeconds())
.setRetryAttempts(myConfig.getConnectionRetryAttempts())
.setRepoUrl(gitRoot.getRepositoryPushURL().get())
.trace(myConfig.getGitTraceEnv())
.call(), gitFacade);
} catch (VcsException v) {
Loggers.VCS.warn("Failed to get information about remote tag: " + tag + " for " + debugInfo, v);
throw v;
}
if (!currentTags.isEmpty()) {
try {
executeCommand(ctx, "push", debugInfo, () -> {
gitFacade.push()
.setRemote(gitRoot.getRepositoryPushURL().toString())
.setRefspec(":" + tag)
.setAuthSettings(gitRoot.getAuthSettings()).setUseNativeSsh(true)
.setTimeout(myConfig.getPushTimeoutSeconds())
.setRetryAttempts(myConfig.getConnectionRetryAttempts())
.setRepoUrl(gitRoot.getRepositoryPushURL().get())
.trace(myConfig.getGitTraceEnv())
.call();
Loggers.VCS.info("Tag '" + tag + "' was successfully removed from " + debugInfo);
return tag;
}, gitFacade);
} catch (VcsException v) {
Loggers.VCS.warn("Failed to remove remote tag: " + tag + " for " + debugInfo, v);
throw v;
}
}
try {
return executeCommand(ctx, "push", debugInfo, () -> {
gitFacade.push()
.setRemote(gitRoot.getRepositoryPushURL().toString())
.setRefspec(tag)
.setAuthSettings(gitRoot.getAuthSettings()).setUseNativeSsh(true)
.setTimeout(myConfig.getPushTimeoutSeconds())
.setRetryAttempts(myConfig.getConnectionRetryAttempts())
.setRepoUrl(gitRoot.getRepositoryPushURL().get())
.trace(myConfig.getGitTraceEnv())
.call();
Loggers.VCS.info("Tag '" + tag + "' was successfully pushed for " + debugInfo);
return tag;
}, gitFacade);
} 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;
}
}