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

import com.atlassian.bitbucket.mesh.git.commit.Commit;
import com.atlassian.bitbucket.mesh.process.handler.AbstractLineStdioHandler;

import javax.annotation.Nonnull;

/**
 * Serves as communicator with {@link CommitPump} that is parsing commit out from git output line by line.
 * Due to line-by-line nature of {@link CommitPump} there is no way to tell when a commit will be detected in the
 * output, when commit will end and commit message will start and so on.
 *
 * {@code CommitSink} serves an intermediary between {@link CommitPump} and a client that uses {@link CommitPump}
 * to read a commit. {@link CommitPump} will call into {@code receiveCommit} whenever a commit
 * is detected so the client will receive it.
 *
 * Additionally, to prevent reading full lines from git output when it is unnecessary {@code CommitSink} provides a
 * way for {@link CommitPump} to indicate that the following line is going to be truncated. That allows any handler
 * that is a {@link AbstractLineStdioHandler} to configure truncation
 * settings accordingly.
 */
public interface CommitSink {

    /**
     * Whenever {@link CommitPump} detects a commit it'll call to this method.
     *
     * @param commit a {@link Commit} that has been parsed from git output
     */
    void onCommit(@Nonnull Commit commit);

    /**
     * Whenever {@link CommitPump} expects that the <i>next</i> line will be truncated anyway, it'll notify the client
     * using this method.
     *
     * @param maxLength the expected length of the next line
     */
    default void onPartialMessage(int maxLength) {
    }
}