Skip to content

Instantly share code, notes, and snippets.

@nibble-4bits
Last active February 28, 2024 23:16
Show Gist options
  • Save nibble-4bits/086f60618a498593616c0e940d52b0d7 to your computer and use it in GitHub Desktop.
Save nibble-4bits/086f60618a498593616c0e940d52b0d7 to your computer and use it in GitHub Desktop.
A perl script that displays the git log and highlights the commits returned by `git cherry`, hence why its called git-v(isual)cherry :)
#!/usr/bin/env perl
use strict;
use warnings;
use Term::ANSIColor;
my $latest_commit = `git log --oneline -n 1`;
my ($commit_hash) = $latest_commit =~ /^([0-9a-f]+)/;
my $commit_length = length $commit_hash;
my $cherry_commits = `git cherry @ARGV`;
my @needed = $cherry_commits =~ /^\+ (\w+)/gm;
my @not_needed = $cherry_commits =~ /^\- (\w+)/gm;
@needed = map ( substr( $_, 0, $commit_length ), @needed );
@not_needed = map ( substr( $_, 0, $commit_length ), @not_needed );
my %needed_set;
@needed_set{@needed} = ();
my %not_needed_set;
@not_needed_set{@not_needed} = ();
my $git_log = `git log --all --decorate --oneline --graph --color=always`;
for my $line ( split( /\n/, $git_log ) ) {
if ( $line =~ /([0-9a-f]{$commit_length})/ ) {
if ( exists $needed_set{$1} ) {
substr( $line, $-[0], $commit_length, colored( $1, "on_ansi22" ) );
}
elsif ( exists $not_needed_set{$1} ) {
substr( $line, $-[0], $commit_length, colored( $1, "on_ansi52" ) );
}
}
print "$line\n";
}
@nibble-4bits
Copy link
Author

To use, add alias to .gitconfig like this:

[alias]
        vcherry = "!f() { path/to/git-vcherry.pl \"$@\" | less; }; f"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment