sourcecontrol
Version:
A modern TypeScript CLI application for source control
113 lines • 4.43 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IndexManager = void 0;
const path_1 = __importDefault(require("path"));
const fs_extra_1 = __importDefault(require("fs-extra"));
const ignore_1 = require("../../core/ignore");
const utils_1 = require("../../utils");
const tree_1 = require("../../core/tree");
const git_index_1 = require("./git-index");
const services_1 = require("./services");
class IndexManager {
constructor(repository) {
this.repository = repository;
this.indexPath = path_1.default.join(repository.gitDirectory().fullpath(), IndexManager.INDEX_FILE_NAME);
this.index = new git_index_1.GitIndex();
this.ignoreManager = new ignore_1.IgnoreManager(repository);
this.treeWalker = new tree_1.TreeWalker(repository);
}
async initialize() {
await this.loadIndex();
await this.ignoreManager.initialize();
}
async add(filePaths) {
const indexFileAdder = new services_1.IndexFileAdder(this.repository, this.repoRoot());
return await indexFileAdder.addFiles(filePaths, this.index);
}
async status() {
await this.loadIndex();
await this.ignoreManager.initialize();
const statusCalculator = new services_1.StatusCalculator(this.repoRoot(), this.treeWalker, this.ignoreManager);
return await statusCalculator.calculateStatus(this.index);
}
async remove(filePaths, deleteFromDisk = false) {
const result = {
removed: [],
failed: [],
};
const pushFailed = (path, reason) => {
result.failed.push({
path,
reason,
});
};
await this.loadIndex();
for (const filePath of filePaths) {
try {
const { absolutePath, relativePath } = this.createAbsAndRelPaths(filePath);
if (!this.index.hasEntry(relativePath)) {
pushFailed(relativePath, 'File not in index');
continue;
}
this.index.removeEntry(relativePath);
result.removed.push(relativePath);
if (deleteFromDisk && (await utils_1.FileUtils.exists(absolutePath))) {
await fs_extra_1.default.unlink(absolutePath);
}
}
catch (error) {
pushFailed(filePath, error.message);
}
}
await this.saveIndex();
return result;
}
async clearIndex() {
this.index.clear();
await this.saveIndex();
}
async loadIndex() {
this.index = await git_index_1.GitIndex.read(this.indexPath);
}
async saveIndex() {
await this.index.write(this.indexPath);
}
async getFilesInDirectory(dirPath, repoRoot) {
const files = [];
const entries = await fs_extra_1.default.readdir(dirPath, { withFileTypes: true });
entries.forEach(async (entry) => {
const fullPath = path_1.default.join(dirPath, entry.name);
const { relativePath } = this.createAbsAndRelPaths(fullPath);
const isIgnored = this.ignoreManager.isIgnored(relativePath, entry.isDirectory());
if (isIgnored)
return;
if (entry.isDirectory()) {
if (entry.name === '.source')
return;
const subFiles = await this.getFilesInDirectory(fullPath, repoRoot);
files.push(...subFiles);
return;
}
if (entry.isFile())
files.push(fullPath);
});
return files;
}
repoRoot() {
return this.repository.workingDirectory().fullpath();
}
createAbsAndRelPaths(filePath) {
const repoRoot = this.repoRoot();
const absolutePath = path_1.default.isAbsolute(filePath)
? path_1.default.normalize(filePath)
: path_1.default.join(repoRoot, filePath);
const relativePath = path_1.default.relative(repoRoot, absolutePath).replace(/\\/g, '/');
return { absolutePath, relativePath };
}
}
exports.IndexManager = IndexManager;
IndexManager.INDEX_FILE_NAME = 'index';
//# sourceMappingURL=index-manager.js.map