3 # Debian::Dgit: functions common to dgit and its helpers and servers
5 # Copyright (C) 2015-2019 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/>.
32 use File::Path qw(:DEFAULT make_path);
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_new
49 upstream_commitish_search resolve_upstream_version
50 stripepoch source_file_leafname is_orig_file_of_p_v
51 server_branch server_ref
52 stat_exists link_ltarget rename_link_xf
54 fail failmsg ensuredir ensurepath
55 must_getcwd executable_on_path
56 waitstatusmsg failedcmd_waitstatus
57 failedcmd_report_cmd failedcmd
58 runcmd shell_cmd cmdoutput cmdoutput_errok
60 git_rev_parse changedir_git_toplevel git_cat_file
61 git_get_ref git_get_symref git_for_each_ref
62 git_for_each_tag_referring is_fast_fwd
64 git_reflog_action_msg git_update_ref_cmd
65 rm_subdir_cached read_tree_subdir
66 read_tree_debian read_tree_upstream
67 make_commit hash_commit hash_commit_text
68 reflog_cache_insert reflog_cache_lookup
69 $package_re $component_re $suite_re $deliberately_re
70 $distro_re $versiontag_re $series_filename_re
71 $orig_f_comp_re $orig_f_sig_re $orig_f_tail_re
72 $extra_orig_namepart_re
75 $ffq_refprefix $gdrlast_refprefix
76 initdebug enabledebug enabledebuglevel
78 $printdebug_when_debuglevel $debugcmd_when_debuglevel
79 $debugprefix *debuglevel *DEBUG
80 shellquote printcmd messagequote
81 $negate_harmful_gitattrs
82 changedir git_slurp_config_src
83 gdr_ffq_prev_branchinfo
84 parsecontrolfh parsecontrol parsechangelog
85 getfield parsechangelog_loop
87 # implicitly uses $main::us
88 %EXPORT_TAGS = ( policyflags => [qw(NOFFCHECK FRESHREPO NOCOMMITCHECK)],
89 playground => [qw(record_maindir $maindir $local_git_cfg
90 $maindir_gitdir $maindir_gitcommon
92 ensure_a_playground)]);
93 @EXPORT_OK = ( @{ $EXPORT_TAGS{policyflags} },
94 @{ $EXPORT_TAGS{playground} } );
99 our $package_re = '[0-9a-z][-+.0-9a-z]*';
100 our $component_re = '[0-9a-zA-Z][-+.0-9a-zA-Z]*';
101 our $suite_re = '[-+.0-9a-z]+';
102 our $deliberately_re = "(?:TEST-)?$package_re";
103 our $distro_re = $component_re;
104 our $versiontag_re = qr{[-+.\%_0-9a-zA-Z/]+};
105 our $branchprefix = 'dgit';
106 our $series_filename_re = qr{(?:^|\.)series(?!\n)$}s;
107 our $extra_orig_namepart_re = qr{[-0-9a-zA-Z]+};
108 our $orig_f_comp_re = qr{orig(?:-$extra_orig_namepart_re)?};
109 our $orig_f_sig_re = '\\.(?:asc|gpg|pgp)';
110 our $orig_f_tail_re = "$orig_f_comp_re\\.tar(?:\\.\\w+)?(?:$orig_f_sig_re)?";
111 our $git_null_obj = '0' x 40;
112 our $ffq_refprefix = 'ffq-prev';
113 our $gdrlast_refprefix = 'debrebase-last';
114 our $printdebug_when_debuglevel = 1;
115 our $debugcmd_when_debuglevel = 1;
117 our (@git) = qw(git);
119 # these three all go together, only valid after record_maindir
122 our $maindir_gitcommon;
124 # policy hook exit status bits
125 # see dgit-repos-server head comment for documentation
126 # 1 is reserved in case something fails with `exit 1' and to spot
127 # dynamic loader, runtime, etc., failures, which report 127 or 255
128 sub NOFFCHECK () { return 0x2; }
129 sub FRESHREPO () { return 0x4; }
130 sub NOCOMMITCHECK () { return 0x8; }
135 our $negate_harmful_gitattrs =
136 "-text -eol -crlf -ident -filter -working-tree-encoding";
137 # ^ when updating this, alter the regexp in dgit:is_gitattrs_setup
139 our $forkcheck_mainprocess;
141 sub forkcheck_setup () {
142 $forkcheck_mainprocess = $$;
145 sub forkcheck_mainprocess () {
146 # You must have called forkcheck_setup or setup_sigwarn already
147 getppid != $forkcheck_mainprocess;
150 sub setup_sigwarn () {
152 $SIG{__WARN__} = sub {
153 confess $_[0] if forkcheck_mainprocess;
159 open DEBUG, ">/dev/null" or confess "$!";
163 open DEBUG, ">&STDERR" or confess "$!";
168 sub enabledebuglevel ($) {
169 my ($newlevel) = @_; # may be undef (eg from env var)
170 confess if $debuglevel;
173 return unless $newlevel;
174 $debuglevel = $newlevel;
179 # Prints a prefix, and @_, to DEBUG. @_ should normally contain
182 # With no (or only empty) arguments just prints the prefix and
183 # leaves the caller to do more with DEBUG. The caller should make
184 # sure then to call printdebug with something ending in "\n" to
185 # get the prefix right in subsequent calls.
187 return unless $debuglevel >= $printdebug_when_debuglevel;
188 our $printdebug_noprefix;
189 print DEBUG $debugprefix unless $printdebug_noprefix;
190 pop @_ while @_ and !length $_[-1];
192 print DEBUG @_ or confess "$!";
193 $printdebug_noprefix = $_[-1] !~ m{\n$};
196 sub messagequote ($) {
202 s{[\000-\037\177]}{ sprintf "\\x%02x", ord $& }ge;
209 defined or confess __ 'internal error' foreach @_;
212 if (!length || m{[^-=_./:0-9a-z]}i) {
219 return join ' ', @out;
224 my $intro = shift @_;
225 print $fh $intro." ".(join '', shellquote @_)."\n" or confess "$!";
229 my $extraprefix = shift @_;
230 printcmd(\*DEBUG,$debugprefix.$extraprefix,@_)
231 if $debuglevel >= $debugcmd_when_debuglevel;
234 sub dep14_version_mangle ($) {
236 # DEP-14 patch proposed 2016-11-09 "Version Mangling"
238 $v =~ s/\.(?=\.|$|lock$)/.#/g;
242 sub debiantag_new ($$) {
243 my ($v,$distro) = @_;
244 return "archive/$distro/".dep14_version_mangle $v;
247 sub debiantag_maintview ($$) {
248 my ($v,$distro) = @_;
249 return "$distro/".dep14_version_mangle $v;
252 sub debiantags ($$) {
253 my ($version,$distro) = @_;
254 map { $_->($version, $distro) } (\&debiantag_new, \&debiantag_maintview);
263 sub upstreamversion ($) {
269 sub source_file_leafname ($$$) {
270 my ($package,$vsn,$sfx) = @_;
271 return "${package}_".(stripepoch $vsn).$sfx
274 sub is_orig_file_of_p_v ($$$) {
275 my ($f, $package, $upstreamvsn) = @_;
276 my $base = source_file_leafname $package, $upstreamvsn, '';
277 return 0 unless $f =~ m/^\Q$base\E\.$orig_f_tail_re$/;
281 sub server_branch ($) { return "$branchprefix/$_[0]"; }
282 sub server_ref ($) { return "refs/".server_branch($_[0]); }
284 sub stat_exists ($) {
287 return 0 if $!==&ENOENT;
288 confess "stat $f: $!";
292 $::us // ($0 =~ m#[^/]*$#, $&);
296 my $s = f_ "error: %s\n", "@_";
298 my $prefix = _us().": ";
299 $s =~ s/^/$prefix/gm;
308 my ($dir) = @_; # does not create parents
309 return if mkdir $dir;
310 return if $! == EEXIST;
311 confess "mkdir $dir: $!";
314 sub ensurepath ($$) {
315 my ($firsttocreate, $subdir) = @_; # creates necessary bits of $subidr
316 ensuredir $firsttocreate;
317 make_path "$firsttocreate/$subdir";
322 defined $d or fail f_ "getcwd failed: %s\n", $!;
326 sub executable_on_path ($) {
328 return 1 if $program =~ m{/};
329 my @path = split /:/, ($ENV{PATH} // "/usr/local/bin:/bin:/usr/bin");
330 foreach my $pe (@path) {
331 my $here = "$pe/$program";
332 return $here if stat_exists $here && -x _;
337 our @signames = split / /, $Config{sig_name};
339 sub waitstatusmsg () {
341 return __ "terminated, reporting successful completion";
342 } elsif (!($? & 255)) {
343 return f_ "failed with error exit status %s", WEXITSTATUS($?);
344 } elsif (WIFSIGNALED($?)) {
345 my $signum=WTERMSIG($?);
346 return f_ "died due to fatal signal %s",
347 ($signames[$signum] // "number $signum").
348 ($? & 128 ? " (core dumped)" : ""); # POSIX(3pm) has no WCOREDUMP
350 return f_ "failed with unknown wait status %s", $?;
354 sub failedcmd_report_cmd {
355 my $intro = shift @_;
356 $intro //= __ "failed command";
357 { local ($!); printcmd \*STDERR, _us().": $intro:", @_ or confess "$!"; };
360 sub failedcmd_waitstatus {
362 return f_ "failed to fork/exec: %s", $!;
364 return f_ "subprocess %s", waitstatusmsg();
366 return __ "subprocess produced invalid output";
371 # Expects $!,$? as set by close - see below.
372 # To use with system(), set $?=-1 first.
374 # Actual behaviour of perl operations:
375 # success $!==0 $?==0 close of piped open
376 # program failed $!==0 $? >0 close of piped open
377 # syscall failure $! >0 $?=-1 close of piped open
378 # failure $! >0 unchanged close of something else
379 # success trashed $?==0 system
380 # program failed trashed $? >0 system
381 # syscall failure $! >0 unchanged system
382 failedcmd_report_cmd undef, @_;
383 fail failedcmd_waitstatus();
389 failedcmd @_ if system @_;
393 my ($first_shell, @cmd) = @_;
394 return qw(sh -ec), $first_shell.'; exec "$@"', 'x', @cmd;
397 sub cmdoutput_errok {
398 confess Dumper(\@_)." ?" if grep { !defined } @_;
399 local $printdebug_when_debuglevel = $debugcmd_when_debuglevel;
401 open P, "-|", @_ or confess "$_[0] $!";
404 { local $/ = undef; $d = <P>; }
405 confess "$!" if P->error;
406 if (!close P) { printdebug "=>!$?\n"; return undef; }
408 if ($debuglevel > 0) {
411 my $more = (length $' ? '...' : ''); #');
412 $dd =~ s{[^\n -~]|\\}{ sprintf "\\x%02x", ord $& }ge;
413 printdebug "=> \`$dd'",$more,"\n";
419 my $d = cmdoutput_errok @_;
420 defined $d or failedcmd @_;
424 sub link_ltarget ($$) {
426 lstat $old or return undef;
428 $old = cmdoutput qw(realpath --), $old;
430 my $r = link $old, $new;
431 $r = symlink $old, $new if !$r && $!==EXDEV;
432 $r or fail "(sym)link $old $new: $!\n";
435 sub rename_link_xf ($$$) {
436 # renames/moves or links/copies $src to $dst,
437 # even if $dst is on a different fs
438 # (May use the filename "$dst.tmp".);
439 # On success, returns true.
440 # On failure, returns false and sets
441 # $@ to a reason message
442 # $! to an errno value, or -1 if not known
443 # having possibly printed something about mv to stderr.
444 # Not safe to use without $keeporig if $dst might be a symlink
445 # to $src, as it might delete $src leaving $dst invalid.
446 my ($keeporig,$src,$dst) = @_;
449 : rename $src, $dst) {
457 $@ = f_ "stat source file: %S", $!;
460 my @src_stat = (stat _)[0..1];
464 @dst_stat = (stat _)[0..1];
465 } elsif ($! == ENOENT) {
467 $@ = f_ "stat destination file: %S", $!;
471 if ("@src_stat" eq "@dst_stat") {
472 # (Symlinks to) the same file. No need for a copy but
473 # we may need to delete the original.
474 printdebug "rename_link_xf $keeporig $src $dst EXDEV but same\n";
477 my @cmd = (qw(cp --), $src, "$dst.tmp");
480 failedcmd_report_cmd undef, @cmd;
481 $@ = failedcmd_waitstatus();
485 if (!rename "$dst.tmp", $dst) {
486 $@ = f_ "finally install file after cp: %S", $!;
492 $@ = f_ "delete old file after cp: %S", $!;
501 my $h = Digest::SHA->new(256);
503 return $h->hexdigest();
506 sub git_rev_parse ($) {
507 return cmdoutput qw(git rev-parse), "$_[0]~0";
510 sub changedir_git_toplevel () {
511 my $toplevel = cmdoutput qw(git rev-parse --show-toplevel);
512 length $toplevel or fail __ <<END;
513 not in a git working tree?
514 (git rev-parse --show-toplevel produced no output)
516 chdir $toplevel or fail f_ "chdir toplevel %s: %s\n", $toplevel, $!;
519 sub git_cat_file ($;$) {
520 my ($objname, $etype) = @_;
521 # => ($type, $data) or ('missing', undef)
522 # in scalar context, just the data
523 # if $etype defined, dies unless type is $etype or in @$etype
524 our ($gcf_pid, $gcf_i, $gcf_o);
525 local $printdebug_when_debuglevel = $debugcmd_when_debuglevel;
527 my ($gtype, $data) = @_;
529 $etype = [$etype] unless ref $etype;
530 confess "$objname expected @$etype but is $gtype"
531 unless grep { $gtype eq $_ } @$etype;
533 return ($gtype, $data);
536 my @cmd = qw(git cat-file --batch);
537 debugcmd "GCF|", @cmd;
538 $gcf_pid = open2 $gcf_o, $gcf_i, @cmd or confess "$!";
540 printdebug "GCF>| $objname\n";
541 print $gcf_i $objname, "\n" or confess "$!";
543 printdebug "GCF<| ", $x;
544 if ($x =~ m/ (missing)$/) { return $chk->($1, undef); }
545 my ($type, $size) = $x =~ m/^.* (\w+) (\d+)\n/ or confess "$objname ?";
547 (read $gcf_o, $data, $size) == $size or confess "$objname $!";
549 $x eq "\n" or confess "$objname ($_) $!";
550 return $chk->($type, $data);
553 sub git_get_symref (;$) {
554 my ($symref) = @_; $symref //= 'HEAD';
555 # => undef if not a symref, otherwise refs/...
556 my @cmd = (qw(git symbolic-ref -q HEAD));
557 my $branch = cmdoutput_errok @cmd;
558 if (!defined $branch) {
559 $?==256 or failedcmd @cmd;
566 sub git_for_each_ref ($$;$) {
567 my ($pattern,$func,$gitdir) = @_;
568 # calls $func->($objid,$objtype,$fullrefname,$reftail);
569 # $reftail is RHS of ref after refs/[^/]+/
570 # breaks if $pattern matches any ref `refs/blah' where blah has no `/'
571 # $pattern may be an array ref to mean multiple patterns
572 $pattern = [ $pattern ] unless ref $pattern;
573 my @cmd = (qw(git for-each-ref), @$pattern);
574 if (defined $gitdir) {
575 @cmd = ('sh','-ec','cd "$1"; shift; exec "$@"','x', $gitdir, @cmd);
577 open GFER, "-|", @cmd or confess "$!";
580 chomp or confess "$_ ?";
581 printdebug "|> ", $_, "\n";
582 m#^(\w+)\s+(\w+)\s+(refs/[^/]+/(\S+))$# or confess "$_ ?";
583 $func->($1,$2,$3,$4);
585 $!=0; $?=0; close GFER or confess "$pattern $? $!";
588 sub git_get_ref ($) {
589 # => '' if no such ref
592 s{^refs/}{[r]efs/} or confess "$refname $_ ?";
593 return cmdoutput qw(git for-each-ref --format=%(objectname)), $_;
596 sub git_for_each_tag_referring ($$) {
597 my ($objreferring, $func) = @_;
598 # calls $func->($tagobjid,$refobjid,$fullrefname,$tagname);
599 printdebug "git_for_each_tag_referring ",
600 ($objreferring // 'UNDEF'),"\n";
601 git_for_each_ref('refs/tags', sub {
602 my ($tagobjid,$objtype,$fullrefname,$tagname) = @_;
603 return unless $objtype eq 'tag';
604 my $refobjid = git_rev_parse $tagobjid;
606 !defined $objreferring # caller wants them all
607 or $tagobjid eq $objreferring
608 or $refobjid eq $objreferring;
609 $func->($tagobjid,$refobjid,$fullrefname,$tagname);
613 sub git_check_unmodified () {
614 foreach my $cached (qw(0 1)) {
615 my @cmd = qw(git diff --quiet);
616 push @cmd, qw(--cached) if $cached;
619 $!=0; $?=-1; system @cmd;
624 ? __ "git index contains changes (does not match HEAD)"
625 : __ "working tree is dirty (does not match HEAD)";
632 sub upstream_commitish_search ($$) {
633 my ($upstream_version, $tried) = @_;
634 # todo: at some point maybe use git-deborig to do this
635 foreach my $tagpfx ('', 'v', 'upstream/') {
636 my $tag = $tagpfx.(dep14_version_mangle $upstream_version);
637 my $new_upstream = git_get_ref "refs/tags/$tag";
639 return $new_upstream if length $new_upstream;
643 sub resolve_upstream_version ($$) {
644 my ($new_upstream, $upstream_version) = @_;
646 my $used = $new_upstream;
647 my $message = __ 'using specified upstream commitish';
648 if (!defined $new_upstream) {
650 $new_upstream = upstream_commitish_search $upstream_version, \@tried;
651 if (!length $new_upstream) {
653 "Could not determine appropriate upstream commitish.\n".
654 " (Tried these tags: %s)\n".
655 " Check version, and specify upstream commitish explicitly.",
659 $message = f_ 'using upstream from git tag %s', $used;
660 } elsif ($new_upstream =~ m{^refs/tags/($versiontag_re)$}s) {
661 $message = f_ 'using upstream from git tag %s', $1;
664 $new_upstream = git_rev_parse $new_upstream;
666 return ($new_upstream, $used, $message);
667 # used is a human-readable idea of what we found
670 sub is_fast_fwd ($$) {
671 my ($ancestor,$child) = @_;
672 my @cmd = (qw(git merge-base), $ancestor, $child);
673 my $mb = cmdoutput_errok @cmd;
675 return git_rev_parse($mb) eq git_rev_parse($ancestor);
677 $?==256 or failedcmd @cmd;
682 sub git_reflog_action_msg ($) {
684 my $rla = $ENV{GIT_REFLOG_ACTION};
685 $msg = "$rla: $msg" if length $rla;
689 sub git_update_ref_cmd {
690 # returns qw(git update-ref), qw(-m), @_
691 # except that message may be modified to honour GIT_REFLOG_ACTION
693 $msg = git_reflog_action_msg $msg;
694 return qw(git update-ref -m), $msg, @_;
697 sub rm_subdir_cached ($) {
699 runcmd qw(git rm --quiet -rf --cached --ignore-unmatch), $subdir;
702 sub read_tree_subdir ($$) {
703 my ($subdir, $new_tree_object) = @_;
704 # If $new_tree_object is '', the subtree is deleted.
705 confess unless defined $new_tree_object;
706 rm_subdir_cached $subdir;
707 runcmd qw(git read-tree), "--prefix=$subdir/", $new_tree_object
708 if length $new_tree_object;
711 sub read_tree_debian ($) {
713 read_tree_subdir 'debian', "$treeish:debian";
714 rm_subdir_cached 'debian/patches';
717 sub read_tree_upstream ($;$$) {
718 my ($treeish, $keep_patches, $tree_with_debian) = @_;
719 # if $tree_with_debian is supplied, will use that for debian/
720 # otherwise will save and restore it. If $tree_with_debian
721 # is '' then debian/ is deleted.
723 defined $tree_with_debian ? "$tree_with_debian:debian"
724 : cmdoutput qw(git write-tree --prefix=debian/);
725 runcmd qw(git read-tree), $treeish;
726 read_tree_subdir 'debian', $debian;
727 rm_subdir_cached 'debian/patches' unless $keep_patches;
732 printdebug "CD $newdir\n";
733 chdir $newdir or confess "chdir: $newdir: $!";
736 sub git_slurp_config_src ($) {
738 # returns $r such that $r->{KEY}[] = VALUE
739 my @cmd = (qw(git config -z --get-regexp), "--$src", qw(.*));
742 local ($debuglevel) = $debuglevel-2;
746 open GITS, "-|", @cmd or confess "$!";
749 printdebug "=> ", (messagequote $_), "\n";
750 m/\n/ or confess "$_ ?";
751 push @{ $r->{$`} }, $'; #';
755 or ($!==0 && $?==256)
760 sub gdr_ffq_prev_branchinfo ($) {
762 # => ('status', "message", [$symref, $ffq_prev, $gdrlast])
764 # branch message is undef
765 # weird-symref } no $symref,
766 # notbranch } no $ffq_prev
767 return ('detached', __ 'detached HEAD') unless defined $symref;
768 return ('weird-symref', __ 'HEAD symref is not to refs/')
769 unless $symref =~ m{^refs/};
770 my $ffq_prev = "refs/$ffq_refprefix/$'";
771 my $gdrlast = "refs/$gdrlast_refprefix/$'";
772 printdebug "ffq_prev_branchinfo branch current $symref\n";
773 return ('branch', undef, $symref, $ffq_prev, $gdrlast);
776 sub parsecontrolfh ($$;$) {
777 my ($fh, $desc, $allowsigned) = @_;
778 our $dpkgcontrolhash_noissigned;
781 my %opts = ('name' => $desc);
782 $opts{allow_pgp}= $allowsigned || !$dpkgcontrolhash_noissigned;
783 $c = Dpkg::Control::Hash->new(%opts);
784 $c->parse($fh,$desc) or fail f_ "parsing of %s failed", $desc;
785 last if $allowsigned;
786 last if $dpkgcontrolhash_noissigned;
787 my $issigned= $c->get_option('is_pgp_signed');
788 if (!defined $issigned) {
789 $dpkgcontrolhash_noissigned= 1;
790 seek $fh, 0,0 or confess "seek $desc: $!";
791 } elsif ($issigned) {
793 "control file %s is (already) PGP-signed. ".
794 " Note that dgit push needs to modify the .dsc and then".
795 " do the signature itself",
805 my ($file, $desc, $allowsigned) = @_;
806 my $fh = new IO::Handle;
807 open $fh, '<', $file or fail f_ "open %s (%s): %s", $file, $desc, $!;
808 my $c = parsecontrolfh($fh,$desc,$allowsigned);
809 $fh->error and confess "$!";
815 my $c = Dpkg::Control::Hash->new(name => 'parsed changelog');
816 my $p = new IO::Handle;
817 my @cmd = (qw(dpkg-parsechangelog), @_);
818 open $p, '-|', @cmd or confess "$!";
820 $?=0; $!=0; close $p or failedcmd @cmd;
825 my ($dctrl,$field) = @_;
826 my $v = $dctrl->{$field};
827 return $v if defined $v;
828 fail f_ "missing field %s in %s", $field, $dctrl->get_option('name');
831 sub parsechangelog_loop ($$$) {
832 my ($clogcmd, $descbase, $fn) = @_;
833 # @$clogcmd is qw(dpkg-parsechangelog ...some...options...)
834 # calls $fn->($thisstanza, $desc);
835 debugcmd "|",@$clogcmd;
836 open CLOGS, "-|", @$clogcmd or confess "$!";
838 my $stanzatext = do { local $/=""; <CLOGS>; };
839 printdebug "clogp stanza ".Dumper($stanzatext) if $debuglevel>1;
840 last if !defined $stanzatext;
842 my $desc = "$descbase, entry no.$.";
843 open my $stanzafh, "<", \$stanzatext or confess;
844 my $thisstanza = parsecontrolfh $stanzafh, $desc, 1;
846 $fn->($thisstanza, $desc);
848 confess "$!" if CLOGS->error;
849 close CLOGS or $?==SIGPIPE or failedcmd @$clogcmd;
852 sub make_commit ($$) {
853 my ($parents, $message_paras) = @_;
854 my $tree = cmdoutput qw(git write-tree);
855 my @cmd = (qw(git commit-tree), $tree);
856 push @cmd, qw(-p), $_ foreach @$parents;
857 push @cmd, qw(-m), $_ foreach @$message_paras;
858 return cmdoutput @cmd;
861 sub hash_commit ($) {
863 return cmdoutput qw(git hash-object -w -t commit), $file;
866 sub hash_commit_text ($) {
869 my @cmd = (qw(git hash-object -w -t commit --stdin));
871 print Dumper($text) if $debuglevel > 1;
872 my $child = open2($out, $in, @cmd) or confess "$!";
875 print $in $text or confess "$!";
876 close $in or confess "$!";
878 $h =~ m/^\w+$/ or confess;
880 printdebug "=> $h\n";
883 waitpid $child, 0 == $child or confess "$child $!";
884 $? and failedcmd @cmd;
888 sub reflog_cache_insert ($$$) {
889 my ($ref, $cachekey, $value) = @_;
890 # you must call this in $maindir
891 # you must have called record_maindir
893 # When we no longer need to support squeeze, use --create-reflog
895 my $parent = $ref; $parent =~ s{/[^/]+$}{};
896 ensurepath "$maindir_gitcommon/logs", "$parent";
897 my $makelogfh = new IO::File "$maindir_gitcommon/logs/$ref", '>>'
900 my $oldcache = git_get_ref $ref;
902 if ($oldcache eq $value) {
903 my $tree = cmdoutput qw(git rev-parse), "$value:";
904 # git update-ref doesn't always update, in this case. *sigh*
905 my $authline = (ucfirst _us()).
906 ' <'._us().'@example.com> 1000000000 +0000';
907 my $dummy = hash_commit_text <<ENDU.(__ <<END);
914 Dummy commit - do not use
916 runcmd qw(git update-ref -m), _us()." - dummy", $ref, $dummy;
918 runcmd qw(git update-ref -m), $cachekey, $ref, $value;
921 sub reflog_cache_lookup ($$) {
922 my ($ref, $cachekey) = @_;
923 # you may call this in $maindir or in a playtree
924 # you must have called record_maindir
925 my @cmd = (qw(git log -g), '--pretty=format:%H %gs', $ref);
926 debugcmd "|(probably)",@cmd;
927 my $child = open GC, "-|"; defined $child or confess "$!";
929 chdir $maindir or confess "$!";
930 if (!stat "$maindir_gitcommon/logs/$ref") {
931 $! == ENOENT or confess "$!";
932 printdebug ">(no reflog)\n";
935 exec @cmd; die f_ "exec %s: %s\n", $cmd[0], $!;
939 printdebug ">| ", $_, "\n" if $debuglevel > 1;
940 next unless m/^(\w+) (\S.*\S)$/ && $2 eq $cachekey;
944 confess "$!" if GC->error;
945 failedcmd unless close GC;
949 # ========== playground handling ==========
953 # $maindir user's git working tree
954 # playground area in .git/ where we can make files, unpack, etc. etc.
955 # playtree git working tree sharing object store with the user's
956 # inside playground, or identical to it
960 # $local_git_cfg hash of arrays of values: git config from $maindir
962 # expected calling pattern
967 # must be run in directory containing .git
968 # assigns to $maindir if not already set
969 # also calls git_slurp_config_src to record git config
970 # in $local_git_cfg, unless it's already set
972 # fresh_playground SUBDIR_PATH_COMPONENTS
973 # e.g fresh_playground 'dgit/unpack' ('.git/' is implied)
974 # default SUBDIR_PATH_COMPONENTS is playground_subdir
975 # calls record_maindir
976 # sets up a new playground (destroying any old one)
977 # returns playground pathname
978 # caller may call multiple times with different subdir paths
979 # createing different playgrounds
981 # ensure_a_playground SUBDIR_PATH_COMPONENTS
982 # like fresh_playground except:
983 # merely ensures the directory exists; does not delete an existing one
987 # changedir playground
991 # # ^ call in some (perhaps trivial) subdir of playground
995 # ----- maindir -----
999 sub record_maindir () {
1000 if (!defined $maindir) {
1001 $maindir = must_getcwd();
1002 if (!stat "$maindir/.git") {
1003 fail f_ "cannot stat %s/.git: %s", $maindir, $!;
1006 # we fall back to this in case we have a pre-worktree
1007 # git, which may not know git rev-parse --git-common-dir
1008 $maindir_gitdir = "$maindir/.git";
1009 $maindir_gitcommon = "$maindir/.git";
1011 $maindir_gitdir = cmdoutput qw(git rev-parse --git-dir);
1012 $maindir_gitcommon = cmdoutput qw(git rev-parse --git-common-dir);
1015 $local_git_cfg //= git_slurp_config_src 'local';
1018 # ----- playgrounds -----
1020 sub ensure_a_playground_parent ($) {
1023 $spc = "$maindir_gitdir/$spc";
1024 my $parent = dirname $spc;
1025 mkdir $parent or $!==EEXIST or fail f_
1026 "failed to mkdir playground parent %s: %s", $parent, $!;
1030 sub ensure_a_playground ($) {
1032 $spc = ensure_a_playground_parent $spc;
1033 mkdir $spc or $!==EEXIST or fail f_
1034 "failed to mkdir a playground %s: %s", $spc, $!;
1038 sub fresh_playground ($) {
1040 $spc = ensure_a_playground_parent $spc;
1042 mkdir $spc or fail f_
1043 "failed to mkdir the playground %s: %s", $spc, $!;
1047 # ----- playtrees -----
1049 sub playtree_setup () {
1050 # for use in the playtree
1051 # $maindir must be set, eg by calling record_maindir or fresh_playground
1052 # this is confusing: we have
1053 # . playtree, not a worktree, has .git/, our cwd
1054 # $maindir might be a worktree so
1055 # $maindir_gitdir contains our main working "dgit", HEAD, etc.
1056 # $maindir_gitcommon the shared stuff, including .objects
1058 # we need to invoke git-playtree-create via git because
1059 # there may be config options it needs which are only available
1060 # to us, sensibly, in @git
1062 # And, we look for it in @INC too. This is a bit perverse.
1063 # We do this because in the Debian packages we want to have
1064 # a copy of this script in each binary package, rather than
1065 # making yet another .deb or tangling the dependencies.
1066 # @INC is conveniently available.
1067 my $newpath = join ':', +(grep { !m/:/ } @INC),
1068 '/usr/share/dgit', $ENV{PATH};
1069 runcmd qw(env), "PATH=$newpath", @git, qw(playtree-create .);
1071 ensuredir '.git/info';
1072 open GA, "> .git/info/attributes" or confess "$!";
1073 print GA "* $negate_harmful_gitattrs\n" or confess "$!";
1074 close GA or confess "$!";