X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=dgit.git;a=blobdiff_plain;f=Debian%2FDgit.pm;h=515a32f600b805634a8b3991e21b702c603dc56d;hp=dcecbd1a840e7f0c7718b398d585e2e4d4e3fe77;hb=d99fbfc09e25cda5a7e95c3c632436d22cb4e59a;hpb=18413810f983efbd083a2fd0cbc2c2cbbadc558b diff --git a/Debian/Dgit.pm b/Debian/Dgit.pm index dcecbd1a..515a32f6 100644 --- a/Debian/Dgit.pm +++ b/Debian/Dgit.pm @@ -29,6 +29,7 @@ use Config; use Digest::SHA; use Data::Dumper; use IPC::Open2; +use File::Path; BEGIN { use Exporter (); @@ -42,10 +43,10 @@ BEGIN { server_branch server_ref stat_exists link_ltarget hashfile - fail ensuredir executable_on_path + fail ensuredir must_getcwd executable_on_path waitstatusmsg failedcmd_waitstatus failedcmd_report_cmd failedcmd - cmdoutput cmdoutput_errok + runcmd cmdoutput cmdoutput_errok git_rev_parse git_cat_file git_get_ref git_for_each_ref git_for_each_tag_referring is_fast_fwd @@ -55,10 +56,15 @@ BEGIN { initdebug enabledebug enabledebuglevel printdebug debugcmd $debugprefix *debuglevel *DEBUG - shellquote printcmd messagequote); + shellquote printcmd messagequote + $negate_harmful_gitattrs + changedir git_slurp_config_src + playtree_setup); # implicitly uses $main::us - %EXPORT_TAGS = ( policyflags => [qw(NOFFCHECK FRESHREPO NOCOMMITCHECK)] ); - @EXPORT_OK = @{ $EXPORT_TAGS{policyflags} }; + %EXPORT_TAGS = ( policyflags => [qw(NOFFCHECK FRESHREPO NOCOMMITCHECK)], + playground => [qw($local_git_cfg)]); + @EXPORT_OK = ( @{ $EXPORT_TAGS{policyflags} }, + @{ $EXPORT_TAGS{playground} } ); } our @EXPORT_OK; @@ -82,6 +88,8 @@ sub NOCOMMITCHECK () { return 0x8; } our $debugprefix; our $debuglevel = 0; +our $negate_harmful_gitattrs = "-text -eol -crlf -ident -filter"; + our $forkcheck_mainprocess; sub forkcheck_setup () { @@ -216,6 +224,12 @@ sub ensuredir ($) { die "mkdir $dir: $!"; } +sub must_getcwd () { + my $d = getcwd(); + defined $d or fail "getcwd failed: $!"; + return $d; +} + sub executable_on_path ($) { my ($program) = @_; return 1 if $program =~ m{/}; @@ -276,6 +290,12 @@ sub failedcmd { fail failedcmd_waitstatus(); } +sub runcmd { + debugcmd "+",@_; + $!=0; $?=-1; + failedcmd @_ if system @_; +} + sub cmdoutput_errok { confess Dumper(\@_)." ?" if grep { !defined } @_; debugcmd "|",@_; @@ -406,4 +426,64 @@ sub is_fast_fwd ($$) { } } +sub changedir ($) { + my ($newdir) = @_; + printdebug "CD $newdir\n"; + chdir $newdir or confess "chdir: $newdir: $!"; +} + +sub git_slurp_config_src ($) { + my ($src) = @_; + # returns $r such that $r->{KEY}[] = VALUE + my @cmd = (qw(git config -z --get-regexp), "--$src", qw(.*)); + debugcmd "|",@cmd; + + local ($debuglevel) = $debuglevel-2; + local $/="\0"; + + my $r = { }; + open GITS, "-|", @cmd or die $!; + while () { + chomp or die; + printdebug "=> ", (messagequote $_), "\n"; + m/\n/ or die "$_ ?"; + push @{ $r->{$`} }, $'; #'; + } + $!=0; $?=0; + close GITS + or ($!==0 && $?==256) + or failedcmd @cmd; + return $r; +} + +# ========== playground handling ========== + +# $local_git_cfg hash of arrays of values: git config from $maindir + +our $local_git_cfg; + +# ----- playtrees ----- + +sub playtree_setup (;$) { + my ($t_local_git_cfg) = @_; + $t_local_git_cfg //= $local_git_cfg; + # should be run in a directory .git/FOO/BAR of a working tree + runcmd qw(git init -q); + runcmd qw(git config gc.auto 0); + foreach my $copy (qw(user.email user.name user.useConfigOnly + core.sharedRepository + core.compression core.looseCompression + core.bigFileThreshold core.fsyncObjectFiles)) { + my $v = $t_local_git_cfg->{$copy}; + next unless $v; + runcmd qw(git config), $copy, $_ foreach @$v; + } + rmtree('.git/objects'); + symlink '../../../../objects','.git/objects' or die $!; + ensuredir '.git/info'; + open GA, "> .git/info/attributes" or die $!; + print GA "* $negate_harmful_gitattrs\n" or die $!; + close GA or die $!; +} + 1;