Add git.clone and git.checkout states

This commit is contained in:
garrettmills
2020-03-05 11:48:38 -06:00
parent 904a4be1b1
commit 9dc6f9d214
5 changed files with 97 additions and 1 deletions

View File

@@ -0,0 +1,45 @@
const State = require('../State')
const Repository = require('../../git/Repository')
class CheckoutState extends State {
static get services() {
return [...super.services, 'output']
}
async apply() {
if ( !(await this.check()) ) {
const repo = await this._repo()
await repo.checkout(this._config.target)
}
}
async check() {
const repo = await this._repo()
const target = await this._config.target
try {
if ( (await repo.current_branch()) === target ) return true
if ( (await repo.current_ref()) === target ) return true
} catch (e) {}
return false
}
async reverse() {
if ( this._config.revert_to ) {
if ( await this.check() ) {
const repo = await this._repo()
await repo.checkout(this._config.revert_to)
}
} else {
this.output.warn('Checkout state does not currently support automatic reversal. Specify the revert_to config key to enable.')
}
}
async _repo() {
const path = await this._host.get_path(this._config.path)
if ( !path.is_valid() ) await path.touch(true)
return new Repository(path)
}
}
module.exports = exports = CheckoutState

View File

@@ -0,0 +1,33 @@
const State = require('../State')
const Repository = require('../../git/Repository')
class CloneState extends State {
async apply() {
if ( !(await this.check()) ) {
const repo = await this._repo()
await repo.clone(this._config.source)
}
}
async check() {
const repo = await this._repo()
return repo.is_repo()
}
async reverse() {
if ( await this.check() ) {
const path = await this._host.get_path(this._config.path)
await path.unlink()
}
}
async _repo() {
const path = await this._host.get_path(this._config.path)
if ( !path.is_valid() ) await path.touch(true)
return new Repository(path)
}
}
module.exports = exports = CloneState