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

import com.atlassian.bitbucket.repository.Repository;
import com.atlassian.bitbucket.scm.git.command.GitCommandBuilderFactory;
import com.atlassian.bitbucket.scm.git.command.config.GitConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

/**
 * @since 2.9
 */
public abstract class AbstractConfigurationUpgradeTask implements SynchronousUpgradeTask {

    protected final GitCommandBuilderFactory builderFactory;
    protected final Logger log;

    protected AbstractConfigurationUpgradeTask(GitCommandBuilderFactory builderFactory) {
        this.builderFactory = builderFactory;

        log = LoggerFactory.getLogger(getClass());
    }

    protected void configure(Repository repository, Map<String, String> configuration) {
        GitConfig builder = builderFactory.builder(repository).config();
        for (Map.Entry<String, String> entry : configuration.entrySet()) {
            log.trace("{}: Configuring {} -> {}", repository, entry.getKey(), entry.getValue());
            builder.set(entry.getKey(), entry.getValue())
                    .build()
                    .call();
        }
    }
}
