summaryrefslogtreecommitdiff
path: root/perl/Git.pm
diff options
context:
space:
mode:
authorJunio C Hamano <gitster@pobox.com>2022-10-28 11:26:54 -0700
committerJunio C Hamano <gitster@pobox.com>2022-10-28 11:26:54 -0700
commit330135ac8163c822934c92ac363bd14cd23e1e13 (patch)
tree261bea7995d84d5e3d10a44cafd47d6a2b7c5ada /perl/Git.pm
parentc5dd7773e138d4b41074d4c6a193651cc187ec1d (diff)
parent20da61f25f8f61a2b581b60f8820ad6116f88e6f (diff)
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
Diffstat (limited to 'perl/Git.pm')
-rw-r--r--perl/Git.pm36
1 files changed, 16 insertions, 20 deletions
diff --git a/perl/Git.pm b/perl/Git.pm
index 080cdc2a21..117765dc73 100644
--- a/perl/Git.pm
+++ b/perl/Git.pm
@@ -177,16 +177,27 @@ sub repository {
-d $opts{Directory} or throw Error::Simple("Directory not found: $opts{Directory} $!");
my $search = Git->repository(WorkingCopy => $opts{Directory});
- my $dir;
+
+ # This rev-parse will throw an exception if we're not in a
+ # repository, which is what we want, but it's kind of noisy.
+ # Ideally we'd capture stderr and relay it, but doing so is
+ # awkward without depending on it fitting in a pipe buffer. So
+ # we just reproduce a plausible error message ourselves.
+ my $out;
try {
- $dir = $search->command_oneline(['rev-parse', '--git-dir'],
- STDERR => 0);
+ # Note that "--is-bare-repository" must come first, as
+ # --git-dir output could contain newlines.
+ $out = $search->command([qw(rev-parse --is-bare-repository --git-dir)],
+ STDERR => 0);
} catch Git::Error::Command with {
- $dir = undef;
+ throw Error::Simple("fatal: not a git repository: $opts{Directory}");
};
+ chomp $out;
+ my ($bare, $dir) = split /\n/, $out, 2;
+
require Cwd;
- if ($dir) {
+ if ($bare ne 'true') {
require File::Spec;
File::Spec->file_name_is_absolute($dir) or $dir = $opts{Directory} . '/' . $dir;
$opts{Repository} = Cwd::abs_path($dir);
@@ -204,21 +215,6 @@ sub repository {
$opts{WorkingSubdir} = $prefix;
} else {
- # A bare repository? Let's see...
- $dir = $opts{Directory};
-
- unless (-d "$dir/refs" and -d "$dir/objects" and -e "$dir/HEAD") {
- # Mimic git-rev-parse --git-dir error message:
- throw Error::Simple("fatal: Not a git repository: $dir");
- }
- my $search = Git->repository(Repository => $dir);
- try {
- $search->command('symbolic-ref', 'HEAD');
- } catch Git::Error::Command with {
- # Mimic git-rev-parse --git-dir error message:
- throw Error::Simple("fatal: Not a git repository: $dir");
- }
-
$opts{Repository} = Cwd::abs_path($dir);
}