UNPKG

git-autoshelve

Version:

TFS-like autoshelving for GIT repository

67 lines 2.82 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); require("module-alias/register"); const SettingProvider_1 = require("@this/src/SettingProvider"); const os = require("os"); const fs = require("fs"); const path = require("path"); const simple_git_1 = require("simple-git"); class Application { async run() { this.setting = await SettingProvider_1.settingProvider.getSetting(); const branch = await this.getCurrentBranch(this.setting.originGit, true); const autoshelveBranchName = `autoshelve/${os.userInfo().username}/${branch.name}`; await this.resetToOrigin(branch, autoshelveBranchName); await this.modifyAutoshelveBranch(); await this.setting.git.add("./*"); await this.setting.git.commit(new Date().toISOString()); await this.setting.git.push("server", autoshelveBranchName); } async getCurrentBranch(git, throwIfNotExists = false) { const branches = await git.branchLocal(); if (!branches) { if (throwIfNotExists) { throw new Error("No branch found!"); } return undefined; } return Object.entries(branches.branches).filter(([_, branch]) => branch.current)[0]?.[1]; } async resetToOrigin(branch, autoshelveBranchName) { await this.setting.git.fetch(["origin"]); const autoshelveBranch = await this.getCurrentBranch(this.setting.git); if (!autoshelveBranch) { console.log("Checkouting origin branch for first time"); await this.setting.git.checkout(["-B", autoshelveBranchName, `origin/${branch.name}`]); } else { await this.setting.git.checkout([autoshelveBranchName]); await this.setting.git.clean(simple_git_1.CleanOptions.FORCE + simple_git_1.CleanOptions.RECURSIVE); await this.setting.git.revert(autoshelveBranch.commit); } } async modifyAutoshelveBranch() { const status = await this.setting.originGit.status(); for (let file of status.files) { const targetFilePath = path.join(this.setting.autoshelveRepositoryPath, file.path); if (file.working_dir == "D") { fs.unlinkSync(targetFilePath); console.log("Deleting file ", file.path); } else { fs.copyFileSync(path.join(this.setting.repositoryBasePath, file.path), targetFilePath); console.log("Modding file ", file.path); } } } } (async () => { try { await new Application().run(); } catch (e) { console.error(e.message, e.stack); process.exit(-1); } })(); //# sourceMappingURL=autoshelve.js.map