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/>.
34 use Dpkg::Control::Hash;
35 use Debian::Dgit::ExitStatus;
36 use Debian::Dgit::I18n;
40 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
44 @EXPORT = qw(setup_sigwarn forkcheck_setup forkcheck_mainprocess
46 debiantags debiantag_old debiantag_new
49 stripepoch source_file_leafname is_orig_file_of_p_v
50 server_branch server_ref
51 stat_exists link_ltarget
53 fail failmsg ensuredir must_getcwd executable_on_path
54 waitstatusmsg failedcmd_waitstatus
55 failedcmd_report_cmd failedcmd
56 runcmd shell_cmd cmdoutput cmdoutput_errok
57 git_rev_parse git_cat_file
58 git_get_ref git_get_symref git_for_each_ref
59 git_for_each_tag_referring is_fast_fwd
61 git_reflog_action_msg git_update_ref_cmd
63 reflog_cache_insert reflog_cache_lookup
64 $package_re $component_re $deliberately_re
65 $distro_re $versiontag_re $series_filename_re
66 $orig_f_comp_re $orig_f_sig_re $orig_f_tail_re
67 $extra_orig_namepart_re
70 $ffq_refprefix $gdrlast_refprefix
71 initdebug enabledebug enabledebuglevel
73 $printdebug_when_debuglevel $debugcmd_when_debuglevel
74 $debugprefix *debuglevel *DEBUG
75 shellquote printcmd messagequote
76 $negate_harmful_gitattrs
77 changedir git_slurp_config_src
78 gdr_ffq_prev_branchinfo
79 parsecontrolfh parsecontrol parsechangelog
80 getfield parsechangelog_loop
82 # implicitly uses $main::us
83 %EXPORT_TAGS = ( policyflags => [qw(NOFFCHECK FRESHREPO NOCOMMITCHECK)],
84 playground => [qw(record_maindir $maindir $local_git_cfg
85 $maindir_gitdir $maindir_gitcommon
87 ensure_a_playground)]);
88 @EXPORT_OK = ( @{ $EXPORT_TAGS{policyflags} },
89 @{ $EXPORT_TAGS{playground} } );
94 our $package_re = '[0-9a-z][-+.0-9a-z]*';
95 our $component_re = '[0-9a-zA-Z][-+.0-9a-zA-Z]*';
96 our $deliberately_re = "(?:TEST-)?$package_re";
97 our $distro_re = $component_re;
98 our $versiontag_re = qr{[-+.\%_0-9a-zA-Z/]+};
99 our $branchprefix = 'dgit';
100 our $series_filename_re = qr{(?:^|\.)series(?!\n)$}s;
101 our $extra_orig_namepart_re = qr{[-0-9a-z]+};
102 our $orig_f_comp_re = qr{orig(?:-$extra_orig_namepart_re)?};
103 our $orig_f_sig_re = '\\.(?:asc|gpg|pgp)';
104 our $orig_f_tail_re = "$orig_f_comp_re\\.tar(?:\\.\\w+)?(?:$orig_f_sig_re)?";
105 our $git_null_obj = '0' x 40;
106 our $ffq_refprefix = 'ffq-prev';
107 our $gdrlast_refprefix = 'debrebase-last';
108 our $printdebug_when_debuglevel = 1;
109 our $debugcmd_when_debuglevel = 1;
111 # these three all go together, only valid after record_maindir
114 our $maindir_gitcommon;
116 # policy hook exit status bits
117 # see dgit-repos-server head comment for documentation
118 # 1 is reserved in case something fails with `exit 1' and to spot
119 # dynamic loader, runtime, etc., failures, which report 127 or 255
120 sub NOFFCHECK () { return 0x2; }
121 sub FRESHREPO () { return 0x4; }
122 sub NOCOMMITCHECK () { return 0x8; }
127 our $negate_harmful_gitattrs =
128 "-text -eol -crlf -ident -filter -working-tree-encoding";
129 # ^ when updating this, alter the regexp in dgit:is_gitattrs_setup
131 our $forkcheck_mainprocess;
133 sub forkcheck_setup () {
134 $forkcheck_mainprocess = $$;
137 sub forkcheck_mainprocess () {
138 # You must have called forkcheck_setup or setup_sigwarn already
139 getppid != $forkcheck_mainprocess;
142 sub setup_sigwarn () {
144 $SIG{__WARN__} = sub {
145 confess $_[0] if forkcheck_mainprocess;
151 open DEBUG, ">/dev/null" or confess $!;
155 open DEBUG, ">&STDERR" or confess $!;
160 sub enabledebuglevel ($) {
161 my ($newlevel) = @_; # may be undef (eg from env var)
162 confess if $debuglevel;
165 return unless $newlevel;
166 $debuglevel = $newlevel;
171 # Prints a prefix, and @_, to DEBUG. @_ should normally contain
174 # With no (or only empty) arguments just prints the prefix and
175 # leaves the caller to do more with DEBUG. The caller should make
176 # sure then to call printdebug with something ending in "\n" to
177 # get the prefix right in subsequent calls.
179 return unless $debuglevel >= $printdebug_when_debuglevel;
180 our $printdebug_noprefix;
181 print DEBUG $debugprefix unless $printdebug_noprefix;
182 pop @_ while @_ and !length $_[-1];
184 print DEBUG @_ or confess $!;
185 $printdebug_noprefix = $_[-1] !~ m{\n$};
188 sub messagequote ($) {
194 s{[\000-\037\177]}{ sprintf "\\x%02x", ord $& }ge;
201 defined or confess __ 'internal error' foreach @_;
204 if (!length || m{[^-=_./:0-9a-z]}i) {
211 return join ' ', @out;
216 my $intro = shift @_;
217 print $fh $intro," " or confess $!;
218 print $fh shellquote @_ or confess $!;
219 print $fh "\n" or confess $!;
223 my $extraprefix = shift @_;
224 printcmd(\*DEBUG,$debugprefix.$extraprefix,@_)
225 if $debuglevel >= $debugcmd_when_debuglevel;
228 sub dep14_version_mangle ($) {
230 # DEP-14 patch proposed 2016-11-09 "Version Mangling"
232 $v =~ s/\.(?=\.|$|lock$)/.#/g;
236 sub debiantag_old ($$) {
237 my ($v,$distro) = @_;
238 return "$distro/". dep14_version_mangle $v;
241 sub debiantag_new ($$) {
242 my ($v,$distro) = @_;
243 return "archive/$distro/".dep14_version_mangle $v;
246 sub debiantag_maintview ($$) {
247 my ($v,$distro) = @_;
248 return "$distro/".dep14_version_mangle $v;
251 sub debiantags ($$) {
252 my ($version,$distro) = @_;
253 map { $_->($version, $distro) } (\&debiantag_new, \&debiantag_old);
262 sub upstreamversion ($) {
268 sub source_file_leafname ($$$) {
269 my ($package,$vsn,$sfx) = @_;
270 return "${package}_".(stripepoch $vsn).$sfx
273 sub is_orig_file_of_p_v ($$$) {
274 my ($f, $package, $upstreamvsn) = @_;
275 my $base = source_file_leafname $package, $upstreamvsn, '';
276 return 0 unless $f =~ m/^\Q$base\E\.$orig_f_tail_re$/;
280 sub server_branch ($) { return "$branchprefix/$_[0]"; }
281 sub server_ref ($) { return "refs/".server_branch($_[0]); }
283 sub stat_exists ($) {
286 return 0 if $!==&ENOENT;
287 confess "stat $f: $!";
291 $::us // ($0 =~ m#[^/]*$#, $&);
295 my $s = f_ "error: %s\n", "@_";
297 my $prefix = _us().": ";
298 $s =~ s/^/$prefix/gm;
307 my ($dir) = @_; # does not create parents
308 return if mkdir $dir;
309 return if $! == EEXIST;
310 confess "mkdir $dir: $!";
315 defined $d or fail f_ "getcwd failed: %s\n", $!;
319 sub executable_on_path ($) {
321 return 1 if $program =~ m{/};
322 my @path = split /:/, ($ENV{PATH} // "/usr/local/bin:/bin:/usr/bin");
323 foreach my $pe (@path) {
324 my $here = "$pe/$program";
325 return $here if stat_exists $here && -x _;
330 our @signames = split / /, $Config{sig_name};
332 sub waitstatusmsg () {
334 return __ "terminated, reporting successful completion";
335 } elsif (!($? & 255)) {
336 return f_ "failed with error exit status %s", WEXITSTATUS($?);
337 } elsif (WIFSIGNALED($?)) {
338 my $signum=WTERMSIG($?);
339 return f_ "died due to fatal signal %s",
340 ($signames[$signum] // "number $signum").
341 ($? & 128 ? " (core dumped)" : ""); # POSIX(3pm) has no WCOREDUMP
343 return f_ "failed with unknown wait status %s", $?;
347 sub failedcmd_report_cmd {
348 my $intro = shift @_;
349 $intro //= __ "failed command";
350 { local ($!); printcmd \*STDERR, _us().": $intro:", @_ or confess $!; };
353 sub failedcmd_waitstatus {
355 return f_ "failed to fork/exec: %s", $!;
357 return f_ "subprocess %s", waitstatusmsg();
359 return __ "subprocess produced invalid output";
364 # Expects $!,$? as set by close - see below.
365 # To use with system(), set $?=-1 first.
367 # Actual behaviour of perl operations:
368 # success $!==0 $?==0 close of piped open
369 # program failed $!==0 $? >0 close of piped open
370 # syscall failure $! >0 $?=-1 close of piped open
371 # failure $! >0 unchanged close of something else
372 # success trashed $?==0 system
373 # program failed trashed $? >0 system
374 # syscall failure $! >0 unchanged system
375 failedcmd_report_cmd undef, @_;
376 fail failedcmd_waitstatus();
382 failedcmd @_ if system @_;
386 my ($first_shell, @cmd) = @_;
387 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
390 sub cmdoutput_errok {
391 confess Dumper(\@_)." ?" if grep { !defined } @_;
392 local $printdebug_when_debuglevel = $debugcmd_when_debuglevel;
394 open P, "-|", @_ or confess "$_[0] $!";
397 { local $/ = undef; $d = <P>; }
398 confess $! if P->error;
399 if (!close P) { printdebug "=>!$?\n"; return undef; }
401 if ($debuglevel > 0) {
404 my $more = (length $' ? '...' : ''); #');
405 $dd =~ s{[^\n -~]|\\}{ sprintf "\\x%02x", ord $& }ge;
406 printdebug "=> \`$dd'",$more,"\n";
412 my $d = cmdoutput_errok @_;
413 defined $d or failedcmd @_;
417 sub link_ltarget ($$) {
419 lstat $old or return undef;
421 $old = cmdoutput qw(realpath --), $old;
423 my $r = link $old, $new;
424 $r = symlink $old, $new if !$r && $!==EXDEV;
425 $r or fail "(sym)link $old $new: $!\n";
430 my $h = Digest::SHA->new(256);
432 return $h->hexdigest();
435 sub git_rev_parse ($) {
436 return cmdoutput qw(git rev-parse), "$_[0]~0";
439 sub git_cat_file ($;$) {
440 my ($objname, $etype) = @_;
441 # => ($type, $data) or ('missing', undef)
442 # in scalar context, just the data
443 # if $etype defined, dies unless type is $etype or in @$etype
444 our ($gcf_pid, $gcf_i, $gcf_o);
445 local $printdebug_when_debuglevel = $debugcmd_when_debuglevel;
447 my ($gtype, $data) = @_;
449 $etype = [$etype] unless ref $etype;
450 confess "$objname expected @$etype but is $gtype"
451 unless grep { $gtype eq $_ } @$etype;
453 return ($gtype, $data);
456 my @cmd = qw(git cat-file --batch);
457 debugcmd "GCF|", @cmd;
458 $gcf_pid = open2 $gcf_o, $gcf_i, @cmd or confess $!;
460 printdebug "GCF>| ", $objname, "\n";
461 print $gcf_i $objname, "\n" or confess $!;
463 printdebug "GCF<| ", $x;
464 if ($x =~ m/ (missing)$/) { return $chk->($1, undef); }
465 my ($type, $size) = $x =~ m/^.* (\w+) (\d+)\n/ or confess "$objname ?";
467 (read $gcf_o, $data, $size) == $size or confess "$objname $!";
469 $x eq "\n" or confess "$objname ($_) $!";
470 return $chk->($type, $data);
473 sub git_get_symref (;$) {
474 my ($symref) = @_; $symref //= 'HEAD';
475 # => undef if not a symref, otherwise refs/...
476 my @cmd = (qw(git symbolic-ref -q HEAD));
477 my $branch = cmdoutput_errok @cmd;
478 if (!defined $branch) {
479 $?==256 or failedcmd @cmd;
486 sub git_for_each_ref ($$;$) {
487 my ($pattern,$func,$gitdir) = @_;
488 # calls $func->($objid,$objtype,$fullrefname,$reftail);
489 # $reftail is RHS of ref after refs/[^/]+/
490 # breaks if $pattern matches any ref `refs/blah' where blah has no `/'
491 # $pattern may be an array ref to mean multiple patterns
492 $pattern = [ $pattern ] unless ref $pattern;
493 my @cmd = (qw(git for-each-ref), @$pattern);
494 if (defined $gitdir) {
495 @cmd = ('sh','-ec','cd "$1"; shift; exec "$@"','x', $gitdir, @cmd);
497 open GFER, "-|", @cmd or confess $!;
500 chomp or confess "$_ ?";
501 printdebug "|> ", $_, "\n";
502 m#^(\w+)\s+(\w+)\s+(refs/[^/]+/(\S+))$# or confess "$_ ?";
503 $func->($1,$2,$3,$4);
505 $!=0; $?=0; close GFER or confess "$pattern $? $!";
508 sub git_get_ref ($) {
509 # => '' if no such ref
512 s{^refs/}{[r]efs/} or confess "$refname $_ ?";
513 return cmdoutput qw(git for-each-ref --format=%(objectname)), $_;
516 sub git_for_each_tag_referring ($$) {
517 my ($objreferring, $func) = @_;
518 # calls $func->($tagobjid,$refobjid,$fullrefname,$tagname);
519 printdebug "git_for_each_tag_referring ",
520 ($objreferring // 'UNDEF'),"\n";
521 git_for_each_ref('refs/tags', sub {
522 my ($tagobjid,$objtype,$fullrefname,$tagname) = @_;
523 return unless $objtype eq 'tag';
524 my $refobjid = git_rev_parse $tagobjid;
526 !defined $objreferring # caller wants them all
527 or $tagobjid eq $objreferring
528 or $refobjid eq $objreferring;
529 $func->($tagobjid,$refobjid,$fullrefname,$tagname);
533 sub git_check_unmodified () {
534 foreach my $cached (qw(0 1)) {
535 my @cmd = qw(git diff --quiet);
536 push @cmd, qw(--cached) if $cached;
539 $!=0; $?=-1; system @cmd;
544 ? __ "git index contains changes (does not match HEAD)"
545 : __ "working tree is dirty (does not match HEAD)";
552 sub is_fast_fwd ($$) {
553 my ($ancestor,$child) = @_;
554 my @cmd = (qw(git merge-base), $ancestor, $child);
555 my $mb = cmdoutput_errok @cmd;
557 return git_rev_parse($mb) eq git_rev_parse($ancestor);
559 $?==256 or failedcmd @cmd;
564 sub git_reflog_action_msg ($) {
566 my $rla = $ENV{GIT_REFLOG_ACTION};
567 $msg = "$rla: $msg" if length $rla;
571 sub git_update_ref_cmd {
572 # returns qw(git update-ref), qw(-m), @_
573 # except that message may be modified to honour GIT_REFLOG_ACTION
575 $msg = git_reflog_action_msg $msg;
576 return qw(git update-ref -m), $msg, @_;
581 printdebug "CD $newdir\n";
582 chdir $newdir or confess "chdir: $newdir: $!";
585 sub git_slurp_config_src ($) {
587 # returns $r such that $r->{KEY}[] = VALUE
588 my @cmd = (qw(git config -z --get-regexp), "--$src", qw(.*));
591 local ($debuglevel) = $debuglevel-2;
595 open GITS, "-|", @cmd or confess $!;
598 printdebug "=> ", (messagequote $_), "\n";
599 m/\n/ or confess "$_ ?";
600 push @{ $r->{$`} }, $'; #';
604 or ($!==0 && $?==256)
609 sub gdr_ffq_prev_branchinfo ($) {
611 # => ('status', "message", [$symref, $ffq_prev, $gdrlast])
613 # branch message is undef
614 # weird-symref } no $symref,
615 # notbranch } no $ffq_prev
616 return ('detached', __ 'detached HEAD') unless defined $symref;
617 return ('weird-symref', __ 'HEAD symref is not to refs/')
618 unless $symref =~ m{^refs/};
619 my $ffq_prev = "refs/$ffq_refprefix/$'";
620 my $gdrlast = "refs/$gdrlast_refprefix/$'";
621 printdebug "ffq_prev_branchinfo branch current $symref\n";
622 return ('branch', undef, $symref, $ffq_prev, $gdrlast);
625 sub parsecontrolfh ($$;$) {
626 my ($fh, $desc, $allowsigned) = @_;
627 our $dpkgcontrolhash_noissigned;
630 my %opts = ('name' => $desc);
631 $opts{allow_pgp}= $allowsigned || !$dpkgcontrolhash_noissigned;
632 $c = Dpkg::Control::Hash->new(%opts);
633 $c->parse($fh,$desc) or fail f_ "parsing of %s failed", $desc;
634 last if $allowsigned;
635 last if $dpkgcontrolhash_noissigned;
636 my $issigned= $c->get_option('is_pgp_signed');
637 if (!defined $issigned) {
638 $dpkgcontrolhash_noissigned= 1;
639 seek $fh, 0,0 or confess "seek $desc: $!";
640 } elsif ($issigned) {
642 "control file %s is (already) PGP-signed. ".
643 " Note that dgit push needs to modify the .dsc and then".
644 " do the signature itself",
654 my ($file, $desc, $allowsigned) = @_;
655 my $fh = new IO::Handle;
656 open $fh, '<', $file or fail f_ "open %s (%s): %s", $file, $desc, $!;
657 my $c = parsecontrolfh($fh,$desc,$allowsigned);
658 $fh->error and confess $!;
664 my $c = Dpkg::Control::Hash->new(name => 'parsed changelog');
665 my $p = new IO::Handle;
666 my @cmd = (qw(dpkg-parsechangelog), @_);
667 open $p, '-|', @cmd or confess $!;
669 $?=0; $!=0; close $p or failedcmd @cmd;
674 my ($dctrl,$field) = @_;
675 my $v = $dctrl->{$field};
676 return $v if defined $v;
677 fail f_ "missing field %s in %s", $field, $dctrl->get_option('name');
680 sub parsechangelog_loop ($$$) {
681 my ($clogcmd, $descbase, $fn) = @_;
682 # @$clogcmd is qw(dpkg-parsechangelog ...some...options...)
683 # calls $fn->($thisstanza, $desc);
684 debugcmd "|",@$clogcmd;
685 open CLOGS, "-|", @$clogcmd or confess $!;
687 my $stanzatext = do { local $/=""; <CLOGS>; };
688 printdebug "clogp stanza ".Dumper($stanzatext) if $debuglevel>1;
689 last if !defined $stanzatext;
691 my $desc = "$descbase, entry no.$.";
692 open my $stanzafh, "<", \$stanzatext or confess;
693 my $thisstanza = parsecontrolfh $stanzafh, $desc, 1;
695 $fn->($thisstanza, $desc);
697 confess $! if CLOGS->error;
698 close CLOGS or $?==SIGPIPE or failedcmd @$clogcmd;
701 sub make_commit_text ($) {
704 my @cmd = (qw(git hash-object -w -t commit --stdin));
706 print Dumper($text) if $debuglevel > 1;
707 my $child = open2($out, $in, @cmd) or confess $!;
710 print $in $text or confess $!;
711 close $in or confess $!;
713 $h =~ m/^\w+$/ or confess;
715 printdebug "=> $h\n";
718 waitpid $child, 0 == $child or confess "$child $!";
719 $? and failedcmd @cmd;
723 sub reflog_cache_insert ($$$) {
724 my ($ref, $cachekey, $value) = @_;
725 # you must call this in $maindir
726 # you must have called record_maindir
728 # When we no longer need to support squeeze, use --create-reflog
730 my $parent = $ref; $parent =~ s{/[^/]+$}{};
731 ensuredir "$maindir_gitcommon/logs/$parent";
732 my $makelogfh = new IO::File "$maindir_gitcommon/logs/$ref", '>>'
735 my $oldcache = git_get_ref $ref;
737 if ($oldcache eq $value) {
738 my $tree = cmdoutput qw(git rev-parse), "$value:";
739 # git update-ref doesn't always update, in this case. *sigh*
740 my $authline = (ucfirst _us()).
741 ' <'._us().'@example.com> 1000000000 +0000';
742 my $dummy = make_commit_text <<ENDU.(__ <<END);
749 Dummy commit - do not use
751 runcmd qw(git update-ref -m), _us()." - dummy", $ref, $dummy;
753 runcmd qw(git update-ref -m), $cachekey, $ref, $value;
756 sub reflog_cache_lookup ($$) {
757 my ($ref, $cachekey) = @_;
758 # you may call this in $maindir or in a playtree
759 # you must have called record_maindir
760 my @cmd = (qw(git log -g), '--pretty=format:%H %gs', $ref);
761 debugcmd "|(probably)",@cmd;
762 my $child = open GC, "-|"; defined $child or confess $!;
764 chdir $maindir or confess $!;
765 if (!stat "$maindir_gitcommon/logs/$ref") {
766 $! == ENOENT or confess $!;
767 printdebug ">(no reflog)\n";
770 exec @cmd; die f_ "exec %s: %s\n", $cmd[0], $!;
774 printdebug ">| ", $_, "\n" if $debuglevel > 1;
775 next unless m/^(\w+) (\S.*\S)$/ && $2 eq $cachekey;
779 confess $! if GC->error;
780 failedcmd unless close GC;
784 # ========== playground handling ==========
788 # $maindir user's git working tree
789 # playground area in .git/ where we can make files, unpack, etc. etc.
790 # playtree git working tree sharing object store with the user's
791 # inside playground, or identical to it
795 # $local_git_cfg hash of arrays of values: git config from $maindir
797 # expected calling pattern
802 # must be run in directory containing .git
803 # assigns to $maindir if not already set
804 # also calls git_slurp_config_src to record git config
805 # in $local_git_cfg, unless it's already set
807 # fresh_playground SUBDIR_PATH_COMPONENTS
808 # e.g fresh_playground 'dgit/unpack' ('.git/' is implied)
809 # default SUBDIR_PATH_COMPONENTS is playground_subdir
810 # calls record_maindir
811 # sets up a new playground (destroying any old one)
812 # returns playground pathname
813 # caller may call multiple times with different subdir paths
814 # createing different playgrounds
816 # ensure_a_playground SUBDIR_PATH_COMPONENTS
817 # like fresh_playground except:
818 # merely ensures the directory exists; does not delete an existing one
822 # changedir playground
825 # playtree_setup $local_git_cfg
826 # # ^ call in some (perhaps trivial) subdir of playground
830 # ----- maindir -----
834 sub record_maindir () {
835 if (!defined $maindir) {
836 $maindir = must_getcwd();
837 if (!stat "$maindir/.git") {
838 fail f_ "cannot stat %s/.git: %s", $maindir, $!;
841 # we fall back to this in case we have a pre-worktree
842 # git, which may not know git rev-parse --git-common-dir
843 $maindir_gitdir = "$maindir/.git";
844 $maindir_gitcommon = "$maindir/.git";
846 $maindir_gitdir = cmdoutput qw(git rev-parse --git-dir);
847 $maindir_gitcommon = cmdoutput qw(git rev-parse --git-common-dir);
850 $local_git_cfg //= git_slurp_config_src 'local';
853 # ----- playgrounds -----
855 sub ensure_a_playground_parent ($) {
858 $spc = "$maindir_gitdir/$spc";
859 my $parent = dirname $spc;
860 mkdir $parent or $!==EEXIST or fail f_
861 "failed to mkdir playground parent %s: %s", $parent, $!;
865 sub ensure_a_playground ($) {
867 $spc = ensure_a_playground_parent $spc;
868 mkdir $spc or $!==EEXIST or fail f_
869 "failed to mkdir a playground %s: %s", $spc, $!;
873 sub fresh_playground ($) {
875 $spc = ensure_a_playground_parent $spc;
877 mkdir $spc or fail f_
878 "failed to mkdir the playground %s: %s", $spc, $!;
882 # ----- playtrees -----
884 sub playtree_setup (;$) {
885 my ($t_local_git_cfg) = @_;
886 $t_local_git_cfg //= $local_git_cfg;
887 # for use in the playtree
888 # $maindir must be set, eg by calling record_maindir or fresh_playground
889 runcmd qw(git init -q);
890 runcmd qw(git config gc.auto 0);
891 foreach my $copy (qw(user.email user.name user.useConfigOnly
892 core.sharedRepository
893 core.compression core.looseCompression
894 core.bigFileThreshold core.fsyncObjectFiles)) {
895 my $v = $t_local_git_cfg->{$copy};
897 runcmd qw(git config), $copy, $_ foreach @$v;
899 # this is confusing: we have
900 # . playtree, not a worktree, has .git/, our cwd
901 # $maindir might be a worktree so
902 # $maindir_gitdir contains our main working "dgit", HEAD, etc.
903 # $maindir_gitcommon the shared stuff, including .objects
904 rmtree('.git/objects');
905 symlink "$maindir_gitcommon/objects",'.git/objects' or confess $!;
906 ensuredir '.git/info';
907 open GA, "> .git/info/attributes" or confess $!;
908 print GA "* $negate_harmful_gitattrs\n" or confess $!;
909 close GA or confess $!;