sourcecontrol
Version:
A modern TypeScript CLI application for source control
121 lines • 4.99 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.SourceRepository = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const repo_1 = require("./repo");
const object_store_1 = require("../../core/object-store");
const utils_1 = require("../../utils");
const exceptions_1 = require("./exceptions");
class SourceRepository extends repo_1.Repository {
constructor() {
super();
this._workingDirectory = null;
this._gitDirectory = null;
this._objectStore = new object_store_1.FileObjectStore();
}
async init(path) {
try {
this._workingDirectory = path.resolve();
this._gitDirectory = this._workingDirectory.resolve(SourceRepository.DEFAULT_GIT_DIR);
if (await SourceRepository.exists(this._workingDirectory)) {
throw new exceptions_1.RepositoryException('Already a git repository: ' + this._workingDirectory);
}
const gitDir = this._gitDirectory;
await this.createDirectories(gitDir);
await this.createDirectories(gitDir.resolve(SourceRepository.DEFAULT_OBJECTS_DIR));
await this.createDirectories(gitDir.resolve(SourceRepository.DEFAULT_REFS_DIR));
await this.createDirectories(gitDir.resolve(SourceRepository.DEFAULT_REFS_DIR).resolve('heads'));
await this.createDirectories(gitDir.resolve(SourceRepository.DEFAULT_REFS_DIR).resolve('tags'));
await this._objectStore.initialize(this._gitDirectory);
await this.createInitialFiles();
}
catch (e) {
if (e instanceof exceptions_1.RepositoryException) {
throw e;
}
throw new exceptions_1.RepositoryException('Failed to initialize repository: ' + e);
}
}
workingDirectory() {
if (!this._workingDirectory) {
throw new exceptions_1.RepositoryException('Repository not initialized');
}
return this._workingDirectory;
}
gitDirectory() {
if (!this._gitDirectory) {
throw new exceptions_1.RepositoryException('Repository not initialized');
}
return this._gitDirectory;
}
objectStore() {
return this._objectStore;
}
async readObject(sha) {
try {
return await this._objectStore.readObject(sha);
}
catch {
throw new exceptions_1.RepositoryException('Failed to read object');
}
}
async writeObject(object) {
try {
return await this._objectStore.writeObject(object);
}
catch {
throw new exceptions_1.RepositoryException('Failed to write object');
}
}
static async findRepository(startPath) {
let current = startPath.resolve();
while (current != null) {
if (await SourceRepository.exists(current)) {
const repo = new SourceRepository();
repo._workingDirectory = current;
repo._gitDirectory = current.resolve(SourceRepository.DEFAULT_GIT_DIR);
try {
await repo._objectStore.initialize(repo._gitDirectory);
}
catch {
return null;
}
return repo;
}
current = current.parent || null;
}
return null;
}
async createInitialFiles() {
if (!this._gitDirectory) {
throw new exceptions_1.RepositoryException('Repository not initialized');
}
const headContent = 'ref: refs/heads/master\n';
await this.createFile(this._gitDirectory.resolve('HEAD'), headContent);
const description = "Unnamed repository; edit this file 'description' to name the repository.\n";
await this.createFile(this._gitDirectory.resolve('description'), description);
const config = '[core]\n' +
' repositoryformatversion = 0\n' +
' filemode = false\n' +
' bare = false\n';
await this.createFile(this._gitDirectory.resolve(SourceRepository.DEFAULT_CONFIG_FILE), config);
}
async createDirectories(path) {
await utils_1.FileUtils.createDirectories(path.fullpath());
}
async createFile(path, content) {
await utils_1.FileUtils.createFile(path.fullpath(), content);
}
static async exists(path) {
return await fs_extra_1.default.pathExists(path.resolve(SourceRepository.DEFAULT_GIT_DIR).fullpath());
}
}
exports.SourceRepository = SourceRepository;
SourceRepository.DEFAULT_GIT_DIR = '.source';
SourceRepository.DEFAULT_OBJECTS_DIR = 'objects';
SourceRepository.DEFAULT_REFS_DIR = 'refs';
SourceRepository.DEFAULT_CONFIG_FILE = 'config';
//# sourceMappingURL=source-repo.js.map