const { Injectable } = require('flitter-di') const UniversalPath = require('../logical/UniversalPath') class Repository extends Injectable { static fromString(string) { const path = UniversalPath.fromString(string) return new this(path) } constructor(root_path) { super() this._host = root_path.host this._path = root_path } async is_repo() { const git_dir = await this._path.child('.git') await git_dir.classify() return git_dir.is_directory() } async init() { if ( !(await this.is_repo()) ) { await this._path.classify() if ( !this._path.is_valid() ) await this._path.touch(true) await this._git_cmd(`init`) } } async destroy() { const git_dir = await this._path.child('.git') await git_dir.unlink() } async clone(from) { await this._path.classify() if ( !this._path.is_valid() ) await this._path.touch(true) await this._git_cmd(`clone ${from} ${this._path.path}`) } async stage(...blobs) { await this._git_cmd(`add ${blobs.map(x => '"'+x+'"').join(' ')}`) } async unstage(...blobs) { await this._git_cmd(`reset ${blobs.map(x => '"'+x+'"').join(' ')}`) } async commit(message) { await this._git_cmd(`commit -m "${message}"`) } async tag(label) { await this._git_cmd(`tag "${label}"`) } async push(target = false, ref = false) { await this._git_cmd(`push${target ? ' '+target : ''}${ref ? ' '+ref : ''} --tags`) } async checkout(ref = 'master') { await this._git_cmd(`checkout "${ref}"`) } async stash() { await this._git_cmd(`stash`) } async _git_cmd(command) { const cd_cmd = await this._host.get_directory_change_command(this._path) const cmd = `${cd_cmd} && git ${command}` return this._host.run(cmd) } } module.exports = exports = Repository