#!/usr/bin/perl # # gbl This script prints the git branch-groups for the current # git environment. # # The branch name convention is: # # component.NNNNNN # # component - a name provided by the user # NNNNNN - a numerically ascending index number # # The distinct components will be printed. # # usage: # gbl [options] # # Revision History # 1.0 Initial revision. 160930 # 1.1 Added license info. 180616 # # Written by Wayne Morrison, 160930. # # Copyright 2016 Wayne Morrison # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # use strict; use Getopt::Long qw(:config no_ignore_case_always); # # Version information. # my $NAME = "gbl"; my $VERS = "$NAME version: 1.1"; ############################################################################ # # Options fields. # my %opts = (); # Options. # # Command line arguments. # my @opts = ( 'last', # Show last branch index. 'help', # Give a help message. 'Version', # Display the program version. ); my $last = 0; # -last flag. ############################################################################ $| = 1; main(); exit(0); #----------------------------------------------------------------------------- # Routine: main() # # Purpose: Do the work -- parse args, get the branch names, get the # distinct component names, and print them. # sub main { # # Munch on the options and arguments. # optsandargs(); # # Print the last branch index names. # if($last) { showlasts(); exit(0); } # # Print the list of unique branch component names. # getbranches(); } #---------------------------------------------------------------------- # Routine: optsandargs() # # Purpose: Parse the command line for options and arguments. # sub optsandargs { # # Parse the options. # GetOptions(\%opts,@opts) || usage(); # # Check for some immediate-action options. # usage() if(defined($opts{'help'})); version() if(defined($opts{'Version'})); # # Ensure we weren't given any arguments. # usage() if(@ARGV > 0); # # Set a few arguments. # $last = 1 if(defined($opts{'last'})); } #---------------------------------------------------------------------- # Routine: getbranches() # # Purpose: Get and print a unique list of branch names. # sub getbranches { my $prev; # Previous index. my @branches = (); # List of branch names. # # Get the list of branches. # @branches = `git branch`; # # Strip the index off each branch name. # for(my $ind = 0; $ind < @branches; $ind++) { chomp $branches[$ind]; $branches[$ind] =~ s/\.[0-9]+$//; } # # Get rid of duplicate branch names. # $prev = 0; for(my $ind = 1; $ind < @branches; $ind++) { if($branches[$ind] eq $branches[$prev]) { $branches[$ind] = ''; } else { $prev = $ind; } } # # Print the unique branch names. # for(my $ind = 0; $ind < @branches; $ind++) { print "$branches[$ind]\n" if($branches[$ind] ne ''); } } #---------------------------------------------------------------------- # Routine: showlasts() # # Purpose: Print the last branch names for each branch. Branches # without indices, such as 'master' will be given as is. # (Last is defined as the branch with the numerically # greatest index.) # sub showlasts { my $curbranch = ''; # Current branch. my $prev; # Previous index. my @branches = (); # List of branch names. my %branches = (); # Branch names and indices. # # Get the list of branches. # @branches = `git branch`; # # Save the greatest index from each branch name. # for(my $ind = 0; $ind < @branches; $ind++) { my $name; # Branch name. my $bind; # Branch index. chomp $branches[$ind]; # # Save the name of the current branch. # if($branches[$ind] =~ /^\* /) { $curbranch = $branches[$ind]; $curbranch =~ s/^\* //; } # # Save the greatest index number for the branch. # if($branches[$ind] =~ /^. (.*)\.([0-9]+)$/) { $name = $1; $bind = $2; if($branches{$name} lt $bind) { $branches{$name} = $bind; } } elsif($branches[$ind] =~ /^. (.*)$/) { # # If the branch has no index, we'll just save the name. # $name = $1; $branches{$name} = ''; } } # # Print the last branch names for each branch. # foreach my $name (sort(keys(%branches))) { my $brname; # Constructed branch name. # # Build the actual branch name. # if($branches{$name} eq '') { $brname = "$name"; } else { $brname = "$name.$branches{$name}"; } # # Add in the prefix spacing and current-branch marker. # if($brname eq $curbranch) { $brname = '* ' . $brname; } else { $brname = ' ' . $brname; } print "$brname\n"; } } #---------------------------------------------------------------------- # Routine: version() # # Purpose: Print the version number(s) and exit. # sub version { print STDERR "$VERS\n"; exit(0); } #---------------------------------------------------------------------- # Routine: usage() # # Purpose: Give usage message and exit. # sub usage { print STDERR "usage: gbl [options]\n"; print STDERR "\n"; print STDERR "\twhere [options] are:\n"; print STDERR "\t\t-last\n"; print STDERR "\t\t-help\n"; print STDERR "\t\t-Version\n"; exit(0); } 1; ############################################################################## =pod =head1 NAME B - display a list of unique git branch names (without index numbers) =head1 SYNOPSIS gbl [options] =head1 DESCRIPTION B displays a list of unique git branch names (without index numbers.) It assumes the branches follow the naming format used by the B command. The B branch-name convention is: component.NNNNNN where: component - a name provided by the user NNNNNN - a numerically ascending index number B prints the unique component names. Following the standard of "B", the name of the current branch will be prefixed with an asterisk. =head1 OPTIONS B takes the following options: =over 4 =item I<-last> Display the last branch name for each branch. The "last branch" is considered to be the branch with the numerically greatest index. Branches without indices, such as 'master' will be given as is. =item I<-Version> Display the version information for B. =item I<-help> Display a help message. =back =head1 LICENSE Copyright 2016 Wayne Morrison Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. =head1 AUTHOR Wayne Morrison, wayne@waynemorrison.com =head1 SEE ALSO B, B =cut