diff options
-rw-r--r-- | lib/Git/Annex.pm | 22 | ||||
-rwxr-xr-x | t/10_init.t | 6 |
2 files changed, 27 insertions, 1 deletions
diff --git a/lib/Git/Annex.pm b/lib/Git/Annex.pm index 6964b89..ad56ca8 100644 --- a/lib/Git/Annex.pm +++ b/lib/Git/Annex.pm @@ -288,7 +288,27 @@ sub _git_path { around BUILDARGS => sub { my (undef, undef, @args) = @_; - { toplevel => $args[0] ? rel2abs($args[0]) : getcwd }; + + my $toplevel = $args[0] ? rel2abs($args[0]) : getcwd; + + # if we're in a working tree, rise up to the root of the working + # tree -- for flexibility, don't require that we're actually in a + # git repo at all + my $pid = fork; + die "fork() failed: $!" unless defined $pid; + if ($pid) { + wait; + if ($?) { + chomp($toplevel = `git -C $toplevel rev-parse --show-toplevel`); + die "git rev-parse --show-toplevel failed" if $?; + } + } else { + close STDERR; + chomp(my $output = `git -C $toplevel rev-parse --is-inside-work-tree`); + exit ($output and $output eq "true"); + } + + return { toplevel => $toplevel }; }; 1; diff --git a/t/10_init.t b/t/10_init.t index 5d72048..b8f67fa 100755 --- a/t/10_init.t +++ b/t/10_init.t @@ -45,4 +45,10 @@ with_temp_annexes { "Git::Repository has correct toplevel"; }; +with_temp_annexes { + my $source1_dir = catfile shift, "source1"; + my $annex = Git::Annex->new(catfile $source1_dir, "foo"); + is $annex->toplevel, $source1_dir, "it rises to top of working tree"; +}; + done_testing; |