Git.pm: use "rev-parse --absolute-git-dir" rather than perl code

When we open a repository with the "Directory" option, we use "rev-parse --git-dir" to get the path relative to that directory, and then use Cwd::abs_path() to make it absolute (since our process working directory may not be the same). These days we can just ask for "--absolute-git-dir" instead, which saves us a little code. That option was added in Git v2.13.0 via a2f5a87626 (rev-parse: add '--absolute-git-dir' option, 2017-02-03). I don't think we make any promises about running mismatched versions of git and Git.pm, but even if somebody tries it, that's sufficiently old that it should be OK. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed Sep 12, 2024 at 18:37 UTC d3edb0bddec29c97fb0314d9b1ee77d2e7d22382
1 file changed +5 -5
perl/Git.pm
+5 -5
@@ -187,7 +187,7 @@ sub repository {
187 try {
188 # Note that "--is-bare-repository" must come first, as
189 # --git-dir output could contain newlines.
190 - $out = $search->command([qw(rev-parse --is-bare-repository --git-dir)],
190 + $out = $search->command([qw(rev-parse --is-bare-repository --absolute-git-dir)],
191 STDERR => 0);
192 } catch Git::Error::Command with {
193 throw Error::Simple("fatal: not a git repository: $opts{Directory}");
@@ -196,12 +196,12 @@ sub repository {
196 chomp $out;
197 my ($bare, $dir) = split /\n/, $out, 2;
198
199 - require Cwd;
200 - require File::Spec;
201 - File::Spec->file_name_is_absolute($dir) or $dir = $opts{Directory} . '/' . $dir;
202 - $opts{Repository} = Cwd::abs_path($dir);
199 + # We know this is an absolute path, because we used
200 + # --absolute-git-dir above.
201 + $opts{Repository} = $dir;
202
203 if ($bare ne 'true') {
204 + require Cwd;
205 # If --git-dir went ok, this shouldn't die either.
206 my $prefix = $search->command_oneline('rev-parse', '--show-prefix');
207 $dir = Cwd::abs_path($opts{Directory}) . '/';