@@ -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
0 commit comments