Perforce.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 (null === $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('client -i < ' . str_replace(" ", "\\ ", $this->getP4ClientSpec()));
  264. $this->executeCommand($p4CreateClientCommand);
  265. }
  266. public function syncCodeBase($sourceReference)
  267. {
  268. $prevDir = getcwd();
  269. chdir($this->path);
  270. $p4SyncCommand = $this->generateP4Command('sync -f ');
  271. if (null !== $sourceReference) {
  272. $p4SyncCommand = $p4SyncCommand . '@' . $sourceReference;
  273. }
  274. $this->executeCommand($p4SyncCommand);
  275. chdir($prevDir);
  276. }
  277. public function writeClientSpecToFile($spec)
  278. {
  279. fwrite($spec, 'Client: ' . $this->getClient() . PHP_EOL . PHP_EOL);
  280. fwrite($spec, 'Update: ' . date('Y/m/d H:i:s') . PHP_EOL . PHP_EOL);
  281. fwrite($spec, 'Access: ' . date('Y/m/d H:i:s') . PHP_EOL);
  282. fwrite($spec, 'Owner: ' . $this->getUser() . PHP_EOL . PHP_EOL);
  283. fwrite($spec, 'Description:' . PHP_EOL);
  284. fwrite($spec, ' Created by ' . $this->getUser() . ' from composer.' . PHP_EOL . PHP_EOL);
  285. fwrite($spec, 'Root: ' . $this->getPath() . PHP_EOL . PHP_EOL);
  286. fwrite($spec, 'Options: noallwrite noclobber nocompress unlocked modtime rmdir' . PHP_EOL . PHP_EOL);
  287. fwrite($spec, 'SubmitOptions: revertunchanged' . PHP_EOL . PHP_EOL);
  288. fwrite($spec, 'LineEnd: local' . PHP_EOL . PHP_EOL);
  289. if ($this->isStream()) {
  290. fwrite($spec, 'Stream:' . PHP_EOL);
  291. fwrite($spec, ' ' . $this->getStreamWithoutLabel($this->p4Stream) . PHP_EOL);
  292. } else {
  293. fwrite(
  294. $spec,
  295. 'View: ' . $this->getStream() . '/... //' . $this->getClient() . '/... ' . PHP_EOL
  296. );
  297. }
  298. }
  299. public function writeP4ClientSpec()
  300. {
  301. $clientSpec = $this->getP4ClientSpec();
  302. $spec = fopen($clientSpec, 'w');
  303. try {
  304. $this->writeClientSpecToFile($spec);
  305. } catch (\Exception $e) {
  306. fclose($spec);
  307. throw $e;
  308. }
  309. fclose($spec);
  310. }
  311. protected function read($pipe, $name)
  312. {
  313. if (feof($pipe)) {
  314. return;
  315. }
  316. $line = fgets($pipe);
  317. while ($line !== false) {
  318. $line = fgets($pipe);
  319. }
  320. return;
  321. }
  322. public function windowsLogin($password)
  323. {
  324. $command = $this->generateP4Command(' login -a');
  325. $process = new Process($command, null, null, $password);
  326. return $process->run();
  327. }
  328. public function p4Login()
  329. {
  330. $this->queryP4User();
  331. if (!$this->isLoggedIn()) {
  332. $password = $this->queryP4Password();
  333. if ($this->windowsFlag) {
  334. $this->windowsLogin($password);
  335. } else {
  336. $command = 'echo ' . $password . ' | ' . $this->generateP4Command(' login -a', false);
  337. $exitCode = $this->executeCommand($command);
  338. $result = trim($this->commandResult);
  339. if ($exitCode) {
  340. throw new \Exception("Error logging in:" . $this->process->getErrorOutput());
  341. }
  342. }
  343. }
  344. }
  345. public function getComposerInformation($identifier)
  346. {
  347. $index = strpos($identifier, '@');
  348. if ($index === false) {
  349. $composerJson = $identifier. '/composer.json';
  350. return $this->getComposerInformationFromPath($composerJson);
  351. }
  352. return $this->getComposerInformationFromLabel($identifier, $index);
  353. }
  354. public function getComposerInformationFromPath($composerJson)
  355. {
  356. $command = $this->generateP4Command(' print ' . $composerJson);
  357. $this->executeCommand($command);
  358. $result = $this->commandResult;
  359. $index = strpos($result, '{');
  360. if ($index === false) {
  361. return '';
  362. }
  363. if ($index >= 0) {
  364. $rawData = substr($result, $index);
  365. $composer_info = json_decode($rawData, true);
  366. return $composer_info;
  367. }
  368. return '';
  369. }
  370. public function getComposerInformationFromLabel($identifier, $index)
  371. {
  372. $composerJsonPath = substr($identifier, 0, $index) . '/composer.json' . substr($identifier, $index);
  373. $command = $this->generateP4Command(' files ' . $composerJsonPath, false);
  374. $this->executeCommand($command);
  375. $result = $this->commandResult;
  376. $index2 = strpos($result, 'no such file(s).');
  377. if ($index2 === false) {
  378. $index3 = strpos($result, 'change');
  379. if (!($index3 === false)) {
  380. $phrase = trim(substr($result, $index3));
  381. $fields = explode(' ', $phrase);
  382. $id = $fields[1];
  383. $composerJson = substr($identifier, 0, $index) . '/composer.json@' . $id;
  384. return $this->getComposerInformationFromPath($composerJson);
  385. }
  386. }
  387. return "";
  388. }
  389. public function getBranches()
  390. {
  391. $possibleBranches = array();
  392. if (!$this->isStream()) {
  393. $possibleBranches[$this->p4Branch] = $this->getStream();
  394. } else {
  395. $command = $this->generateP4Command('streams //' . $this->p4Depot . '/...');
  396. $this->executeCommand($command);
  397. $result = $this->commandResult;
  398. $resArray = explode(PHP_EOL, $result);
  399. foreach ($resArray as $line) {
  400. $resBits = explode(' ', $line);
  401. if (count($resBits) > 4) {
  402. $branch = preg_replace('/[^A-Za-z0-9 ]/', '', $resBits[4]);
  403. $possibleBranches[$branch] = $resBits[1];
  404. }
  405. }
  406. }
  407. $command = $this->generateP4Command('changes '. $this->getStream() . '/...', false);
  408. $this->executeCommand($command);
  409. $result = $this->commandResult;
  410. $resArray = explode(PHP_EOL, $result);
  411. $lastCommit = $resArray[0];
  412. $lastCommitArr = explode(' ', $lastCommit);
  413. $lastCommitNum = $lastCommitArr[1];
  414. $branches = array('master' => $possibleBranches[$this->p4Branch] . '@'. $lastCommitNum);
  415. return $branches;
  416. }
  417. public function getTags()
  418. {
  419. $command = $this->generateP4Command('labels');
  420. $this->executeCommand($command);
  421. $result = $this->commandResult;
  422. $resArray = explode(PHP_EOL, $result);
  423. $tags = array();
  424. foreach ($resArray as $line) {
  425. $index = strpos($line, 'Label');
  426. if (!($index === false)) {
  427. $fields = explode(' ', $line);
  428. $tags[$fields[1]] = $this->getStream() . '@' . $fields[1];
  429. }
  430. }
  431. return $tags;
  432. }
  433. public function checkStream()
  434. {
  435. $command = $this->generateP4Command('depots', false);
  436. $this->executeCommand($command);
  437. $result = $this->commandResult;
  438. $resArray = explode(PHP_EOL, $result);
  439. foreach ($resArray as $line) {
  440. $index = strpos($line, 'Depot');
  441. if (!($index === false)) {
  442. $fields = explode(' ', $line);
  443. if (strcmp($this->p4Depot, $fields[1]) === 0) {
  444. $this->p4DepotType = $fields[3];
  445. return $this->isStream();
  446. }
  447. }
  448. }
  449. return false;
  450. }
  451. /**
  452. * @param $reference
  453. * @return mixed|null
  454. */
  455. protected function getChangeList($reference)
  456. {
  457. $index = strpos($reference, '@');
  458. if ($index === false) {
  459. return null;
  460. }
  461. $label = substr($reference, $index);
  462. $command = $this->generateP4Command(' changes -m1 ' . $label);
  463. $this->executeCommand($command);
  464. $changes = $this->commandResult;
  465. if (strpos($changes, 'Change') !== 0) {
  466. return null;
  467. }
  468. $fields = explode(' ', $changes);
  469. $changeList = $fields[1];
  470. return $changeList;
  471. }
  472. /**
  473. * @param $fromReference
  474. * @param $toReference
  475. * @return mixed|null
  476. */
  477. public function getCommitLogs($fromReference, $toReference)
  478. {
  479. $fromChangeList = $this->getChangeList($fromReference);
  480. if ($fromChangeList === null) {
  481. return null;
  482. }
  483. $toChangeList = $this->getChangeList($toReference);
  484. if ($toChangeList === null) {
  485. return null;
  486. }
  487. $index = strpos($fromReference, '@');
  488. $main = substr($fromReference, 0, $index) . '/...';
  489. $command = $this->generateP4Command('filelog ' . $main . '@' . $fromChangeList. ',' . $toChangeList);
  490. $this->executeCommand($command);
  491. $result = $this->commandResult;
  492. return $result;
  493. }
  494. public function getFilesystem()
  495. {
  496. if (empty($this->filesystem)) {
  497. $this->filesystem = new Filesystem($this->process);
  498. }
  499. return $this->filesystem;
  500. }
  501. public function setFilesystem(Filesystem $fs)
  502. {
  503. $this->filesystem = $fs;
  504. }
  505. }