-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathp4_api.cc
More file actions
342 lines (286 loc) · 8.36 KB
/
p4_api.cc
File metadata and controls
342 lines (286 loc) · 8.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright (c) 2022 Salesforce, Inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
#include "p4_api.h"
#include <csignal>
#include <memory>
#include "commands/stream_result.h"
#include "utils/std_helpers.h"
#include "p4/p4libs.h"
#include "p4/signaler.h"
#include "minitrace.h"
ClientResult::ClientSpecData P4API::ClientSpec;
std::string P4API::P4PORT;
std::string P4API::P4USER;
std::string P4API::P4CLIENT;
int P4API::CommandRetries = 1;
int P4API::CommandRefreshThreshold = 1;
std::mutex P4API::InitializationMutex;
P4API::P4API()
{
if (!Initialize())
{
ERR("Could not initialize P4API");
return;
}
AddClientSpecView(ClientSpec.mapping);
}
bool P4API::Initialize()
{
MTR_SCOPE("P4", __func__);
// Helix Core C++ API seems to crash while making connections parallely.
std::unique_lock<std::mutex> lock(InitializationMutex);
Error e;
StrBuf msg;
m_Usage = 0;
m_ClientAPI.SetPort(P4PORT.c_str());
m_ClientAPI.SetUser(P4USER.c_str());
m_ClientAPI.SetClient(P4CLIENT.c_str());
m_ClientAPI.SetProtocol("tag", "");
m_ClientAPI.Init(&e);
if (!CheckErrors(e, msg))
{
ERR("Could not initialize Helix Core C/C++ API");
return false;
}
return true;
}
bool P4API::Deinitialize()
{
std::unique_lock<std::mutex> lock(InitializationMutex);
Error e;
StrBuf msg;
m_ClientAPI.Final(&e);
CheckErrors(e, msg);
return true;
}
bool P4API::Reinitialize()
{
MTR_SCOPE("P4", __func__);
bool status = Deinitialize() && Initialize();
return status;
}
P4API::~P4API()
{
if (!Deinitialize())
{
ERR("P4API context was not destroyed successfully");
}
}
bool P4API::IsDepotPathValid(const std::string& depotPath)
{
return STDHelpers::EndsWith(depotPath, "/...") && STDHelpers::StartsWith(depotPath, "//");
}
bool P4API::IsFileUnderDepotPath(const std::string& fileRevision, const std::string& depotPath)
{
return STDHelpers::Contains(fileRevision, depotPath.substr(0, depotPath.size() - 3)); // -3 to remove the trailing "..."
}
bool P4API::IsDepotPathUnderClientSpec(const std::string& depotPath)
{
return m_ClientMapping.IsInLeft(depotPath);
}
bool P4API::IsFileUnderClientSpec(const std::string& fileRevision)
{
return m_ClientMapping.IsInRight(fileRevision);
}
bool P4API::CheckErrors(Error& e, StrBuf& msg)
{
if (e.Test())
{
e.Fmt(&msg);
ERR(msg.Text());
return false;
}
return true;
}
bool P4API::InitializeLibraries()
{
Error e;
StrBuf msg;
P4Libraries::Initialize(P4LIBRARIES_INIT_ALL, &e);
if (e.Test())
{
e.Fmt(&msg);
ERR(msg.Text());
ERR("Failed to initialize P4Libraries");
return false;
}
// We disable the default signaler to stop it from deleting memory from the wrong heap
// https://www.perforce.com/manuals/p4api/Content/P4API/chapter.clientprogramming.signaler.html
std::signal(SIGINT, SIG_DFL);
signaler.Disable();
SUCCESS("Initialized P4Libraries successfully");
return true;
}
bool P4API::ShutdownLibraries()
{
Error e;
StrBuf msg;
P4Libraries::Shutdown(P4LIBRARIES_INIT_ALL, &e);
if (e.Test())
{
e.Fmt(&msg);
ERR(msg.Text());
return false;
}
return true;
}
void P4API::AddClientSpecView(const std::vector<std::string>& viewStrings)
{
m_ClientMapping.InsertTranslationMapping(viewStrings);
}
void P4API::UpdateClientSpec()
{
Run<Result>("client", {});
}
std::unique_ptr<ClientResult> P4API::Client()
{
return Run<ClientResult>("client", { "-o" });
}
std::unique_ptr<StreamResult> P4API::Stream(const std::string& path)
{
return Run<StreamResult>("stream", { "-o", path });
}
std::unique_ptr<TestResult> P4API::TestConnection(const int retries)
{
return RunEx<TestResult>("changes", { "-m", "1", "//..." }, retries);
}
std::unique_ptr<ChangesResult> P4API::ShortChanges(const std::string& path)
{
std::unique_ptr<ChangesResult> changes = Run<ChangesResult>("changes", {
"-r", // Get CLs from earliest to latest
"-s", "submitted", // Only include submitted CLs
path // Depot path to get CLs from
});
return changes;
}
std::unique_ptr<ChangesResult> P4API::Changes(const std::string& path)
{
MTR_SCOPE("P4", __func__);
return Run<ChangesResult>("changes", {
"-l", // Get full descriptions instead of sending cut-short ones
"-s", "submitted", // Only include submitted CLs
path // Depot path to get CLs from
});
}
std::unique_ptr<ChangesResult> P4API::Changes(const std::string& path, const std::string& from, int32_t maxCount)
{
std::vector<std::string> args = {
"-l", // Get full descriptions instead of sending cut-short ones
"-s", "submitted", // Only include submitted CLs
"-r" // Send CLs in chronological order
};
// This needs to be declared outside the if scope below to
// keep the internal character array alive till the p4 call is made
std::string maxCountStr;
if (maxCount != -1)
{
maxCountStr = std::to_string(maxCount);
args.push_back("-m"); // Only send max this many number of CLs
args.push_back(maxCountStr);
}
std::string pathAddition;
if (!from.empty())
{
// Appending "@CL_NUMBER,@now" seems to include the current CL,
// which makes this awkward to deal with in general. So instead,
// we append "@>CL_NUMBER" so that we only receive the CLs after
// the current one.
pathAddition = "@>" + from;
}
args.push_back(path + pathAddition);
std::unique_ptr<ChangesResult> result = Run<ChangesResult>("changes", args);
return result;
}
std::unique_ptr<ChangesResult> P4API::ChangesFromTo(const std::string& path, const std::string& from, const std::string& to)
{
std::string pathArg = path + "@" + from + "," + to;
return Run<ChangesResult>("changes", {
"-s", "submitted", // Only include submitted CLs
pathArg // Depot path to get CLs from
});
}
std::unique_ptr<ChangesResult> P4API::LatestChange(const std::string& path)
{
MTR_SCOPE("P4", __func__);
return Run<ChangesResult>("changes", {
"-s", "submitted", // Only include submitted CLs,
"-m", "1", // Get top-most change
path // Depot path to get CLs from
});
}
std::unique_ptr<ChangesResult> P4API::OldestChange(const std::string& path)
{
std::unique_ptr<ChangesResult> changes = Run<ChangesResult>("changes", {
"-r", // List from earliest to latest
"-s", "submitted", // Only include submitted CLs,
"-m", "1", // Get top-most change
path // Depot path to get CLs from
});
return changes;
}
std::unique_ptr<DescribeResult> P4API::Describe(const std::string& cl)
{
MTR_SCOPE("P4", __func__);
return Run<DescribeResult>("describe", { "-s", // Omit the diffs
cl });
}
std::unique_ptr<FileLogResult> P4API::FileLog(const std::string& changelist)
{
return Run<FileLogResult>("filelog", {
"-c", // restrict output to a single changelist
changelist,
"-m1", // don't get the full history, just the first entry.
"//..." // rather than require the path to be passed in, just list all files.
});
}
std::unique_ptr<SizesResult> P4API::Size(const std::string& file)
{
return Run<SizesResult>("sizes", { "-a", "-s", file });
}
std::unique_ptr<Result> P4API::Sync()
{
return Run<Result>("sync", {});
}
std::unique_ptr<SyncResult> P4API::GetFilesToSyncAtCL(const std::string& path, const std::string& cl)
{
std::string clCommand = "@" + cl;
return Run<SyncResult>("sync", {
"-n", // Only preview the files to sync. Don't send file contents...yet
clCommand,
});
}
std::unique_ptr<PrintResult> P4API::PrintFile(const std::string& filePathRevision)
{
return Run<PrintResult>("print", {
filePathRevision,
});
}
std::unique_ptr<PrintResult> P4API::PrintFiles(const std::vector<std::string>& fileRevisions)
{
MTR_SCOPE("P4", __func__);
if (fileRevisions.empty())
{
return std::unique_ptr<PrintResult>(new PrintResult());
}
return Run<PrintResult>("print", fileRevisions);
}
std::unique_ptr<Result> P4API::Sync(const std::string& path)
{
return Run<Result>("sync", {
path // Sync a particular depot path
});
}
std::unique_ptr<UsersResult> P4API::Users()
{
return Run<UsersResult>("users", {
"-a" // Include service accounts
});
}
std::unique_ptr<InfoResult> P4API::Info()
{
return Run<InfoResult>("info", {});
}