Last active
May 13, 2022 23:01
-
-
Save geraldlai/8832480756443311ca4f4941cafe8029 to your computer and use it in GitHub Desktop.
git-macho: git checkout drop-in replacement
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/perl | |
# git (ma)gic (ch)eck(o)ut - Oooh keep it together, git! | |
# | |
# Each branch maintains its stashed changes separately & contained. | |
# Usage: 'git macho' drop-in replacement for 'git checkout' | |
# Author: Gerald Lai | |
# License: 0BSD | |
use warnings; | |
use strict; | |
use List::Util qw(first); | |
sub run { system(@_) == 0 or exit($? >> 8) } | |
sub the_goods { | |
chomp(my $bronzh = qx{ git symbolic-ref --short --quiet HEAD || git rev-parse --quiet HEAD }); | |
return "WIP so git-macho, $bronzh"; | |
} | |
my $bomb = the_goods(); | |
run( qw(git stash save --all --quiet --message), $bomb ); | |
# the core | |
my $awyiss = system( qw(git checkout), @ARGV ); | |
my $kapow = the_goods(); | |
my $mustash = first { /: \Q$kapow\E$/ } qx{ git stash list }; | |
if (defined $mustash) { | |
my ($mu) = split /:/, $mustash; | |
run( qw(git stash pop --index --quiet), $mu ); | |
} | |
exit($awyiss >> 8); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment