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