package com.atlassian.stash.internal.scm.git.command.tag;

import com.atlassian.bitbucket.repository.Ref;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.repository.Tag;
import com.atlassian.bitbucket.scm.CommandOutputHandler;
import com.atlassian.bitbucket.scm.git.command.GitCommandFactory;
import com.atlassian.bitbucket.scm.git.command.AbstractLoggingCommandOutputHandler;

/**
 * A {@link CommandOutputHandler} which produces a {@link Tag}.
 *
 * @since 3.6
 */
public class CreateTagOutputHandler extends AbstractLoggingCommandOutputHandler<Tag> {

    private final GitCommandFactory commandFactory;
    private final String name;
    private final Repository repository;

    public CreateTagOutputHandler(GitCommandFactory commandFactory, String name, Repository repository) {
        this.commandFactory = commandFactory;
        this.name = name;
        this.repository = repository;
    }

    @Override
    public Tag getOutput() {
        Ref ref = commandFactory.resolveRef(repository, name).call();

        return ref instanceof Tag ? (Tag) ref : null;
    }
}
