Perforce.php 16 KB

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