Skip to content

Instantly share code, notes, and snippets.

@homburg
Created May 10, 2023 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save homburg/7a6ee1bea40af30bfe1957b1e1761564 to your computer and use it in GitHub Desktop.
Save homburg/7a6ee1bea40af30bfe1957b1e1761564 to your computer and use it in GitHub Desktop.
gsww - pick and checkout git branch, sorted after last checked out
#!/usr/bin/env node
const { execSync, spawnSync } = require('child_process');
const readline = require('readline');
const stream = require('stream');
const lines = execSync("git reflog --no-color");
var bufferStream = new stream.PassThrough();
bufferStream.end(lines);
var rl = readline.createInterface({
input: bufferStream,
});
const branches = [];
rl.on('close', function (line) {
spawnSync("sh", ['-c', `git switch $(echo "${branches.join('\n')}" | fzf)`], {stdio: 'inherit', });
});
rl.on('line', function (line) {
const match = line.match(/moving from (?<branch>[^\s]+)/);
const branch = match?.groups?.branch;
if (branch && !branches.includes(branch)) {
branches.push(branch);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment