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

import com.atlassian.bitbucket.scm.ScmRequest;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.AbstractRepositoryScmRequest;
import com.atlassian.bitbucket.scm.git.command.GitCommandBuilderFactory;
import com.atlassian.bitbucket.scm.git.command.GitScmCommandBuilder;
import com.atlassian.bitbucket.scm.git.GitScmConfig;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static com.google.common.base.Preconditions.checkNotNull;

/**
 * Common base class for building {@link ScmRequest SCM request} handlers for git's various
 * supported protocols.
 *
 * @since 2.4
 */
public abstract class AbstractGitRepositoryScmRequest extends AbstractRepositoryScmRequest {

    protected final GitScmConfig config;

    private final GitCommandBuilderFactory builderFactory;

    protected AbstractGitRepositoryScmRequest(@Nonnull Repository repository, boolean write,
                                              @Nonnull GitCommandBuilderFactory builderFactory,
                                              @Nonnull GitScmConfig config) {
        super(repository, write);

        this.builderFactory = checkNotNull(builderFactory, "builderFactory");
        this.config = checkNotNull(config, "config");
    }

    /**
     * Creates a {@link GitScmCommandBuilder builder} for executing commands against the {@link Repository repository}
     * provided when the request was constructed.
     *
     * @return a new command builder
     */
    protected GitScmCommandBuilder builder() {
        return builderFactory.builder(getRepository());
    }
}
