Skip to content

Commit d3edb0b

Browse files
peffgitster
authored andcommitted
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 a2f5a87 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent e4b353d commit d3edb0b

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

perl/Git.pm

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ sub repository {
187187
try {
188188
# Note that "--is-bare-repository" must come first, as
189189
# --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)],
191191
STDERR => 0);
192192
} catch Git::Error::Command with {
193193
throw Error::Simple("fatal: not a git repository: $opts{Directory}");
@@ -196,12 +196,12 @@ sub repository {
196196
chomp $out;
197197
my ($bare, $dir) = split /\n/, $out, 2;
198198

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;
203202

204203
if ($bare ne 'true') {
204+
require Cwd;
205205
# If --git-dir went ok, this shouldn't die either.
206206
my $prefix = $search->command_oneline('rev-parse', '--show-prefix');
207207
$dir = Cwd::abs_path($opts{Directory}) . '/';

0 commit comments

Comments
 (0)