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

import com.atlassian.bitbucket.ServerException;
import com.atlassian.bitbucket.i18n.I18nService;
import com.atlassian.bitbucket.i18n.KeyedMessage;
import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.git.command.GitCommandExitHandler;
import com.atlassian.bitbucket.scm.git.command.merge.GitNoCommonAncestorException;

/**
 * Handles the situation where there is no common ancestor between multiple commits.
 * <p>
 * {@code git merge-base} exits non-zero when there is no common ancestor between the commits provided. This
 * handler replaces the {@link ServerException ServerException} produced by the
 * base class with a {@link GitNoCommonAncestorException} that has a more detailed message.
 *
 * @since 3.0
 */
public abstract class AbstractMergeBaseExitHandler extends GitCommandExitHandler {

    protected AbstractMergeBaseExitHandler(I18nService i18nService, Repository repository) {
        super(i18nService, repository);
    }

    @Override
    protected boolean isNonZeroExitCodeException(Throwable thrown) {
        if (super.isNonZeroExitCodeException(thrown)) {
            throw new GitNoCommonAncestorException(noCommonAncestorMessage());
        }

        return false;
    }

    protected abstract KeyedMessage noCommonAncestorMessage();
}
