#!/bin/env perl
use strict;
use warnings FATAL => 'all';
use Cwd 'abs_path';
use File::Basename;
my ( $program, $path, $argpath, $system, $branch, $repo );
$program = basename( $0 );
$path = dirname( abs_path(__FILE__) );
$system = "no";
$branch = "no";
$repo = "no";
$argpath = "";
sub usage
{
print <<EOF;
Usage: $program [options] [PATH]
Options:
--system - get system info othervise get build-system info;
--branch - get branch name;
--repo - get reposytory URL;
--help - print this help message and exit.
Args:
PATH - path to the root directory of SCM repository
or the path to a file located in the root directory
of the repository.
If options `--branch' and `--repo' are not specified then '$program'
returns the SCM revision.
EOF
exit;
}
foreach ( @ARGV )
{
if ( /--system/ )
{
$system = "yes";
next;
}
elsif ( /--branch/ )
{
$repo = "no";
$branch = "yes";
next;
}
elsif ( /--repo/ )
{
$branch = "no";
$repo = "yes";
next;
}
elsif( /--help/ )
{
usage;
}
else
{
if( $_ )
{
if ( -d "$_" )
{
$argpath = abs_path( $_ );
}
elsif ( -f "$_" )
{
$argpath = dirname( abs_path( $_ ) );
}
else
{
usage;
}
}
}
}
if( $argpath eq "" )
{
if ( $system eq "yes" )
{
$path = $path . "/..";
}
}
else
{
$path = $argpath;
}
sub get_svn_release
{
my $dir = shift;
my $info = `svn info $dir`;
$info =~ m!Revision: (.*)!;
return ( "$1" );
}
sub get_svn_branch
{
my $dir = shift;
my $info = `svn info $dir`;
$info =~ m!URL: (.*)!;
my $url = $1;
if ( $url =~ m!branches/([^/]*)/?! )
{
return ( "branches/" . "$1" );
}
elsif ( $url =~ m!tags/([^/]*)/?! )
{
return ( "tags/" . "$1" );
}
else {
return ( "trunk" );
}
}
sub get_svn_repo
{
my $dir = shift;
my $info = `svn info $dir`;
$info =~ m!Repository Root: (.*)!;
return ( "$1" );
}
sub get_git_release
{
my $dir = shift;
my $info = `cd $dir && if \$(git rev-parse --is-inside-work-tree 2>/dev/null) ; then echo -n "\$(git rev-list --count HEAD)" ; else echo -n "0" ; fi`;
return ( "$info" );
}
sub get_git_branch
{
my $dir = shift;
my $info = `cd $dir && if \$(git rev-parse --is-inside-work-tree 2>/dev/null) ; then echo -n "\$(git rev-parse --abbrev-ref HEAD)" ; else echo -n "unknown" ; fi`;
return ( "$info" );
}
sub get_git_repo
{
my $dir = shift;
my $info = `cd $dir && if \$(git rev-parse --is-inside-work-tree 2>/dev/null) ; then echo -n "\$(git config --get remote.origin.url)" ; else echo -n "unknown" ; fi`;
return ( "$info" );
}
if( -d "$path/.svn" )
{
#
# Subversion SCM:
#
if ( $branch eq "yes" )
{
print get_svn_branch( $path ) . "\n";
}
elsif ( $repo eq "yes" )
{
print get_svn_repo( $path ) . "\n";
}
else
{
print get_svn_release( $path ) . "\n";
}
}
elsif ( -d "$path/.git" )
{
#
# Git SCM:
#
if ( $branch eq "yes" )
{
print get_git_branch( $path ) . "\n";
}
elsif ( $repo eq "yes" )
{
print get_git_repo( $path ) . "\n";
}
else
{
print get_git_release( $path ) . "\n";
}
}
else
{
#
# Unknown SCM:
#
print "Unknown\n";
}