Skip to content

Instantly share code, notes, and snippets.

@geraldlai
Last active May 13, 2022 23:01
Show Gist options
  • Save geraldlai/8832480756443311ca4f4941cafe8029 to your computer and use it in GitHub Desktop.
Save geraldlai/8832480756443311ca4f4941cafe8029 to your computer and use it in GitHub Desktop.
git-macho: git checkout drop-in replacement
#!/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