Code forensics
0.1
Generate historical information about code changes
|
Go to the documentation of this file.
4 #include <sqlite_orm/sqlite_orm.h>
8 #include <unordered_map>
12 #include "dod_base.hpp"
17 template <dod::IsIdType T>
18 auto operator<<(std::ostream& stream, T
id) -> std::ostream& {
22 stream <<
id.getValue();
28 template <dod::IsIdType T>
29 struct type_printer<T> :
public integer_printer {};
31 template <dod::IsIdType T>
32 struct statement_binder<T> {
33 auto bind(sqlite3_stmt* stmt,
int index, T value) ->
int {
35 return sqlite3_bind_null(stmt, index);
38 return statement_binder<typename T::id_base_type>().bind(
39 stmt, index, value.getValue());
44 template <dod::IsIdType T>
45 struct field_printer<T> {
50 return field_printer<typename T::id_base_type>()(t.getValue());
55 template <dod::IsIdType T>
56 struct row_extractor<T> {
57 auto extract(
const char* row_value) -> T {
58 return T::FromValue(std::stoi(row_value));
61 auto extract(sqlite3_stmt* stmt,
int columnIndex) -> T {
62 return T::FromValue(sqlite3_column_int(stmt, columnIndex));
103 return this->path == other.path;
152 return name == other.name && parent == other.parent;
162 return text == other.text;
174 return name == other.name && email == other.email;
195 return author == other.author && time == other.time &&
196 content == other.content;
207 template <
typename T,
typename... Rest>
208 inline void hash_combine(std::size_t& seed,
const T& v, Rest... rest) {
210 seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
215 #define MAKE_HASHABLE(__type, __varname, ...) \
218 struct hash<__type> { \
219 auto operator()(const __type& __varname) const \
221 std::size_t ret = 0; \
222 hash_combine(ret, __VA_ARGS__); \
240 dod::InternStore<AuthorId, Author>,
241 dod::InternStore<LineId, LineData>,
242 dod::Store<FileId, File>,
243 dod::InternStore<FilePathId, FilePath>,
244 dod::Store<CommitId, Commit>,
245 dod::InternStore<DirectoryId, Directory>,
246 dod::InternStore<StringId, String>
254 if (dir.has_parent_path()) {
255 auto parent = dir.parent_path();
256 auto native = parent.native();
257 if (prefixes.contains(native)) {
258 return prefixes.at(native);
260 auto result = getDirectory(parent);
261 prefixes.insert({parent, result});
265 return Opt<DirectoryId>{};
272 .
parent = parentDirectory(dir), .name = dir.native()});
276 if (file.starts_with(
" ")) {
277 std::cerr << file << std::endl;
283 .
path = add(
String{file}), .dir = parentDirectory(file)});
285 assert(!at(at(result).path).text.starts_with(
" "));
291 template <dod::IsIdType Id>
292 auto at(Id
id) ->
typename dod::value_type_t<Id>& {
293 return multi.at<Id>(id);
296 template <dod::IsIdType Id>
297 [[nodiscard]]
auto cat(Id
id)
const -> CR<dod::value_type_t<Id>> {
298 return multi.at<Id>(id);
302 template <
typename T>
303 [[nodiscard]]
auto add(CR<T> it) -> dod::id_type_t<T> {
304 return multi.add<T>(it);
317 :
File{.
commit_id = CommitId::Nil(), .path = FilePathId::Nil()}
318 , id(FileId::Nil()) {}
327 :
Commit{.
author = AuthorId::Nil()}, id(CommitId::Nil()) {}
329 :
Commit(base), id(_id) {}
337 inline orm_dir(DirectoryId _id, CR<Directory> base)
347 :
String(base), id(_id) {}
357 :
Author(base), id(_id) {}
364 :
LineData{.
author = AuthorId::Nil(), .content = StringId::Nil(), .commit = CommitId::Nil()}
365 , id(LineId::Nil()) {}
390 inline void exec(sqlite3* db, Str query) {
391 int rc = sqlite3_exec(db, query.c_str(),
nullptr,
nullptr,
nullptr);
392 if (rc != SQLITE_OK) {
393 throw std::runtime_error(
394 "DB execution failure for query '" + query +
395 "': " + sqlite3_errmsg(db) +
" error: " + sqlite3_errstr(rc));
401 auto storage = make_storage(
403 make_table<orm_renamed_file>(
405 make_column(
"rcommit", &orm_renamed_file::commit),
406 make_column(
"old_path", &orm_renamed_file::old_path),
407 make_column(
"new_path", &orm_renamed_file::new_path)),
408 make_table<orm_file_path>(
410 make_column(
"id", &orm_file_path::id, primary_key()),
411 make_column(
"path", &orm_file_path::path),
412 make_column(
"dir", &orm_file_path::dir)),
413 make_table<orm_commit>(
415 make_column(
"id", &orm_commit::id, primary_key()),
416 make_column(
"author", &orm_commit::author),
417 make_column(
"time", &orm_commit::time),
418 make_column(
"hash", &orm_commit::hash),
419 make_column(
"period", &orm_commit::period),
420 make_column(
"timezone", &orm_commit::timezone),
421 make_column(
"message", &orm_commit::message)),
422 make_table<orm_file>(
424 make_column(
"id", &orm_file::id, primary_key()),
426 make_column(
"path", &orm_file::path)),
427 make_table<orm_author>(
429 make_column(
"id", &orm_author::id, primary_key()),
430 make_column(
"name", &orm_author::name),
431 make_column(
"email", &orm_author::email)),
432 make_table<orm_edited_files>(
434 make_column(
"rcommit", &orm_edited_files::commit),
435 make_column(
"added", &orm_edited_files::added),
436 make_column(
"removed", &orm_edited_files::removed),
437 make_column(
"path", &orm_edited_files::path)),
438 make_table<orm_line>(
440 make_column(
"id", &orm_line::id, primary_key()),
441 make_column(
"author", &orm_line::author),
442 make_column(
"time", &orm_line::time),
443 make_column(
"content", &orm_line::content),
444 make_column(
"rcommit", &orm_line::commit),
445 make_column(
"nesting", &orm_line::nesting)),
446 make_table<orm_lines_table>(
448 make_column(
"file", &orm_lines_table::file),
449 make_column(
"idx", &orm_lines_table::index),
450 make_column(
"line", &orm_lines_table::line)),
453 make_column(
"id", &orm_dir::id, primary_key()),
454 make_column(
"parent", &orm_dir::parent),
455 make_column(
"name", &orm_dir::name)),
456 make_table<orm_string>(
458 make_column(
"id", &orm_string::id, primary_key()),
459 make_column(
"text", &orm_string::text)));
461 storage.on_open = [](sqlite3* db) {
463 exec(db,
"DROP VIEW IF EXISTS file_version_with_path;--");
465 CREATE VIEW file_version_with_path AS SELECT file.id AS id,
467 paths.path AS path_id,
468 strings.text AS PATH,
472 ON file.path = paths.id
474 ON path_id = strings.id;--
476 exec(db, "DROP VIEW IF EXISTS file_version_with_path_dir;--");
478 CREATE VIEW file_version_with_path_dir AS SELECT fv.id,
479 fv.rcommit as rcommit,
481 fv.path_id as path_id,
483 FROM file_version_with_path AS fv
485 ON fv.dir = dir.id;--
488 exec(db, "DROP VIEW IF EXISTS file_path_with_dir;--");
490 CREATE VIEW file_path_with_dir AS SELECT paths.id AS path_id,
491 strings.text AS PATH,
495 ON paths.path = strings.id
497 ON paths.dir = dir.id;--
CommitId commit
Definition: git_ir.hpp:379
Definition: git_ir.hpp:382
orm_line(LineId _id, CR< LineData > base)
Definition: git_ir.hpp:367
Definition: git_ir.hpp:27
ORM wrapper for the author data.
Definition: git_ir.hpp:351
ORM wrapper for the string data.
Definition: git_ir.hpp:342
file path with associated parent directory information
Definition: git_ir.hpp:97
Definition: git_ir.hpp:113
Vec< RenamedFile > renamed_files
Definition: git_ir.hpp:129
auto operator==(CR< LineData > other) const -> bool
Line indentation depth.
Definition: git_ir.hpp:194
orm_file(FileId _id, CR< File > base)
Definition: git_ir.hpp:319
Opt< ir::DirectoryId > dir
Definition: git_ir.hpp:100
LineId id
Definition: git_ir.hpp:362
auto cat(Id id) const -> CR< dod::value_type_t< Id >>
Definition: git_ir.hpp:297
Definition: git_ir.hpp:378
orm_commit()
Definition: git_ir.hpp:326
ORM wrapper for the line data.
Definition: git_ir.hpp:361
ORM wrapper for the commit data.
Definition: git_ir.hpp:323
ORM wrapper for the directory data.
Definition: git_ir.hpp:333
FilePathId getFilePath(CR< Str > file)
Definition: git_ir.hpp:275
orm_string(StringId _id, CR< String > base)
Definition: git_ir.hpp:346
Str message
Number of the period that commit was attributed to.
Definition: git_ir.hpp:127
int nesting
Definition: git_ir.hpp:192
auto operator==(CR< Directory > other) const -> bool
Id of the string.
Definition: git_ir.hpp:151
orm_line()
Definition: git_ir.hpp:363
int added
Definition: git_ir.hpp:109
Definition: git_ir.hpp:68
int index
Definition: git_ir.hpp:374
CommitId commit
Content of the line.
Definition: git_ir.hpp:191
DECL_ID_TYPE(Author, AuthorId, int)
Definition: git_ir.hpp:80
Str hash
timezone where commit was taken
Definition: git_ir.hpp:125
auto operator()(T t) const -> std::string
Definition: git_ir.hpp:46
AuthorId id_type
Definition: git_ir.hpp:169
Str name
Parent directory ID.
Definition: git_ir.hpp:149
ir::StringId path
Definition: git_ir.hpp:99
orm_dir(DirectoryId _id, CR< Directory > base)
Definition: git_ir.hpp:337
CommitId commit_id
Definition: git_ir.hpp:137
CommitId commit
Definition: git_ir.hpp:383
Vec< LineId > lines
Definition: git_ir.hpp:140
decltype(create_db("")) DbConnection
Database connection type alias.
Definition: git_ir.hpp:505
StringId content
Time line was written.
Definition: git_ir.hpp:190
orm_author()
Definition: git_ir.hpp:355
ORM wrapper for the file data.
Definition: git_ir.hpp:314
StringId id
Definition: git_ir.hpp:343
auto at(Id id) -> typename dod::value_type_t< Id > &
Get reference to value pointed to by the ID.
Definition: git_ir.hpp:292
Str text
Definition: git_ir.hpp:160
FilePathId id_type
Definition: git_ir.hpp:98
i64 time
references unique author id
Definition: git_ir.hpp:123
Table of interned stirngs for different purposes.
Definition: git_ir.hpp:158
orm_string()
Definition: git_ir.hpp:345
FileId id_type
Definition: git_ir.hpp:136
ORM wrapper for the file lines data ir::File::lines.
Definition: git_ir.hpp:372
bool operator==(CR< FilePath > other) const
Definition: git_ir.hpp:102
single version of the file that appeared in some commit
Definition: git_ir.hpp:135
#define MAKE_HASHABLE(__type, __varname,...)
Declare boilerplate type hasing using list of fields.
Definition: git_ir.hpp:215
Vec< EditedFile > edited_files
Commit message.
Definition: git_ir.hpp:128
auto bind(sqlite3_stmt *stmt, int index, T value) -> int
Definition: git_ir.hpp:33
ir::StringId type
Definition: git_ir.hpp:85
auto parentDirectory(CR< Path > dir) -> Opt< DirectoryId >
Get optional parent directory Id from the path.
Definition: git_ir.hpp:253
i64 time
Line author ID.
Definition: git_ir.hpp:189
CommitId id_type
Definition: git_ir.hpp:121
auto getDirectory(CR< Path > dir) -> DirectoryId
Get directory ID from the provided path.
Definition: git_ir.hpp:270
CommitId id
Definition: git_ir.hpp:324
Opt< DirectoryId > parent
Definition: git_ir.hpp:148
orm_commit(CommitId _id, CR< Commit > base)
Definition: git_ir.hpp:328
FileId file
Definition: git_ir.hpp:373
auto add(CR< T > it) -> dod::id_type_t< T >
Push in a value, return newly generated ID.
Definition: git_ir.hpp:303
Definition: git_ir.hpp:107
DirectoryId id
Definition: git_ir.hpp:334
void exec(sqlite3 *db, Str query)
Definition: git_ir.hpp:390
Author - name and email found during the source code analysis.
Definition: git_ir.hpp:168
Str email
Definition: git_ir.hpp:171
Full directory path and it's parent ID.
Definition: git_ir.hpp:146
Str name
Definition: git_ir.hpp:170
orm_file()
Definition: git_ir.hpp:316
ir::FilePathId old_path
Definition: git_ir.hpp:114
DirectoryId id_type
Definition: git_ir.hpp:147
auto operator<<(std::ostream &stream, T id) -> std::ostream &
Definition: git_ir.hpp:18
Unique combination of author+time+content for some line in database.
Definition: git_ir.hpp:186
auto create_db(CR< Str > storagePath)
Instantiate database connection.
Definition: git_ir.hpp:400
AuthorId id
Definition: git_ir.hpp:352
orm_author(AuthorId _id, CR< Author > base)
Definition: git_ir.hpp:356
int timezone
posix time
Definition: git_ir.hpp:124
Main store for repository analysis.
Definition: git_ir.hpp:238
FileId id
Definition: git_ir.hpp:315
AuthorId author
Definition: git_ir.hpp:188
FilePathId id
Definition: git_ir.hpp:387
LineId line
Definition: git_ir.hpp:375
orm_dir()
Definition: git_ir.hpp:336
AuthorId author
Definition: git_ir.hpp:122
auto operator==(CR< String > other) const -> bool
Textual content of the line.
Definition: git_ir.hpp:161
auto operator==(CR< Author > other) const -> bool
Definition: git_ir.hpp:173
std::unordered_map< Str, DirectoryId > prefixes
Definition: git_ir.hpp:250
const git_oid * commit_id(const git_commit *commit)
Definition: gitwrap.hpp:3841
void hash_combine(std::size_t &seed)
Definition: git_ir.hpp:204
int removed
Definition: git_ir.hpp:110
single commit by author, taken at some point in time
Definition: git_ir.hpp:120
ir::FilePathId new_path
Definition: git_ir.hpp:115
dod::MultiStore< dod::InternStore< AuthorId, Author >, dod::InternStore< LineId, LineData >, dod::Store< FileId, File >, dod::InternStore< FilePathId, FilePath >, dod::Store< CommitId, Commit >, dod::InternStore< DirectoryId, Directory >, dod::InternStore< StringId, String > > multi
Definition: git_ir.hpp:248
LineId id_type
Definition: git_ir.hpp:187
Definition: git_ir.hpp:386
StringId id_type
Definition: git_ir.hpp:159
int period
git hash of the commit
Definition: git_ir.hpp:126
ir::FilePathId path
Definition: git_ir.hpp:108