chiark / gitweb /
dgit-maint-debrebase(7): Add cross-refs to git-debrebase(1) and (5)
[dgit.git] / Debian / Dgit.pm
index de37261d1820b86d54e874860932be631ab2f135..c4a61af1f081b62f21938ac86783a0deef000286 100644 (file)
@@ -64,6 +64,7 @@ BEGIN {
     # implicitly uses $main::us
     %EXPORT_TAGS = ( policyflags => [qw(NOFFCHECK FRESHREPO NOCOMMITCHECK)],
                     playground => [qw(record_maindir $maindir $local_git_cfg
+                                      $maindir_gitdir $maindir_gitcommon
                                       fresh_playground $playground
                                        ensure_a_playground)]);
     @EXPORT_OK   = ( @{ $EXPORT_TAGS{policyflags} },
@@ -509,11 +510,29 @@ sub git_slurp_config_src ($) {
 
 # ----- maindir -----
 
+# these three all go together
 our $maindir;
+our $maindir_gitdir;
+our $maindir_gitcommon;
+
 our $local_git_cfg;
 
 sub record_maindir () {
-    $maindir //= must_getcwd();
+    if (!defined $maindir) {
+       $maindir = must_getcwd();
+       if (!stat "$maindir/.git") {
+           fail "cannot stat $maindir/.git: $!";
+       }
+       if (-d _) {
+           # we fall back to this in case we have a pre-worktree
+           # git, which may not know git rev-parse --git-common-dir
+           $maindir_gitdir    = "$maindir/.git";
+           $maindir_gitcommon = "$maindir/.git";
+       } else {
+           $maindir_gitdir    = cmdoutput qw(git rev-parse --git-dir);
+           $maindir_gitcommon = cmdoutput qw(git rev-parse --git-common-dir);
+       }
+    }
     $local_git_cfg //= git_slurp_config_src 'local';
 }
 
@@ -524,9 +543,10 @@ our $playground;
 sub ensure_a_playground_parent ($) {
     my ($spc) = @_;
     record_maindir();
-    $spc = ".git/$spc";
+    $spc = "$maindir_gitdir/$spc";
     my $parent = dirname $spc;
-    mkdir $parent or $!==EEXIST or fail "failed to mkdir $parent: $!";
+    mkdir $parent or $!==EEXIST
+       or fail "failed to mkdir playground parent $parent: $!";
     return $spc;
 }    
 
@@ -541,8 +561,8 @@ sub fresh_playground ($) {
     my ($spc) = @_;
     $spc = ensure_a_playground_parent $spc;
     rmtree $spc;
-    mkdir $spc or die "$spc $!";
-    return $playground = "$maindir/$spc";
+    mkdir $spc or fail "failed to mkdir the playground $spc: $!";
+    return $playground = $spc;
 }
 
 # ----- playtrees -----
@@ -562,8 +582,13 @@ sub playtree_setup (;$) {
        next unless $v;
        runcmd qw(git config), $copy, $_ foreach @$v;
     }
+    # this is confusing: we have
+    #   .                   playtree, not a worktree, has .git/, our cwd
+    #   $maindir            might be a worktree so
+    #   $maindir_gitdir     contains our main working "dgit", HEAD, etc.
+    #   $maindir_gitcommon  the shared stuff, including .objects
     rmtree('.git/objects');
-    symlink "$maindir/.git/objects",'.git/objects' or die $!;
+    symlink "$maindir_gitcommon/objects",'.git/objects' or die $!;
     ensuredir '.git/info';
     open GA, "> .git/info/attributes" or die $!;
     print GA "* $negate_harmful_gitattrs\n" or die $!;