package com.atlassian.bitbucket.mesh.git.exception;

import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilder;
import com.atlassian.bitbucket.mesh.git.ref.Branch;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcGitError;
import com.atlassian.bitbucket.mesh.rpc.v1.RpcRevertConflicted;

import javax.annotation.Nonnull;

import static java.util.Objects.requireNonNull;

/**
 * An exception that is raised when the {@link GitProcessBuilder#revert() revert} operation fails due to file conflicts.
 *
 * @since 2.1
 */
public class RevertConflictedException extends RevertException {

    private final Branch sourceBranch;

    public RevertConflictedException(@Nonnull String commitId, @Nonnull Branch sourceBranch) {
        super("The revert could not be completed due to file conflicts", requireNonNull(commitId, "commitId"));

        this.sourceBranch = requireNonNull(sourceBranch, "sourceBranch");
    }

    @Nonnull
    @Override
    public RpcGitError toError() {
        return RpcGitError.newBuilder()
                .setRevertConflicted(RpcRevertConflicted.newBuilder()
                        .setCommitId(commitId)
                        .setSourceBranchCommitId(sourceBranch.getLatestCommit())
                        .build())
                .build();
    }
}
