Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jforns/729e69d971f5f58f0aef to your computer and use it in GitHub Desktop.
Save jforns/729e69d971f5f58f0aef to your computer and use it in GitHub Desktop.
Switch to a recently modified branch through a selection menu
#!/usr/bin/env node
String.prototype.format = function() {
var formatted = this;
for (var i = 0; i < arguments.length; i++) {
var regexp = new RegExp('\\{'+i+'\\}', 'gi');
formatted = formatted.replace(regexp, arguments[i]);
}
return formatted;
};
var options = [];
var cmd = "git for-each-ref --count=12 --sort=-committerdate --format='%(refname:short)' refs/heads/";
var process = require('child_process');
process.exec(cmd, function (err, stdout, stderr) {
if (err) {
console.error(err);
return;
}
var inquirer = require("inquirer");
options = stdout.trim().split("\n");
options.push(new inquirer.Separator());
inquirer.prompt([{
type: "list",
name: "select",
message: "Choose a branch:",
choices: options
}], function(answer) {
process.exec("git checkout {0}".format(answer.select), function(err, stdout, stderr) {
if (err) {
console.error(err);
}
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment