227{
228 boost::program_options::options_description desc("This program validates OKS git repository for commit by pre-receive hook");
229
230 std::vector<std::string> created, updated, deleted;
231 bool circular_dependency_between_includes_is_error = true;
232 bool verbose = false;
233 std::string user;
234 std::size_t pipeline_size = 4;
235
236 try
237 {
238 std::vector<std::string> app_types_list;
239 std::vector<std::string> segments_list;
240
241 desc.add_options()
242 ("add,a", boost::program_options::value<std::vector<std::string> >(&created)->multitoken(), "list of new OKS files and directories to be added to the repository")
243 ("update,u", boost::program_options::value<std::vector<std::string> >(&updated)->multitoken(), "list of new OKS files and directories to be updated in the repository")
244 ("remove,r", boost::program_options::value<std::vector<std::string> >(&deleted)->multitoken(), "list of new OKS files and directories to be removed from the repository")
245 ("permissive-circular-dependencies-between-includes,C", "downgrade severity of detected circular dependencies between includes from errors to warnings")
246 ("user,U", boost::program_options::value<std::string>(&user), "user id")
247 ("threads-number,t", boost::program_options::value<std::size_t>(&pipeline_size)->default_value(pipeline_size), "number of threads used by validation pipeline")
248 ("verbose,v", "Print debug information")
249 ("help,h", "Print help message");
250
251 boost::program_options::variables_map vm;
252 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
253
254 if (vm.count("help"))
255 {
256 std::cout << desc << std::endl;
258 }
259
260 if (vm.count("permissive-circular-dependencies-between-includes"))
261 circular_dependency_between_includes_is_error = false;
262
263 if (vm.count("verbose"))
264 verbose = true;
265
266 boost::program_options::notify(vm);
267
268 }
269 catch (std::exception& ex)
270 {
271 std::cerr << "Command line parsing errors occurred:\n" << ex.what() << std::endl;
273 }
274
275
276 try
277 {
279
282
284 {
285 std::cerr << "There is no OKS repository set (check TDAQ_DB_REPOSITORY)" << std::endl;
287 }
288
290
291 auto start_usage = std::chrono::steady_clock::now();
292
293
294
295 std::set<std::string> directories;
296
297
298
299 std::map<std::string, std::set<std::string>> file_explicit_includes;
300
301 for (auto& p : std::filesystem::recursive_directory_iterator("."))
302 if (std::filesystem::is_directory(p))
303 directories.insert(p.path().native().substr(2));
304 else if (std::filesystem::is_regular_file(p) && p.path().native().find("./.git") != 0 && p.path().native().find("./admin") != 0 && p.path().native().find("./README.md") != 0)
305 kernel.
get_includes(p.path().native(), file_explicit_includes[p.path().native().substr(2)],
true);
306
307 if (verbose)
308 log_timestamp(
Debug) <<
"scan " << file_explicit_includes.size() <<
" repository files in " << std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now()-start_usage).count() / 1000. <<
" ms\n";
309
310
311 auto start_usage2 = std::chrono::steady_clock::now();
312
313 std::set<std::string> all_includes;
314
315
316 for (const auto& f : file_explicit_includes)
317 {
318 for (
const auto& i :
f.second)
319 {
320 if(file_explicit_includes.find(i) != file_explicit_includes.end())
321 {
322 all_includes.insert(i);
323 }
324 else
325 {
326 std::cerr <<
"Cannot find file \"" << i <<
"\" included by \"" <<
f.first <<
"\"" << std::endl;
328 }
329 }
330 }
331
332 if (verbose)
333 log_timestamp(
Debug) <<
"check existence of includes in " << std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::steady_clock::now()-start_usage2).count() / 1000. <<
" ms\n";
334
335
336 auto start_usage3 = std::chrono::steady_clock::now();
337
338
339 std::map<std::string, std::set<std::string>> file_all_includes;
340
341 for(auto& x : file_explicit_includes)
342 {
344 define_includes(x.first, x.second, file_all_includes, file_explicit_includes, cd_fuse);
345 }
346
347 auto stop_usage3 = std::chrono::steady_clock::now();
348
349 if (verbose)
350 log_timestamp(
Debug) <<
"calculated inclusion graph in " << std::chrono::duration_cast<std::chrono::microseconds>(stop_usage3-start_usage3).count() / 1000. <<
" ms\n";
351
352 log_timestamp() <<
"process " << file_explicit_includes.size() <<
" repository files and their includes in " << std::chrono::duration_cast<std::chrono::microseconds>(stop_usage3-start_usage).count() / 1000. <<
" ms" << std::endl;
353
354
356 {
358
359 if (circular_dependency_between_includes_is_error == true)
361 }
362
364 {
365 std::ostringstream text;
366 text << "ALL INCLUDES:\n";
367
368 for (const auto& x : file_all_includes)
369 {
370 text << "FILE \"" << x.first << "\" has " << x.second.size() << " includes:\n";
371
372 for (const auto& y : x.second)
373 text << " - \"" << y << "\"\n";
374 }
375
377 }
378
380
381
382 auto ignore_files = [](std::string& x)
383 {
384 static const std::string readme_file("README.md");
385 return x != readme_file;
386 };
387
388
389 std::set<std::string> modified;
390 std::copy_if(created.begin(), created.end(), std::inserter(modified, modified.end()), ignore_files);
391 std::copy_if(updated.begin(), updated.end(), std::inserter(modified, modified.end()), ignore_files);
392
393
394 for (const auto& x : modified)
396
397 std::copy_if(deleted.begin(), deleted.end(), std::inserter(modified, modified.end()), ignore_files);
398
399 for (const auto& f : file_explicit_includes)
400 if (all_includes.find(
f.first) == all_includes.end())
401 {
402 if (modified.empty() == false)
403 {
404 if (modified.find(
f.first) == modified.end())
405 {
406 const auto& file_includes = file_all_includes[
f.first];
407
408 bool found = false;
409
410 for (const auto& x : modified)
411 if (file_includes.find(x) != file_includes.end())
412 {
413 found = true;
414 TLOG_DEBUG(1) <<
"file \"" <<
f.first <<
"\" contains modified include \"" << x <<
'\"';
415 break;
416 }
417
418 if(found == false)
419 {
420 TLOG_DEBUG(1) <<
"skip file \"" <<
f.first <<
'\"';
421 continue;
422 }
423 }
424 else
425 {
426 TLOG_DEBUG(1) <<
"list of modified files contains file \"" <<
f.first <<
'\"';
427 }
428 }
429
430 if (modified.find(
f.first) == modified.end())
432 }
433
434 pipeline.waitForCompletion();
435
437 {
440 }
441 }
443 {
446 }
447 catch (std::exception & e)
448 {
451 }
452 catch (...)
453 {
456 }
457
459}
Provides interface to the OKS kernel.
const std::string & get_user_repository_root() const
Get user OKS repository root.
void set_allow_duplicated_objects_mode(const bool b)
Set status of duplicated objects mode. To switch 'On'/'Off' use the method's parameter:
void set_test_duplicated_objects_via_inheritance_mode(const bool b)
Set status of test inherited duplicated objects mode. To switch 'On'/'Off' use the method's parameter...
void get_includes(const std::string &file_name, std::set< std::string > &includes, bool use_repository_name=false)
Opens file and reads its shallow includes.
#define TLOG_DEBUG(lvl,...)
std::ostream & log_timestamp(__LogSeverity__ severity=Log)
struct FoundCircularDependency s_circular_dependency_message