PerforceComponent.cpp 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzToolsFramework/SourceControl/PerforceComponent.h>
  9. #include <AzCore/Component/TickBus.h>
  10. #include <AzCore/IO/SystemFile.h>
  11. #include <AzCore/Serialization/EditContext.h>
  12. #include <AzCore/std/string/conversions.h>
  13. #include <AzCore/std/parallel/lock.h>
  14. #include <AzFramework/StringFunc/StringFunc.h>
  15. #include <AzFramework/Process/ProcessWatcher.h>
  16. #include <AzToolsFramework/SourceControl/PerforceConnection.h>
  17. #include "PerforceSettings.h"
  18. #include <QProcess>
  19. namespace AzToolsFramework
  20. {
  21. namespace
  22. {
  23. PerforceConnection* s_perforceConn = nullptr;
  24. }
  25. void PerforceSettingResult::UpdateSettingInfo(const AZStd::string& value)
  26. {
  27. if (value.empty())
  28. {
  29. m_settingInfo.m_status = SourceControlSettingStatus::Unset;
  30. return;
  31. }
  32. AZStd::size_t pos = value.find(" (set)");
  33. if (pos != AZStd::string::npos)
  34. {
  35. m_settingInfo.m_status = SourceControlSettingStatus::Set;
  36. m_settingInfo.m_value = value.substr(0, pos);
  37. return;
  38. }
  39. pos = value.find(" (config");
  40. if (pos != AZStd::string::npos)
  41. {
  42. m_settingInfo.m_status = SourceControlSettingStatus::Config;
  43. m_settingInfo.m_value = value.substr(0, pos);
  44. AZStd::size_t fileStart = value.find('\'', pos);
  45. AZStd::size_t fileEnd = value.find('\'', pos + 10); // 10: (config '
  46. if (fileStart != AZStd::string::npos && fileEnd > fileStart)
  47. {
  48. m_settingInfo.m_context = value.substr(fileStart, fileEnd - fileStart);
  49. }
  50. return;
  51. }
  52. m_settingInfo.m_status = SourceControlSettingStatus::None;
  53. m_settingInfo.m_value = value;
  54. }
  55. void PerforceComponent::Activate()
  56. {
  57. AZ_Assert(!s_perforceConn, "You may only have one Perforce component.\n");
  58. m_shutdownThreadSignal = false;
  59. m_waitingOnTrust = false;
  60. m_autoChangelistDescription = "*Open 3D Engine Auto";
  61. m_connectionState = SourceControlState::Disabled;
  62. m_validConnection = false;
  63. m_testConnection = false;
  64. m_offlineMode = true;
  65. m_trustedKey = false;
  66. m_resolveKey = true;
  67. m_testTrust = false;
  68. // set up signals before we start thread.
  69. m_shutdownThreadSignal = false;
  70. m_WorkerThread = AZStd::thread(
  71. { /*m_name =*/ "Perforce worker" },
  72. [this]
  73. {
  74. ThreadWorker();
  75. }
  76. );
  77. SourceControlConnectionRequestBus::Handler::BusConnect();
  78. SourceControlCommandBus::Handler::BusConnect();
  79. }
  80. void PerforceComponent::Deactivate()
  81. {
  82. SourceControlCommandBus::Handler::BusDisconnect();
  83. SourceControlConnectionRequestBus::Handler::BusDisconnect();
  84. m_shutdownThreadSignal = true; // tell the thread to die.
  85. m_WorkerSemaphore.release(1); // wake up the thread so that it sees the signal
  86. m_WorkerThread.join(); // wait for the thread to finish.
  87. m_WorkerThread = AZStd::thread();
  88. SetConnection(nullptr);
  89. }
  90. void PerforceComponent::SetConnection(PerforceConnection* connection)
  91. {
  92. delete s_perforceConn;
  93. s_perforceConn = connection;
  94. }
  95. void PerforceComponent::Reflect(AZ::ReflectContext* context)
  96. {
  97. if (AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(context))
  98. {
  99. serializeContext->Class<PerforceComponent, AZ::Component>()
  100. ;
  101. if (AZ::EditContext* editContext = serializeContext->GetEditContext())
  102. {
  103. editContext->Class<PerforceComponent>(
  104. "Perforce Connectivity", "Manages Perforce connectivity and executes Perforce commands.")
  105. ->ClassElement(AZ::Edit::ClassElements::EditorData, "")
  106. ->Attribute(AZ::Edit::Attributes::Category, "Editor")
  107. ;
  108. }
  109. }
  110. }
  111. void PerforceComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  112. {
  113. provided.push_back(AZ_CRC_CE("SourceControlService"));
  114. provided.push_back(AZ_CRC_CE("PerforceService"));
  115. }
  116. void PerforceComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  117. {
  118. incompatible.push_back(AZ_CRC_CE("PerforceService"));
  119. }
  120. void PerforceComponent::GetFileInfo(const char* fullFilePath, const SourceControlResponseCallback& respCallback)
  121. {
  122. AZ_Assert(fullFilePath, "Must specify a file path");
  123. AZ_Assert(respCallback, "Must specify a callback!");
  124. PerforceJobRequest topReq(PerforceJobRequest::PJR_Stat, fullFilePath, respCallback);
  125. QueueJobRequest(AZStd::move(topReq));
  126. }
  127. void PerforceComponent::GetBulkFileInfo(const AZStd::unordered_set<AZStd::string>& fullFilePaths, const SourceControlResponseCallbackBulk& respCallback)
  128. {
  129. AZ_Assert(!fullFilePaths.empty(), "Must specify a file path");
  130. AZ_Assert(respCallback, "Must specify a callback!");
  131. PerforceJobRequest topReq(PerforceJobRequest::PJR_StatBulk, fullFilePaths, respCallback);
  132. QueueJobRequest(AZStd::move(topReq));
  133. }
  134. void PerforceComponent::RequestEdit(const char* fullFilePath, bool allowMultiCheckout, const SourceControlResponseCallback& respCallback)
  135. {
  136. AZ_Assert(fullFilePath, "Must specify a file path");
  137. AZ_Assert(respCallback, "Must specify a callback!");
  138. PerforceJobRequest topReq(PerforceJobRequest::PJR_Edit, fullFilePath, respCallback);
  139. topReq.m_allowMultiCheckout = allowMultiCheckout;
  140. QueueJobRequest(AZStd::move(topReq));
  141. }
  142. void PerforceComponent::RequestEditBulk(const AZStd::unordered_set<AZStd::string>& fullFilePaths, bool allowMultiCheckout, const SourceControlResponseCallbackBulk& respCallback)
  143. {
  144. AZ_Assert(!fullFilePaths.empty(), "Must specify a file path");
  145. AZ_Assert(respCallback, "Must specify a callback!");
  146. PerforceJobRequest topReq(PerforceJobRequest::PJR_EditBulk, fullFilePaths, respCallback);
  147. topReq.m_allowMultiCheckout = allowMultiCheckout;
  148. QueueJobRequest(AZStd::move(topReq));
  149. }
  150. void PerforceComponent::RequestDelete(const char* fullFilePath, const SourceControlResponseCallback& respCallback)
  151. {
  152. RequestDeleteExtended(fullFilePath, false, respCallback);
  153. }
  154. void PerforceComponent::RequestDeleteExtended(const char* fullFilePath, bool skipReadOnly, const SourceControlResponseCallback& respCallback)
  155. {
  156. AZ_Assert(fullFilePath, "Must specify a file path");
  157. AZ_Assert(respCallback, "Must specify a callback!");
  158. PerforceJobRequest topReq(PerforceJobRequest::PJR_Delete, fullFilePath, respCallback);
  159. topReq.m_skipReadonly = skipReadOnly;
  160. QueueJobRequest(AZStd::move(topReq));
  161. }
  162. void PerforceComponent::RequestDeleteBulk(const char* fullFilePath, const SourceControlResponseCallbackBulk& respCallback)
  163. {
  164. RequestDeleteBulkExtended(fullFilePath, false, respCallback);
  165. }
  166. void PerforceComponent::RequestDeleteBulkExtended(const char* fullFilePath, bool skipReadOnly, const SourceControlResponseCallbackBulk& respCallback)
  167. {
  168. AZ_Assert(fullFilePath, "Must specify a file path");
  169. AZ_Assert(respCallback, "Must specify a callback!");
  170. PerforceJobRequest topReq(PerforceJobRequest::PJR_DeleteBulk, fullFilePath, respCallback);
  171. topReq.m_skipReadonly = skipReadOnly;
  172. QueueJobRequest(AZStd::move(topReq));
  173. }
  174. void PerforceComponent::RequestRevert(const char* fullFilePath, const SourceControlResponseCallback& respCallback)
  175. {
  176. AZ_Assert(fullFilePath, "Must specify a file path");
  177. AZ_Assert(respCallback, "Must specify a callback!");
  178. PerforceJobRequest topReq(PerforceJobRequest::PJR_Revert, fullFilePath, respCallback);
  179. QueueJobRequest(AZStd::move(topReq));
  180. }
  181. void PerforceComponent::RequestLatest(const char* fullFilePath, const SourceControlResponseCallback& respCallback)
  182. {
  183. AZ_Assert(fullFilePath, "Must specify a file path");
  184. AZ_Assert(respCallback, "Must specify a callback!");
  185. PerforceJobRequest topReq(PerforceJobRequest::PJR_Sync, fullFilePath, respCallback);
  186. QueueJobRequest(AZStd::move(topReq));
  187. }
  188. void PerforceComponent::RequestRename(const char* sourcePathFull, const char* destPathFull, const SourceControlResponseCallback& respCallback)
  189. {
  190. RequestRenameExtended(sourcePathFull, destPathFull, false, respCallback);
  191. }
  192. void PerforceComponent::RequestRenameExtended(const char* sourcePathFull, const char* destPathFull, bool skipReadOnly, const SourceControlResponseCallback& respCallback)
  193. {
  194. AZ_Assert(sourcePathFull, "Must specify a source file path");
  195. AZ_Assert(destPathFull, "Must specify a destination file path");
  196. AZ_Assert(respCallback, "Must specify a callback!");
  197. PerforceJobRequest topReq(PerforceJobRequest::PJR_Rename, sourcePathFull, respCallback);
  198. topReq.m_targetPath = destPathFull;
  199. topReq.m_skipReadonly = skipReadOnly;
  200. QueueJobRequest(AZStd::move(topReq));
  201. }
  202. void PerforceComponent::RequestRenameBulk(const char* sourcePathFull, const char* destPathFull, const SourceControlResponseCallbackBulk& respCallback)
  203. {
  204. RequestRenameBulkExtended(sourcePathFull, destPathFull, false, respCallback);
  205. }
  206. void PerforceComponent::RequestRenameBulkExtended(const char* sourcePathFull, const char* destPathFull, bool skipReadOnly, const SourceControlResponseCallbackBulk& respCallback)
  207. {
  208. AZ_Assert(sourcePathFull, "Must specify a source file path");
  209. AZ_Assert(destPathFull, "Must specify a destination file path");
  210. AZ_Assert(respCallback, "Must specify a callback!");
  211. PerforceJobRequest topReq(PerforceJobRequest::PJR_RenameBulk, sourcePathFull, respCallback);
  212. topReq.m_targetPath = destPathFull;
  213. topReq.m_skipReadonly = skipReadOnly;
  214. QueueJobRequest(AZStd::move(topReq));
  215. }
  216. void PerforceComponent::EnableSourceControl(bool enable)
  217. {
  218. m_testTrust = enable;
  219. m_testConnection = enable;
  220. m_resolveKey = enable;
  221. m_offlineMode = !enable;
  222. m_WorkerSemaphore.release(1);
  223. }
  224. void PerforceComponent::EnableTrust(bool enable, AZStd::string fingerprint)
  225. {
  226. s_perforceConn->m_command.ExecuteTrust(enable, fingerprint);
  227. m_trustedKey = IsTrustKeyValid();
  228. m_waitingOnTrust = false;
  229. // If our key is now valid, prompt user if it expires / becomes invalid
  230. // If our key is now invalid, wait until we receive a bus message to prompt user
  231. m_resolveKey = m_trustedKey ? true : false;
  232. m_WorkerSemaphore.release(1);
  233. }
  234. void PerforceComponent::SetConnectionSetting(const char* key, const char* value, const SourceControlSettingCallback& respCallBack)
  235. {
  236. PerforceSettingResult result;
  237. result.m_callback = respCallBack;
  238. if (ExecuteAndParseSet(key, value))
  239. {
  240. result.UpdateSettingInfo(s_perforceConn->m_command.GetOutputValue(key));
  241. }
  242. QueueSettingResponse(result);
  243. }
  244. void PerforceComponent::GetConnectionSetting(const char* key, const SourceControlSettingCallback& respCallBack)
  245. {
  246. PerforceSettingResult result;
  247. result.m_callback = respCallBack;
  248. if (ExecuteAndParseSet(nullptr, nullptr))
  249. {
  250. result.UpdateSettingInfo(s_perforceConn->m_command.GetOutputValue(key));
  251. }
  252. QueueSettingResponse(result);
  253. }
  254. void PerforceComponent::OpenSettings()
  255. {
  256. PerforceSettings dialog;
  257. if (dialog.exec() == QDialog::Accepted)
  258. {
  259. dialog.Apply();
  260. }
  261. }
  262. SourceControlFileInfo PerforceComponent::GetFileInfo(const char* filePath)
  263. {
  264. SourceControlFileInfo newInfo;
  265. newInfo.m_status = SCS_OpSuccess;
  266. newInfo.m_filePath = filePath;
  267. if (AZ::IO::SystemFile::IsWritable(filePath) || !AZ::IO::SystemFile::Exists(filePath))
  268. {
  269. newInfo.m_flags |= SCF_Writeable;
  270. }
  271. if (!CheckConnectivityForAction("file info", filePath))
  272. {
  273. newInfo.m_status = m_trustedKey ? SCS_ProviderIsDown : SCS_CertificateInvalid;
  274. return newInfo;
  275. }
  276. bool sourceAwareFile = false;
  277. if (!ExecuteAndParseFstat(filePath, sourceAwareFile))
  278. {
  279. if (!sourceAwareFile)
  280. {
  281. AZ_TracePrintf(SCC_WINDOW, "Perforce - File is not tracked\n");
  282. return newInfo;
  283. }
  284. newInfo.m_status = SCS_ProviderError;
  285. return newInfo;
  286. }
  287. bool newFileAfterDeletionAtHead = s_perforceConn->m_command.NewFileAfterDeletedRev();
  288. if (!newFileAfterDeletionAtHead)
  289. {
  290. if (s_perforceConn->m_command.GetHeadRevision() != s_perforceConn->m_command.GetHaveRevision())
  291. {
  292. newInfo.m_flags |= SCF_OutOfDate;
  293. AZ_TracePrintf(SCC_WINDOW, "Perforce - File is out of date\n");
  294. }
  295. }
  296. if (!s_perforceConn->m_command.ExclusiveOpen())
  297. {
  298. newInfo.m_flags |= SCF_MultiCheckOut;
  299. }
  300. bool openByOthers = s_perforceConn->m_command.IsOpenByOtherUsers();
  301. if (openByOthers)
  302. {
  303. newInfo.m_flags |= SCF_OtherOpen;
  304. newInfo.m_StatusUser = s_perforceConn->m_command.GetOtherUserCheckedOut();
  305. }
  306. if (s_perforceConn->m_command.IsOpenByCurrentUser())
  307. {
  308. newInfo.m_flags |= SCF_Tracked | SCF_OpenByUser;
  309. if (s_perforceConn->m_command.CurrentActionIsAdd())
  310. {
  311. newInfo.m_flags |= SCF_PendingAdd;
  312. AZ_TracePrintf(SCC_WINDOW, "Perforce - File is marked for add\n");
  313. }
  314. else if (s_perforceConn->m_command.CurrentActionIsDelete())
  315. {
  316. newInfo.m_flags |= SCF_PendingDelete;
  317. AZ_TracePrintf(SCC_WINDOW, "Perforce - File is marked for delete\n");
  318. }
  319. else
  320. {
  321. AZ_TracePrintf(SCC_WINDOW, "Perforce - File is checked out by you\n");
  322. if (openByOthers)
  323. {
  324. AZ_TracePrintf(SCC_WINDOW, " and Others\n");
  325. }
  326. }
  327. }
  328. else if (openByOthers)
  329. {
  330. newInfo.m_flags |= SCF_Tracked;
  331. AZ_TracePrintf(SCC_WINDOW, "Perforce - File is opened by %s\n", newInfo.m_StatusUser.c_str());
  332. }
  333. else if (newFileAfterDeletionAtHead)
  334. {
  335. AZ_TracePrintf(SCC_WINDOW, "Perforce - File is deleted at head revision\n");
  336. }
  337. else
  338. {
  339. newInfo.m_flags |= SCF_Tracked;
  340. AZ_TracePrintf(SCC_WINDOW, "Perforce - File is Checked In\n");
  341. }
  342. AZ_TracePrintf(SCC_WINDOW, "Perforce - Received file info %s\n\n", newInfo.m_filePath.c_str());
  343. return newInfo;
  344. }
  345. bool PerforceComponent::RequestEdit(const char* filePath, bool allowMultiCheckout)
  346. {
  347. if (!CheckConnectivityForAction("checkout", filePath))
  348. {
  349. return false;
  350. }
  351. return ExecuteEdit(filePath, allowMultiCheckout, true);
  352. }
  353. bool PerforceComponent::RequestEditBulk(const AZStd::unordered_set<AZStd::string>& fullFilePath, bool allowMultiCheckout)
  354. {
  355. if (!CheckConnectivityForAction("checkout", fullFilePath.begin()->c_str()))
  356. {
  357. return false;
  358. }
  359. return ExecuteEditBulk(fullFilePath, allowMultiCheckout, true);
  360. }
  361. bool PerforceComponent::RequestDelete(const char* filePath)
  362. {
  363. if (!CheckConnectivityForAction("delete", filePath))
  364. {
  365. return false;
  366. }
  367. return ExecuteDelete(filePath);
  368. }
  369. bool PerforceComponent::RequestDeleteBulk(const char* filePath)
  370. {
  371. if (!CheckConnectivityForAction("delete", filePath))
  372. {
  373. return false;
  374. }
  375. return ExecuteDeleteBulk(filePath);
  376. }
  377. bool PerforceComponent::RequestLatest(const char* filePath)
  378. {
  379. if (!CheckConnectivityForAction("sync", filePath))
  380. {
  381. return false;
  382. }
  383. return ExecuteSync(filePath);
  384. }
  385. bool PerforceComponent::RequestRevert(const char* filePath)
  386. {
  387. if (!CheckConnectivityForAction("revert", filePath))
  388. {
  389. return false;
  390. }
  391. return ExecuteRevert(filePath);
  392. }
  393. bool PerforceComponent::RequestRename(const char* sourcePath, const char* destPath)
  394. {
  395. if (!CheckConnectivityForAction("rename", sourcePath))
  396. {
  397. return false;
  398. }
  399. return ExecuteMove(sourcePath, destPath);
  400. }
  401. bool PerforceComponent::RequestRenameBulk(const char* sourcePathFull, const char* destPathFull)
  402. {
  403. if (!CheckConnectivityForAction("rename", sourcePathFull))
  404. {
  405. return false;
  406. }
  407. return ExecuteMoveBulk(sourcePathFull, destPathFull);
  408. }
  409. // claim changed file just moves the file to the new changelist - it assumes its already on your changelist
  410. bool PerforceComponent::ClaimChangedFile(const char* path, int changeListNumber)
  411. {
  412. AZStd::string changeListStr = AZStd::string::format("claim changed file to changelist %i", changeListNumber);
  413. if (!CheckConnectivityForAction(changeListStr.c_str(), path))
  414. {
  415. return false;
  416. }
  417. changeListStr = AZStd::to_string(changeListNumber);
  418. s_perforceConn->m_command.ExecuteClaimChangedFile(path, changeListStr);
  419. return CommandSucceeded();
  420. }
  421. bool PerforceComponent::ExecuteAdd(const char* filePath)
  422. {
  423. bool sourceAwareFile = false;
  424. if (!ExecuteAndParseFstat(filePath, sourceAwareFile))
  425. {
  426. if (sourceAwareFile)
  427. {
  428. return false;
  429. }
  430. }
  431. int changeListNumber = GetOrCreateOurChangelist();
  432. if (changeListNumber <= 0)
  433. {
  434. return false;
  435. }
  436. AZStd::string changeListNumberStr = AZStd::to_string(changeListNumber);
  437. s_perforceConn->m_command.ExecuteAdd(changeListNumberStr, filePath);
  438. if (!CommandSucceeded())
  439. {
  440. return false;
  441. }
  442. // Interesting result of our intention based system -
  443. // The perforce Add operation does not remove 'read only' status from a file; however,
  444. // since our intention is to say "place this file in a state ready for work," we need
  445. // to remove read only status (otherwise subsequent save operations would fail)
  446. AZ::IO::SystemFile::SetWritable(filePath, true);
  447. ExecuteAndParseFstat(filePath, sourceAwareFile);
  448. if (!s_perforceConn->m_command.CurrentActionIsAdd())
  449. {
  450. AZ_Warning(SCC_WINDOW, false, "Perforce - Unable to add file '%s' (Perforce is connected but file was not added)\n", filePath);
  451. return false;
  452. }
  453. AZ_TracePrintf(SCC_WINDOW, "Perforce - Successfully added file %s\n", filePath);
  454. return true;
  455. }
  456. bool PerforceComponent::ExecuteEdit(const char* filePath, bool allowMultiCheckout, bool allowAdd)
  457. {
  458. bool sourceAwareFile = false;
  459. if (!ExecuteAndParseFstat(filePath, sourceAwareFile))
  460. {
  461. if (sourceAwareFile)
  462. {
  463. return false;
  464. }
  465. if (!AZ::IO::SystemFile::Exists(filePath))
  466. {
  467. sourceAwareFile = false;
  468. }
  469. }
  470. if (sourceAwareFile && (s_perforceConn->m_command.IsOpenByOtherUsers() && !allowMultiCheckout))
  471. {
  472. AZ_Warning(SCC_WINDOW, false, "Perforce - Unable to edit file %s, it's already checked out by %s\n",
  473. filePath, s_perforceConn->m_command.GetOtherUserCheckedOut().c_str());
  474. return false;
  475. }
  476. if (!sourceAwareFile)
  477. {
  478. if (allowAdd)
  479. {
  480. if (!ExecuteAdd(filePath))
  481. {
  482. AZ_Warning(SCC_WINDOW, false, "Perforce - Unable to add file %s\n", filePath);
  483. // if the file is writable, this does not count as an error as we are likely
  484. // simply outside the workspace.
  485. return AZ::IO::SystemFile::IsWritable(filePath);
  486. }
  487. return true;
  488. }
  489. return false;
  490. }
  491. int changeListNumber = GetOrCreateOurChangelist();
  492. if (changeListNumber <= 0)
  493. {
  494. return false;
  495. }
  496. AZStd::string changeListNumberStr = AZStd::to_string(changeListNumber);
  497. if (s_perforceConn->m_command.IsOpenByCurrentUser())
  498. {
  499. if (s_perforceConn->m_command.CurrentActionIsEdit() || s_perforceConn->m_command.CurrentActionIsAdd())
  500. {
  501. // Claim the file to our working changelist if it's in the default changelist.
  502. const AZStd::string& currentCL = s_perforceConn->m_command.GetCurrentChangelistNumber();
  503. if (currentCL == "default")
  504. {
  505. if (!ClaimChangedFile(filePath, changeListNumber))
  506. {
  507. AZ_TracePrintf(SCC_WINDOW, "Perforce - Unable to move file %s to changelist %s\n", filePath, changeListNumberStr.c_str());
  508. }
  509. }
  510. AZ_TracePrintf(SCC_WINDOW, "Perforce - File %s is already checked out by you\n", filePath);
  511. return true;
  512. }
  513. else if (s_perforceConn->m_command.CurrentActionIsDelete())
  514. {
  515. // we have it checked out for delete by us - we need to revert the delete before we can check it out
  516. AZ_TracePrintf(SCC_WINDOW, "Perforce - Reverting deleted file %s in order to check it out\n", filePath);
  517. if (!ExecuteRevert(filePath))
  518. {
  519. AZ_Warning(SCC_WINDOW, false, "Perforce - Failed to revert deleted file %s\n", filePath);
  520. return false;
  521. }
  522. }
  523. else
  524. {
  525. AZ_Warning(SCC_WINDOW, false, "Perforce - Requesting edit on file %s opened by current user, but not marked for add, edit or delete", filePath);
  526. return false;
  527. }
  528. }
  529. else if (s_perforceConn->m_command.HeadActionIsDelete() && allowAdd)
  530. {
  531. // it's currently deleted - we need to fix that before we can check it out
  532. // this Add operation is all we need since it puts the file under our control
  533. AZ_TracePrintf(SCC_WINDOW, "Perforce - re-Add fully deleted file %s in order to check it out\n", filePath);
  534. if (!ExecuteAdd(filePath))
  535. {
  536. AZ_Warning(SCC_WINDOW, false, "Perforce - Failed to revert deleted file %s in order to check it out\n", filePath);
  537. return false;
  538. }
  539. return true;
  540. }
  541. // If we get here it means it's not opened by us
  542. // attempt check out.
  543. s_perforceConn->m_command.ExecuteEdit(changeListNumberStr, filePath);
  544. if (!CommandSucceeded())
  545. {
  546. return false;
  547. }
  548. if (s_perforceConn->m_command.NeedsReopening() && !ClaimChangedFile(filePath, changeListNumber))
  549. {
  550. AZ_Warning(SCC_WINDOW, false, "Perforce - Failed to reopen file %s\n", filePath);
  551. return false;
  552. }
  553. ExecuteAndParseFstat(filePath, sourceAwareFile);
  554. if (s_perforceConn->m_command.IsMarkedForAdd())
  555. {
  556. AZ_Warning(SCC_WINDOW, false, "Perforce - Can't edit file %s, already marked for add\n", filePath);
  557. return false;
  558. }
  559. if (!s_perforceConn->m_command.CurrentActionIsEdit())
  560. {
  561. AZ_TracePrintf(SCC_WINDOW, "Perforce - Failed to edit file %s", filePath);
  562. return false;
  563. }
  564. AZ_TracePrintf(SCC_WINDOW, "Perforce - Checked out file %s\n", filePath);
  565. return true;
  566. }
  567. bool PerforceComponent::ExecuteEditBulk(const AZStd::unordered_set<AZStd::string>& filePaths, bool allowMultiCheckout, bool allowAdd)
  568. {
  569. AZStd::vector<PerforceMap> commandMap;
  570. ExecuteAndParseFstat(filePaths, commandMap);
  571. for (const PerforceMap& map : commandMap)
  572. {
  573. if(s_perforceConn->m_command.IsOpenByOtherUsers(&map) && !allowMultiCheckout)
  574. {
  575. AZ_Warning(SCC_WINDOW, false, "Perforce - Unable to edit file %s, it's already checked out by %s\n",
  576. s_perforceConn->m_command.GetOutputValue("clientFile", &map).c_str(), s_perforceConn->m_command.GetOtherUserCheckedOut(&map).c_str());
  577. return false;
  578. }
  579. }
  580. int changeListNumber = GetOrCreateOurChangelist();
  581. if (changeListNumber <= 0)
  582. {
  583. return false;
  584. }
  585. AZStd::string changeListNumberStr = AZStd::to_string(changeListNumber);
  586. if (allowAdd)
  587. {
  588. s_perforceConn->m_command.ExecuteAdd(changeListNumberStr, filePaths);
  589. }
  590. s_perforceConn->m_command.ExecuteEdit(changeListNumberStr, filePaths);
  591. return true;
  592. }
  593. bool PerforceComponent::ExecuteDelete(const char* filePath)
  594. {
  595. bool sourceAwareFile = false;
  596. if (!ExecuteAndParseFstat(filePath, sourceAwareFile) || !sourceAwareFile)
  597. {
  598. return false;
  599. }
  600. if (s_perforceConn->m_command.IsOpenByCurrentUser())
  601. {
  602. if (!s_perforceConn->m_command.CurrentActionIsDelete())
  603. {
  604. if (!ExecuteRevert(filePath))
  605. {
  606. AZ_Warning(SCC_WINDOW, false, "Perforce - Unable to revert file %s\n", filePath);
  607. return false;
  608. }
  609. }
  610. }
  611. if (!s_perforceConn->m_command.CurrentActionIsDelete() || !s_perforceConn->m_command.HeadActionIsDelete())
  612. {
  613. /* not already being deleted by current or head action - delete destructively (DANGER) */
  614. int changeListNumber = GetOrCreateOurChangelist();
  615. if (changeListNumber <= 0)
  616. {
  617. return false;
  618. }
  619. AZStd::string changeListNumberStr = AZStd::to_string(changeListNumber);
  620. s_perforceConn->m_command.ExecuteDelete(changeListNumberStr, filePath);
  621. if (!CommandSucceeded())
  622. {
  623. AZ_Warning(SCC_WINDOW, false, "Perforce - Failed to delete file %s\n", filePath);
  624. return false;
  625. }
  626. AZ_TracePrintf(SCC_WINDOW, "Perforce - Deleted file %s\n", filePath);
  627. }
  628. // this will execute even if the file isn't part of SCS yet
  629. AZ::IO::SystemFile::Delete(filePath);
  630. return true;
  631. }
  632. bool PerforceComponent::ExecuteDeleteBulk(const char* filePath)
  633. {
  634. int changeListNumber = GetOrCreateOurChangelist();
  635. if (changeListNumber <= 0)
  636. {
  637. return false;
  638. }
  639. AZStd::string changeListNumberStr = AZStd::to_string(changeListNumber);
  640. s_perforceConn->m_command.ExecuteDelete(changeListNumberStr, filePath);
  641. if (!CommandSucceeded())
  642. {
  643. AZ_Warning(SCC_WINDOW, false, "Perforce - Failed to delete file %s\n", filePath);
  644. return false;
  645. }
  646. AZ_TracePrintf(SCC_WINDOW, "Perforce - Deleted file %s\n", filePath);
  647. return true;
  648. }
  649. bool PerforceComponent::ExecuteSync(const char* filePath)
  650. {
  651. bool sourceAwareFile = false;
  652. if (!ExecuteAndParseFstat(filePath, sourceAwareFile))
  653. {
  654. AZ_TracePrintf(SCC_WINDOW, "Perforce - Unable to get latest on file '%s'\n", filePath);
  655. return false;
  656. }
  657. // If we're working on a new file - unknown to p4 or a 'new file' after a deletion
  658. if (!sourceAwareFile || s_perforceConn->m_command.NewFileAfterDeletedRev())
  659. {
  660. return true;
  661. }
  662. if (AZ::IO::SystemFile::IsWritable(filePath) && !s_perforceConn->m_command.IsOpenByCurrentUser())
  663. {
  664. AZ_TracePrintf(SCC_WINDOW, "Perforce - Unable to get latest on file '%s' (File is not checked out but has changes locally. Please reconcile offline work!)\n", filePath);
  665. return false;
  666. }
  667. if (s_perforceConn->m_command.GetHeadRevision() == s_perforceConn->m_command.GetHaveRevision())
  668. {
  669. return true;
  670. }
  671. if (s_perforceConn->m_command.IsOpenByCurrentUser())
  672. {
  673. AZ_TracePrintf(SCC_WINDOW, "Perforce - Unable to get latest on file '%s' (File is open by current user, but not at head revision!)\n", filePath);
  674. return false;
  675. }
  676. s_perforceConn->m_command.ExecuteSync(filePath);
  677. return CommandSucceeded();
  678. }
  679. bool PerforceComponent::ExecuteRevert(const char* filePath)
  680. {
  681. bool sourceAwareFile = false;
  682. if (!ExecuteAndParseFstat(filePath, sourceAwareFile) || !sourceAwareFile)
  683. {
  684. AZ_TracePrintf(SCC_WINDOW, "Perforce - Unable to revert file '%s' (Perforce is connected but file is not able to be reverted)\n", filePath);
  685. return false;
  686. }
  687. if (!s_perforceConn->m_command.IsOpenByCurrentUser())
  688. {
  689. AZ_TracePrintf(SCC_WINDOW, "Perforce - Unable to revert file '%s' (Perforce is connected but file is not open by current user)\n", filePath);
  690. return false;
  691. }
  692. s_perforceConn->m_command.ExecuteRevert(filePath);
  693. if (!CommandSucceeded())
  694. {
  695. return false;
  696. }
  697. ExecuteAndParseFstat(filePath, sourceAwareFile);
  698. if (s_perforceConn->m_command.IsOpenByCurrentUser())
  699. {
  700. AZ_Warning(SCC_WINDOW, false, "Perforce - Unable to revert file '%s' (Perforce is connected but file was not reverted)\n", filePath);
  701. return false;
  702. }
  703. AZ_TracePrintf(SCC_WINDOW, "Perforce - Revert succeeded for %s\n", filePath);
  704. return true;
  705. }
  706. bool PerforceComponent::ExecuteMove(const char* sourcePath, const char* destPath)
  707. {
  708. if (!AZ::IO::SystemFile::Exists(sourcePath))
  709. {
  710. AZ_TracePrintf(SCC_WINDOW, "Perforce - Unable to move file '%s' (source file does not exist)\n", sourcePath);
  711. return false;
  712. }
  713. // Blocked output - File exists or SourceAwareFile and File is not deleted at head
  714. if (AZ::IO::SystemFile::Exists(destPath))
  715. {
  716. AZ_TracePrintf(SCC_WINDOW, "Perforce - Unable to move file '%s' (destination file '%s' already exists)\n", sourcePath, destPath);
  717. return false;
  718. }
  719. bool sourceAwareFile = false;
  720. ExecuteAndParseFstat(destPath, sourceAwareFile);
  721. if (sourceAwareFile)
  722. {
  723. if (!s_perforceConn->m_command.HeadActionIsDelete())
  724. {
  725. AZ_TracePrintf(SCC_WINDOW, "Perforce - Unable to move file '%s' (destination file '%s' exists in perforce)\n", sourcePath, destPath);
  726. return false;
  727. }
  728. }
  729. // Make sure we're not going to trash any data.
  730. if (!ExecuteEdit(sourcePath, false, false))
  731. {
  732. AZ_TracePrintf(SCC_WINDOW, "Perforce - Unable to mark file '%s' for edit prior to move operation\n", sourcePath);
  733. return false;
  734. }
  735. int changeListNumber = GetOrCreateOurChangelist();
  736. if (changeListNumber <= 0)
  737. {
  738. return false;
  739. }
  740. AZStd::string changeListNumberStr = AZStd::to_string(changeListNumber);
  741. s_perforceConn->m_command.ExecuteMove(changeListNumberStr, sourcePath, destPath);
  742. return CommandSucceeded();
  743. }
  744. bool PerforceComponent::ExecuteMoveBulk(const char* sourcePath, const char* destPath)
  745. {
  746. int changeListNumber = GetOrCreateOurChangelist();
  747. if (changeListNumber <= 0)
  748. {
  749. return false;
  750. }
  751. AZStd::string changeListNumberStr = AZStd::to_string(changeListNumber);
  752. s_perforceConn->m_command.ExecuteEdit(changeListNumberStr, sourcePath);
  753. s_perforceConn->m_command.ExecuteMove(changeListNumberStr, sourcePath, destPath);
  754. return true;
  755. }
  756. void PerforceComponent::QueueJobRequest(PerforceJobRequest&& jobRequest)
  757. {
  758. AZStd::lock_guard<AZStd::mutex> locker(m_WorkerQueueMutex);
  759. m_workerQueue.push(AZStd::move(jobRequest));
  760. m_WorkerSemaphore.release(1);
  761. }
  762. void PerforceComponent::QueueSettingResponse(const PerforceSettingResult& result)
  763. {
  764. AZStd::lock_guard<AZStd::mutex> locker(m_SettingsQueueMutex);
  765. m_settingsQueue.push(AZStd::move(result));
  766. AZ::TickBus::QueueFunction(&PerforceComponent::ProcessResultQueue, this);
  767. }
  768. bool PerforceComponent::CheckConnectivityForAction(const char* actionDesc, const char* filePath) const
  769. {
  770. (void)actionDesc;
  771. AZ_Assert(m_ProcessThreadID != AZStd::thread::id(), "The perforce worker thread has not started.");
  772. AZ_Assert(AZStd::this_thread::get_id() == m_ProcessThreadID, "You may only call this function from the perforce worker thread.");
  773. AZStd::string fileMessage = AZStd::string::format(" for file %s", filePath);
  774. AZ_TracePrintf(SCC_WINDOW, "Perforce - Requested %s%s\n", actionDesc, filePath ? fileMessage.c_str() : "");
  775. if (m_connectionState != SourceControlState::Active)
  776. {
  777. fileMessage = AZStd::string::format(" file %s", filePath);
  778. AZ_Warning(SCC_WINDOW, false, "Perforce - Unable to %s%s (Perforce is not connected)\n", actionDesc, filePath ? fileMessage.c_str() : "");
  779. return false;
  780. }
  781. return true;
  782. }
  783. bool PerforceComponent::ExecuteAndParseFstat(const char* filePath, bool& sourceAwareFile)
  784. {
  785. s_perforceConn->m_command.ExecuteFstat(filePath);
  786. sourceAwareFile = CommandSucceeded();
  787. AZStd::lock_guard<AZStd::mutex> locker(s_perforceConn->m_command.m_commandMutex);
  788. return ParseOutput(s_perforceConn->m_command.m_commandOutputMap, s_perforceConn->m_command.m_rawOutput.outputResult);
  789. }
  790. bool PerforceComponent::ExecuteAndParseFstat(const char* filePath, AZStd::vector<PerforceMap>& commandMap) const
  791. {
  792. s_perforceConn->m_command.ExecuteFstat(filePath);
  793. AZStd::lock_guard<AZStd::mutex> locker(s_perforceConn->m_command.m_commandMutex);
  794. return ParseDuplicateOutput(commandMap, s_perforceConn->m_command.m_rawOutput.outputResult);
  795. }
  796. bool PerforceComponent::ExecuteAndParseFstat(const AZStd::unordered_set<AZStd::string>& filePaths, AZStd::vector<PerforceMap>& commandMap) const
  797. {
  798. s_perforceConn->m_command.ExecuteFstat(filePaths);
  799. AZStd::lock_guard<AZStd::mutex> locker(s_perforceConn->m_command.m_commandMutex);
  800. return ParseDuplicateOutput(commandMap, s_perforceConn->m_command.m_rawOutput.outputResult);
  801. }
  802. bool PerforceComponent::ExecuteAndParseSet(const char* key, const char* value) const
  803. {
  804. // a valid key will attempt to set the setting
  805. // a null value will clear the setting
  806. if (key)
  807. {
  808. AZStd::string keystr = key;
  809. AZStd::string valstr = value;
  810. s_perforceConn->m_command.ExecuteSet(keystr, valstr);
  811. }
  812. s_perforceConn->m_command.ExecuteSet();
  813. if (s_perforceConn->CommandHasFailed())
  814. {
  815. return false;
  816. }
  817. AZStd::lock_guard<AZStd::mutex> locker(s_perforceConn->m_command.m_commandMutex);
  818. return ParseOutput(s_perforceConn->m_command.m_commandOutputMap, s_perforceConn->m_command.m_rawOutput.outputResult, "=");
  819. }
  820. bool PerforceComponent::CommandSucceeded()
  821. {
  822. // This is misleading - FileExists only returns false if the phrase 'no such file(s)'
  823. // is present in output. Therefore, this is safe to call even on perforce commands
  824. // that are not directly related to files (p4 set / p4 change / p4 describe / etc)
  825. if (!s_perforceConn->m_command.FileExists())
  826. {
  827. return false;
  828. }
  829. if (s_perforceConn->CommandHasFailed())
  830. {
  831. m_testTrust = true;
  832. m_testConnection = true;
  833. return false;
  834. }
  835. return true;
  836. }
  837. bool PerforceComponent::UpdateTrust()
  838. {
  839. AZ_Assert(m_ProcessThreadID != AZStd::thread::id(), "The perforce worker thread has not started.");
  840. AZ_Assert(AZStd::this_thread::get_id() == m_ProcessThreadID, "You may only call this function from the perforce worker thread.");
  841. if (m_testTrust)
  842. {
  843. TestConnectionTrust(m_resolveKey);
  844. }
  845. if (!m_trustedKey && m_waitingOnTrust)
  846. {
  847. return false;
  848. }
  849. return true;
  850. }
  851. bool PerforceComponent::IsTrustKeyValid() const
  852. {
  853. s_perforceConn->m_command.ExecuteInfo();
  854. if (s_perforceConn->CommandHasError() && s_perforceConn->m_command.HasTrustIssue())
  855. {
  856. return false;
  857. }
  858. return true;
  859. }
  860. void PerforceComponent::VerifyP4PortIsSet()
  861. {
  862. AZ_Assert(m_ProcessThreadID != AZStd::thread::id(), "The perforce worker thread has not started.");
  863. AZ_Assert(AZStd::this_thread::get_id() == m_ProcessThreadID, "You may only call this function from the perforce worker thread.");
  864. if (!s_perforceConn)
  865. {
  866. s_perforceConn = new PerforceConnection();
  867. }
  868. if (m_testConnection)
  869. {
  870. bool p4PortSet = false;
  871. if (ExecuteAndParseSet(nullptr, nullptr))
  872. {
  873. p4PortSet = !s_perforceConn->m_command.GetOutputValue("P4PORT").empty();
  874. }
  875. if (!p4PortSet)
  876. {
  877. // The warnings need to show up repeatedly, as the user might be turning on and off perforce and configuring it.
  878. if (!s_perforceConn->CommandApplicationFound())
  879. {
  880. AZ_Warning(SCC_WINDOW, false, "Perforce - p4 executable not found on path, will not use Perforce source control!\n");
  881. }
  882. else
  883. {
  884. AZ_Warning(SCC_WINDOW, false, "Perforce - p4 executable found, but P4PORT (server address) is not set, Perforce not available!\n");
  885. }
  886. // Disable any further connection status testing
  887. m_testTrust = false;
  888. m_testConnection = false;
  889. m_validConnection = false;
  890. }
  891. }
  892. }
  893. void PerforceComponent::TestConnectionTrust(bool attemptResolve)
  894. {
  895. AZ_Assert(m_ProcessThreadID != AZStd::thread::id(), "The perforce worker thread has not started.");
  896. AZ_Assert(AZStd::this_thread::get_id() == m_ProcessThreadID, "You may only call this function from the perforce worker thread.");
  897. m_trustedKey = IsTrustKeyValid();
  898. if (!m_trustedKey && attemptResolve)
  899. {
  900. if (SourceControlNotificationBus::HasHandlers())
  901. {
  902. m_waitingOnTrust = true;
  903. AZStd::string errOutput = s_perforceConn->m_command.m_rawOutput.errorResult;
  904. // tokenize the buffer into line by line key/value pairs
  905. AZStd::vector<AZStd::string> tokens;
  906. AzFramework::StringFunc::Tokenize(errOutput.c_str(), tokens, "\r\n");
  907. // the fingerprint is on the 4th line of error output
  908. static const AZ::u32 fingerprintIdx = 3;
  909. if (tokens.size() > fingerprintIdx)
  910. {
  911. AZStd::string fingerprint = tokens[fingerprintIdx];
  912. if (AZ::TickBus::IsFunctionQueuing())
  913. {
  914. // Push to the main thread for convenience.
  915. AZStd::function<void()> trustNotify = [fingerprint]()
  916. {
  917. SourceControlNotificationBus::Broadcast(&SourceControlNotificationBus::Events::RequestTrust, fingerprint.c_str());
  918. };
  919. AZ::TickBus::QueueFunction(trustNotify);
  920. }
  921. }
  922. }
  923. else
  924. {
  925. // No one is listening. No need to hold up the worker thread.
  926. m_waitingOnTrust = false;
  927. }
  928. }
  929. m_testTrust = false;
  930. }
  931. bool PerforceComponent::IsConnectionValid() const
  932. {
  933. s_perforceConn->m_command.ExecuteTicketStatus();
  934. if (!s_perforceConn->CommandApplicationFound())
  935. {
  936. return false;
  937. }
  938. if (s_perforceConn->CommandHasError())
  939. {
  940. return false;
  941. }
  942. if (!CacheClientConfig())
  943. {
  944. return false;
  945. }
  946. return true;
  947. }
  948. bool PerforceComponent::CacheClientConfig() const
  949. {
  950. s_perforceConn->m_command.ExecuteInfo();
  951. if (s_perforceConn->CommandHasFailed() || !ParseOutput(s_perforceConn->m_infoResultMap, s_perforceConn->m_command.m_rawOutput.outputResult))
  952. {
  953. return false;
  954. }
  955. // P4 workaround, according to P4 documentation and forum posts, in order to keep P4 info fast it may
  956. // not do a server query to fetch the userName field.
  957. // In that case, we will do an additional p4 info -s and update the original m_infoResultMap with the
  958. // retrieved user name.
  959. if (s_perforceConn->GetUser().compare("*unknown*") == 0)
  960. {
  961. PerforceMap shortInfoMap;
  962. s_perforceConn->m_command.ExecuteShortInfo();
  963. if (s_perforceConn->CommandHasFailed() || !ParseOutput(shortInfoMap, s_perforceConn->m_command.m_rawOutput.outputResult))
  964. {
  965. return false;
  966. }
  967. s_perforceConn->m_infoResultMap["userName"] = shortInfoMap["userName"];
  968. }
  969. if (s_perforceConn->GetUser().empty() || s_perforceConn->GetClientName().empty())
  970. {
  971. AZ_Warning(SCC_WINDOW, false, "Perforce - Your client or user is empty, Perforce not available!\n");
  972. return false;
  973. }
  974. if (s_perforceConn->GetClientName().compare("*unknown*") == 0)
  975. {
  976. AZ_Warning(SCC_WINDOW, false, "Perforce - client spec not found, Perforce not available!\n");
  977. return false;
  978. }
  979. if (s_perforceConn->GetClientRoot().empty())
  980. {
  981. AZ_Warning(SCC_WINDOW, false, "Perforce - Your workspace root is empty, Perforce not available!\n");
  982. return false;
  983. }
  984. if (s_perforceConn->GetServerAddress().empty() || s_perforceConn->GetServerUptime().empty())
  985. {
  986. AZ_Warning(SCC_WINDOW, false, "Perforce - Could not get server information, Perforce not available!\n");
  987. return false;
  988. }
  989. AZ_TracePrintf(SCC_WINDOW, "Perforce - Connected, User: %s | Client: %s\n", s_perforceConn->GetUser().c_str(), s_perforceConn->GetClientName().c_str());
  990. return true;
  991. }
  992. void PerforceComponent::TestConnectionValid()
  993. {
  994. AZ_Assert(m_ProcessThreadID != AZStd::thread::id(), "The perforce worker thread has not started.");
  995. AZ_Assert(AZStd::this_thread::get_id() == m_ProcessThreadID, "You may only call this function from the perforce worker thread.");
  996. if (!s_perforceConn)
  997. {
  998. s_perforceConn = new PerforceConnection();
  999. }
  1000. m_validConnection = IsConnectionValid();
  1001. m_testConnection = false;
  1002. }
  1003. bool PerforceComponent::UpdateConnectivity()
  1004. {
  1005. AZ_Assert(m_ProcessThreadID != AZStd::thread::id(), "The perforce worker thread has not started.");
  1006. AZ_Assert(AZStd::this_thread::get_id() == m_ProcessThreadID, "You may only call this function from the perforce worker thread.");
  1007. SourceControlState prevState = m_connectionState;
  1008. if (m_testConnection)
  1009. {
  1010. TestConnectionValid();
  1011. }
  1012. SourceControlState currentState = SourceControlState::Disabled;
  1013. if (!m_offlineMode)
  1014. {
  1015. currentState = (m_trustedKey && m_validConnection) ? SourceControlState::Active : SourceControlState::ConfigurationInvalid;
  1016. }
  1017. m_connectionState = currentState;
  1018. if (m_connectionState != prevState)
  1019. {
  1020. switch (m_connectionState)
  1021. {
  1022. case SourceControlState::Disabled:
  1023. AZ_TracePrintf(SCC_WINDOW, "Perforce disabled\n");
  1024. break;
  1025. case SourceControlState::ConfigurationInvalid:
  1026. AZ_TracePrintf(SCC_WINDOW, "Perforce configuration invalid\n");
  1027. break;
  1028. case SourceControlState::Active:
  1029. AZ_TracePrintf(SCC_WINDOW, "Perforce connected\n");
  1030. break;
  1031. }
  1032. if (AZ::TickBus::IsFunctionQueuing())
  1033. {
  1034. // Push to the main thread for convenience.
  1035. AZStd::function<void()> connectivityNotify = [currentState]()
  1036. {
  1037. SourceControlNotificationBus::Broadcast(&SourceControlNotificationBus::Events::ConnectivityStateChanged, currentState);
  1038. };
  1039. AZ::TickBus::QueueFunction(connectivityNotify);
  1040. }
  1041. }
  1042. return true;
  1043. }
  1044. void PerforceComponent::DropConnectivity()
  1045. {
  1046. AZ_Assert(m_ProcessThreadID != AZStd::thread::id(), "The perforce worker thread has not started.");
  1047. AZ_Assert(AZStd::this_thread::get_id() == m_ProcessThreadID, "You may only call this function from the perforce worker thread.");
  1048. if (!s_perforceConn)
  1049. {
  1050. return;
  1051. }
  1052. delete s_perforceConn;
  1053. s_perforceConn = nullptr;
  1054. AZ_TracePrintf(SCC_WINDOW, "Perforce connectivity dropped\n");
  1055. }
  1056. int PerforceComponent::GetOrCreateOurChangelist()
  1057. {
  1058. if (!CheckConnectivityForAction("list changelists", nullptr))
  1059. {
  1060. return 0;
  1061. }
  1062. int changelistNum = FindExistingChangelist();
  1063. if (changelistNum > 0)
  1064. {
  1065. return changelistNum;
  1066. }
  1067. AzFramework::ProcessWatcher* pWatcher = s_perforceConn->m_command.ExecuteNewChangelistInput();
  1068. AZ_Assert(pWatcher, "No process found for p4!");
  1069. if (!pWatcher)
  1070. {
  1071. return 0;
  1072. }
  1073. AzFramework::ProcessCommunicator* pCommunicator = pWatcher->GetCommunicator();
  1074. AZ_Assert(pCommunicator, "No communicator found for p4!");
  1075. if (!pCommunicator)
  1076. {
  1077. return 0;
  1078. }
  1079. AZStd::string changelistForm = s_perforceConn->m_command.CreateChangelistForm(s_perforceConn->GetClientName(), s_perforceConn->GetUser(), m_autoChangelistDescription);
  1080. AZ_TracePrintf(SCC_WINDOW, "Perforce - Changelist Form\n%s", changelistForm.c_str());
  1081. pCommunicator->WriteInput(changelistForm.data(), static_cast<AZ::u32>(changelistForm.size()));
  1082. if (!pWatcher->WaitForProcessToExit(30))
  1083. {
  1084. pWatcher->TerminateProcess(0);
  1085. }
  1086. delete pWatcher;
  1087. changelistNum = FindExistingChangelist();
  1088. if (changelistNum > 0)
  1089. {
  1090. AZ_TracePrintf(SCC_WINDOW, "Perforce - Changelist number is %i\n", changelistNum);
  1091. }
  1092. else
  1093. {
  1094. AZ_TracePrintf(SCC_WINDOW, "Perforce - %s changelist could not be found, problem creating changelist!\n", m_autoChangelistDescription.c_str());
  1095. }
  1096. return changelistNum;
  1097. }
  1098. int PerforceComponent::FindExistingChangelist()
  1099. {
  1100. s_perforceConn->m_command.ExecuteShowChangelists(s_perforceConn->GetUser(), s_perforceConn->GetClientName());
  1101. if (!CommandSucceeded())
  1102. {
  1103. return 0;
  1104. }
  1105. if (ParseDuplicateOutput(s_perforceConn->m_command.m_commandOutputMapList, s_perforceConn->m_command.m_rawOutput.outputResult))
  1106. {
  1107. int foundChangelist = 0;
  1108. AZStd::vector<PerforceMap>::iterator perforceIterator = s_perforceConn->m_command.FindMapWithPartiallyMatchingValueForKey("desc", m_autoChangelistDescription);
  1109. if (perforceIterator != nullptr)
  1110. {
  1111. if (s_perforceConn->m_command.OutputKeyExists("change", perforceIterator))
  1112. {
  1113. foundChangelist = atoi(s_perforceConn->m_command.GetOutputValue("change", perforceIterator).c_str());
  1114. }
  1115. }
  1116. if (foundChangelist > 0)
  1117. {
  1118. return foundChangelist;
  1119. }
  1120. }
  1121. return 0;
  1122. }
  1123. void PerforceComponent::ThreadWorker()
  1124. {
  1125. m_ProcessThreadID = AZStd::this_thread::get_id();
  1126. while (true)
  1127. {
  1128. // block until signaled:
  1129. m_WorkerSemaphore.acquire();
  1130. if (m_shutdownThreadSignal)
  1131. {
  1132. break; // abandon ship!
  1133. }
  1134. // Verify P4PORT is set before running any commands
  1135. VerifyP4PortIsSet();
  1136. // wait for trust issues to be resolved
  1137. if (!UpdateTrust())
  1138. {
  1139. continue;
  1140. }
  1141. if (!UpdateConnectivity())
  1142. {
  1143. continue;
  1144. }
  1145. if (m_workerQueue.empty())
  1146. {
  1147. continue;
  1148. }
  1149. // if we get here, a job is waiting for us! pop it off the queue (use the queue for as short a time as possible
  1150. PerforceJobRequest topReq;
  1151. {
  1152. AZStd::lock_guard<AZStd::mutex> locker(m_WorkerQueueMutex);
  1153. topReq = m_workerQueue.front();
  1154. m_workerQueue.pop();
  1155. }
  1156. // execute the queued item
  1157. if (!m_offlineMode)
  1158. {
  1159. ProcessJob(topReq);
  1160. }
  1161. else
  1162. {
  1163. ProcessJobOffline(topReq);
  1164. }
  1165. }
  1166. DropConnectivity();
  1167. m_ProcessThreadID = AZStd::thread::id();
  1168. }
  1169. void PerforceComponent::ProcessJob(const PerforceJobRequest& request)
  1170. {
  1171. AZ_Assert(m_ProcessThreadID != AZStd::thread::id(), "The perforce worker thread has not started.");
  1172. AZ_Assert(AZStd::this_thread::get_id() == m_ProcessThreadID, "You may only call this function from the perforce worker thread.");
  1173. PerforceJobResult resp;
  1174. bool validPerforceRequest = true;
  1175. switch (request.m_requestType)
  1176. {
  1177. case PerforceJobRequest::PJR_Stat:
  1178. {
  1179. resp.m_fileInfo = AZStd::move(GetFileInfo(request.m_requestPath.c_str()));
  1180. resp.m_succeeded = resp.m_fileInfo.m_status == SCS_OpSuccess;
  1181. }
  1182. break;
  1183. case PerforceJobRequest::PJR_StatBulk:
  1184. {
  1185. resp.m_bulkFileInfo = AZStd::move(GetBulkFileInfo(request.m_bulkFilePaths));
  1186. resp.m_succeeded = !resp.m_bulkFileInfo.empty();
  1187. }
  1188. break;
  1189. case PerforceJobRequest::PJR_Edit:
  1190. {
  1191. resp.m_succeeded = RequestEdit(request.m_requestPath.c_str(), request.m_allowMultiCheckout);
  1192. resp.m_fileInfo = AZStd::move(GetFileInfo(request.m_requestPath.c_str()));
  1193. }
  1194. break;
  1195. case PerforceJobRequest::PJR_EditBulk:
  1196. {
  1197. resp.m_succeeded = RequestEditBulk(request.m_bulkFilePaths, request.m_allowMultiCheckout);
  1198. resp.m_bulkFileInfo = GetBulkFileInfo(request.m_bulkFilePaths);
  1199. }
  1200. break;
  1201. case PerforceJobRequest::PJR_Delete:
  1202. {
  1203. resp.m_succeeded = RequestDelete(request.m_requestPath.c_str());
  1204. resp.m_fileInfo = AZStd::move(GetFileInfo(request.m_requestPath.c_str()));
  1205. }
  1206. break;
  1207. case PerforceJobRequest::PJR_DeleteBulk:
  1208. {
  1209. resp.m_succeeded = RequestDeleteBulk(request.m_requestPath.c_str());
  1210. resp.m_bulkFileInfo = GetBulkFileInfo(request.m_requestPath.c_str());
  1211. }
  1212. break;
  1213. case PerforceJobRequest::PJR_Revert:
  1214. {
  1215. resp.m_succeeded = RequestRevert(request.m_requestPath.c_str());
  1216. resp.m_fileInfo = AZStd::move(GetFileInfo(request.m_requestPath.c_str()));
  1217. }
  1218. break;
  1219. case PerforceJobRequest::PJR_Rename:
  1220. {
  1221. resp.m_succeeded = RequestRename(request.m_requestPath.c_str(), request.m_targetPath.c_str());
  1222. resp.m_fileInfo = AZStd::move(GetFileInfo(request.m_targetPath.c_str()));
  1223. }
  1224. break;
  1225. case PerforceJobRequest::PJR_RenameBulk:
  1226. {
  1227. resp.m_succeeded = RequestRenameBulk(request.m_requestPath.c_str(), request.m_targetPath.c_str());
  1228. resp.m_bulkFileInfo = GetBulkFileInfo(request.m_targetPath.c_str());
  1229. }
  1230. break;
  1231. case PerforceJobRequest::PJR_Sync:
  1232. {
  1233. resp.m_succeeded = RequestLatest(request.m_requestPath.c_str());
  1234. resp.m_fileInfo = AZStd::move(GetFileInfo(request.m_requestPath.c_str()));
  1235. }
  1236. break;
  1237. default:
  1238. {
  1239. validPerforceRequest = false;
  1240. AZ_Assert(false, "Invalid type of perforce job request.");
  1241. }
  1242. break;
  1243. }
  1244. if (validPerforceRequest)
  1245. {
  1246. resp.m_callback = request.m_callback;
  1247. resp.m_bulkCallback = request.m_bulkCallback;
  1248. {
  1249. AZStd::lock_guard<AZStd::mutex> locker(m_ResultQueueMutex);
  1250. m_resultQueue.push(AZStd::move(resp));
  1251. AZ::TickBus::QueueFunction(&PerforceComponent::ProcessResultQueue, this); // narrow events to the main thread.
  1252. }
  1253. }
  1254. }
  1255. AZStd::vector<SourceControlFileInfo> PerforceComponent::GetBulkFileInfo(const char* requestPath) const
  1256. {
  1257. return GetBulkFileInfo(AZStd::unordered_set<AZStd::string>{ requestPath });
  1258. }
  1259. AZStd::vector<SourceControlFileInfo> PerforceComponent::GetBulkFileInfo(const AZStd::unordered_set<AZStd::string>& requestPaths) const
  1260. {
  1261. AZStd::vector<SourceControlFileInfo> fileInfoList;
  1262. AZStd::vector<PerforceMap> commandMap;
  1263. ExecuteAndParseFstat(requestPaths, commandMap);
  1264. for(const AZStd::string& filePath : requestPaths)
  1265. {
  1266. if (!s_perforceConn->m_command.FileExists(filePath.c_str()))
  1267. {
  1268. SourceControlFileInfo newInfo;
  1269. newInfo.m_filePath = filePath;
  1270. newInfo.m_status = SCS_OpSuccess;
  1271. if (AZ::IO::SystemFile::IsWritable(filePath.c_str()) || !AZ::IO::SystemFile::Exists(filePath.c_str()))
  1272. {
  1273. newInfo.m_flags |= SCF_Writeable;
  1274. }
  1275. fileInfoList.push_back(newInfo);
  1276. }
  1277. }
  1278. for (const auto& map : commandMap)
  1279. {
  1280. auto fileItr = map.find("clientFile");
  1281. if (fileItr != map.end())
  1282. {
  1283. SourceControlFileInfo newInfo;
  1284. newInfo.m_filePath = fileItr->second;
  1285. newInfo.m_status = SCS_OpSuccess;
  1286. newInfo.m_flags |= SCF_Tracked;
  1287. if(AZ::IO::SystemFile::IsWritable(newInfo.m_filePath.c_str()) || !AZ::IO::SystemFile::Exists(newInfo.m_filePath.c_str()))
  1288. {
  1289. newInfo.m_flags |= SCF_Writeable;
  1290. }
  1291. bool newFileAfterDeletionAtHead = s_perforceConn->m_command.NewFileAfterDeletedRev(&map);
  1292. if (!newFileAfterDeletionAtHead)
  1293. {
  1294. if (s_perforceConn->m_command.GetHeadRevision(&map) != s_perforceConn->m_command.GetHaveRevision(&map))
  1295. {
  1296. newInfo.m_flags |= SCF_OutOfDate;
  1297. }
  1298. }
  1299. if(!s_perforceConn->m_command.ExclusiveOpen(&map))
  1300. {
  1301. newInfo.m_flags |= SCF_MultiCheckOut;
  1302. }
  1303. bool openByOthers = s_perforceConn->m_command.IsOpenByOtherUsers(&map);
  1304. if (openByOthers)
  1305. {
  1306. newInfo.m_flags |= SCF_OtherOpen;
  1307. newInfo.m_StatusUser = s_perforceConn->m_command.GetOtherUserCheckedOut(&map);
  1308. }
  1309. if(s_perforceConn->m_command.IsOpenByCurrentUser(&map))
  1310. {
  1311. newInfo.m_flags |= SCF_OpenByUser;
  1312. if (s_perforceConn->m_command.CurrentActionIsMove(&map))
  1313. {
  1314. newInfo.m_flags |= SCF_PendingMove;
  1315. }
  1316. else if (s_perforceConn->m_command.CurrentActionIsAdd(&map))
  1317. {
  1318. newInfo.m_flags |= SCF_PendingAdd;
  1319. }
  1320. else if (s_perforceConn->m_command.CurrentActionIsDelete(&map))
  1321. {
  1322. newInfo.m_flags |= SCF_PendingDelete;
  1323. }
  1324. }
  1325. fileInfoList.push_back(newInfo);
  1326. }
  1327. }
  1328. return fileInfoList;
  1329. }
  1330. void PerforceComponent::ProcessJobOffline(const PerforceJobRequest& request)
  1331. {
  1332. AZ_Assert(m_ProcessThreadID != AZStd::thread::id(), "The perforce worker thread has not started.");
  1333. AZ_Assert(AZStd::this_thread::get_id() == m_ProcessThreadID, "You may only call this function from the perforce worker thread.");
  1334. AZ_Assert(m_offlineMode, "Processing Source Control Jobs in offline mode when we are connected to Source Control");
  1335. switch (request.m_requestType)
  1336. {
  1337. case PerforceJobRequest::PJR_Stat:
  1338. m_localFileSCComponent.GetFileInfo(request.m_requestPath.c_str(), request.m_callback);
  1339. break;
  1340. case PerforceJobRequest::PJR_StatBulk:
  1341. m_localFileSCComponent.GetBulkFileInfo(request.m_bulkFilePaths, request.m_bulkCallback);
  1342. break;
  1343. case PerforceJobRequest::PJR_Edit:
  1344. m_localFileSCComponent.RequestEdit(request.m_requestPath.c_str(), request.m_allowMultiCheckout, request.m_callback);
  1345. break;
  1346. case PerforceJobRequest::PJR_EditBulk:
  1347. m_localFileSCComponent.RequestEditBulk(request.m_bulkFilePaths, request.m_allowMultiCheckout, request.m_bulkCallback);
  1348. break;
  1349. case PerforceJobRequest::PJR_Delete:
  1350. m_localFileSCComponent.RequestDeleteExtended(request.m_requestPath.c_str(), request.m_skipReadonly, request.m_callback);
  1351. break;
  1352. case PerforceJobRequest::PJR_DeleteBulk:
  1353. m_localFileSCComponent.RequestDeleteBulkExtended(request.m_requestPath.c_str(), request.m_skipReadonly, request.m_bulkCallback);
  1354. break;
  1355. case PerforceJobRequest::PJR_Revert:
  1356. m_localFileSCComponent.RequestRevert(request.m_requestPath.c_str(), request.m_callback);
  1357. break;
  1358. case PerforceJobRequest::PJR_Rename:
  1359. m_localFileSCComponent.RequestRenameExtended(request.m_requestPath.c_str(), request.m_targetPath.c_str(), request.m_skipReadonly, request.m_callback);
  1360. break;
  1361. case PerforceJobRequest::PJR_RenameBulk:
  1362. m_localFileSCComponent.RequestRenameBulkExtended(request.m_requestPath.c_str(), request.m_targetPath.c_str(), request.m_skipReadonly, request.m_bulkCallback);
  1363. break;
  1364. case PerforceJobRequest::PJR_Sync:
  1365. m_localFileSCComponent.RequestLatest(request.m_requestPath.c_str(), request.m_callback);
  1366. break;
  1367. default:
  1368. AZ_Assert(false, "Invalid type of perforce job request.");
  1369. break;
  1370. }
  1371. }
  1372. // narrow events to the main thread.
  1373. void PerforceComponent::ProcessResultQueue()
  1374. {
  1375. while (!m_resultQueue.empty())
  1376. {
  1377. PerforceJobResult resp;
  1378. {
  1379. AZStd::lock_guard<AZStd::mutex> locker(m_ResultQueueMutex);
  1380. resp = AZStd::move(m_resultQueue.front());
  1381. m_resultQueue.pop();
  1382. }
  1383. // dispatch the callback outside the scope of the lock so that more elements could be queued by threads...
  1384. if (resp.m_callback)
  1385. {
  1386. resp.m_callback(resp.m_succeeded, resp.m_fileInfo);
  1387. }
  1388. else
  1389. {
  1390. resp.m_bulkCallback(resp.m_succeeded, resp.m_bulkFileInfo);
  1391. }
  1392. }
  1393. while (!m_settingsQueue.empty())
  1394. {
  1395. PerforceSettingResult result;
  1396. {
  1397. AZStd::lock_guard<AZStd::mutex> locker(m_SettingsQueueMutex);
  1398. result = AZStd::move(m_settingsQueue.front());
  1399. m_settingsQueue.pop();
  1400. }
  1401. // dispatch the callback outside the scope of the lock so that more elements could be queued by threads...
  1402. result.m_callback(result.m_settingInfo);
  1403. }
  1404. }
  1405. bool PerforceComponent::ParseOutput(PerforceMap& perforceMap, AZStd::string& perforceOutput, const char* lineDelim) const
  1406. {
  1407. const char* delimiter = lineDelim ? lineDelim : " ";
  1408. // replace "..." with empty strings, each line is in the format of "... key value"
  1409. AzFramework::StringFunc::Replace(perforceOutput, "...", "");
  1410. // tokenize the buffer into line by line key/value pairs
  1411. AZStd::vector<AZStd::string> lineTokens;
  1412. AzFramework::StringFunc::Tokenize(perforceOutput.c_str(), lineTokens, "\r\n");
  1413. // add all key/value pairs to a map
  1414. perforceMap.clear();
  1415. AZStd::vector<AZStd::string> keyValueTokens;
  1416. AZStd::string key, value;
  1417. for (auto lineToken : lineTokens)
  1418. {
  1419. // tokenize line, add kvp, clear tokens
  1420. AzFramework::StringFunc::Tokenize(lineToken.c_str(), keyValueTokens, delimiter);
  1421. // ensure we found a key
  1422. if (keyValueTokens.size() > 0)
  1423. {
  1424. key = keyValueTokens[0];
  1425. }
  1426. else
  1427. {
  1428. continue;
  1429. }
  1430. // check for a value, not all keys will have values e.g. isMapped (although most keys do)
  1431. value = "";
  1432. if (keyValueTokens.size() > 1)
  1433. {
  1434. for (int i = 1; i < keyValueTokens.size(); i++)
  1435. {
  1436. value += keyValueTokens[i];
  1437. }
  1438. }
  1439. perforceMap.insert(AZStd::make_pair(key, value));
  1440. keyValueTokens.clear();
  1441. }
  1442. return !perforceMap.empty();
  1443. }
  1444. bool PerforceComponent::ParseDuplicateOutput(AZStd::vector<PerforceMap>& perforceMapList, AZStd::string& perforceOutput) const
  1445. {
  1446. // replace "..." with empty strings, each line is in the format of "... key value"
  1447. AzFramework::StringFunc::Replace(perforceOutput, "...", "");
  1448. /* insert a group divider in the middle of "\r\n\r\n", a new group of key/values seems to be noted by a double carriage return
  1449. when the group divider is found, the current mapped values will be inserted into the vector and a new map will be started */
  1450. AzFramework::StringFunc::Replace(perforceOutput, "\r\n\r\n", "\r\n|-|\r\n");
  1451. // tokenize the buffer into line by line key/value pairs
  1452. AZStd::vector<AZStd::string> lineTokens;
  1453. AzFramework::StringFunc::Tokenize(perforceOutput.c_str(), lineTokens, "\r\n");
  1454. // add all key/value pairs to a map
  1455. perforceMapList.clear();
  1456. AZStd::vector<AZStd::string> keyValueTokens;
  1457. AZStd::string key, value;
  1458. PerforceMap currentMap;
  1459. for (const auto& lineToken : lineTokens)
  1460. {
  1461. // tokenize line, add kvp, clear tokens
  1462. AzFramework::StringFunc::Tokenize(lineToken.c_str(), keyValueTokens, " ");
  1463. // ensure we found a key
  1464. if (!keyValueTokens.empty())
  1465. {
  1466. key = keyValueTokens[0];
  1467. // insert the map into the vector when the kvp group divider is found
  1468. if (key == "|-|")
  1469. {
  1470. perforceMapList.push_back(currentMap);
  1471. currentMap.clear();
  1472. keyValueTokens.clear();
  1473. continue;
  1474. }
  1475. }
  1476. else
  1477. {
  1478. continue;
  1479. }
  1480. // check for a value, not all keys will have values e.g. isMapped (although most keys do)
  1481. value = "";
  1482. if (keyValueTokens.size() > 1)
  1483. {
  1484. for (int i = 1; i < keyValueTokens.size(); i++)
  1485. {
  1486. value.append(keyValueTokens[i]);
  1487. if (i != keyValueTokens.size() - 1)
  1488. {
  1489. value.append(" ");
  1490. }
  1491. }
  1492. }
  1493. currentMap.insert(AZStd::make_pair(key, value));
  1494. keyValueTokens.clear();
  1495. }
  1496. return !perforceMapList.empty();
  1497. }
  1498. } // namespace AzToolsFramework