Skip to content

Instantly share code, notes, and snippets.

@afirth
Last active August 2, 2018 03:01
Show Gist options
  • Save afirth/cadc9e6aa115d02992e852773a47aaae to your computer and use it in GitHub Desktop.
Save afirth/cadc9e6aa115d02992e852773a47aaae to your computer and use it in GitHub Desktop.
looks for git branches whose head is the same ref as HEAD
#!/usr/bin/env perl
##### git-matching-branches #####
# aws codebuild checks out webhook payloads in a detached head state
# This script looks for branches whose head is the same ref as HEAD
# It prints the short branch names, newline separated, and should play nicely with xargs
# e.g. ./git-matching-branches | xargs -n1 -I '{}' cp tarball_version.tar.gz tarball_version-'{}'.tar.gz
#
# N.B. Branches whose name does not satisfy semver 2.0.0 item 9 will cause this script to exit abnormally
# and probably break the build [0-9A-Za-Z.-]
# This is because the branch-name is used as a pre-release suffix in the semver
# TODO add a pre-commit hook to catch this
#
# LICENSE: MIT LICENSE
# @afirth 08/2018
### STOP! ###
# recent git supports --points-to for git-branch, git-tag, and git-for-each-ref
# try e.g. git for-each-ref --format="%(refname:lstrip=1)" --points-at `git rev-parse HEAD`
die("use git")
use strict; use warnings;
use v5.10;
my $sha = `git rev-parse HEAD`;
chomp $sha;
my @branches;
open my $cmd, '-|', 'git show-ref --heads';
while ( <$cmd> ) {
chomp();
if ( m|^(?<sha>\w+) refs/heads/(?<branch>.*)$| ) {
if ( $sha eq $+{sha} ) {
must_comply_semver( $+{branch} );
push( @branches, $+{branch} );
}
}
else { # defensive
die "Unable to parse output of git show-ref --heads: $_"
}
}
close $cmd;
say join( " ", @branches );
sub must_comply_semver{
my $str = shift;
unless ( $str =~ m/^[0-9A-Za-z.-]+$/ ) {
die ( "Branch is not semver 2.0.0 item 9 compliant. Builds at this SHA broken. Delete the branch: $str\n" );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment