Created
January 30, 2017 07:31
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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