Perforce.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Util;
  12. use Composer\IO\IOInterface;
  13. use Symfony\Component\Process\Process;
  14. /**
  15. * @author Matt Whittom <Matt.Whittom@veteransunited.com>
  16. */
  17. class Perforce
  18. {
  19. protected $path;
  20. protected $p4Depot;
  21. protected $p4Client;
  22. protected $p4User;
  23. protected $p4Password;
  24. protected $p4Port;
  25. protected $p4Stream;
  26. protected $p4ClientSpec;
  27. protected $p4DepotType;
  28. protected $p4Branch;
  29. protected $process;
  30. protected $uniquePerforceClientName;
  31. protected $windowsFlag;
  32. protected $commandResult;
  33. protected $io;
  34. protected $filesystem;
  35. public function __construct($repoConfig, $port, $path, ProcessExecutor $process, $isWindows, IOInterface $io)
  36. {
  37. $this->windowsFlag = $isWindows;
  38. $this->p4Port = $port;
  39. $this->initializePath($path);
  40. $this->process = $process;
  41. $this->initialize($repoConfig);
  42. $this->io = $io;
  43. }
  44. public static function create($repoConfig, $port, $path, ProcessExecutor $process, IOInterface $io)
  45. {
  46. return new Perforce($repoConfig, $port, $path, $process, Platform::isWindows(), $io);
  47. }
  48. public static function checkServerExists($url, ProcessExecutor $processExecutor)
  49. {
  50. $output = null;
  51. return 0 === $processExecutor->execute('p4 -p ' . $url . ' info -s', $output);
  52. }
  53. public function initialize($repoConfig)
  54. {
  55. $this->uniquePerforceClientName = $this->generateUniquePerforceClientName();
  56. if (!$repoConfig) {
  57. return;
  58. }
  59. if (isset($repoConfig['unique_perforce_client_name'])) {
  60. $this->uniquePerforceClientName = $repoConfig['unique_perforce_client_name'];
  61. }
  62. if (isset($repoConfig['depot'])) {
  63. $this->p4Depot = $repoConfig['depot'];
  64. }
  65. if (isset($repoConfig['branch'])) {
  66. $this->p4Branch = $repoConfig['branch'];
  67. }
  68. if (isset($repoConfig['p4user'])) {
  69. $this->p4User = $repoConfig['p4user'];
  70. } else {
  71. $this->p4User = $this->getP4variable('P4USER');
  72. }
  73. if (isset($repoConfig['p4password'])) {
  74. $this->p4Password = $repoConfig['p4password'];
  75. }
  76. }
  77. public function initializeDepotAndBranch($depot, $branch)
  78. {
  79. if (isset($depot)) {
  80. $this->p4Depot = $depot;
  81. }
  82. if (isset($branch)) {
  83. $this->p4Branch = $branch;
  84. }
  85. }
  86. public function generateUniquePerforceClientName()
  87. {
  88. return gethostname() . "_" . time();
  89. }
  90. public function cleanupClientSpec()
  91. {
  92. $client = $this->getClient();
  93. $task = 'client -d ' . $client;
  94. $useP4Client = false;
  95. $command = $this->generateP4Command($task, $useP4Client);
  96. $this->executeCommand($command);
  97. $clientSpec = $this->getP4ClientSpec();
  98. $fileSystem = $this->getFilesystem();
  99. $fileSystem->remove($clientSpec);
  100. }
  101. protected function executeCommand($command)
  102. {
  103. $this->commandResult = "";
  104. $exit_code = $this->process->execute($command, $this->commandResult);
  105. return $exit_code;
  106. }
  107. public function getClient()
  108. {
  109. if (!isset($this->p4Client)) {
  110. $cleanStreamName = str_replace('@', '', str_replace('/', '_', str_replace('//', '', $this->getStream())));
  111. $this->p4Client = 'composer_perforce_' . $this->uniquePerforceClientName . '_' . $cleanStreamName;
  112. }
  113. return $this->p4Client;
  114. }
  115. protected function getPath()
  116. {
  117. return $this->path;
  118. }
  119. public function initializePath($path)
  120. {
  121. $this->path = $path;
  122. $fs = $this->getFilesystem();
  123. $fs->ensureDirectoryExists($path);
  124. }
  125. protected function getPort()
  126. {
  127. return $this->p4Port;
  128. }
  129. public function setStream($stream)
  130. {
  131. $this->p4Stream = $stream;
  132. $index = strrpos($stream, '/');
  133. //Stream format is //depot/stream, while non-streaming depot is //depot
  134. if ($index > 2) {
  135. $this->p4DepotType = 'stream';
  136. }
  137. }
  138. public function isStream()
  139. {
  140. return (strcmp($this->p4DepotType, 'stream') === 0);
  141. }
  142. public function getStream()
  143. {
  144. if (!isset($this->p4Stream)) {
  145. if ($this->isStream()) {
  146. $this->p4Stream = '//' . $this->p4Depot . '/' . $this->p4Branch;
  147. } else {
  148. $this->p4Stream = '//' . $this->p4Depot;
  149. }
  150. }
  151. return $this->p4Stream;
  152. }
  153. public function getStreamWithoutLabel($stream)
  154. {
  155. $index = strpos($stream, '@');
  156. if ($index === false) {
  157. return $stream;
  158. }
  159. return substr($stream, 0, $index);
  160. }
  161. public function getP4ClientSpec()
  162. {
  163. $p4clientSpec = $this->path . '/' . $this->getClient() . '.p4.spec';
  164. return $p4clientSpec;
  165. }
  166. public function getUser()
  167. {
  168. return $this->p4User;
  169. }
  170. public function setUser($user)
  171. {
  172. $this->p4User = $user;
  173. }
  174. public function queryP4User()
  175. {
  176. $this->getUser();
  177. if (strlen($this->p4User) > 0) {
  178. return;
  179. }
  180. $this->p4User = $this->getP4variable('P4USER');
  181. if (strlen($this->p4User) > 0) {
  182. return;
  183. }
  184. $this->p4User = $this->io->ask('Enter P4 User:');
  185. if ($this->windowsFlag) {
  186. $command = 'p4 set P4USER=' . $this->p4User;
  187. } else {
  188. $command = 'export P4USER=' . $this->p4User;
  189. }
  190. $this->executeCommand($command);
  191. }
  192. protected function getP4variable($name)
  193. {
  194. if ($this->windowsFlag) {
  195. $command = 'p4 set';
  196. $this->executeCommand($command);
  197. $result = trim($this->commandResult);
  198. $resArray = explode(PHP_EOL, $result);
  199. foreach ($resArray as $line) {
  200. $fields = explode('=', $line);
  201. if (strcmp($name, $fields[0]) == 0) {
  202. $index = strpos($fields[1], ' ');
  203. if ($index === false) {
  204. $value = $fields[1];
  205. } else {
  206. $value = substr($fields[1], 0, $index);
  207. }
  208. $value = trim($value);
  209. return $value;
  210. }
  211. }
  212. return null;
  213. } else {
  214. $command = 'echo $' . $name;
  215. $this->executeCommand($command);
  216. $result = trim($this->commandResult);
  217. return $result;
  218. }
  219. }
  220. public function queryP4Password()
  221. {
  222. if (isset($this->p4Password)) {
  223. return $this->p4Password;
  224. }
  225. $password = $this->getP4variable('P4PASSWD');
  226. if (strlen($password) <= 0) {
  227. $password = $this->io->askAndHideAnswer('Enter password for Perforce user ' . $this->getUser() . ': ');
  228. }
  229. $this->p4Password = $password;
  230. return $password;
  231. }
  232. public function generateP4Command($command, $useClient = true)
  233. {
  234. $p4Command = 'p4 ';
  235. $p4Command = $p4Command . '-u ' . $this->getUser() . ' ';
  236. if ($useClient) {
  237. $p4Command = $p4Command . '-c ' . $this->getClient() . ' ';
  238. }
  239. $p4Command = $p4Command . '-p ' . $this->getPort() . ' ';
  240. $p4Command = $p4Command . $command;
  241. return $p4Command;
  242. }
  243. public function isLoggedIn()
  244. {
  245. $command = $this->generateP4Command('login -s', false);
  246. $exitCode = $this->executeCommand($command);
  247. if ($exitCode) {
  248. $errorOutput = $this->process->getErrorOutput();
  249. $index = strpos($errorOutput, $this->getUser());
  250. if ($index === false) {
  251. $index = strpos($errorOutput, 'p4');
  252. if ($index === false) {
  253. return false;
  254. }
  255. throw new \Exception('p4 command not found in path: ' . $errorOutput);
  256. }
  257. throw new \Exception('Invalid user name: ' . $this->getUser());
  258. }
  259. return true;
  260. }
  261. public function connectClient()
  262. {
  263. $p4CreateClientCommand = $this->generateP4Command(
  264. 'client -i < ' . str_replace(" ", "\\ ", $this->getP4ClientSpec())
  265. );
  266. $this->executeCommand($p4CreateClientCommand);
  267. }
  268. public function syncCodeBase($sourceReference)
  269. {
  270. $prevDir = getcwd();
  271. chdir($this->path);
  272. $p4SyncCommand = $this->generateP4Command('sync -f ');
  273. if (null !== $sourceReference) {
  274. $p4SyncCommand = $p4SyncCommand . '@' . $sourceReference;
  275. }
  276. $this->executeCommand($p4SyncCommand);
  277. chdir($prevDir);
  278. }
  279. public function writeClientSpecToFile($spec)
  280. {
  281. fwrite($spec, 'Client: ' . $this->getClient() . PHP_EOL . PHP_EOL);
  282. fwrite($spec, 'Update: ' . date('Y/m/d H:i:s') . PHP_EOL . PHP_EOL);
  283. fwrite($spec, 'Access: ' . date('Y/m/d H:i:s') . PHP_EOL);
  284. fwrite($spec, 'Owner: ' . $this->getUser() . PHP_EOL . PHP_EOL);
  285. fwrite($spec, 'Description:' . PHP_EOL);
  286. fwrite($spec, ' Created by ' . $this->getUser() . ' from composer.' . PHP_EOL . PHP_EOL);
  287. fwrite($spec, 'Root: ' . $this->getPath() . PHP_EOL . PHP_EOL);
  288. fwrite($spec, 'Options: noallwrite noclobber nocompress unlocked modtime rmdir' . PHP_EOL . PHP_EOL);
  289. fwrite($spec, 'SubmitOptions: revertunchanged' . PHP_EOL . PHP_EOL);
  290. fwrite($spec, 'LineEnd: local' . PHP_EOL . PHP_EOL);
  291. if ($this->isStream()) {
  292. fwrite($spec, 'Stream:' . PHP_EOL);
  293. fwrite($spec, ' ' . $this->getStreamWithoutLabel($this->p4Stream) . PHP_EOL);
  294. } else {
  295. fwrite(
  296. $spec,
  297. 'View: ' . $this->getStream() . '/... //' . $this->getClient() . '/... ' . PHP_EOL
  298. );
  299. }
  300. }
  301. public function writeP4ClientSpec()
  302. {
  303. $clientSpec = $this->getP4ClientSpec();
  304. $spec = fopen($clientSpec, 'w');
  305. try {
  306. $this->writeClientSpecToFile($spec);
  307. } catch (\Exception $e) {
  308. fclose($spec);
  309. throw $e;
  310. }
  311. fclose($spec);
  312. }
  313. protected function read($pipe, $name)
  314. {
  315. if (feof($pipe)) {
  316. return;
  317. }
  318. $line = fgets($pipe);
  319. while ($line !== false) {
  320. $line = fgets($pipe);
  321. }
  322. return;
  323. }
  324. public function windowsLogin($password)
  325. {
  326. $command = $this->generateP4Command(' login -a');
  327. $process = new Process($command, null, null, $password);
  328. return $process->run();
  329. }
  330. public function p4Login()
  331. {
  332. $this->queryP4User();
  333. if (!$this->isLoggedIn()) {
  334. $password = $this->queryP4Password();
  335. if ($this->windowsFlag) {
  336. $this->windowsLogin($password);
  337. } else {
  338. $command = 'echo ' . $password . ' | ' . $this->generateP4Command(' login -a', false);
  339. $exitCode = $this->executeCommand($command);
  340. $result = trim($this->commandResult);
  341. if ($exitCode) {
  342. throw new \Exception("Error logging in:" . $this->process->getErrorOutput());
  343. }
  344. }
  345. }
  346. }
  347. public function getComposerInformation($identifier)
  348. {
  349. $index = strpos($identifier, '@');
  350. if ($index === false) {
  351. $composerJson = $identifier. '/composer.json';
  352. return $this->getComposerInformationFromPath($composerJson);
  353. }
  354. return $this->getComposerInformationFromLabel($identifier, $index);
  355. }
  356. public function getFileContent($file, $identifier)
  357. {
  358. $path = $this->getFilePath($file, $identifier);
  359. $command = $this->generateP4Command(' print ' . $path);
  360. $this->executeCommand($command);
  361. $result = $this->commandResult;
  362. if (!trim($result)) {
  363. return null;
  364. }
  365. return $result;
  366. }
  367. public function getFilePath($file, $identifier)
  368. {
  369. $index = strpos($identifier, '@');
  370. if ($index === false) {
  371. $path = $identifier. '/' . $file;
  372. return $path;
  373. } else {
  374. $path = substr($identifier, 0, $index) . '/' . $file . substr($identifier, $index);
  375. $command = $this->generateP4Command(' files ' . $path, false);
  376. $this->executeCommand($command);
  377. $result = $this->commandResult;
  378. $index2 = strpos($result, 'no such file(s).');
  379. if ($index2 === false) {
  380. $index3 = strpos($result, 'change');
  381. if ($index3 !== false) {
  382. $phrase = trim(substr($result, $index3));
  383. $fields = explode(' ', $phrase);
  384. return substr($identifier, 0, $index) . '/' . $file . '@' . $fields[1];
  385. }
  386. }
  387. }
  388. return null;
  389. }
  390. public function getComposerInformationFromPath($composerJson)
  391. {
  392. $command = $this->generateP4Command(' print ' . $composerJson);
  393. $this->executeCommand($command);
  394. $result = $this->commandResult;
  395. $index = strpos($result, '{');
  396. if ($index === false) {
  397. return '';
  398. }
  399. if ($index >= 0) {
  400. $rawData = substr($result, $index);
  401. $composer_info = json_decode($rawData, true);
  402. return $composer_info;
  403. }
  404. return '';
  405. }
  406. public function getComposerInformationFromLabel($identifier, $index)
  407. {
  408. $composerJsonPath = substr($identifier, 0, $index) . '/composer.json' . substr($identifier, $index);
  409. $command = $this->generateP4Command(' files ' . $composerJsonPath, false);
  410. $this->executeCommand($command);
  411. $result = $this->commandResult;
  412. $index2 = strpos($result, 'no such file(s).');
  413. if ($index2 === false) {
  414. $index3 = strpos($result, 'change');
  415. if (!($index3 === false)) {
  416. $phrase = trim(substr($result, $index3));
  417. $fields = explode(' ', $phrase);
  418. $id = $fields[1];
  419. $composerJson = substr($identifier, 0, $index) . '/composer.json@' . $id;
  420. return $this->getComposerInformationFromPath($composerJson);
  421. }
  422. }
  423. return "";
  424. }
  425. public function getBranches()
  426. {
  427. $possibleBranches = array();
  428. if (!$this->isStream()) {
  429. $possibleBranches[$this->p4Branch] = $this->getStream();
  430. } else {
  431. $command = $this->generateP4Command('streams //' . $this->p4Depot . '/...');
  432. $this->executeCommand($command);
  433. $result = $this->commandResult;
  434. $resArray = explode(PHP_EOL, $result);
  435. foreach ($resArray as $line) {
  436. $resBits = explode(' ', $line);
  437. if (count($resBits) > 4) {
  438. $branch = preg_replace('/[^A-Za-z0-9 ]/', '', $resBits[4]);
  439. $possibleBranches[$branch] = $resBits[1];
  440. }
  441. }
  442. }
  443. $command = $this->generateP4Command('changes '. $this->getStream() . '/...', false);
  444. $this->executeCommand($command);
  445. $result = $this->commandResult;
  446. $resArray = explode(PHP_EOL, $result);
  447. $lastCommit = $resArray[0];
  448. $lastCommitArr = explode(' ', $lastCommit);
  449. $lastCommitNum = $lastCommitArr[1];
  450. $branches = array('master' => $possibleBranches[$this->p4Branch] . '@'. $lastCommitNum);
  451. return $branches;
  452. }
  453. public function getTags()
  454. {
  455. $command = $this->generateP4Command('labels');
  456. $this->executeCommand($command);
  457. $result = $this->commandResult;
  458. $resArray = explode(PHP_EOL, $result);
  459. $tags = array();
  460. foreach ($resArray as $line) {
  461. $index = strpos($line, 'Label');
  462. if (!($index === false)) {
  463. $fields = explode(' ', $line);
  464. $tags[$fields[1]] = $this->getStream() . '@' . $fields[1];
  465. }
  466. }
  467. return $tags;
  468. }
  469. public function checkStream()
  470. {
  471. $command = $this->generateP4Command('depots', false);
  472. $this->executeCommand($command);
  473. $result = $this->commandResult;
  474. $resArray = explode(PHP_EOL, $result);
  475. foreach ($resArray as $line) {
  476. $index = strpos($line, 'Depot');
  477. if (!($index === false)) {
  478. $fields = explode(' ', $line);
  479. if (strcmp($this->p4Depot, $fields[1]) === 0) {
  480. $this->p4DepotType = $fields[3];
  481. return $this->isStream();
  482. }
  483. }
  484. }
  485. return false;
  486. }
  487. /**
  488. * @param $reference
  489. * @return mixed|null
  490. */
  491. protected function getChangeList($reference)
  492. {
  493. $index = strpos($reference, '@');
  494. if ($index === false) {
  495. return null;
  496. }
  497. $label = substr($reference, $index);
  498. $command = $this->generateP4Command(' changes -m1 ' . $label);
  499. $this->executeCommand($command);
  500. $changes = $this->commandResult;
  501. if (strpos($changes, 'Change') !== 0) {
  502. return null;
  503. }
  504. $fields = explode(' ', $changes);
  505. $changeList = $fields[1];
  506. return $changeList;
  507. }
  508. /**
  509. * @param $fromReference
  510. * @param $toReference
  511. * @return mixed|null
  512. */
  513. public function getCommitLogs($fromReference, $toReference)
  514. {
  515. $fromChangeList = $this->getChangeList($fromReference);
  516. if ($fromChangeList === null) {
  517. return null;
  518. }
  519. $toChangeList = $this->getChangeList($toReference);
  520. if ($toChangeList === null) {
  521. return null;
  522. }
  523. $index = strpos($fromReference, '@');
  524. $main = substr($fromReference, 0, $index) . '/...';
  525. $command = $this->generateP4Command('filelog ' . $main . '@' . $fromChangeList. ',' . $toChangeList);
  526. $this->executeCommand($command);
  527. $result = $this->commandResult;
  528. return $result;
  529. }
  530. public function getFilesystem()
  531. {
  532. if (empty($this->filesystem)) {
  533. $this->filesystem = new Filesystem($this->process);
  534. }
  535. return $this->filesystem;
  536. }
  537. public function setFilesystem(Filesystem $fs)
  538. {
  539. $this->filesystem = $fs;
  540. }
  541. }