Skip to content

Commit 330135a

Browse files
committed
Merge branch 'mm/git-pm-try-catch-syntax-fix'
Fix a longstanding syntax error in Git.pm error codepath. * mm/git-pm-try-catch-syntax-fix: Git.pm: trust rev-parse to find bare repositories Git.pm: add semicolon after catch statement
2 parents c5dd777 + 20da61f commit 330135a

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

perl/Git.pm

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,27 @@ sub repository {
177177
-d $opts{Directory} or throw Error::Simple("Directory not found: $opts{Directory} $!");
178178

179179
my $search = Git->repository(WorkingCopy => $opts{Directory});
180-
my $dir;
180+
181+
# This rev-parse will throw an exception if we're not in a
182+
# repository, which is what we want, but it's kind of noisy.
183+
# Ideally we'd capture stderr and relay it, but doing so is
184+
# awkward without depending on it fitting in a pipe buffer. So
185+
# we just reproduce a plausible error message ourselves.
186+
my $out;
181187
try {
182-
$dir = $search->command_oneline(['rev-parse', '--git-dir'],
183-
STDERR => 0);
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)],
191+
STDERR => 0);
184192
} catch Git::Error::Command with {
185-
$dir = undef;
193+
throw Error::Simple("fatal: not a git repository: $opts{Directory}");
186194
};
187195

196+
chomp $out;
197+
my ($bare, $dir) = split /\n/, $out, 2;
198+
188199
require Cwd;
189-
if ($dir) {
200+
if ($bare ne 'true') {
190201
require File::Spec;
191202
File::Spec->file_name_is_absolute($dir) or $dir = $opts{Directory} . '/' . $dir;
192203
$opts{Repository} = Cwd::abs_path($dir);
@@ -204,21 +215,6 @@ sub repository {
204215
$opts{WorkingSubdir} = $prefix;
205216

206217
} else {
207-
# A bare repository? Let's see...
208-
$dir = $opts{Directory};
209-
210-
unless (-d "$dir/refs" and -d "$dir/objects" and -e "$dir/HEAD") {
211-
# Mimic git-rev-parse --git-dir error message:
212-
throw Error::Simple("fatal: Not a git repository: $dir");
213-
}
214-
my $search = Git->repository(Repository => $dir);
215-
try {
216-
$search->command('symbolic-ref', 'HEAD');
217-
} catch Git::Error::Command with {
218-
# Mimic git-rev-parse --git-dir error message:
219-
throw Error::Simple("fatal: Not a git repository: $dir");
220-
}
221-
222218
$opts{Repository} = Cwd::abs_path($dir);
223219
}
224220

t/t9700-perl-git.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ test_expect_success \
4545
git config --add test.pathmulti bar
4646
'
4747

48+
test_expect_success 'set up bare repository' '
49+
git init --bare bare.git
50+
'
51+
4852
test_expect_success 'use t9700/test.pl to test Git.pm' '
4953
"$PERL_PATH" "$TEST_DIRECTORY"/t9700/test.pl 2>stderr &&
5054
test_must_be_empty stderr

t/t9700/test.pl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ sub adjust_dirsep {
3030
# set up
3131
our $abs_repo_dir = cwd();
3232
ok(our $r = Git->repository(Directory => "."), "open repository");
33+
{
34+
local $ENV{GIT_TEST_ASSUME_DIFFERENT_OWNER} = 1;
35+
my $failed;
36+
37+
$failed = eval { Git->repository(Directory => $abs_repo_dir) };
38+
ok(!$failed, "reject unsafe non-bare repository");
39+
like($@, qr/not a git repository/i, "unsafe error message");
40+
41+
$failed = eval { Git->repository(Directory => "$abs_repo_dir/bare.git") };
42+
ok(!$failed, "reject unsafe bare repository");
43+
like($@, qr/not a git repository/i, "unsafe error message");
44+
}
3345

3446
# config
3547
is($r->config("test.string"), "value", "config scalar: string");

0 commit comments

Comments
 (0)