UNPKG

git-autoshelve

Version:

TFS-like autoshelving for GIT repository

76 lines 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.settingProvider = void 0; const Setting_1 = require("@this/src/Setting"); const path = require("path"); const fs = require("fs"); const hidefile = require("hidefile"); const simpleGitPromise = require("simple-git/promise"); exports.settingProvider = { getSetting }; function getRepositoryBasePath() { const repoPath = process.argv[2]; if (repoPath == undefined) { throw new Error("You must specify base repository path as program argument."); } if (path.isAbsolute(repoPath)) { return repoPath; } return path.resolve(process.cwd(), repoPath); } async function getRemote(originGit, remoteName) { const remotes = await originGit.getRemotes(true); let serverRemote = undefined; if (remotes.length == 1) { serverRemote = remotes[0].refs.push; } else { serverRemote = remotes.filter(remote => remote.name == "origin" || (remoteName && remote.name == remoteName))[0]?.refs.push; } if (!serverRemote) { throw new Error("No remote repository found."); } return serverRemote; } async function initAutoshelve(getGit, originGit, autoshelveRepositoryPath, repositoryBasePath, remoteName) { fs.mkdirSync(autoshelveRepositoryPath); hidefile.hideSync(autoshelveRepositoryPath); const git = getGit(); await git.init(); await git.addRemote("origin", repositoryBasePath); const serverRemote = await getRemote(originGit, remoteName); await git.addRemote("server", serverRemote); } function getAutoshelveRepositoryPath(repositoryBasePath) { return path.resolve(repositoryBasePath, "..", `.${path.basename(repositoryBasePath)}_autoshelved`); } function ensureAutoshelveRepo(getGit, originGit, autoshelveRepositoryPath, repositoryBasePath) { return new Promise((resolve, reject) => { try { hidefile.isHidden(autoshelveRepositoryPath, async (error, result) => { if (error) { await initAutoshelve(getGit, originGit, autoshelveRepositoryPath, repositoryBasePath, process.argv[3]); } resolve(autoshelveRepositoryPath); }); } catch (e) { reject(e); } }); } async function getSetting() { const repositoryBasePath = getRepositoryBasePath(); const autoshelveRepositoryPath = getAutoshelveRepositoryPath(repositoryBasePath); const getGit = () => simpleGitPromise(autoshelveRepositoryPath); const originGit = simpleGitPromise(repositoryBasePath); await ensureAutoshelveRepo(getGit, originGit, autoshelveRepositoryPath, repositoryBasePath); return new Setting_1.Setting({ repositoryBasePath, autoshelveRepositoryPath, originGit, git: getGit() }); } //# sourceMappingURL=SettingProvider.js.map