-
-
Save homburg/7a6ee1bea40af30bfe1957b1e1761564 to your computer and use it in GitHub Desktop.
gsww - pick and checkout git branch, sorted after last checked out
This file contains 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 | |
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