Perforce.php 15 KB

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