diff --git a/src/applications/repository/storage/PhabricatorRepositoryURI.php b/src/applications/repository/storage/PhabricatorRepositoryURI.php index 9aeb0c6868..364aad0a29 100644 --- a/src/applications/repository/storage/PhabricatorRepositoryURI.php +++ b/src/applications/repository/storage/PhabricatorRepositoryURI.php @@ -1,524 +1,517 @@ true, self::CONFIG_COLUMN_SCHEMA => array( 'uri' => 'text255', 'builtinProtocol' => 'text32?', 'builtinIdentifier' => 'text32?', 'credentialPHID' => 'phid?', 'ioType' => 'text32', 'displayType' => 'text32', 'isDisabled' => 'bool', ), self::CONFIG_KEY_SCHEMA => array( 'key_builtin' => array( 'columns' => array( 'repositoryPHID', 'builtinProtocol', 'builtinIdentifier', ), 'unique' => true, ), ), ) + parent::getConfiguration(); } public static function initializeNewURI(PhabricatorRepository $repository) { return id(new self()) ->attachRepository($repository) ->setRepositoryPHID($repository->getPHID()) ->setIoType(self::IO_DEFAULT) ->setDisplayType(self::DISPLAY_DEFAULT) ->setIsDisabled(0); } public function generatePHID() { return PhabricatorPHID::generateNewPHID( PhabricatorRepositoryURIPHIDType::TYPECONST); } public function attachRepository(PhabricatorRepository $repository) { $this->repository = $repository; return $this; } public function getRepository() { return $this->assertAttached($this->repository); } public function getRepositoryURIBuiltinKey() { if (!$this->getBuiltinProtocol()) { return null; } $parts = array( $this->getBuiltinProtocol(), $this->getBuiltinIdentifier(), ); return implode('.', $parts); } public function isBuiltin() { return (bool)$this->getBuiltinProtocol(); } public function getEffectiveDisplayType() { $display = $this->getDisplayType(); - if ($display != self::IO_DEFAULT) { + if ($display != self::DISPLAY_DEFAULT) { return $display; } return $this->getDefaultDisplayType(); } public function getDefaultDisplayType() { switch ($this->getEffectiveIOType()) { case self::IO_MIRROR: case self::IO_OBSERVE: - return self::DISPLAY_NEVER; case self::IO_NONE: - if ($this->isBuiltin()) { - return self::DISPLAY_NEVER; - } else { - return self::DISPLAY_ALWAYS; - } + return self::DISPLAY_NEVER; case self::IO_READ: case self::IO_READWRITE: // By default, only show the "best" version of the builtin URI, not the // other redundant versions. - if ($this->isBuiltin()) { - $repository = $this->getRepository(); - $other_uris = $repository->getURIs(); - - $identifier_value = array( - self::BUILTIN_IDENTIFIER_CALLSIGN => 3, - self::BUILTIN_IDENTIFIER_SHORTNAME => 2, - self::BUILTIN_IDENTIFIER_ID => 1, - ); - - $have_identifiers = array(); - foreach ($other_uris as $other_uri) { - if ($other_uri->getIsDisabled()) { - continue; - } - - $identifier = $other_uri->getBuiltinIdentifier(); - if (!$identifier) { - continue; - } - - $have_identifiers[$identifier] = $identifier_value[$identifier]; + $repository = $this->getRepository(); + $other_uris = $repository->getURIs(); + + $identifier_value = array( + self::BUILTIN_IDENTIFIER_CALLSIGN => 3, + self::BUILTIN_IDENTIFIER_SHORTNAME => 2, + self::BUILTIN_IDENTIFIER_ID => 1, + ); + + $have_identifiers = array(); + foreach ($other_uris as $other_uri) { + if ($other_uri->getIsDisabled()) { + continue; } - $best_identifier = max($have_identifiers); - $this_identifier = $identifier_value[$this->getBuiltinIdentifier()]; - - if ($this_identifier < $best_identifier) { - return self::DISPLAY_NEVER; + $identifier = $other_uri->getBuiltinIdentifier(); + if (!$identifier) { + continue; } + + $have_identifiers[$identifier] = $identifier_value[$identifier]; + } + + $best_identifier = max($have_identifiers); + $this_identifier = $identifier_value[$this->getBuiltinIdentifier()]; + + if ($this_identifier < $best_identifier) { + return self::DISPLAY_NEVER; } return self::DISPLAY_ALWAYS; } return self::DISPLAY_NEVER; } public function getEffectiveIOType() { $io = $this->getIoType(); if ($io != self::IO_DEFAULT) { return $io; } return $this->getDefaultIOType(); } public function getDefaultIOType() { if ($this->isBuiltin()) { $repository = $this->getRepository(); $other_uris = $repository->getURIs(); $any_observe = false; foreach ($other_uris as $other_uri) { if ($other_uri->getIoType() == self::IO_OBSERVE) { $any_observe = true; break; } } if ($any_observe) { return self::IO_READ; } else { return self::IO_READWRITE; } } return self::IO_NONE; } public function getDisplayURI() { $uri = new PhutilURI($this->getURI()); $protocol = $this->getForcedProtocol(); if ($protocol) { $uri->setProtocol($protocol); } $user = $this->getForcedUser(); if ($user) { $uri->setUser($user); } $host = $this->getForcedHost(); if ($host) { $uri->setDomain($host); } $port = $this->getForcedPort(); if ($port) { $uri->setPort($port); } $path = $this->getForcedPath(); if ($path) { $uri->setPath($path); } return $uri; } private function getForcedProtocol() { switch ($this->getBuiltinProtocol()) { case self::BUILTIN_PROTOCOL_SSH: return 'ssh'; case self::BUILTIN_PROTOCOL_HTTP: return 'http'; case self::BUILTIN_PROTOCOL_HTTPS: return 'https'; default: return null; } } private function getForcedUser() { switch ($this->getBuiltinProtocol()) { case self::BUILTIN_PROTOCOL_SSH: return AlmanacKeys::getClusterSSHUser(); default: return null; } } private function getForcedHost() { $phabricator_uri = PhabricatorEnv::getURI('/'); $phabricator_uri = new PhutilURI($phabricator_uri); $phabricator_host = $phabricator_uri->getDomain(); switch ($this->getBuiltinProtocol()) { case self::BUILTIN_PROTOCOL_SSH: $ssh_host = PhabricatorEnv::getEnvConfig('diffusion.ssh-host'); if ($ssh_host !== null) { return $ssh_host; } return $phabricator_host; case self::BUILTIN_PROTOCOL_HTTP: case self::BUILTIN_PROTOCOL_HTTPS: return $phabricator_host; default: return null; } } private function getForcedPort() { switch ($this->getBuiltinProtocol()) { case self::BUILTIN_PROTOCOL_SSH: return PhabricatorEnv::getEnvConfig('diffusion.ssh-port'); case self::BUILTIN_PROTOCOL_HTTP: case self::BUILTIN_PROTOCOL_HTTPS: default: return null; } } private function getForcedPath() { if (!$this->isBuiltin()) { return null; } $repository = $this->getRepository(); $id = $repository->getID(); $callsign = $repository->getCallsign(); $short_name = $repository->getRepositorySlug(); $clone_name = $repository->getCloneName(); if ($repository->isGit()) { $suffix = '.git'; } else if ($repository->isHg()) { $suffix = '/'; } else { $suffix = ''; } switch ($this->getBuiltinIdentifier()) { case self::BUILTIN_IDENTIFIER_ID: return "/diffusion/{$id}/{$clone_name}{$suffix}"; case self::BUILTIN_IDENTIFIER_SHORTNAME: return "/source/{$short_name}{$suffix}"; case self::BUILTIN_IDENTIFIER_CALLSIGN: return "/diffusion/{$callsign}/{$clone_name}{$suffix}"; default: return null; } } public function getViewURI() { $id = $this->getID(); return $this->getRepository()->getPathURI("uri/view/{$id}/"); } public function getEditURI() { $id = $this->getID(); return $this->getRepository()->getPathURI("uri/edit/{$id}/"); } public function getAvailableIOTypeOptions() { $options = array( self::IO_DEFAULT, self::IO_NONE, ); if ($this->isBuiltin()) { $options[] = self::IO_READ; $options[] = self::IO_WRITE; } else { $options[] = self::IO_OBSERVE; $options[] = self::IO_MIRROR; } $map = array(); $io_map = self::getIOTypeMap(); foreach ($options as $option) { $spec = idx($io_map, $option, array()); $label = idx($spec, 'label', $option); $short = idx($spec, 'short'); $name = pht('%s: %s', $label, $short); $map[$option] = $name; } return $map; } public function getAvailableDisplayTypeOptions() { $options = array( self::DISPLAY_DEFAULT, self::DISPLAY_ALWAYS, self::DISPLAY_NEVER, ); $map = array(); $display_map = self::getDisplayTypeMap(); foreach ($options as $option) { $spec = idx($display_map, $option, array()); $label = idx($spec, 'label', $option); $short = idx($spec, 'short'); $name = pht('%s: %s', $label, $short); $map[$option] = $name; } return $map; } public static function getIOTypeMap() { return array( self::IO_DEFAULT => array( 'label' => pht('Default'), 'short' => pht('Use default behavior.'), ), self::IO_OBSERVE => array( 'icon' => 'fa-download', 'color' => 'green', 'label' => pht('Observe'), 'note' => pht( 'Phabricator will observe changes to this URI and copy them.'), 'short' => pht('Copy from a remote.'), ), self::IO_MIRROR => array( 'icon' => 'fa-upload', 'color' => 'green', 'label' => pht('Mirror'), 'note' => pht( 'Phabricator will push a copy of any changes to this URI.'), 'short' => pht('Push a copy to a remote.'), ), self::IO_NONE => array( 'icon' => 'fa-times', 'color' => 'grey', 'label' => pht('No I/O'), 'note' => pht( 'Phabricator will not push or pull any changes to this URI.'), 'short' => pht('Do not perform any I/O.'), ), self::IO_READ => array( 'icon' => 'fa-folder', 'color' => 'blue', 'label' => pht('Read Only'), 'note' => pht( 'Phabricator will serve a read-only copy of the repository from '. 'this URI.'), 'short' => pht('Serve repository in read-only mode.'), ), self::IO_READWRITE => array( 'icon' => 'fa-folder-open', 'color' => 'blue', 'label' => pht('Read/Write'), 'note' => pht( 'Phabricator will serve a read/write copy of the repository from '. 'this URI.'), 'short' => pht('Serve repository in read/write mode.'), ), ); } public static function getDisplayTypeMap() { return array( self::DISPLAY_DEFAULT => array( 'label' => pht('Default'), 'short' => pht('Use default behavior.'), ), self::DISPLAY_ALWAYS => array( 'icon' => 'fa-eye', 'color' => 'green', 'label' => pht('Visible'), 'note' => pht('This URI will be shown to users as a clone URI.'), 'short' => pht('Show as a clone URI.'), ), self::DISPLAY_NEVER => array( 'icon' => 'fa-eye-slash', 'color' => 'grey', 'label' => pht('Hidden'), 'note' => pht( 'This URI will be hidden from users.'), 'short' => pht('Do not show as a clone URI.'), ), ); } /* -( PhabricatorApplicationTransactionInterface )------------------------- */ public function getApplicationTransactionEditor() { return new DiffusionURIEditor(); } public function getApplicationTransactionObject() { return $this; } public function getApplicationTransactionTemplate() { return new PhabricatorRepositoryURITransaction(); } public function willRenderTimeline( PhabricatorApplicationTransactionView $timeline, AphrontRequest $request) { return $timeline; } /* -( PhabricatorPolicyInterface )----------------------------------------- */ public function getCapabilities() { return array( PhabricatorPolicyCapability::CAN_VIEW, PhabricatorPolicyCapability::CAN_EDIT, ); } public function getPolicy($capability) { switch ($capability) { case PhabricatorPolicyCapability::CAN_VIEW: case PhabricatorPolicyCapability::CAN_EDIT: return PhabricatorPolicies::getMostOpenPolicy(); } } public function hasAutomaticCapability($capability, PhabricatorUser $viewer) { return false; } public function describeAutomaticCapability($capability) { return null; } /* -( PhabricatorExtendedPolicyInterface )--------------------------------- */ public function getExtendedPolicy($capability, PhabricatorUser $viewer) { $extended = array(); switch ($capability) { case PhabricatorPolicyCapability::CAN_EDIT: // To edit a repository URI, you must be able to edit the // corresponding repository. $extended[] = array($this->getRepository(), $capability); break; } return $extended; } } diff --git a/src/docs/user/userguide/diffusion_uris.diviner b/src/docs/user/userguide/diffusion_uris.diviner index 3ff122e85e..b560f4ab3f 100644 --- a/src/docs/user/userguide/diffusion_uris.diviner +++ b/src/docs/user/userguide/diffusion_uris.diviner @@ -1,47 +1,275 @@ @title Diffusion User Guide: URIs @group userguide Guide to configuring repository URIs for fetching, cloning and mirroring. Overview ======== WARNING: This document describes a feature which is still under development, and is not necessarily accurate or complete. -Phabricator can host, observe, mirror, and proxy repositories. For example, -here are some supported use cases: +Phabricator can create, host, observe, mirror, proxy, and import repositories. +For example, you can: **Host Repositories**: Phabricator can host repositories locally. Phabricator maintains the writable master version of the repository, and you can push and pull the repository. This is the most straightforward kind of repository configuration, and similar to repositories on other services like GitHub or Bitbucket. **Observe Repositories**: Phabricator can create a copy of an repository which is hosted elsewhere (like GitHub or Bitbucket) and track updates to the remote repository. This will create a read-only copy of the repository in Phabricator. **Mirror Repositories**: Phabricator can publish any repository to mirrors, updating the mirrors as changes are made to the repository. This works with both local hosted repositories and remote repositories that Phabricator is observing. **Proxy Repositories**: If you are observing a repository, you can allow users to read Phabricator's copy of the repository. Phabricator supports granular read permissions, so this can let you open a private repository up a little bit in a flexible way. **Import Repositories**: If you have a repository elsewhere that you want to host on Phabricator, you can observe the remote repository first, then turn the tracking off once the repository fully synchronizes. This allows you to copy an existing repository and begin hosting it in Phabricator. You can also import repositories by creating an empty hosted repository and then pushing everything to the repository directly. You configure the behavior of a Phabricator repository by adding and configuring URIs and marking them to be fetched from, mirrored to, clonable, and so on. By configuring all the URIs that a repository should interact with and expose to users, you configure the read, write, and mirroring behavior of the repository. + +The remainder of this document walks through this configuration in greater +detail. + + +Host a Repository +================= + +You can create new repositories that Phabricator will host, like you would +create repositories on services like GitHub or Bitbucket. Phabricator will +serve a read-write copy of the repository and you can clone it from Phabricator +and push changes to Phabricator. + +If you haven't already, you may need to configure Phabricator for hosting +before you can create your first hosted repository. For a detailed guide, +see @{article:Diffusion User Guide: Repository Hosting}. + +This is the default mode for new repositories. To host a repository: + + - Create a new repository. + - Activate it. + +Phabricator will create an empty repository and allow you to fetch from it and +push to it. + + +Observe a Repository +==================== + +If you have an existing repository hosted on another service (like GitHub, +Bitbucket, or a private server) that you want to work with in Phabricator, +you can configure Phabricator to observe it. + +When observing a repository, Phabricator will keep track of changes in the +remote repository and allow you to browse and interact with the repository from +the web UI in Diffusion and other applications, but you can continue hosting it +elsewhere. + +To observe a repository: + + - Create a new repository, but don't activate it yet. + - Add the remote URI you want to observe as a repository URI. + - Set the **I/O Type** for the URI to **Observe**. + - If necessary, configure a credential. + - Activate the repository. + +Phabricator will perform an initial import of the repository, creating a local +read-only copy. Once this process completes, it will continue keeping track of +changes in the remote, fetching them, and reflecting them in the UI. + + +Mirror a Repository +=================== + +NOTE: Mirroring is not supported in Subversion. + +You can create a read-only mirror of an existing repository. Phabricator will +push all changes made to the repository to the mirror. + +For example, if you have a repository hosted in Phabricator that you want to +mirror to GitHub, you can configure Phabricator to automatically maintain the +mirror. This is how the upstream repositories are set up. + +You can mirror any repository, even if Phabricator is only observing it and not +hosting it directly. + +To begin mirroring a repository: + + - Create a hosted or observed repository by following the relevant + instructions above. + - Add the remote URI you want to mirror to as a repository URI. + - Set the **I/O Type** for the URI to **Mirror**. + - If necessary, configure a credential. + +To stop mirroring: + + - Disable the mirror URI; or + - Change the **I/O Type** for the URI to **None**. + + +Import a Repository +=================== + +If you have an existing repository that you want to move so it is hosted on +Phabricator, there are three ways to do it: + +**Push Everything**: //(Git, Mercurial)// Create a new empty hosted repository +according to the instructions above. Once the empty repository initializes, +push your entire existing repository to it. + +**Observe First**: //(Git, Mercurial)// Observe the existing repository first, +according to the instructions above. Once Phabricator's copy of the repository +is fully synchronized, change the **I/O Type** for the **Observe** URI to +**None** to stop fetching changes from the remote. + +By default, this will automatically make Phabricator's copy of the repository +writable, and you can begin pushing to it. If you've adjusted URI +configuration away from the defaults, you may need to set at least one URI +to **Read/Write** mode so you can push to it. + +**Copy on Disk**: //(Git, Mercurial, Subversion)// Create a new empty hosted +repository according to the instructions above, but do not activate it yet. + +Using the **Storage** tab, find the location of the repository's working copy +on disk, and place a working copy of the repository you wish to import there. + +For Git and Mercurial, use a bare working copy for best results. + +This is the only way to import a Subversion repository because only the master +copy of the repository has history. + +Once you've put a working copy in the right place on disk, activate the +repository. + + +Customizing Displayed Clone URIs +================================ + +If you have an unusual configuration and want the UI to offers users specific +clone URIs other than the URIs that Phabricator serves or interacts with, you +can add those URIs with the **I/O Type** set to **None** and then set their +**Display Type** to **Always**. + +Likewise, you can set the **Display Type** of any URIs you do //not// want +to be visible to **Never**. + +This allows you to precisely configure which clone URIs are shown to users for +a repository. + + +Reference: I/O Types +==================== + +This section details the available **I/O Type** options for URIs. + +Each repository has some **builtin** URIs. These are URIs hosted by Phabricator +itself. The modes available for each URI depend primarily on whether it is a +builtin URI or not. + +**Default**: This setting has Phabricator guess the correct option for the +URI. + +For **builtin** URIs, the default behavior is //Read/Write// if the repository +is hosted, and //Read-Only// if the repository is observed. + +For custom URIs, the default type is //None// because we can not automatically +guess if you want to ignore, observe, or mirror a URI and //None// is the +safest default. + +**Observe**: Phabricator will observe this repository and regularly fetch any +changes made to it to a local read-only copy. + +You can not observe builtin URIs because reading a repository from itself +does not make sense. + +You can not add a URI in Observe mode if an existing builtin URI is in +//Read/Write// mode, because this would mean the repository had two different +authorities: the observed remote copy and the hosted local copy. Take the +other URI out of //Read/Write// mode first. + +**Mirror**: Phabricator will push any changes made to this repository to the +remote URI, keeping a read-only mirror hosted at that URI up to date. + +This works for both observed and hosted repositories. + +This option is not available for builtin URIs because it does not make sense +to mirror a repository to itself. + +It is possible to mirror a repository to another repository that is also +hosted by Phabricator by adding that other repository's URI, although this is +silly and probably very rarely of any use. + +**None**: Phabricator will not fetch changes from or push changes to this URI. +For builtin URIs, it will not let users fetch changes from or push changes to +this URI. + +You can use this mode to turn off an Observe URI after an import, stop a Mirror +URI from updating, or to add URIs that you're only using to customize which +clone URIs are displayed to the user but don't want Phabricator to interact +with directly. + +**Read Only**: Phabricator will serve the repository from this URI in read-only +mode. Users will be able to fetch from it but will not be able to push to it. + +Because Phabricator must be able to serve the repository from URIs configured +in this mode, this option is only available for builtin URIs. + +**Read/Write**: Phabricator will serve the repository from this URI in +read/write mode. Users will be able to fetch from it and push to it. + +URIs can not be set into this mode if another URI is set to //Observe// mode, +because that would mean the repository had two different authorities: the +observed remote copy and the hosted local copy. Take the other URI out of +//Observe// mode first. + +Because Phabricator must be able to serve the repository from URIs configured +in this mode, this option is only available for builtin URIs. + + +Reference: Display Types +======================== + +This section details the available **Display Type** options for URIs. + +**Default**: Phabricator will guess the correct option for the URI. It +guesses based on the configured **I/O Type** and whether the URI is +**builtin** or not. + +For //Observe//, //Mirror// and //None// URIs, the default is //Never//. + +For builtin URIs in //Read Only// or //Read/Write// mode, the most +human-readable URI defaults to //Always// and the others default to //Never//. + +**Always**: This URI will be shown to users as a clone/checkout URI. You can +add URIs in this mode to cutomize exactly what users are shown. + +**Never**: This URI will not be shown to users. You can hide less-preferred +URIs to guide users to the URIs they should be using to interact with the +repository. + + +Next Steps +========== + +Continue by: + + - configuring Phabricator to host repositories with + @{article:Diffusion User Guide: Repository Hosting}.