sourcecontrol
Version:
A modern TypeScript CLI application for source control
97 lines • 3.41 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BranchInfoService = void 0;
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const objects_1 = require("../../../core/objects");
const utils_1 = require("../../../utils");
const branch_ref_1 = require("./branch-ref");
class BranchInfoService {
constructor(repository, refManager, refService) {
this.repository = repository;
this.refManager = refManager;
this.refService = refService;
}
async listBranches() {
const branchDir = path_1.default.join(this.refManager.getRefsPath(), branch_ref_1.BranchRefService.BRANCH_DIR_NAME);
if (!(await utils_1.FileUtils.exists(branchDir))) {
return [];
}
const branchNames = await fs_extra_1.default.readdir(branchDir);
const branches = [];
for (const name of branchNames) {
if (name.startsWith('.'))
continue;
try {
const info = await this.getBranchInfo(name);
branches.push(info);
}
catch (error) {
}
}
return branches.sort((a, b) => {
if (a.isCurrentBranch)
return -1;
if (b.isCurrentBranch)
return 1;
return a.name.localeCompare(b.name);
});
}
async getBranchInfo(branchName) {
const sha = await this.refService.getBranchSha(branchName);
const currentBranch = await this.refService.getCurrentBranch();
const isCurrentBranch = currentBranch === branchName;
const commitDetails = await this.getCommitDetails(sha);
const commitCount = await this.countCommits(sha);
return {
name: branchName,
sha,
isCurrentBranch,
...commitDetails,
commitCount,
};
}
async getCommitDetails(sha) {
try {
const commit = await this.repository.readObject(sha);
if (!objects_1.ObjectValidator.isCommit(commit)) {
return {};
}
const details = {};
const msg = commit.message?.split('\n')[0];
if (msg !== undefined)
details.lastCommitMessage = msg;
if (commit.author)
details.lastCommitDate = new Date(commit.author.timestamp * 1000);
return details;
}
catch {
return {};
}
}
async countCommits(startSha) {
const visited = new Set();
const queue = new utils_1.Queue([startSha]);
while (queue.length > 0) {
const sha = queue.shift();
if (visited.has(sha))
continue;
visited.add(sha);
try {
const commit = await this.repository.readObject(sha);
if (objects_1.ObjectValidator.isCommit(commit)) {
queue.push(...commit.parentShas);
}
}
catch {
continue;
}
}
return visited.size;
}
}
exports.BranchInfoService = BranchInfoService;
//# sourceMappingURL=branch-info.js.map
;