123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518 |
- <?php
- /*
- * This file is part of Composer.
- *
- * (c) Nils Adermann <naderman@naderman.de>
- * Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Composer\Util;
- use Composer\IO\IOInterface;
- use Symfony\Component\Process\Process;
- /**
- * @author Matt Whittom <Matt.Whittom@veteransunited.com>
- */
- class Perforce
- {
- protected $path;
- protected $p4Depot;
- protected $p4Client;
- protected $p4User;
- protected $p4Password;
- protected $p4Port;
- protected $p4Stream;
- protected $p4ClientSpec;
- protected $p4DepotType;
- protected $p4Branch;
- protected $process;
- protected $uniquePerforceClientName;
- protected $windowsFlag;
- public static function createPerforce($repoConfig, $port, $path, ProcessExecutor $process = null)
- {
- if (!isset($process)) {
- $process = new ProcessExecutor;
- }
- $isWindows = defined('PHP_WINDOWS_VERSION_BUILD');
- $perforce = new Perforce($repoConfig, $port, $path, $process, $isWindows);
- return $perforce;
- }
- public function __construct($repoConfig, $port, $path, ProcessExecutor $process, $isWindows)
- {
- $this->windowsFlag = $isWindows;
- $this->p4Port = $port;
- $this->path = $path;
- $fs = new Filesystem();
- $fs->ensureDirectoryExists($path);
- $this->process = $process;
- $this->initialize($repoConfig);
- }
- public function initialize($repoConfig)
- {
- $this->uniquePerforceClientName = $this->generateUniquePerforceClientName();
- if (null == $repoConfig) {
- return;
- }
- if (isset($repoConfig['unique_perforce_client_name'])) {
- $this->uniquePerforceClientName = $repoConfig['unique_perforce_client_name'];
- }
- if (isset($repoConfig['depot'])) {
- $this->p4Depot = $repoConfig['depot'];
- }
- if (isset($repoConfig['branch'])) {
- $this->p4Branch = $repoConfig['branch'];
- }
- if (isset($repoConfig['p4user'])) {
- $this->p4User = $repoConfig['p4user'];
- } else {
- $this->p4User = $this->getP4variable('P4USER');
- }
- if (isset($repoConfig['p4password'])) {
- $this->p4Password = $repoConfig['p4password'];
- }
- }
- public function initializeDepotAndBranch($depot, $branch)
- {
- if (isset($depot)) {
- $this->p4Depot = $depot;
- }
- if (isset($branch)) {
- $this->p4Branch = $branch;
- }
- }
- public function generateUniquePerforceClientName()
- {
- return gethostname() . "_" . time();
- }
- public function cleanupClientSpec()
- {
- $client = $this->getClient();
- $command = 'p4 client -d $client';
- $this->executeCommand($command);
- $clientSpec = $this->getP4ClientSpec();
- $fileSystem = new FileSystem($this->process);
- $fileSystem->remove($clientSpec);
- }
- protected function executeCommand($command)
- {
- $result = "";
- $this->process->execute($command, $result);
- return $result;
- }
- public function getClient()
- {
- if (!isset($this->p4Client)) {
- $cleanStreamName = str_replace('@', '', str_replace('/', '_', str_replace('//', '', $this->getStream())));
- $this->p4Client = 'composer_perforce_' . $this->uniquePerforceClientName . '_' . $cleanStreamName;
- }
- return $this->p4Client;
- }
- protected function getPath()
- {
- return $this->path;
- }
- protected function getPort()
- {
- return $this->p4Port;
- }
- public function setStream($stream)
- {
- $this->p4Stream = $stream;
- $index = strrpos($stream, '/');
- //Stream format is //depot/stream, while non-streaming depot is //depot
- if ($index > 2) {
- $this->p4DepotType = 'stream';
- }
- }
- public function isStream()
- {
- return (strcmp($this->p4DepotType, 'stream') === 0);
- }
- public function getStream()
- {
- if (!isset($this->p4Stream)) {
- if ($this->isStream()) {
- $this->p4Stream = '//' . $this->p4Depot . '/' . $this->p4Branch;
- } else {
- $this->p4Stream = '//' . $this->p4Depot;
- }
- }
- return $this->p4Stream;
- }
- public function getStreamWithoutLabel($stream)
- {
- $index = strpos($stream, '@');
- if ($index === false) {
- return $stream;
- }
- return substr($stream, 0, $index);
- }
- public function getP4ClientSpec()
- {
- $p4clientSpec = $this->path . '/' . $this->getClient() . '.p4.spec';
- return $p4clientSpec;
- }
- public function getUser()
- {
- return $this->p4User;
- }
- public function queryP4User(IOInterface $io)
- {
- $this->getUser();
- if (strlen($this->p4User) > 0) {
- return;
- }
- $this->p4User = $this->getP4variable('P4USER');
- if (strlen($this->p4User) > 0) {
- return;
- }
- $this->p4User = $io->ask('Enter P4 User:');
- if ($this->windowsFlag) {
- $command = 'p4 set P4USER=' . $this->p4User;
- } else {
- $command = 'export P4USER=' . $this->p4User;
- }
- $result = $this->executeCommand($command);
- }
- protected function getP4variable($name)
- {
- if ($this->windowsFlag) {
- $command = 'p4 set';
- $result = $this->executeCommand($command);
- $resArray = explode(PHP_EOL, $result);
- foreach ($resArray as $line) {
- $fields = explode('=', $line);
- if (strcmp($name, $fields[0]) == 0) {
- $index = strpos($fields[1], ' ');
- if ($index === false) {
- $value = $fields[1];
- } else {
- $value = substr($fields[1], 0, $index);
- }
- $value = trim($value);
- return $value;
- }
- }
- } else {
- $command = 'echo $' . $name;
- $result = trim($this->executeCommand($command));
- return $result;
- }
- }
- public function queryP4Password(IOInterface $io)
- {
- if (isset($this->p4Password)) {
- return $this->p4Password;
- }
- $password = $this->getP4variable('P4PASSWD');
- if (strlen($password) <= 0) {
- $password = $io->askAndHideAnswer('Enter password for Perforce user ' . $this->getUser() . ': ');
- }
- $this->p4Password = $password;
- return $password;
- }
- public function generateP4Command($command, $useClient = true)
- {
- $p4Command = 'p4 ';
- $p4Command = $p4Command . '-u ' . $this->getUser() . ' ';
- if ($useClient) {
- $p4Command = $p4Command . '-c ' . $this->getClient() . ' ';
- }
- $p4Command = $p4Command . '-p ' . $this->getPort() . ' ';
- $p4Command = $p4Command . $command;
- return $p4Command;
- }
- public function isLoggedIn()
- {
- $command = $this->generateP4Command('login -s', false);
- $result = trim($this->executeCommand($command));
- $index = strpos($result, $this->getUser());
- if ($index === false) {
- return false;
- }
- return true;
- }
- public function connectClient()
- {
- $p4CreateClientCommand = $this->generateP4Command('client -i < ' . $this->getP4ClientSpec());
- $this->executeCommand($p4CreateClientCommand);
- }
- public function syncCodeBase($label)
- {
- $prevDir = getcwd();
- chdir($this->path);
- $p4SyncCommand = $this->generateP4Command('sync -f ');
- if (isset($label)) {
- if (strcmp($label, 'dev-master') != 0) {
- $p4SyncCommand = $p4SyncCommand . '@' . $label;
- }
- }
- $result = $this->executeCommand($p4SyncCommand);
- chdir($prevDir);
- }
- public function writeClientSpecToFile($spec)
- {
- fwrite($spec, 'Client: ' . $this->getClient() . PHP_EOL . PHP_EOL);
- fwrite($spec, 'Update: ' . date('Y/m/d H:i:s') . PHP_EOL . PHP_EOL);
- fwrite($spec, 'Access: ' . date('Y/m/d H:i:s') . PHP_EOL);
- fwrite($spec, 'Owner: ' . $this->getUser() . PHP_EOL . PHP_EOL);
- fwrite($spec, 'Description:' . PHP_EOL);
- fwrite($spec, ' Created by ' . $this->getUser() . ' from composer.' . PHP_EOL . PHP_EOL);
- fwrite($spec, 'Root: ' . $this->getPath() . PHP_EOL . PHP_EOL);
- fwrite($spec, 'Options: noallwrite noclobber nocompress unlocked modtime rmdir' . PHP_EOL . PHP_EOL);
- fwrite($spec, 'SubmitOptions: revertunchanged' . PHP_EOL . PHP_EOL);
- fwrite($spec, 'LineEnd: local' . PHP_EOL . PHP_EOL);
- if ($this->isStream()) {
- fwrite($spec, 'Stream:' . PHP_EOL);
- fwrite($spec, ' ' . $this->getStreamWithoutLabel($this->p4Stream) . PHP_EOL);
- } else {
- fwrite(
- $spec,
- 'View: ' . $this->getStream() . '/... //' . $this->getClient() . '/... ' . PHP_EOL
- );
- }
- }
- public function writeP4ClientSpec()
- {
- $clientSpec = $this->getP4ClientSpec();
- $spec = fopen($clientSpec, 'w');
- try {
- $this->writeClientSpecToFile($spec);
- } catch (\Exception $e) {
- fclose($spec);
- throw $e;
- }
- fclose($spec);
- }
- protected function read($pipe, $name)
- {
- if (feof($pipe)) {
- return;
- }
- $line = fgets($pipe);
- while ($line != false) {
- $line = fgets($pipe);
- }
- return;
- }
- public function windowsLogin($password)
- {
- $command = $this->generateP4Command(' login -a');
- $process = new Process($command, null, null, $password);
- return $process->run();
- }
- public function p4Login(IOInterface $io)
- {
- $this->queryP4User($io);
- if (!$this->isLoggedIn()) {
- $password = $this->queryP4Password($io);
- if ($this->windowsFlag) {
- $this->windowsLogin($password);
- } else {
- $command = 'echo ' . $password . ' | ' . $this->generateP4Command(' login -a', false);
- $this->executeCommand($command);
- }
- }
- }
- public static function checkServerExists($url, ProcessExecutor $processExecutor)
- {
- $result = '';
- $processExecutor->execute('p4 -p ' . $url . ' info -s', $result);
- return false === strpos($result, 'error');
- }
- public function getComposerInformation($identifier)
- {
- $index = strpos($identifier, '@');
- if ($index === false) {
- $composerJson = $identifier. '/composer.json';
- return $this->getComposerInformationFromPath($composerJson);
- }
- return $this->getComposerInformationFromLabel($identifier, $index);
- }
- public function getComposerInformationFromPath($composerJson)
- {
- $command = $this->generateP4Command(' print ' . $composerJson);
- $result = $this->executeCommand($command);
- $index = strpos($result, '{');
- if ($index === false) {
- return '';
- }
- if ($index >= 0) {
- $rawData = substr($result, $index);
- $composer_info = json_decode($rawData, true);
- return $composer_info;
- }
- return '';
- }
- public function getComposerInformationFromLabel($identifier, $index)
- {
- $composerJsonPath = substr($identifier, 0, $index) . '/composer.json' . substr($identifier, $index);
- $command = $this->generateP4Command(' files ' . $composerJsonPath, false);
- $result = $this->executeCommand($command);
- $index2 = strpos($result, 'no such file(s).');
- if ($index2 === false) {
- $index3 = strpos($result, 'change');
- if (!($index3 === false)) {
- $phrase = trim(substr($result, $index3));
- $fields = explode(' ', $phrase);
- $id = $fields[1];
- $composerJson = substr($identifier, 0, $index) . '/composer.json@' . $id;
- return $this->getComposerInformationFromPath($composerJson);
- }
- }
- return "";
- }
- public function getBranches()
- {
- $possibleBranches = array();
- if (!$this->isStream()) {
- $possibleBranches[$this->p4Branch] = $this->getStream();
- } else {
- $command = $this->generateP4Command('streams //' . $this->p4Depot . '/...');
- $result = $this->executeCommand($command);
- $resArray = explode(PHP_EOL, $result);
- foreach ($resArray as $line) {
- $resBits = explode(' ', $line);
- if (count($resBits) > 4) {
- $branch = preg_replace('/[^A-Za-z0-9 ]/', '', $resBits[4]);
- $possibleBranches[$branch] = $resBits[1];
- }
- }
- }
- $branches = array();
- $branches['master'] = $possibleBranches[$this->p4Branch];
- return $branches;
- }
- public function getTags()
- {
- $command = $this->generateP4Command('labels');
- $result = $this->executeCommand($command);
- $resArray = explode(PHP_EOL, $result);
- $tags = array();
- foreach ($resArray as $line) {
- $index = strpos($line, 'Label');
- if (!($index === false)) {
- $fields = explode(' ', $line);
- $tags[$fields[1]] = $this->getStream() . '@' . $fields[1];
- }
- }
- return $tags;
- }
- public function checkStream()
- {
- $command = $this->generateP4Command('depots', false);
- $result = $this->executeCommand($command);
- $resArray = explode(PHP_EOL, $result);
- foreach ($resArray as $line) {
- $index = strpos($line, 'Depot');
- if (!($index === false)) {
- $fields = explode(' ', $line);
- if (strcmp($this->p4Depot, $fields[1]) === 0) {
- $this->p4DepotType = $fields[3];
- return $this->isStream();
- }
- }
- }
- return false;
- }
- protected function getChangeList($reference)
- {
- $index = strpos($reference, '@');
- if ($index === false) {
- return;
- }
- $label = substr($reference, $index);
- $command = $this->generateP4Command(' changes -m1 ' . $label);
- $changes = $this->executeCommand($command);
- if (strpos($changes, 'Change') !== 0) {
- return;
- }
- $fields = explode(' ', $changes);
- $changeList = $fields[1];
- return $changeList;
- }
- public function getCommitLogs($fromReference, $toReference)
- {
- $fromChangeList = $this->getChangeList($fromReference);
- if ($fromChangeList == null) {
- return;
- }
- $toChangeList = $this->getChangeList($toReference);
- if ($toChangeList == null) {
- return;
- }
- $index = strpos($fromReference, '@');
- $main = substr($fromReference, 0, $index) . '/...';
- $command = $this->generateP4Command('filelog ' . $main . '@' . $fromChangeList. ',' . $toChangeList);
- $result = $this->executeCommand($command);
- return $result;
- }
- }
|