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

import com.atlassian.bitbucket.mesh.git.exception.InvalidFollowPathspecException;
import com.atlassian.bitbucket.mesh.process.ProcessSummary;

import javax.annotation.Nonnull;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

public class LogCompletionHandler extends RevCoreCompletionHandler {

    private static final String FOLLOW_ONE_PATH = "fatal: --follow requires exactly one pathspec";

    public LogCompletionHandler(boolean ignoreMissing) {
        this(Collections.emptySet(), Collections.emptySet(), ignoreMissing);
    }

    public LogCompletionHandler(@Nonnull Collection<String> revs, boolean ignoreMissing) {
        super(revs, ignoreMissing);
    }

    public LogCompletionHandler(@Nonnull Set<String> includes, @Nonnull Set<String> excludes,
                                boolean ignoreMissing) {
        super(includes, excludes, ignoreMissing);
    }

    @Override
    protected void evaluateStderr(@Nonnull ProcessSummary summary, @Nonnull String stderr) {
        boolean hasStdErr = isNotBlank(summary.getStderr());

        if (hasStdErr && summary.getStderr().startsWith(FOLLOW_ONE_PATH)) {
            throw new InvalidFollowPathspecException();
        }

        super.evaluateStderr(summary, stderr);
    }
}
