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

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

import javax.annotation.Nonnull;

public class DefaultGitUpdateRefStdinBuilder
        extends AbstractGitProcessBuilder<GitUpdateRefStdinBuilder>
        implements GitUpdateRefStdinBuilder {

    private boolean deref;
    private boolean nullTerminated;

    public DefaultGitUpdateRefStdinBuilder(GitProcessBuilder builder) {
        super(builder);

        deref = true; // match the git default
    }

    @Nonnull
    @Override
    public GitUpdateRefStdinBuilder deref(boolean value) {
        deref = value;
        return self();
    }

    @Nonnull
    @Override
    public GitUpdateRefStdinBuilder nullTerminated(boolean value) {
        nullTerminated = value;

        return self();
    }

    @Override
    protected void applyArguments() {
        builder.argument("--stdin");
        if (nullTerminated) {
            builder.argument("-z");
        }
        if (!deref) {
            builder.argument("--no-deref");
        }
    }

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