28 int diff_output(
const git_diff_delta* d,
const git_diff_hunk* h,
29 const git_diff_line* l,
void* p);
33 void GitTracker::SaveGitDetails() {
35 bdm_installation_ = GetAbsolutePath(bdm_installation_);
37 simulation_output_ = GetAbsolutePath(simulation_output_);
40 std::string filename = simulation_output_ +
"/bdm_git_info.txt";
41 SaveGitInfo(filename, bdm_installation_);
44 filename = simulation_output_ +
"/bdm_sim_git_info.txt";
45 SaveGitInfo(filename, cwd_);
48 filename = simulation_output_ +
"/bdm_git_diff.patch";
49 SaveGitDiff(filename, bdm_installation_);
52 filename = simulation_output_ +
"/bdm_sim_git_diff.patch";
53 SaveGitDiff(filename, cwd_);
56 void GitTracker::SaveGitInfo(
const std::string& file,
57 const std::string& repository_path)
const {
58 std::ofstream out(file);
59 PrintGitInfo(repository_path, out);
62 void GitTracker::PrintGitInfo(
const std::string& repository_path,
63 std::ostream& out)
const {
65 std::string abs_repo_path = GetAbsolutePath(repository_path);
69 git_repository* repo =
nullptr;
70 git_repository_open(&repo, abs_repo_path.c_str());
71 if (repo ==
nullptr) {
72 Log::Error(
"GitTracker",
"Error: ", abs_repo_path,
73 " is not a valid git repository");
78 const char* repo_name = git_repository_path(repo);
81 git_reference* head =
nullptr;
82 git_reference_lookup(&head, repo,
"HEAD");
83 git_object* head_commit =
nullptr;
84 git_reference_peel(&head_commit, head, GIT_OBJ_COMMIT);
85 const git_oid* oid = git_commit_id((git_commit*)head_commit);
86 char oid_str[GIT_OID_HEXSZ + 1];
87 git_oid_tostr(oid_str, GIT_OID_HEXSZ + 1, oid);
90 const char* commit_message = git_commit_message((git_commit*)head_commit);
93 const git_signature* commit_author =
94 git_commit_author((git_commit*)head_commit);
97 const char* author_name = commit_author->name;
100 const char* author_email = commit_author->email;
103 const char* branch_name = git_reference_shorthand(head);
106 git_reference* target =
nullptr;
107 git_reference_resolve(&target, head);
108 const char* target_name = git_reference_name(target);
111 out <<
"Git repository : " << repo_name <<
"\n"
112 <<
"Git branch name : " << target_name <<
"\n"
113 <<
" : " << branch_name <<
"\n"
114 <<
"Git commit hash : " << oid_str <<
"\n"
115 <<
"Git author email : " << author_email <<
"\n"
116 <<
"Git author name : " << author_name <<
"\n"
117 <<
"Git commit message : " << commit_message <<
"\n"
121 git_object_free(head_commit);
122 git_reference_free(head);
123 git_reference_free(target);
124 git_repository_free(repo);
125 git_libgit2_shutdown();
128 void GitTracker::SaveGitDiff(
const std::string& file,
129 const std::string& repository_path)
const {
131 std::string abs_repo_path = GetAbsolutePath(repository_path);
135 git_repository* repo =
nullptr;
136 git_repository_open(&repo, abs_repo_path.c_str());
137 if (repo ==
nullptr) {
138 Log::Error(
"GitTracker",
"Error: ", abs_repo_path,
139 " is not a valid git repository");
144 git_diff* diff =
nullptr;
145 git_diff_index_to_workdir(&diff, repo,
nullptr,
nullptr);
148 FILE* fp = fopen(file.c_str(),
"w");
152 git_diff_print(diff, GIT_DIFF_FORMAT_PATCH, diff_output, fp);
161 git_repository_free(repo);
162 git_libgit2_shutdown();
165 std::string GitTracker::GetAbsolutePath(
const std::string& path)
const {
167 return std::filesystem::absolute(path).string();
170 void GitTracker::ConstructFolderNames() {
172 cwd_ = std::filesystem::current_path();
175 if (cwd_.substr(cwd_.size() - 5, 5) ==
"build") {
176 cwd_ = cwd_.substr(0, cwd_.size() - 6);
179 bdm_installation_ = std::getenv(
"BDMSYS");
181 bdm_installation_ = GetAbsolutePath(bdm_installation_);
184 std::filesystem::path(bdm_installation_).parent_path().string();
190 int diff_output(
const git_diff_delta* d,
const git_diff_hunk* h,
191 const git_diff_line* l,
void* p) {
200 if (l->origin == GIT_DIFF_LINE_CONTEXT ||
201 l->origin == GIT_DIFF_LINE_ADDITION ||
202 l->origin == GIT_DIFF_LINE_DELETION)
203 fputc(l->origin, fp);
205 fwrite(l->content, 1, l->content_len, fp);
210 #endif // USE_LIBGIT2