Perforce.php 16 KB

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