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

import com.atlassian.bitbucket.mesh.git.GitProcessCompletionHandler;
import com.atlassian.bitbucket.mesh.git.exception.ObjectNotFoundException;
import com.atlassian.bitbucket.mesh.process.ProcessSummary;

import javax.annotation.Nonnull;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DeleteTagCompletionHandler extends GitProcessCompletionHandler {

    private static final Pattern PATTERN_TAG_NOT_FOUND =
            Pattern.compile("error: tag '(.+)' not found\\.$", Pattern.MULTILINE);

    @Override
    protected void evaluateStderr(@Nonnull ProcessSummary summary, @Nonnull String stderr) {
        super.evaluateStderr(summary, stderr);

        Matcher matcher = PATTERN_TAG_NOT_FOUND.matcher(stderr);
        if (matcher.find()) {
            String name = matcher.group(1);
            throw new ObjectNotFoundException(name);
        }
    }
}