1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-19 05:12:41 +01:00

Support Git implicit file:// URIs

Summary: @s reported an issue with implicit file:// URIs in Git, see P270.
Recognize and handle URIs in this format. For URIs we don't understand, raise an
exception.

Test Plan:
  - Added failing tests.
  - Fixed code.
  - Tests pass.

Reviewers: btrahan, jungejason, s

Reviewed By: s

CC: aran, epriestley, s

Differential Revision: https://secure.phabricator.com/D1362
This commit is contained in:
epriestley 2012-01-11 08:38:21 -08:00
parent d75007cf42
commit b10fa8023f
3 changed files with 47 additions and 7 deletions

View file

@ -1,7 +1,7 @@
<?php
/*
* Copyright 2011 Facebook, Inc.
* Copyright 2012 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -75,6 +75,18 @@ final class PhabricatorRepositoryGitCommitDiscoveryDaemonTestCase
true,
'Optional trailing slash should not prevent matches.',
),
array(
'file:///path/to/local/repo.git',
'file:///path/to/local/repo.git',
true,
'file:// protocol should be supported.',
),
array(
'/path/to/local/repo.git',
'file:///path/to/local/repo.git',
true,
'Implicit file:// protocol should be recognized.',
),
);
foreach ($cases as $case) {

View file

@ -74,14 +74,22 @@ class PhabricatorRepository extends PhabricatorRepositoryDAO {
}
public static function newPhutilURIFromGitURI($raw_uri) {
// If there's no protocol (git implicit SSH) reformat the URI to be a
// normal URI. These git URIs look like "user@domain.com:path" instead of
// "ssh://user@domain/path".
$uri = new PhutilURI($raw_uri);
if (!$uri->getProtocol()) {
list($domain, $path) = explode(':', $raw_uri, 2);
$uri = new PhutilURI('ssh://'.$domain.'/'.$path);
if (strpos($raw_uri, '/') === 0) {
// If the URI starts with a '/', it's an implicit file:// URI on the
// local disk.
$uri = new PhutilURI('file://'.$raw_uri);
} else if (strpos($raw_uri, ':') !== false) {
// If there's no protocol (git implicit SSH) but the URI has a colon,
// it's a git implicit SSH URI. Reformat the URI to be a normal URI.
// These git URIs look like "user@domain.com:path" instead of
// "ssh://user@domain/path".
list($domain, $path) = explode(':', $raw_uri, 2);
$uri = new PhutilURI('ssh://'.$domain.'/'.$path);
} else {
throw new Exception("The Git URI '{$raw_uri}' could not be parsed.");
}
}
return $uri;

View file

@ -23,6 +23,7 @@ final class PhabricatorRepositoryTestCase
static $map = array(
'ssh://user@domain.com/path.git' => 'ssh://user@domain.com/path.git',
'user@domain.com:path.git' => 'ssh://user@domain.com/path.git',
'/path/to/local/repo.git' => 'file:///path/to/local/repo.git',
);
foreach ($map as $raw => $expect) {
@ -34,6 +35,25 @@ final class PhabricatorRepositoryTestCase
}
}
public function testParseBadGitURI() {
$junk = array(
'herp derp moon balloon',
);
foreach ($junk as $garbage) {
$ex = null;
try {
$uri = PhabricatorRepository::newPhutilURIFromGitURI($garbage);
} catch (Exception $caught) {
$ex = $caught;
}
$this->assertEqual(
true,
(bool)$ex,
'Expect exception when parsing garbage.');
}
}
public function testBranchFilter() {
$git = PhabricatorRepositoryType::REPOSITORY_TYPE_GIT;