ice.fo.utils
Version:
131 lines (104 loc) • 3.3 kB
JavaScript
const { execSync } = require('child_process')
exports.pullLatest = function pullLatest () {
return execSync('git pull').toString().trim()
}
exports.pushCommits = function pushCommits () {
console.log('[Push Commits]')
return execSync('git push').toString().trim()
}
exports.pushBranches = function pushBranches ({ branches }) {
const currentBranch = exports.getCurrentBrannch()
for (const branchName of branches) {
try {
console.log('[Push to Remote]', branchName)
exports.checkoutToBranch(branchName)
exports.pullLatest()
exports.pushCommits()
} catch (error) {
console.error(error)
}
}
exports.checkoutToBranch(currentBranch)
}
exports.checkHasDirtyChanges = function checkHasDirtyChanges () {
return execSync('git status -s').toString().trim()
}
exports.undoLatestCommits = function (step = 1) {
return execSync('git reset --soft HEAD^')
}
exports.checkHasUncommittedChanges = function () {
}
exports.getCurrentBrannch = function getCurrentBrannch () {
return execSync('git branch --show-current').toString().trim()
}
exports.getLastCommitHash = function getLastCommitHash () {
try {
return execSync('git rev-parse --short HEAD').toString().trim()
} catch (_) {
return ''
}
}
exports.checkoutToBranch = function (name) {
return execSync(`git checkout ${name}`).toString().trim()
}
exports.removeAllUnpushedCommits = function removeAllUnpushedCommits () {
const currentBranch = exports.getCurrentBrannch()
return execSync(`git clean -fd :/ && git reset -q --hard origin/${currentBranch}`).toString().trim()
}
exports.cherryPickCommit = function cherryPickCommit (id) {
try {
exports.removeAllUnpushedCommits()
} catch (_) {}
const result = execSync(`git cherry-pick ${id}`).toString().trim()
const yes = exports.checkHasDirtyChanges()
if (yes) {
execSync('git cherry-pick --abort')
exports.removeAllUnpushedCommits()
throw new Error(result)
}
return result
}
exports.resetBranches = function resetBranches ({ branches }) {
const currentBranch = exports.getCurrentBrannch()
for (const branchName of branches) {
try {
console.log('[Reset Branch]', branchName)
exports.checkoutToBranch(branchName)
exports.removeAllUnpushedCommits()
exports.pullLatest()
} catch (error) {
console.error(error)
}
}
exports.checkoutToBranch(currentBranch)
}
exports.cherryPickToBranches = function cherryPickToBranches ({ id, branches }) {
if (!id) {
console.log('Commit ID cannot be empty')
return
}
const currentBranch = exports.getCurrentBrannch()
let success = false
try {
for (const branchName of branches) {
console.log('[Chery Pick]', id, branchName)
exports.checkoutToBranch(branchName)
exports.pullLatest()
exports.cherryPickCommit(id)
console.log('[Cherry Pick] Succeeded', branchName)
}
success = true
} catch (error) {
console.error(error)
// revert all cherry-picked
for (const branchName of branches) {
exports.checkoutToBranch(branchName)
exports.removeAllUnpushedCommits()
}
console.log('Reverted all cherry-pick')
success = false
} finally {
exports.checkoutToBranch(currentBranch)
return success
}
}