1#!/usr/bin/env perl
2
3use strict;
4use warnings;
5use Getopt::Long;
6use Time::gmtime;
7
8my $rv = GetOptions(
9    'input|I=s' => \my $Input,
10    'tag|basetag|t' => \my $PrintBase,
11    'version|v' => \my $PrintVersion,
12    'pre-release|P' => \my $PrintPTag,
13    'ncommits|n' => \my $PrintCount,
14    'sha|s' => \my $PrintSHA,
15    'force|f' => \my $Force,
16    'rpm-full' => \my $RPM_FULL,
17    'rpm-ver' => \my $RPM_VER,
18    'rpm-rel' => \my $RPM_REL,
19    'deb' => \my $DEB,
20    'tar' => \my $TAR,
21    'help|h' => \my $WantHelp);
22
23my $HELPTEXT =
24<<EOF;
25    -I --input      Input (instead of git exe, for testing)
26    -t --basetag    Print base tag (i.e. x.y.z-foo)
27    -n --ncommits   Print number of commits since base
28    -s --sha        Print abbreviated SHA1
29    -f --force      Always print ncommits/sha, even if HEAD is a tag
30    -v --version    Print base x.y.z version
31    -P --pre-release Print prerelease tag, e.g. 'beta'
32       --deb        Print proper "Debian" version number
33       --rpm-full   Print proper "RPM" version number
34       --rpm-ver    Print RPM "Version" string
35       --rpm-rel    Print RPM "Release" string
36       --tar        Print proper "Tarball" name
37EOF
38
39if (!$rv) {
40    print $HELPTEXT;
41    exit(1);
42}
43if ($WantHelp) {
44    print $HELPTEXT;
45    exit(0);
46}
47
48my $output;
49if (!$Input) {
50    $output = qx(git describe --long --abbrev=10);
51} else {
52    $output = $Input;
53}
54$output =~ s/\s+$//g;
55$output =~ s/^\s+//g;
56
57my @components = split('-', $output);
58my $sha = pop @components;
59my $ncommits = pop @components;
60my $version = join('-', @components);
61my ($v_maj, $v_min, $v_patch, $extras) =
62    ($version =~ m/(\d+)\.(\d+)\.(\d+)(-(.+))?/);
63
64my $xyz_version = join('.', $v_maj, $v_min, $v_patch);
65if ($extras) {
66    # Strip leading '-' from extras
67    $extras =~ s/^[+-]+//g;
68}
69
70if ($PrintBase) {
71    print "$version\n";
72}
73
74if (!$Force) {
75    $Force = int($ncommits);
76}
77
78if ($PrintSHA && $Force) {
79    print "$sha\n";
80}
81
82if ($PrintCount && $Force) {
83    print "$ncommits\n";
84}
85
86if ($PrintPTag) {
87    print "$extras\n";
88}
89
90if ($PrintVersion) {
91    printf("%d.%d.%d\n", $v_maj, $v_min, $v_patch);
92}
93
94if ($DEB) {
95    my $vbase = $xyz_version;
96    if ($extras) {
97        $vbase .= "~$extras";
98    }
99
100    if ($ncommits) {
101        $vbase .= "+r$ncommits$sha";
102    }
103
104    print "$vbase\n";
105}
106
107sub get_rpm_versions {
108    my $vbase = $xyz_version;
109    my $reltag;
110
111    # Figure out the release tag.
112    # If we're a pre-release AND a snapshot, then the build number goes at the end.
113    # Otherwise,
114    my $relno;
115
116    if ($extras) {
117        $relno = 0;
118    } else {
119        if (!$ncommits) {
120            $relno = 1;
121        } else {
122            $relno = $ncommits + 1;
123        }
124    }
125
126
127    $reltag = "$relno";
128
129    # Pre-release, no commits
130    if ($extras) {
131        if ($ncommits) {
132            $reltag .= ".r${ncommits}${sha}";
133        } else {
134            $reltag .= ".0";
135        }
136        $reltag .= ".$extras";
137    } elsif ($ncommits) {
138        # No extras. Do we still have something here?
139        $reltag .= ".r${ncommits}${sha}.SP";
140    }
141    return ($vbase, $reltag);
142}
143
144if ($RPM_FULL) {
145    my ($vbase,$rel) = get_rpm_versions();
146    print "$vbase-$rel\n";
147}
148if ($RPM_VER) {
149    my ($vbase,$rel) = get_rpm_versions();
150    print "$vbase\n";
151}
152if ($RPM_REL) {
153    my ($vbase,$rel) = get_rpm_versions();
154    print "$rel\n";
155}
156
157if ($TAR) {
158    my $copy;
159    if (!$ncommits) {
160        $copy = $version;
161    } else {
162        $copy = $output;
163    }
164    $copy =~ s/-/_/g;
165    print "$copy\n";
166}
167