2020-03-05 18:15:00 +00:00
|
|
|
const AbstractGitState = require('./AbstractGitState')
|
2020-03-05 17:48:38 +00:00
|
|
|
|
2020-03-05 18:15:00 +00:00
|
|
|
class CheckoutState extends AbstractGitState {
|
2020-03-05 17:48:38 +00:00
|
|
|
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.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:11:10 +00:00
|
|
|
failure_message() {
|
|
|
|
return `The Git repo at "${this._config.path}" on host "${this._host.name}" is not checked out to the correct ref (${this._config.target}).`
|
|
|
|
}
|
|
|
|
|
|
|
|
check_message() {
|
|
|
|
return this.failure_message()
|
|
|
|
}
|
2020-03-05 17:48:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = CheckoutState
|