Skip to content

Instantly share code, notes, and snippets.

@mscandal
Last active September 30, 2015 22:30
Show Gist options
  • Select an option

  • Save mscandal/fe23af96227776894597 to your computer and use it in GitHub Desktop.

Select an option

Save mscandal/fe23af96227776894597 to your computer and use it in GitHub Desktop.
git regex add
const co = require('co');
const spawn = require('child_process').spawn;
const minimatch = require('minimatch');
const pathfinder = /\s*.+\s+(.*)/;
const r = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
let results = ['add'];
const getStatus = () => {
let status = '';
return callback => {
spawn('git', ['status', '--porcelain']).stdout.on('data', chunk => {
status += chunk;
}).on('end', () => callback(null, status));
};
};
const ask = (question) => {
return (callback) => {
r.question(question, answer => callback(null, answer));
};
};
co(function*() {
const status = yield getStatus();
console.log(status);
const lines = status
.split('\n')
.map(line => line.trim())
.filter(line => line.length)
.map(line => pathfinder.exec(line)[1]);
const args = process.argv.slice(2);
if (args && args.length) {
results = results.concat(args
.map(glob => lines.filter(minimatch.filter(glob)))
.reduce((a, b) => a.concat(b)));
} else {
const glob = yield ask('Enter glob (quit): ');
results = results.concat(lines.filter(minimatch.filter(glob)));
}
if (results.length > 1) {
console.log(results.filter(p => p !== 'add').join(', '));
const answer = yield ask('Add? (yes): ');
const lAns = answer.toLowerCase();
if (!answer.length || lAns === 'yes' || lAns === 'y') {
spawn('git', results, {
stdio: [
'ignore',
process.stdout,
process.stderr
]
});
}
} else {
console.log('No results');
}
}).then(() => {
r.close();
}, () => {
r.close();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment