3 # Debian::Dgit: functions common to dgit and its helpers and servers
5 # Copyright (C) 2015-2016 Ian Jackson
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
37 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
41 @EXPORT = qw(setup_sigwarn forkcheck_setup forkcheck_mainprocess
43 debiantags debiantag_old debiantag_new
44 server_branch server_ref
45 stat_exists link_ltarget
47 fail failmsg ensuredir must_getcwd executable_on_path
48 waitstatusmsg failedcmd_waitstatus
49 failedcmd_report_cmd failedcmd
50 runcmd cmdoutput cmdoutput_errok
51 git_rev_parse git_cat_file
52 git_get_ref git_get_symref git_for_each_ref
53 git_for_each_tag_referring is_fast_fwd
55 $package_re $component_re $deliberately_re
56 $distro_re $versiontag_re $series_filename_re
57 $extra_orig_namepart_re
60 $ffq_refprefix $gdrlast_refprefix
61 initdebug enabledebug enabledebuglevel
63 $debugprefix *debuglevel *DEBUG
64 shellquote printcmd messagequote
65 $negate_harmful_gitattrs
66 changedir git_slurp_config_src
68 # implicitly uses $main::us
69 %EXPORT_TAGS = ( policyflags => [qw(NOFFCHECK FRESHREPO NOCOMMITCHECK)],
70 playground => [qw(record_maindir $maindir $local_git_cfg
71 $maindir_gitdir $maindir_gitcommon
73 ensure_a_playground)]);
74 @EXPORT_OK = ( @{ $EXPORT_TAGS{policyflags} },
75 @{ $EXPORT_TAGS{playground} } );
80 our $package_re = '[0-9a-z][-+.0-9a-z]*';
81 our $component_re = '[0-9a-zA-Z][-+.0-9a-zA-Z]*';
82 our $deliberately_re = "(?:TEST-)?$package_re";
83 our $distro_re = $component_re;
84 our $versiontag_re = qr{[-+.\%_0-9a-zA-Z/]+};
85 our $branchprefix = 'dgit';
86 our $series_filename_re = qr{(?:^|\.)series(?!\n)$}s;
87 our $extra_orig_namepart_re = qr{[-0-9a-z]+};
88 our $git_null_obj = '0' x 40;
89 our $ffq_refprefix = 'ffq-prev';
90 our $gdrlast_refprefix = 'debrebase-last';
92 # policy hook exit status bits
93 # see dgit-repos-server head comment for documentation
94 # 1 is reserved in case something fails with `exit 1' and to spot
95 # dynamic loader, runtime, etc., failures, which report 127 or 255
96 sub NOFFCHECK () { return 0x2; }
97 sub FRESHREPO () { return 0x4; }
98 sub NOCOMMITCHECK () { return 0x8; }
103 our $negate_harmful_gitattrs = "-text -eol -crlf -ident -filter";
105 our $forkcheck_mainprocess;
107 sub forkcheck_setup () {
108 $forkcheck_mainprocess = $$;
111 sub forkcheck_mainprocess () {
112 # You must have called forkcheck_setup or setup_sigwarn already
113 getppid != $forkcheck_mainprocess;
116 sub setup_sigwarn () {
118 $SIG{__WARN__} = sub {
119 confess $_[0] if forkcheck_mainprocess;
125 open DEBUG, ">/dev/null" or die $!;
129 open DEBUG, ">&STDERR" or die $!;
134 sub enabledebuglevel ($) {
135 my ($newlevel) = @_; # may be undef (eg from env var)
139 return unless $newlevel;
140 $debuglevel = $newlevel;
145 print DEBUG $debugprefix, @_ or die $! if $debuglevel>0;
148 sub messagequote ($) {
154 s{[\000-\037\177]}{ sprintf "\\x%02x", ord $& }ge;
161 defined or confess 'internal error' foreach @_;
164 if (!length || m{[^-=_./:0-9a-z]}i) {
171 return join ' ', @out;
176 my $intro = shift @_;
177 print $fh $intro," " or die $!;
178 print $fh shellquote @_ or die $!;
179 print $fh "\n" or die $!;
183 my $extraprefix = shift @_;
184 printcmd(\*DEBUG,$debugprefix.$extraprefix,@_) if $debuglevel>0;
187 sub dep14_version_mangle ($) {
189 # DEP-14 patch proposed 2016-11-09 "Version Mangling"
191 $v =~ s/\.(?=\.|$|lock$)/.#/g;
195 sub debiantag_old ($$) {
196 my ($v,$distro) = @_;
197 return "$distro/". dep14_version_mangle $v;
200 sub debiantag_new ($$) {
201 my ($v,$distro) = @_;
202 return "archive/$distro/".dep14_version_mangle $v;
205 sub debiantags ($$) {
206 my ($version,$distro) = @_;
207 map { $_->($version, $distro) } (\&debiantag_new, \&debiantag_old);
210 sub server_branch ($) { return "$branchprefix/$_[0]"; }
211 sub server_ref ($) { return "refs/".server_branch($_[0]); }
213 sub stat_exists ($) {
216 return 0 if $!==&ENOENT;
221 $::us // ($0 =~ m#[^/]*$#, $&);
227 my $prefix = _us().": ";
228 $s =~ s/^/$prefix/gm;
237 my ($dir) = @_; # does not create parents
238 return if mkdir $dir;
239 return if $! == EEXIST;
240 die "mkdir $dir: $!";
245 defined $d or fail "getcwd failed: $!";
249 sub executable_on_path ($) {
251 return 1 if $program =~ m{/};
252 my @path = split /:/, ($ENV{PATH} // "/usr/local/bin:/bin:/usr/bin");
253 foreach my $pe (@path) {
254 my $here = "$pe/$program";
255 return $here if stat_exists $here && -x _;
260 our @signames = split / /, $Config{sig_name};
262 sub waitstatusmsg () {
264 return "terminated, reporting successful completion";
265 } elsif (!($? & 255)) {
266 return "failed with error exit status ".WEXITSTATUS($?);
267 } elsif (WIFSIGNALED($?)) {
268 my $signum=WTERMSIG($?);
269 return "died due to fatal signal ".
270 ($signames[$signum] // "number $signum").
271 ($? & 128 ? " (core dumped)" : ""); # POSIX(3pm) has no WCOREDUMP
273 return "failed with unknown wait status ".$?;
277 sub failedcmd_report_cmd {
278 my $intro = shift @_;
279 $intro //= "failed command";
280 { local ($!); printcmd \*STDERR, _us().": $intro:", @_ or die $!; };
283 sub failedcmd_waitstatus {
285 return "failed to fork/exec: $!";
287 return "subprocess ".waitstatusmsg();
289 return "subprocess produced invalid output";
294 # Expects $!,$? as set by close - see below.
295 # To use with system(), set $?=-1 first.
297 # Actual behaviour of perl operations:
298 # success $!==0 $?==0 close of piped open
299 # program failed $!==0 $? >0 close of piped open
300 # syscall failure $! >0 $?=-1 close of piped open
301 # failure $! >0 unchanged close of something else
302 # success trashed $?==0 system
303 # program failed trashed $? >0 system
304 # syscall failure $! >0 unchanged system
305 failedcmd_report_cmd undef, @_;
306 fail failedcmd_waitstatus();
312 failedcmd @_ if system @_;
315 sub cmdoutput_errok {
316 confess Dumper(\@_)." ?" if grep { !defined } @_;
318 open P, "-|", @_ or die "$_[0] $!";
321 { local $/ = undef; $d = <P>; }
323 if (!close P) { printdebug "=>!$?\n"; return undef; }
325 if ($debuglevel > 0) {
328 my $more = (length $' ? '...' : ''); #');
329 $dd =~ s{[^\n -~]|\\}{ sprintf "\\x%02x", ord $& }ge;
330 printdebug "=> \`$dd'",$more,"\n";
336 my $d = cmdoutput_errok @_;
337 defined $d or failedcmd @_;
341 sub link_ltarget ($$) {
343 lstat $old or return undef;
345 $old = cmdoutput qw(realpath --), $old;
347 my $r = link $old, $new;
348 $r = symlink $old, $new if !$r && $!==EXDEV;
349 $r or die "(sym)link $old $new: $!";
354 my $h = Digest::SHA->new(256);
356 return $h->hexdigest();
359 sub git_rev_parse ($) {
360 return cmdoutput qw(git rev-parse), "$_[0]~0";
363 sub git_cat_file ($;$) {
364 my ($objname, $etype) = @_;
365 # => ($type, $data) or ('missing', undef)
366 # in scalar context, just the data
367 # if $etype defined, dies unless type is $etype or in @$etype
368 our ($gcf_pid, $gcf_i, $gcf_o);
370 my ($gtype, $data) = @_;
372 $etype = [$etype] unless ref $etype;
373 confess "$objname expected @$etype but is $gtype"
374 unless grep { $gtype eq $_ } @$etype;
376 return ($gtype, $data);
379 my @cmd = qw(git cat-file --batch);
380 debugcmd "GCF|", @cmd;
381 $gcf_pid = open2 $gcf_o, $gcf_i, @cmd or die $!;
383 printdebug "GCF>| ", $objname, "\n";
384 print $gcf_i $objname, "\n" or die $!;
386 printdebug "GCF<| ", $x;
387 if ($x =~ m/ (missing)$/) { return $chk->($1, undef); }
388 my ($type, $size) = $x =~ m/^.* (\w+) (\d+)\n/ or die "$objname ?";
390 (read $gcf_o, $data, $size) == $size or die "$objname $!";
392 $x eq "\n" or die "$objname ($_) $!";
393 return $chk->($type, $data);
396 sub git_get_symref (;$) {
397 my ($symref) = @_; $symref //= 'HEAD';
398 # => undef if not a symref, otherwise refs/...
399 my @cmd = (qw(git symbolic-ref -q HEAD));
400 my $branch = cmdoutput_errok @cmd;
401 if (!defined $branch) {
402 $?==256 or failedcmd @cmd;
409 sub git_for_each_ref ($$;$) {
410 my ($pattern,$func,$gitdir) = @_;
411 # calls $func->($objid,$objtype,$fullrefname,$reftail);
412 # $reftail is RHS of ref after refs/[^/]+/
413 # breaks if $pattern matches any ref `refs/blah' where blah has no `/'
414 # $pattern may be an array ref to mean multiple patterns
415 $pattern = [ $pattern ] unless ref $pattern;
416 my @cmd = (qw(git for-each-ref), @$pattern);
417 if (defined $gitdir) {
418 @cmd = ('sh','-ec','cd "$1"; shift; exec "$@"','x', $gitdir, @cmd);
420 open GFER, "-|", @cmd or die $!;
424 printdebug "|> ", $_, "\n";
425 m#^(\w+)\s+(\w+)\s+(refs/[^/]+/(\S+))$# or die "$_ ?";
426 $func->($1,$2,$3,$4);
428 $!=0; $?=0; close GFER or die "$pattern $? $!";
431 sub git_get_ref ($) {
432 # => '' if no such ref
435 s{^refs/}{[r]efs/} or die "$refname $_ ?";
436 return cmdoutput qw(git for-each-ref --format=%(objectname)), $_;
439 sub git_for_each_tag_referring ($$) {
440 my ($objreferring, $func) = @_;
441 # calls $func->($tagobjid,$refobjid,$fullrefname,$tagname);
442 printdebug "git_for_each_tag_referring ",
443 ($objreferring // 'UNDEF'),"\n";
444 git_for_each_ref('refs/tags', sub {
445 my ($tagobjid,$objtype,$fullrefname,$tagname) = @_;
446 return unless $objtype eq 'tag';
447 my $refobjid = git_rev_parse $tagobjid;
449 !defined $objreferring # caller wants them all
450 or $tagobjid eq $objreferring
451 or $refobjid eq $objreferring;
452 $func->($tagobjid,$refobjid,$fullrefname,$tagname);
456 sub git_check_unmodified () {
457 foreach my $cached (qw(0 1)) {
458 my @cmd = qw(git diff --quiet);
459 push @cmd, qw(--cached) if $cached;
462 $!=0; $?=-1; system @cmd;
467 ? "git index contains changes (does not match HEAD)"
468 : "working tree is dirty (does not match HEAD)";
475 sub is_fast_fwd ($$) {
476 my ($ancestor,$child) = @_;
477 my @cmd = (qw(git merge-base), $ancestor, $child);
478 my $mb = cmdoutput_errok @cmd;
480 return git_rev_parse($mb) eq git_rev_parse($ancestor);
482 $?==256 or failedcmd @cmd;
489 printdebug "CD $newdir\n";
490 chdir $newdir or confess "chdir: $newdir: $!";
493 sub git_slurp_config_src ($) {
495 # returns $r such that $r->{KEY}[] = VALUE
496 my @cmd = (qw(git config -z --get-regexp), "--$src", qw(.*));
499 local ($debuglevel) = $debuglevel-2;
503 open GITS, "-|", @cmd or die $!;
506 printdebug "=> ", (messagequote $_), "\n";
508 push @{ $r->{$`} }, $'; #';
512 or ($!==0 && $?==256)
517 # ========== playground handling ==========
521 # $maindir user's git working tree
522 # playground area in .git/ where we can make files, unpack, etc. etc.
523 # playtree git working tree sharing object store with the user's
524 # inside playground, or identical to it
528 # $local_git_cfg hash of arrays of values: git config from $maindir
530 # expected calling pattern
535 # must be run in directory containing .git
536 # assigns to $maindir if not already set
537 # also calls git_slurp_config_src to record git config
538 # in $local_git_cfg, unless it's already set
540 # fresh_playground SUBDIR_PATH_COMPONENTS
541 # e.g fresh_playground 'dgit/unpack' ('.git/' is implied)
542 # default SUBDIR_PATH_COMPONENTS is playground_subdir
543 # calls record_maindir
544 # sets up a new playground (destroying any old one)
545 # returns playground pathname
546 # caller may call multiple times with different subdir paths
547 # createing different playgrounds
549 # ensure_a_playground SUBDIR_PATH_COMPONENTS
550 # like fresh_playground except:
551 # merely ensures the directory exists; does not delete an existing one
555 # changedir playground
558 # playtree_setup $local_git_cfg
559 # # ^ call in some (perhaps trivial) subdir of playground
563 # ----- maindir -----
565 # these three all go together
568 our $maindir_gitcommon;
572 sub record_maindir () {
573 if (!defined $maindir) {
574 $maindir = must_getcwd();
575 if (!stat "$maindir/.git") {
576 fail "cannot stat $maindir/.git: $!";
579 # we fall back to this in case we have a pre-worktree
580 # git, which may not know git rev-parse --git-common-dir
581 $maindir_gitdir = "$maindir/.git";
582 $maindir_gitcommon = "$maindir/.git";
584 $maindir_gitdir = cmdoutput qw(git rev-parse --git-dir);
585 $maindir_gitcommon = cmdoutput qw(git rev-parse --git-common-dir);
588 $local_git_cfg //= git_slurp_config_src 'local';
591 # ----- playgrounds -----
593 sub ensure_a_playground_parent ($) {
596 $spc = "$maindir_gitdir/$spc";
597 my $parent = dirname $spc;
598 mkdir $parent or $!==EEXIST
599 or fail "failed to mkdir playground parent $parent: $!";
603 sub ensure_a_playground ($) {
605 $spc = ensure_a_playground_parent $spc;
606 mkdir $spc or $!==EEXIST or fail "failed to mkdir a playground $spc: $!";
610 sub fresh_playground ($) {
612 $spc = ensure_a_playground_parent $spc;
614 mkdir $spc or fail "failed to mkdir the playground $spc: $!";
618 # ----- playtrees -----
620 sub playtree_setup (;$) {
621 my ($t_local_git_cfg) = @_;
622 $t_local_git_cfg //= $local_git_cfg;
623 # for use in the playtree
624 # $maindir must be set, eg by calling record_maindir or fresh_playground
625 runcmd qw(git init -q);
626 runcmd qw(git config gc.auto 0);
627 foreach my $copy (qw(user.email user.name user.useConfigOnly
628 core.sharedRepository
629 core.compression core.looseCompression
630 core.bigFileThreshold core.fsyncObjectFiles)) {
631 my $v = $t_local_git_cfg->{$copy};
633 runcmd qw(git config), $copy, $_ foreach @$v;
635 # this is confusing: we have
636 # . playtree, not a worktree, has .git/, our cwd
637 # $maindir might be a worktree so
638 # $maindir_gitdir contains our main working "dgit", HEAD, etc.
639 # $maindir_gitcommon the shared stuff, including .objects
640 rmtree('.git/objects');
641 symlink "$maindir_gitcommon/objects",'.git/objects' or die $!;
642 ensuredir '.git/info';
643 open GA, "> .git/info/attributes" or die $!;
644 print GA "* $negate_harmful_gitattrs\n" or die $!;