Skip to content

Instantly share code, notes, and snippets.

@kevincharm
Created January 30, 2017 07:31
Show Gist options
  • Save kevincharm/4f1b7e24cbf8f7cd3bfeb0cfd7fd59ce to your computer and use it in GitHub Desktop.
Save kevincharm/4f1b7e24cbf8f7cd3bfeb0cfd7fd59ce to your computer and use it in GitHub Desktop.
Node script that automates git checkout --ours or --theirs for all files in a merge conflict
#!/usr/bin/env node
'use strict'
const childProcess = require('child_process')
function bash() {
return new Promise((resolve, reject) => {
const terminal = childProcess.spawn('bash')
resolve(terminal)
})
}
function parseBothModifiedFiles(out) {
return out.split('\n')
.map(line => {
const test = line.match(/^\s+both modified\:\s+(.+)$/)
if (test && test[1]) {
return test[1]
} else {
return null
}
})
.filter(line => !!line)
}
function gitCheckoutOurs(files) {
bash().then(term => {
files.forEach(file => {
term.stdin.write(`git checkout --ours ${file}\ngit add ${file}\n`)
})
term.stdin.end()
})
}
function run() {
bash().then(term => {
let out = ''
term.stdout.on('data', buffer => out += buffer.toString())
term.stdout.on('end', () => {
const files = parseBothModifiedFiles(out)
gitCheckoutOurs(files)
})
term.stdin.write('git status\n')
term.stdin.end()
})
}
run()
#!/usr/bin/env node
'use strict'
const childProcess = require('child_process')
function bash() {
return new Promise((resolve, reject) => {
const terminal = childProcess.spawn('bash')
resolve(terminal)
})
}
function parseBothModifiedFiles(out) {
return out.split('\n')
.map(line => {
const test = line.match(/^\s+both modified\:\s+(.+)$/)
if (test && test[1]) {
return test[1]
} else {
return null
}
})
.filter(line => !!line)
}
function gitCheckoutOurs(files) {
bash().then(term => {
files.forEach(file => {
term.stdin.write(`git checkout --theirs ${file}\ngit add ${file}\n`)
})
term.stdin.end()
})
}
function run() {
bash().then(term => {
let out = ''
term.stdout.on('data', buffer => out += buffer.toString())
term.stdout.on('end', () => {
const files = parseBothModifiedFiles(out)
gitCheckoutOurs(files)
})
term.stdin.write('git status\n')
term.stdin.end()
})
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment