maestro/app/classes/git/Repository.js

101 lines
2.8 KiB
JavaScript
Raw Normal View History

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 create_tag(label) {
await this._git_cmd(`tag "${label}"`)
}
async delete_tag(label) {
await this._git_cmd(`tag --delete "${label}"`)
}
async get_tag(label) {
const cd_cmd = await this._host.get_directory_change_command(this._path)
const cmd = `${cd_cmd} && git rev-parse "${label}"`
return await this._host.run_line_result(cmd)
}
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}"`)
}
2020-03-05 17:48:38 +00:00
async current_branch() {
const cd_cmd = await this._host.get_directory_change_command(this._path)
const cmd = `${cd_cmd} && git symbolic-ref HEAD`
const full_ref = await this._host.run_line_result(cmd)
return full_ref.replace('refs/heads/', '')
}
async current_ref() {
const cd_cmd = await this._host.get_directory_change_command(this._path)
const cmd = `${cd_cmd} && git show-ref HEAD`
const ref = await this._host.run_line_result(cmd)
return ref.split(' ')[0]
}
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