sourcecontrol
Version:
A modern TypeScript CLI application for source control
55 lines • 2.26 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.BranchCreator = void 0;
const branch_validator_1 = require("./branch-validator");
const utils_1 = require("../../../utils");
const objects_1 = require("../../../core/objects");
class BranchCreator {
constructor(repository, refService, infoService) {
this.repository = repository;
this.refService = refService;
this.infoService = infoService;
}
async createBranch(branchName, options = {}) {
branch_validator_1.BranchValidator.validateAndThrow(branchName);
if (!options.force && (await this.refService.exists(branchName))) {
throw new Error(`Branch '${branchName}' already exists`);
}
const startSha = await this.resolveStartPoint(options.startPoint);
await this.refService.updateBranch(branchName, startSha);
utils_1.logger.info(`Created branch '${branchName}' at ${startSha.substring(0, 7)}`);
if (options.track) {
await this.setUpstream(branchName, options.track);
}
return await this.infoService.getBranchInfo(branchName);
}
async resolveStartPoint(startPoint) {
if (!startPoint) {
try {
const currentBranch = await this.refService.getCurrentBranch();
if (!currentBranch) {
throw new Error('Cannot create branch: no commits yet');
}
return await this.refService.getBranchSha(currentBranch);
}
catch {
throw new Error('Cannot create branch: no commits yet');
}
}
try {
return await this.refService.getBranchSha(startPoint);
}
catch {
const commit = await this.repository.readObject(startPoint);
if (!objects_1.ObjectValidator.isCommit(commit)) {
throw new Error(`Invalid start point: ${startPoint}`);
}
return startPoint;
}
}
async setUpstream(localBranch, remoteBranch) {
utils_1.logger.info(`Branch '${localBranch}' set up to track '${remoteBranch}'`);
}
}
exports.BranchCreator = BranchCreator;
//# sourceMappingURL=branch-creation.js.map
;