Skip to content

Commit ac7c739

Browse files
author
epriestley
committed
Fix --depth N clones in Git
Summary: Ref T2230. Fixes T4079. As it turns out, this is Git being weird. See comments for some detials about what's going on here. Test Plan: Created shallow and deep Git clones. Reviewers: hach-que, btrahan Reviewed By: btrahan CC: aran Maniphest Tasks: T4079, T2230 Differential Revision: https://secure.phabricator.com/D7554
1 parent dc7f716 commit ac7c739

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/applications/diffusion/controller/DiffusionServeController.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,14 @@ private function serveGitRequest(
328328
->write($input)
329329
->resolve();
330330

331+
if ($err) {
332+
if ($this->isValidGitShallowCloneResponse($stdout, $stderr)) {
333+
// Ignore the error if the response passes this special check for
334+
// validity.
335+
$err = 0;
336+
}
337+
}
338+
331339
if ($err) {
332340
return new PhabricatorVCSResponse(
333341
500,
@@ -512,5 +520,27 @@ private function formatMercurialArguments($command, array $arguments) {
512520
return implode('', $out);
513521
}
514522

523+
private function isValidGitShallowCloneResponse($stdout, $stderr) {
524+
// If you execute `git clone --depth N ...`, git sends a request which
525+
// `git-http-backend` responds to by emitting valid output and then exiting
526+
// with a failure code and an error message. If we ignore this error,
527+
// everything works.
528+
529+
// This is a pretty funky fix: it would be nice to more precisely detect
530+
// that a request is a `--depth N` clone request, but we don't have any code
531+
// to decode protocol frames yet. Instead, look for reasonable evidence
532+
// in the error and output that we're looking at a `--depth` clone.
533+
534+
// For evidence this isn't completely crazy, see:
535+
// https://github.com/schacon/grack/pull/7
536+
537+
$stdout_regexp = '(^Content-Type: application/x-git-upload-pack-result)m';
538+
$stderr_regexp = '(The remote end hung up unexpectedly)';
539+
540+
$has_pack = preg_match($stdout_regexp, $stdout);
541+
$is_hangup = preg_match($stderr_regexp, $stderr);
542+
543+
return $has_pack && $is_hangup;
544+
}
515545
}
516546

0 commit comments

Comments
 (0)