chiark / gitweb /
playground refactoring: Dgit.pm: Expose $local_git_cfg
[dgit.git] / Debian / Dgit.pm
index aef0112aebb975ce49cca55699808fbe6cf99922..515a32f600b805634a8b3991e21b702c603dc56d 100644 (file)
@@ -43,7 +43,7 @@ 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
                       runcmd cmdoutput cmdoutput_errok
@@ -58,10 +58,13 @@ BEGIN {
                       $debugprefix *debuglevel *DEBUG
                       shellquote printcmd messagequote
                       $negate_harmful_gitattrs
-                      workarea_setup);
+                     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;
@@ -221,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{/};
@@ -417,9 +426,47 @@ sub is_fast_fwd ($$) {
     }
 }
 
-sub workarea_setup ($) {
-    # for use in the workarea
+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 (<GITS>) {
+       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);