Debian Codegraph @

eg/build-git

raw
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4 use Pod::Usage;
5 use File::Spec;
6 use File::Path qw( remove_tree );
7 use File::Temp qw( tempdir );
8 use Cwd qw( cwd );
9 use Getopt::Long;
10 use Git::Repository;
11 use Git::Version::Compare qw( cmp_git );
12 use System::Command 1.117; # loop_on
13
14 # command-line options
15 my %option = (
16 source => '/opt/src/git',
17 destination => '/opt/git',
18 limit => 0,
19 verbose => 0,
20 rc => 1,
21 );
22 GetOptions(
23 \%option, 'source=s', 'destination=s', 'verbose+', 'quiet',
24 'since=s', 'until=s', 'limit=i', 'force',
25 'list', 'missing', 'installed', 'rc!',
26 'fetch', 'test', 'docs',
27 'help', 'manual',
28 ) or pod2usage( -verbose => 0 );
29
30 # simple help/manual
31 pod2usage( -verbose => 1 ) if $option{help};
32 pod2usage( -verbose => 2 ) if $option{manual};
33
34 # verbosity options
35 my %run_opt;
36 $option{quiet} = 1 if $option{test};
37 $option{verbose} = 0 if $option{quiet};
38 $run_opt{stderr} = undef if !$option{verbose};
39 $run_opt{stdout} = sub { print shift } if $option{verbose} > 1;
40
41 # git.git
42 my $r = Git::Repository->new( work_tree => $option{source} );
43
44 # fetch recent commits
45 $r->run( 'fetch' ) if $option{fetch};
46
47 # map version numbers to tags
48 my %tag_for = map { ( my $v = substr $_, 1 ) =~ y/-/./; ( $v => $_ ) }
49 grep /^v[^0]/ && !/^v1\.0rc/, # skip anything before 1.0
50 $r->run( tag => '-l', 'v*' );
51
52 # select the versions to build and install
53 my @versions = sort cmp_git @ARGV ? @ARGV : keys %tag_for;
54
55 # replace aliases with the canonical name
56 {
57 my %alias = (
58 '1.0.1' => '1.0.0a',
59 '1.0.2' => '1.0.0b',
60 );
61 my %seen;
62 @versions = grep !$seen{$_}++, map $alias{$_} || $_, @versions;
63 }
64
65 @versions = grep !/rc/, @versions if !$option{rc};
66
67 @versions = grep !is_installed($_), @versions if $option{missing};
68 @versions = grep is_installed($_), @versions if $option{installed};
69
70 @versions = grep cmp_git( $option{since}, $_ ) <= 0, @versions if $option{since};
71 @versions = grep cmp_git( $_, $option{until} ) <= 0, @versions if $option{until};
72
73 @versions = $option{limit} > 0
74 ? @versions[ -$option{limit} .. -1 ] # <limit> most recent
75 : @versions[ 0 .. -$option{limit} - 1 ] # <limit> most ancient
76 if $option{limit};
77
78
79 # pick up invalid versions
80 my @nope = grep !exists $tag_for{$_}, @versions;
81 die "Can't compile non-existent versions: @nope\n" if @nope;
82
83 # just list the selected versions
84 print map "$_\n", @versions and exit if $option{list};
85
86 # test outputs TAP
87 if ( $option{test} ) {
88 require Test::More;
89 import Test::More;
90 plan( tests => scalar @versions );
91 $option{destination} = tempdir( CLEANUP => 1 );
92 }
93
94 # build install select versions
95 chdir $option{source} or die "Can't chdir to $option{source}: $!";
96 for my $version (@versions) {
97
98 # skip if that git already exists (and runs)
99 if ( is_installed($version) && !$option{force} ) {
100 print "*** GIT $version ALREADY INSTALLED ***\n" if !$option{quiet};
101 next;
102 }
103 else {
104 print "*** GIT $version ***\n" if !$option{quiet};
105 $r->run( checkout => '-f', '-q', $tag_for{$version} );
106 $r->run( clean => '-xqdf' );
107
108 # Fix various issues in the Git sources
109
110 # Fix GIT-VERSION-GEN to use `git describe` instead of `git-describe`
111 if (
112 cmp_git( $version, '1.3.3' ) <= 0
113 && cmp_git( '1.1.0', $version ) <= 0
114 && do { no warnings; `git-describe`; $? != 0 }
115 )
116 {
117 local ( $^I, @ARGV ) = ( '', 'GIT-VERSION-GEN' );
118 s/git-describe/git describe/, print while <>;
119 }
120
121 # fix GIT_VERSION in the Makefile
122 if ( cmp_git( $version, '1.0.9' ) == 0 ) {
123 local ( $^I, @ARGV ) = ( '', 'Makefile' );
124 s/^GIT_VERSION = .*/GIT_VERSION = $version/, print while <>;
125 }
126
127 # add missing #include <sys/resource.h>
128 elsif ( cmp_git( $version, '1.7.5.rc0' ) <= 0
129 && cmp_git( '1.7.4.2', $version ) <= 0 )
130 {
131 $r->run( 'cherry-pick', '-n',
132 'ebae9ff95de2d0b36b061c7db833df4f7e01a41d' );
133
134 # force the expected version number
135 my $version_file = File::Spec->catfile( $r->work_tree, 'version' );
136 open my $fh, '>', $version_file
137 or die "Can't open $version_file: $!";
138 print $fh "$version\n";
139 }
140
141 # settings
142 my $prefix = File::Spec->catdir( $option{destination}, $version );
143 my @make = ( make => "prefix=$prefix" );
144
145 # clean up environment (possibly set by local::lib)
146 local $ENV{PERL_MB_OPT};
147 local $ENV{PERL_MM_OPT};
148 remove_tree( $prefix ) if -e $prefix;
149
150 # build
151 run_cmd( @make => '-j3' );
152
153 # install
154 run_cmd( @make => 'install' );
155 run_cmd( @make => 'install-doc' ) if $option{docs};
156
157 # test the installation and remove all
158 if ( $option{test} ) {
159 ok( is_installed($version), "$version installed successfully" );
160 remove_tree( $prefix );
161 }
162 }
163 }
164
165 sub run_cmd {
166 print "* @_\n" if !$option{quiet};
167 System::Command->loop_on( command => \@_, %run_opt ) or die "FAIL: @_\n";
168 }
169
170 sub is_installed {
171 my ($version) = @_;
172 my $git =
173 File::Spec->catfile( $option{destination}, $version, 'bin', 'git' );
174 return eval { Git::Repository->version_eq( $version, { git => $git } ) };
175 }
176
177 __END__
178
179 =pod
180
181 =head1 NAME
182
183 build-git - Build and install any Git
184
185 =head1 SYNOPSIS
186
187 # clone git.git
188
189 # build and install Git 1.7.2
190 $ build-git 1.7.2
191
192 # build and install all versions between 1.6.5 and 2.1.0
193 $ build-git --since 1.6.5 --until 2.1.0
194
195 # build and install all versions of Git (since 1.0.0)
196 $ build-git
197
198 # build and install the 5 most recent versions of the selection
199 $ build-git --limit 5 ...
200
201 # build and install the 5 most ancient versions of the selection
202 $ build-git --limit -5 ...
203
204 # fetch the latest commit and install the latest git
205 $ build-git --fetch --limit 1
206
207 =head1 OPTIONS AND ARGUMENTS
208
209 =head2 Options
210
211 --source <directory> The location of the git.git clone checkout
212
213 --destination <directory> The location of the Git collection
214
215 --fetch Start by doing a `git fetch`
216
217 --list List the selected versions and exit
218
219 --test Compile, install, test and uninstall
220 the selected versions. Outputs TAP.
221 (Implies --quiet and force --destination to
222 a temporary directory.)
223
224 --since <version> Select versions greater or equal to <version>
225
226 --until <version> Select versions less or equal to <version>
227
228 --missing Select only non-installed versions
229
230 --installed Select only installed versions
231 (incompatible with the --missing option)
232
233 --limit <count> Limit the number of versions in the selection
234 (if <count> is positive, keep the most recent
235 ones, if <count> is negative, keep the oldest)
236
237 --verbose Used once, shows the STDERR of the commands
238 run to compile and install Git.
239 Used twice, also shows the STDOUT.
240
241 --quiet Silence all output, including the progress status
242
243 =head2 Arguments
244
245 If no argument is given, all versions are selected.
246
247 =head1 DESCRIPTION
248
249 B<build-git> is a small utility to build and install any version of Git.
250
251 It automatically applies some necessary patches that are needed to compile
252 Git on recent systems.
253
254 It is used to test the L<Git::Repository> module against all versions
255 of Git.
256
257 =head1 AUTHOR
258
259 Philippe Bruhat (BooK) <[email protected]>
260
261 =head1 COPYRIGHT
262
263 Copyright 2016 Philippe Bruhat (BooK), all rights reserved.
264
265 =head1 LICENSE
266
267 This program is free software; you can redistribute it and/or modify it
268 under the same terms as Perl itself.
269
270 =cut