package com.atlassian.bitbucket.mesh.transaction;

import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilder;
import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilderConfigurer;

import javax.annotation.Nonnull;

/**
 * Configures the directory with incoming pack(s) as an alternate if a transaction is active <em>AND</em>
 * a pack was staged for the transaction.
 */
public class TransactionGitProcessConfigurer implements GitProcessBuilderConfigurer {

    private final TransactionManager transactionManager;

    public TransactionGitProcessConfigurer(TransactionManager transactionManager) {
        this.transactionManager = transactionManager;
    }

    @Override
    public void configure(@Nonnull GitProcessBuilder builder) {
        transactionManager.getCurrentTransaction()
                .flatMap(Transaction::getIncomingDir)
                .ifPresent(builder::alternate);
    }
}
