package com.atlassian.bitbucket.test;

import com.google.common.base.Objects;
import com.google.common.collect.ImmutableMap;

import javax.annotation.Nonnull;
import java.util.Map;

public class GitTestHelper {

    private static final Map<String, String> COMMITTER_ENV = ImmutableMap.<String, String>builder()
            .putAll(System.getenv())
            .put("GIT_COMMITTER_NAME", "Stash Func Test Runner")
            .put("GIT_COMMITTER_EMAIL", "stash-dev@atlassian.com")
            .put("GIT_AUTHOR_NAME", "Stash Func Test Runner")
            .put("GIT_AUTHOR_EMAIL", "stash-dev@atlassian.com")
            .build();

    private static String gitPath;

    private GitTestHelper() {
        throw new UnsupportedOperationException(GitTestHelper.class.getName() + " should not be instantiated");
    }

    @Nonnull
    public static Map<String, String> createCommitterEnv() {
        return COMMITTER_ENV;
    }

    /**
     * Gets the configured (or default) git executable path.
     * This is manually for the Windows builds which don't have Git on the PATH.
     */
    @SuppressWarnings("deprecation") // Use Guava < 18 API Objects class so as not to break plugin integration tests
    @Nonnull
    public static String getGitPath() {
        if (gitPath == null) {
            gitPath = Objects.firstNonNull(System.getenv("GIT_PATH"), "git");
        }
        return gitPath;
    }
}
