package com.atlassian.bitbucket.mesh.git.builder.packrefs;

import com.atlassian.bitbucket.mesh.git.GitConstants;
import com.atlassian.bitbucket.mesh.git.builder.AbstractGitProcessBuilder;
import com.atlassian.bitbucket.mesh.git.builder.GitProcessBuilder;

import javax.annotation.Nonnull;

public class DefaultGitPackRefsBuilder
        extends AbstractGitProcessBuilder<GitPackRefsBuilder>
        implements GitPackRefsBuilder {

    private boolean all;
    private boolean prune;

    public DefaultGitPackRefsBuilder(GitProcessBuilder builder) {
        //Don't invoke reference-transaction hooks when packing refs
        super(builder.command("pack-refs").withConfiguration(GitConstants.CONFIG_CORE_HOOKS_PATH, ""));

        prune = true; // git pack-refs prunes by default, so this matches that behavior
    }

    @Nonnull
    @Override
    public GitPackRefsBuilder all(boolean value) {
        all = value;

        return self();
    }

    @Nonnull
    @Override
    public GitPackRefsBuilder prune(boolean value) {
        prune = value;

        return self();
    }

    @Override
    protected void applyArguments() {
        if (all) {
            builder.argument("--all");
        }
        builder.argument(prune ? "--prune" : "--no-prune");
    }

    @Nonnull
    @Override
    protected GitPackRefsBuilder self() {
        return this;
    }
}
