getNormalizedPath() == $norm_b->getNormalizedPath()) { * // URIs appear to point at the same repository. * } else { * // URIs are very unlikely to be the same repository. * } * * Because a repository can be hosted at arbitrarly many arbitrary URIs, there * is no way to completely prevent false negatives by only examining URIs * (that is, repositories with totally different URIs could really be the same). * However, normalization is relatively agressive and false negatives should * be rare: if normalization says two URIs are different repositories, they * probably are. * * @task normal Normalizing URIs */ final class PhabricatorRepositoryURINormalizer extends Phobject { const TYPE_GIT = 'git'; const TYPE_SVN = 'svn'; const TYPE_MERCURIAL = 'hg'; private $type; private $uri; public function __construct($type, $uri) { switch ($type) { case self::TYPE_GIT: case self::TYPE_SVN: case self::TYPE_MERCURIAL: break; default: throw new Exception(pht('Unknown URI type "%s"!')); } $this->type = $type; $this->uri = $uri; } /* -( Normalizing URIs )--------------------------------------------------- */ /** * @task normal */ public function getPath() { switch ($this->type) { case self::TYPE_GIT: $uri = new PhutilURI($this->uri); if ($uri->getProtocol()) { return $uri->getPath(); } $uri = new PhutilGitURI($this->uri); if ($uri->getDomain()) { return $uri->getPath(); } return $this->uri; case self::TYPE_SVN: case self::TYPE_MERCURIAL: $uri = new PhutilURI($this->uri); if ($uri->getProtocol()) { return $uri->getPath(); } return $this->uri; } } /** * @task normal */ public function getNormalizedPath() { $path = $this->getPath(); $path = trim($path, '/'); switch ($this->type) { case self::TYPE_GIT: $path = preg_replace('/\.git$/', '', $path); break; case self::TYPE_SVN: case self::TYPE_MERCURIAL: break; } // If this is a Phabricator URI, strip it down to the callsign. We mutably // allow you to clone repositories as "/diffusion/X/anything.git", for // example. $matches = null; if (preg_match('@^(diffusion/[A-Z]+)@', $path, $matches)) { $path = $matches[1]; } return $path; } }