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>
This commit is contained in:
Jeff King
2024-09-12 18:37:25 -04:00
committed by Junio C Hamano
parent e4b353d0a1
commit d3edb0bdde

View File

@ -187,7 +187,7 @@ sub repository {
try { try {
# Note that "--is-bare-repository" must come first, as # Note that "--is-bare-repository" must come first, as
# --git-dir output could contain newlines. # --git-dir output could contain newlines.
$out = $search->command([qw(rev-parse --is-bare-repository --git-dir)], $out = $search->command([qw(rev-parse --is-bare-repository --absolute-git-dir)],
STDERR => 0); STDERR => 0);
} catch Git::Error::Command with { } catch Git::Error::Command with {
throw Error::Simple("fatal: not a git repository: $opts{Directory}"); throw Error::Simple("fatal: not a git repository: $opts{Directory}");
@ -196,12 +196,12 @@ sub repository {
chomp $out; chomp $out;
my ($bare, $dir) = split /\n/, $out, 2; my ($bare, $dir) = split /\n/, $out, 2;
require Cwd; # We know this is an absolute path, because we used
require File::Spec; # --absolute-git-dir above.
File::Spec->file_name_is_absolute($dir) or $dir = $opts{Directory} . '/' . $dir; $opts{Repository} = $dir;
$opts{Repository} = Cwd::abs_path($dir);
if ($bare ne 'true') { if ($bare ne 'true') {
require Cwd;
# If --git-dir went ok, this shouldn't die either. # If --git-dir went ok, this shouldn't die either.
my $prefix = $search->command_oneline('rev-parse', '--show-prefix'); my $prefix = $search->command_oneline('rev-parse', '--show-prefix');
$dir = Cwd::abs_path($opts{Directory}) . '/'; $dir = Cwd::abs_path($opts{Directory}) . '/';