"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupEnvironment = exports.resolveGitExecPath = exports.resolveGitBinary = exports.resolveGitDir = exports.resolveEmbeddedGitDir = void 0;
const path = __importStar(require("path"));
const env_map_1 = require("./env-map");
function getWin32GitSubfolder() {
    if (process.arch === 'x64') {
        return 'mingw64';
    }
    else if (process.arch === 'arm64') {
        return 'clangarm64';
    }
    else {
        return 'mingw32';
    }
}
function resolveEmbeddedGitDir() {
    if (process.platform === 'darwin' ||
        process.platform === 'linux' ||
        process.platform === 'android' ||
        process.platform === 'win32') {
        const s = path.sep;
        return path
            .resolve(__dirname, '..', '..', 'git')
            .replace(/[\\\/]app.asar[\\\/]/, `${s}app.asar.unpacked${s}`);
    }
    throw new Error('Git not supported on platform: ' + process.platform);
}
exports.resolveEmbeddedGitDir = resolveEmbeddedGitDir;
/**
 *  Find the path to the embedded Git environment.
 *
 *  If a custom Git directory path is defined as the `LOCAL_GIT_DIRECTORY` environment variable, then
 *  returns with it after resolving it as a path.
 */
function resolveGitDir(localGitDir = process.env.LOCAL_GIT_DIRECTORY) {
    return localGitDir ? path.resolve(localGitDir) : resolveEmbeddedGitDir();
}
exports.resolveGitDir = resolveGitDir;
/**
 *  Find the path to the embedded Git binary.
 */
function resolveGitBinary(localGitDir = process.env.LOCAL_GIT_DIRECTORY) {
    const gitDir = resolveGitDir(localGitDir);
    if (process.platform === 'win32') {
        return path.join(gitDir, 'cmd', 'git.exe');
    }
    else {
        return path.join(gitDir, 'bin', 'git');
    }
}
exports.resolveGitBinary = resolveGitBinary;
/**
 * Find the path to the embedded git exec path.
 *
 * If a custom git exec path is given as the `GIT_EXEC_PATH` environment variable,
 * then it returns with it after resolving it as a path.
 */
function resolveGitExecPath(localGitDir = process.env.LOCAL_GIT_DIRECTORY, gitExecPath = process.env.GIT_EXEC_PATH) {
    if (gitExecPath) {
        return path.resolve(gitExecPath);
    }
    const gitDir = resolveGitDir(localGitDir);
    if (process.platform === 'win32') {
        return path.join(gitDir, getWin32GitSubfolder(), 'libexec', 'git-core');
    }
    else {
        return path.join(gitDir, 'libexec', 'git-core');
    }
}
exports.resolveGitExecPath = resolveGitExecPath;
/**
 * Setup the process environment before invoking Git.
 *
 * This method resolves the Git executable and creates the array of key-value
 * pairs which should be used as environment variables.
 *
 * @param additional options to include with the process
 */
function setupEnvironment(environmentVariables, processEnv = process.env) {
    const env = new env_map_1.EnvMap([
        ...Object.entries(processEnv),
        ...Object.entries(environmentVariables),
    ]);
    const localGitDir = env.get('LOCAL_GIT_DIRECTORY');
    const gitLocation = resolveGitBinary(localGitDir);
    const gitDir = resolveGitDir(localGitDir);
    if (process.platform === 'win32') {
        env.set('PATH', `${gitDir}\\${getWin32GitSubfolder()}\\bin;${gitDir}\\${getWin32GitSubfolder()}\\usr\\bin;${env.get('PATH') ?? ''}`);
    }
    env.set('GIT_EXEC_PATH', resolveGitExecPath(localGitDir, env.get('GIT_EXEC_PATH')));
    // On Windows the contained Git environment (minGit) ships with a system level
    // gitconfig that we can control but on macOS and Linux /etc/gitconfig is used\
    // as the system-wide configuration file and we're unable to modify it.
    //
    // So in order to be able to provide our own sane defaults that can be overriden
    // by the user's global and local configuration we'll tell Git to use
    // dugite-native's custom gitconfig on those platforms.
    if (process.platform !== 'win32' && !env.get('GIT_CONFIG_SYSTEM')) {
        env.set('GIT_CONFIG_SYSTEM', path.join(gitDir, 'etc', 'gitconfig'));
    }
    if (process.platform === 'darwin' || process.platform === 'linux') {
        // templates are used to populate your .git folder
        // when a repository is initialized locally
        const templateDir = `${gitDir}/share/git-core/templates`;
        env.set('GIT_TEMPLATE_DIR', templateDir);
    }
    if (process.platform === 'linux') {
        // when building Git for Linux and then running it from
        // an arbitrary location, you should set PREFIX for the
        // process to ensure that it knows how to resolve things
        env.set('PREFIX', gitDir);
        if (!env.get('GIT_SSL_CAINFO') && !env.get('LOCAL_GIT_DIRECTORY')) {
            // use the SSL certificate bundle included in the distribution only
            // when using embedded Git and not providing your own bundle
            const distDir = resolveEmbeddedGitDir();
            const sslCABundle = `${distDir}/ssl/cacert.pem`;
            env.set('GIT_SSL_CAINFO', sslCABundle);
        }
    }
    return { env: Object.fromEntries(env.entries()), gitLocation };
}
exports.setupEnvironment = setupEnvironment;
//# sourceMappingURL=git-environment.js.map