UNPKG

sourcecontrol

Version:

A modern TypeScript CLI application for source control

59 lines 2.31 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BranchRefService = void 0; const path_1 = __importDefault(require("path")); class BranchRefService { constructor(refManager) { this.refManager = refManager; } toBranchRefPath(branchName) { return path_1.default.join(BranchRefService.BRANCH_DIR_NAME, branchName); } async exists(branchName) { const refPath = this.toBranchRefPath(branchName); return await this.refManager.exists(refPath); } async getBranchSha(branchName) { const refPath = this.toBranchRefPath(branchName); return await this.refManager.resolveReferenceToSha(refPath); } async updateBranch(branchName, sha) { const refPath = this.toBranchRefPath(branchName); await this.refManager.updateRef(refPath, sha); } async deleteBranch(branchName) { const refPath = this.toBranchRefPath(branchName); return await this.refManager.deleteRef(refPath); } async getCurrentBranch() { try { const headContent = await this.refManager.readRef(BranchRefService.HEAD_FILE); const isDetached = !headContent?.startsWith(BranchRefService.HEAD_PREFIX); if (isDetached) return null; return headContent.substring(BranchRefService.HEAD_PREFIX.length); } catch { return null; } } async isDetached() { const headContent = await this.refManager.readRef(BranchRefService.HEAD_FILE); return !headContent?.startsWith(BranchRefService.HEAD_PREFIX); } async setCurrentBranch(branchName) { const headRef = `${BranchRefService.HEAD_PREFIX}${branchName}`; await this.refManager.updateRef(BranchRefService.HEAD_FILE, headRef); } async setDetachedHead(commitSha) { await this.refManager.updateRef(BranchRefService.HEAD_FILE, commitSha); } } exports.BranchRefService = BranchRefService; BranchRefService.BRANCH_DIR_NAME = 'heads'; BranchRefService.HEAD_FILE = 'HEAD'; BranchRefService.HEAD_PREFIX = 'ref: refs/heads/'; //# sourceMappingURL=branch-ref.js.map