-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild-test-repo
executable file
·488 lines (403 loc) · 13.2 KB
/
build-test-repo
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#!/usr/bin/env perl
use strict;
use warnings;
use Cwd;
use File::Spec;
use File::Basename;
use System::Command;
use Git::Repository;
use Test::Git;
use Getopt::Long;
use Pod::Usage;
use Encode;
use utf8;
# command-line options
my %option = (
remote => 'git@github.com:book/git-test-repository.git',
);
GetOptions(
\%option, 'verbose', 'readme', 'gitk', 'bundle=s', 'force',
'remote=s', 'push', 'help', 'manual'
) or pod2usage( -verbose => 1, -exitval => 2, );
# simple on-line help
pod2usage( -verbose => 1 ) if $option{help};
pod2usage( -verbose => 2 ) if $option{manual};
$option{bundle} &&= File::Spec->rel2abs( $option{bundle} );
# check if we have a recent enough git
my $version = Git::Repository->version;
die "git version 1.7.8 need for commit -S, this is only $version\n"
if Git::Repository->version_lt('1.7.8');
# check if gpg is available
my $gpg = eval { System::Command->new(qw( gpg --version ))->stdout->getline }
or die "gpg not found in the path\n";
# setup our local GnuPG environment
$ENV{LC_ALL} = 'C';
$ENV{GNUPGHOME}
= File::Spec->rel2abs( File::Spec->catdir( dirname($0), 'gnupg' ) );
my $keyid = '7621C403';
# setup a test repository
my $r = test_repository( git => { quiet => 1 } );
# some settings
$r->run( config => qw( i18n.commitencoding utf-8 ) );
$ENV{GIT_AUTHOR_DATE} = time - 3600;
$ENV{GIT_AUTHOR_NAME} = 'Example Author';
$ENV{GIT_AUTHOR_EMAIL} = 'author@example.com';
$ENV{GIT_COMMITTER_DATE} = time;
$ENV{GIT_COMMITTER_NAME} = 'Example Committer';
$ENV{GIT_COMMITTER_EMAIL} = 'committer@example.com';
### helper subs
# put content in a file, with an optional encoding
my $file = File::Spec->catfile( $r->work_tree => 'file.txt' );
sub update_file {
my ( $content, $encoding ) = @_;
$encoding ||= 'UTF-8';
open my $fh, ">:encoding($encoding)", $file or die "Can't open $file: $!";
print {$fh} $content;
close $fh;
$r->run( add => $file );
}
# do the bookkeeping around the object creation
my %seen;
# create an object in the repository, with some extra information
#
# arguments:
# - tag: the tag name, if the object is a tag
# - content: the content of $file
# - content_encoding: the encoding of $file content
# - commit_encoding: the encoding the commit message
# - code: a coderef that will generate the actual object
sub create_object {
my %arg = @_;
# update the file if needed
if ( $arg{content} ) {
update_file( $arg{content}, $arg{content_encoding} );
}
# click a tick
$ENV{GIT_AUTHOR_DATE}++;
# update the environment
local %ENV = %ENV;
$ENV{$_} = $arg{$_} for grep /GIT_/, keys %arg;
# execute the coderef
$r->run( config => 'i18n.commitencoding', $arg{commit_encoding} )
if $arg{commit_encoding};
$arg{code}->();
$r->run( qw( config --unset i18n.commitencoding ), $arg{commit_encoding} )
if $arg{commit_encoding};
# record the description
my $head
= $arg{tag}
? ( split / /, $r->run( 'show-ref', $arg{tag} ) )[0]
: $r->run( log => -1 => '--pretty=format:%H' );
return if exists $seen{$head};
$seen{ $arg{tag} || $head } = $arg{comment};
# show the raw commit for --verbose
return if !$option{verbose};
print "---- $head ", '-' x 32, "\n";
print "$_\n"
for $r->run( 'cat-file', $arg{tag} ? 'tag' : 'commit', $head );
print "\n";
}
### main program
create_object(
GIT_AUTHOR_NAME => ' ',
comment => 'no author name',
content => "Hello\n", # e965047ad7c57865823c7d992b1d046ea66edf78
code => sub {
$r->run( commit => -m => 'hello (en)' );
},
);
create_object(
GIT_AUTHOR_NAME => 'André', # utf-8
comment => 'utf-8 author name, utf-8 message, utf-8 content',
content => "Hello Áńdŕé\n",
code => sub {
$r->run( commit => -m => 'héllò' );
},
);
create_object(
comment => 'empty tree, parentless',
code => sub {
my $tree = $r->run( mktree => { input => '' } );
my $head = $r->run( 'commit-tree', $tree, -m => 'empty tree' );
$r->run( 'update-ref', 'refs/heads/slave' => $head );
$r->run( checkout => 'slave' );
}
);
create_object(
comment => 'tag pointing to a tree (the empty tree)',
tag => 'empty',
code => sub {
$r->run(
tag => -m => 'empty tree',
empty => '4b825dc642cb6eb9a060e54bf8d69288fbee4904'
);
}
);
create_object(
comment => 'tag pointing to a blob',
tag => 'hello',
code => sub {
$r->run(
tag => -m => '"Hello\\n" blob',
hello => 'e965047ad7c57865823c7d992b1d046ea66edf78'
);
}
);
$r->run( checkout => 'master' );
create_object(
comment => 'basic merge',
code => sub {
my @opts;
push @opts, '--allow-unrelated-histories' if $r->version_ge('2.9.0');
$r->run( merge => -s => ours => @opts, 'slave' );
}
);
# make an encoding branch
$r->run( branch => 'encoding' );
$r->run( branch => 'mergetags' );
create_object(
comment => 'utf-8 content, utf-8 message',
content => "こんにちは\n",
code => sub { $r->run( commit => -am => 'こんにちは (ja)' ); },
);
create_object(
comment => 'gpgsig',
content => "hej\n",
code => sub {
$r->run( commit => "-S$keyid" => -m => 'hej (da)' );
}
);
# now work in the encoding branch
$r->run( checkout => 'encoding' );
create_object(
comment => 'utf-8 content, iso-8859-1 message',
content => "góður dagur\n", # utf-8
commit_encoding => 'iso-8859-1',
code => sub {
my $cmd = $r->command( commit => '-aF-' );
print { $cmd->stdin } encode( 'iso-8859-1', 'góður dagur (is)' );
$cmd->close;
}
);
create_object(
GIT_AUTHOR_NAME => encode( 'iso-8859-1', 'André' ), # latin-1
comment => 'utf-8 content, iso-8859-1 message and author name',
content => "halló\n", # utf-8
commit_encoding => 'iso-8859-1',
code => sub {
my $cmd = $r->command( commit => '-aF-' );
print { $cmd->stdin } encode( 'iso-8859-1', 'halló (is)' );
$cmd->close;
}
);
create_object(
tag => 'v1.0.0',
comment => 'gpgsig',
code => sub {
$r->run( tag => -m => 'signed tag', -u => $keyid, -s => 'v1.0.0' );
}
);
create_object(
comment => 'shift-jis content, shift-jis message',
content => "こんにちは\n",
content_encoding => 'shift-jis',
commit_encoding => 'shift-jis',
code => sub {
my $cmd = $r->command( commit => '-aF-' );
print { $cmd->stdin } encode( 'shift-jis', 'こんにちは (ja)' );
$cmd->close;
}
);
create_object(
tag => 'v1.0.1',
comment => 'utf-8 message, gpgsig',
code => sub {
$r->run(
tag => -m => 'こんにちは',
-u => $keyid,
-s => 'v1.0.1'
);
}
);
create_object(
comment => 'utf-8 content, iso-8859-1 message, gpgsig',
content => 'olá',
content_encoding => 'iso-8859-1',
commit_encoding => 'iso-8859-1',
code => sub {
my $cmd = $r->command( commit => '-aF-' => "-S$keyid" );
print { $cmd->stdin } encode( 'iso-8859-1', 'olá (pt)' );
$cmd->close;
}
);
create_object(
tag => 'v1.0.2',
comment => 'gpgsig',
code => sub {
$r->run( tag => -m => 'signed tag', -u => $keyid, -s => 'v1.0.2' );
}
);
create_object(
comment => 'commit with empty message',
content => "bonjour\n",
code => sub {
my $tree = $r->run('write-tree');
my $parent = $r->run( log => -1 => '--pretty=format:%H' );
my $head
= $r->run( 'commit-tree', $tree, -p => $parent, { input => '' } );
$r->run( 'update-ref', 'refs/heads/encoding' => $head );
}
);
create_object(
tag => 'v1.0.3',
comment => 'gpgsig',
code => sub {
$r->run( tag => -m => 'signed tag', -u => $keyid, -s => 'v1.0.3' );
}
);
$r->run( checkout => 'master' );
create_object(
comment => 'mergetag',
code => sub { $r->run( merge => -s => 'ours', 'v1.0.0' ); }
);
create_object(
tag => 'v1.1.0',
comment => 'gpgsig',
code => sub {
$r->run( tag => -m => 'signed tag', -u => $keyid, -s => 'v1.1.0' );
}
);
create_object(
comment => 'iso-8859-1 message, mergetag',
commit_encoding => 'iso-8859-1',
code => sub {
$r->run( merge => -s => 'ours', '--no-commit', 'v1.0.1' );
my $cmd = $r->command( commit => '-F-' );
print { $cmd->stdin } encode( 'iso-8859-1', 'dobrý den (cs)' );
$cmd->close;
}
);
create_object(
comment => 'mergetag, gpgsig',
code => sub {
$r->run( merge => -s => 'ours', '--no-commit', 'v1.0.2' );
$r->run( commit => -am => 'bom dia', "-S$keyid" );
}
);
create_object(
comment => 'iso-8859-1 message, gpgsig, mergetag',
commit_encoding => 'iso-8859-1',
code => sub {
$r->run( merge => -s => 'ours', '--no-commit', 'v1.0.3' );
my $cmd = $r->command( commit => '-aF-' => "-S$keyid" );
print { $cmd->stdin } encode( 'iso-8859-1', 'buen día (es)' );
$cmd->close;
}
);
$r->run( checkout => 'mergetags' );
create_object(
comment => 'mergetag, mergetag',
code => sub { $r->run( merge => -s => 'ours', 'v1.1.0', 'v1.0.1' ); }
);
$r->run( checkout => 'slave' );
create_object(
comment => 'mergetag, mergetag, gpgsig',
code => sub {
$r->run( merge => -s => 'ours', '--no-commit', 'v1.1.0', 'v1.0.2' );
$r->run( commit => '-am' => 'buon giorno (it)' => "-S$keyid" );
}
);
### final steps
$r->run( checkout => 'master' );
# README
{
my @readme
= $r->run( log => qw( --graph --pretty=oneline --decorate --all ) );
open my $fh, '>', File::Spec->catfile( $r->work_tree, 'README.md' );
print {$fh} << 'README';
git-test-repository
===================
This repository contains all the "special cases" of objects that I
know of.
The (Perl) program used to build it can be found at:
http://github.com/book/git-test-repo-tool/
The test repository itself is entirely regenerated whenever I update the
builder script. If you use it in your tests, you should generate a bundle
with the `build-test-repo` program found in the `git-test-repo-tool`
repository and use that.
Here's an ascii-art view of the annotated commit graph,
with the details related to each commit:
README
print $fh " $_\n"
for map { s/(([a-f0-9]{40})(?: \([^)]+\))?) .*/$1 $seen{$2}/g; $_ }
@readme;
print {$fh} << 'README';
Here's the list of the references in the repository,
with the details related to each of the annotated tags:
README
print $fh " $_\n"
for map { s:refs/tags/(.*):refs/tags/$1 $seen{$1}:; $_ }
$r->run(qw( show-ref ));
print $fh "\n";
close $fh;
$r->run( add => 'README.md' );
$r->run( commit => -am => 'add the README' );
if ( $option{readme} ) {
open $fh, '<', File::Spec->catfile( $r->work_tree, 'README.md' );
print <$fh>;
close $fh;
}
}
# bundle
if ( $option{bundle} ) {
if ( !-e $option{bundle} || $option{force} ) {
$r->run( bundle => create => $option{bundle} => '--all' );
}
else {
die "File $option{bundle} already exists, use --force to overwrite\n";
}
}
# push to remote
if ( $option{push} && $option{remote} ) {
$r->run( remote => add => origin => $option{remote} );
$r->run( push => qw( --all --force origin ) );
}
# gitk
if ( $option{gitk} ) {
my $home = getcwd();
chdir $r->work_tree;
`gitk --all`;
chdir $home;
}
__END__
=head1 NAME
build-test-repo - Create a Git repository full of special cases
=head1 SYNOPSIS
build-test-repo [options] [arguments]
=head1 OPTIONS AND ARGUMENTS
In typical L<Getopt::Long> fashion, all options can be abbreviated
as long as the shorter version is unambiguous.
=head2 Options
--verbose Print the content of each commit and tag created
--readme Print the content of the README file
--gitk Open `gitk --all --date-order` on the repository
--bundle <file> Save a bundle for the repository in <file>
--force Overwrite the bundle file if it already exists
--remote <url> Define the remote repository URL
--push Push the content of the repository to the remote
--help Print a short help summary and exit
--manual Print the full manual page and exit
=head1 DESCRIPTION
B<build-test-repo> generates a Git repository with a number of rare cases.
The program main output is a small Git bundle (currently less than 10 K)
that can be used in a test setup.
See the F<README.md> file contained in the generated repository for
details about each commit it contains.
=head1 AUTHOR
Philippe Bruhat (BooK), C<< <book@cpan.org> >>.
=head1 COPYRIGHT
Copyright 2013 Philippe Bruhat (BooK), all rights reserved.
=head1 LICENSE
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut