232{
233 boost::program_options::options_description desc("This program validates OKS git repository for commit by pre-receive hook");
234
235 std::vector<std::string> created, updated, deleted;
236 bool circular_dependency_between_includes_is_error = true;
237 bool verbose = false;
238 std::string user;
239 std::size_t pipeline_size = 4;
240
241 try
242 {
243 std::vector<std::string> app_types_list;
244 std::vector<std::string> segments_list;
245
246 desc.add_options()
247 ("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")
248 ("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")
249 ("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")
250 ("permissive-circular-dependencies-between-includes,C", "downgrade severity of detected circular dependencies between includes from errors to warnings")
251 ("user,U", boost::program_options::value<std::string>(&user), "user id")
252 ("threads-number,t", boost::program_options::value<std::size_t>(&pipeline_size)->default_value(pipeline_size), "number of threads used by validation pipeline")
253 ("verbose,v", "Print debug information")
254 ("help,h", "Print help message");
255
256 boost::program_options::variables_map vm;
257 boost::program_options::store(boost::program_options::parse_command_line(argc, argv, desc), vm);
258
259 if (vm.count("help"))
260 {
261 std::cout << desc << std::endl;
263 }
264
265 if (vm.count("permissive-circular-dependencies-between-includes"))
266 circular_dependency_between_includes_is_error = false;
267
268 if (vm.count("verbose"))
269 verbose = true;
270
271 boost::program_options::notify(vm);
272
273 }
274 catch (std::exception& ex)
275 {
276 std::cerr << "Command line parsing errors occurred:\n" << ex.what() << std::endl;
278 }
279
280
281 try
282 {
284
287
289 {
290 std::cerr << "There is no OKS repository set (check TDAQ_DB_REPOSITORY)" << std::endl;
292 }
293
295
296 auto start_usage = std::chrono::steady_clock::now();
297
298
299
300 std::set<std::string> directories;
301
302
303
304 std::map<std::string, std::set<std::string>> file_explicit_includes;
305
306 for (auto& p : std::filesystem::recursive_directory_iterator("."))
307 if (std::filesystem::is_directory(p))
308 directories.insert(p.path().native().substr(2));
309 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)
310 kernel.
get_includes(p.path().native(), file_explicit_includes[p.path().native().substr(2)],
true);
311
312 if (verbose)
313 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";
314
315
316 auto start_usage2 = std::chrono::steady_clock::now();
317
318 std::set<std::string> all_includes;
319
320
321 for (const auto& f : file_explicit_includes)
322 {
323 for (
const auto& i :
f.second)
324 {
325 if(file_explicit_includes.find(i) != file_explicit_includes.end())
326 {
327 all_includes.insert(i);
328 }
329 else
330 {
331 std::cerr <<
"Cannot find file \"" << i <<
"\" included by \"" <<
f.first <<
"\"" << std::endl;
333 }
334 }
335 }
336
337 if (verbose)
338 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";
339
340
341 auto start_usage3 = std::chrono::steady_clock::now();
342
343
344 std::map<std::string, std::set<std::string>> file_all_includes;
345
346 for(auto& x : file_explicit_includes)
347 {
349 define_includes(x.first, x.second, file_all_includes, file_explicit_includes, cd_fuse);
350 }
351
352 auto stop_usage3 = std::chrono::steady_clock::now();
353
354 if (verbose)
355 log_timestamp(
Debug) <<
"calculated inclusion graph in " << std::chrono::duration_cast<std::chrono::microseconds>(stop_usage3-start_usage3).count() / 1000. <<
" ms\n";
356
357 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;
358
359
361 {
363
364 if (circular_dependency_between_includes_is_error == true)
366 }
367
369 {
370 std::ostringstream text;
371 text << "ALL INCLUDES:\n";
372
373 for (const auto& x : file_all_includes)
374 {
375 text << "FILE \"" << x.first << "\" has " << x.second.size() << " includes:\n";
376
377 for (const auto& y : x.second)
378 text << " - \"" << y << "\"\n";
379 }
380
382 }
383
385
386
387 auto ignore_files = [](std::string& x)
388 {
389 static const std::string readme_file("README.md");
390 return x != readme_file;
391 };
392
393
394 std::set<std::string> modified;
395 std::copy_if(created.begin(), created.end(), std::inserter(modified, modified.end()), ignore_files);
396 std::copy_if(updated.begin(), updated.end(), std::inserter(modified, modified.end()), ignore_files);
397
398
399 for (const auto& x : modified)
401
402 std::copy_if(deleted.begin(), deleted.end(), std::inserter(modified, modified.end()), ignore_files);
403
404 for (const auto& f : file_explicit_includes)
405 if (all_includes.find(
f.first) == all_includes.end())
406 {
407 if (modified.empty() == false)
408 {
409 if (modified.find(
f.first) == modified.end())
410 {
411 const auto& file_includes = file_all_includes[
f.first];
412
413 bool found = false;
414
415 for (const auto& x : modified)
416 if (file_includes.find(x) != file_includes.end())
417 {
418 found = true;
419 TLOG_DEBUG(1) <<
"file \"" <<
f.first <<
"\" contains modified include \"" << x <<
'\"';
420 break;
421 }
422
423 if(found == false)
424 {
425 TLOG_DEBUG(1) <<
"skip file \"" <<
f.first <<
'\"';
426 continue;
427 }
428 }
429 else
430 {
431 TLOG_DEBUG(1) <<
"list of modified files contains file \"" <<
f.first <<
'\"';
432 }
433 }
434
435 if (modified.find(
f.first) == modified.end())
437 }
438
439 pipeline.waitForCompletion();
440
442 {
445 }
446 }
448 {
451 }
452 catch (std::exception & e)
453 {
456 }
457 catch (...)
458 {
461 }
462
464}
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