[ Index ] |
PHP Cross Reference of Phabricator |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Normalize repository URIs. For example, these URIs are generally equivalent 5 * and all point at the same repository: 6 * 7 * ssh://user@host/repo 8 * ssh://user@host/repo/ 9 * ssh://user@host:22/repo 10 * user@host:/repo 11 * ssh://user@host/repo.git 12 * 13 * This class can be used to normalize URIs like this, in order to detect 14 * alternate spellings of the same repository URI. In particular, the 15 * @{method:getNormalizedPath} method will return: 16 * 17 * repo 18 * 19 * ...for all of these URIs. Generally, usage looks like this: 20 * 21 * $norm_a = new PhabricatorRepositoryURINormalizer($type, $uri_a); 22 * $norm_b = new PhabricatorRepositoryURINormalizer($type, $uri_b); 23 * 24 * if ($norm_a->getNormalizedPath() == $norm_b->getNormalizedPath()) { 25 * // URIs appear to point at the same repository. 26 * } else { 27 * // URIs are very unlikely to be the same repository. 28 * } 29 * 30 * Because a repository can be hosted at arbitrarly many arbitrary URIs, there 31 * is no way to completely prevent false negatives by only examining URIs 32 * (that is, repositories with totally different URIs could really be the same). 33 * However, normalization is relatively agressive and false negatives should 34 * be rare: if normalization says two URIs are different repositories, they 35 * probably are. 36 * 37 * @task normal Normalizing URIs 38 */ 39 final class PhabricatorRepositoryURINormalizer extends Phobject { 40 41 const TYPE_GIT = 'git'; 42 const TYPE_SVN = 'svn'; 43 const TYPE_MERCURIAL = 'hg'; 44 45 private $type; 46 private $uri; 47 48 public function __construct($type, $uri) { 49 switch ($type) { 50 case self::TYPE_GIT: 51 case self::TYPE_SVN: 52 case self::TYPE_MERCURIAL: 53 break; 54 default: 55 throw new Exception(pht('Unknown URI type "%s"!')); 56 } 57 58 $this->type = $type; 59 $this->uri = $uri; 60 } 61 62 63 /* -( Normalizing URIs )--------------------------------------------------- */ 64 65 66 /** 67 * @task normal 68 */ 69 public function getPath() { 70 switch ($this->type) { 71 case self::TYPE_GIT: 72 $uri = new PhutilURI($this->uri); 73 if ($uri->getProtocol()) { 74 return $uri->getPath(); 75 } 76 77 $uri = new PhutilGitURI($this->uri); 78 if ($uri->getDomain()) { 79 return $uri->getPath(); 80 } 81 82 return $this->uri; 83 case self::TYPE_SVN: 84 case self::TYPE_MERCURIAL: 85 $uri = new PhutilURI($this->uri); 86 if ($uri->getProtocol()) { 87 return $uri->getPath(); 88 } 89 90 return $this->uri; 91 } 92 } 93 94 95 /** 96 * @task normal 97 */ 98 public function getNormalizedPath() { 99 $path = $this->getPath(); 100 $path = trim($path, '/'); 101 102 switch ($this->type) { 103 case self::TYPE_GIT: 104 $path = preg_replace('/\.git$/', '', $path); 105 break; 106 case self::TYPE_SVN: 107 case self::TYPE_MERCURIAL: 108 break; 109 } 110 111 // If this is a Phabricator URI, strip it down to the callsign. We mutably 112 // allow you to clone repositories as "/diffusion/X/anything.git", for 113 // example. 114 115 $matches = null; 116 if (preg_match('@^(diffusion/[A-Z]+)@', $path, $matches)) { 117 $path = $matches[1]; 118 } 119 120 return $path; 121 } 122 123 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Sun Nov 30 09:20:46 2014 | Cross-referenced by PHPXref 0.7.1 |