-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitwrapper.cpp
More file actions
158 lines (133 loc) · 3.67 KB
/
Copy pathgitwrapper.cpp
File metadata and controls
158 lines (133 loc) · 3.67 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
#include <iostream>
#include <fstream>
#include <ctime>
#include <chrono>
#include <unistd.h>
#include <vector>
#include <cstring>
#include <regex>
#include <sys/wait.h>
#include <filesystem>
#include <iomanip>
std::string getLastLine(std::string logFileName) {
std::fstream logFile(logFileName, std::ios_base::in);
std::string lastLine = "";
if (!logFile.is_open()) {
perror("Failed to open log");
return "";
}
logFile.seekg(0, std::ios_base::end);
auto pos = logFile.tellg();
if (pos == 0) {
return "";
}
pos -= 2;
while(pos >= 0) {
logFile.seekg(pos);
char c;
logFile.get(c);
if (c == '\n') {
pos += 1;
logFile.seekg(pos);
break;
}
pos -= 1;
}
std::getline(logFile, lastLine);
return lastLine;
}
std::string getCurrentDate() {
std::time_t now = std::time(NULL);
std::tm localTime;
localtime_r(&now, &localTime);
std::ostringstream oss;
oss << std::setw(2) << std::setfill('0') << localTime.tm_mday << '-'
<< std::setw(2) << std::setfill('0') << (localTime.tm_mon + 1) << '-'
<< std::setw(4) << std::setfill('0') << (localTime.tm_year + 1900);
return oss.str();
}
std::string getCurrentTime() {
std::time_t now = std::time(NULL);
std::tm localTime;
localtime_r(&now, &localTime);
std::ostringstream oss;
oss << std::setw(2) << std::setfill('0') << localTime.tm_hour << ':'
<< std::setw(2) << std::setfill('0') << localTime.tm_min << ':'
<< std::setw(2) << std::setfill('0') << localTime.tm_sec;
return oss.str();
}
int logCommit(std::string msg) {
auto execPath = std::filesystem::canonical("/proc/self/exe").parent_path();
std::string logFileName = execPath.string() + "/log.txt";
std::fstream logFile(logFileName, std::ios_base::app);
if (!logFile.is_open()) {
perror("Failed to open log");
return 1;
}
std::string curDate = getCurrentDate();
std::string curTime = getCurrentTime();
logFile << '[' << curDate << "] [" << curTime << "] '" << msg << "'\n";
logFile.close();
return 0;
}
std::string getFileContent(const std::string& path) {
std::ifstream file(path);
if (!file.is_open()) {
perror("File failed to open");
return "-1";
}
std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
return content;
}
int debugLogGitCmd(std::string msg) {
std::string commitMsg = getFileContent(msg);
auto execPath = std::filesystem::canonical("/proc/self/exe").parent_path();
std::string logFileName = execPath.string() + "/debugLog.txt";
std::fstream logFile(logFileName, std::ios_base::app);
if (!logFile.is_open()) {
perror("Failed to open debug log");
return 1;
}
logFile << "[" << commitMsg << "]\n";
logFile.close();
return 0;
}
int main(int argc, char* argv[]) {
std::vector<char*> execArgs;
execArgs.push_back((char*)"/usr/bin/git");
for (int i = 1; i < argc; ++i) {
execArgs.push_back(argv[i]);
}
execArgs.push_back(nullptr);
pid_t pid = fork();
if (pid == 0) {
execvp(execArgs[0], execArgs.data());
perror("Error executing git");
_exit(127);
}
else if (pid > 0) {
if (argc > 2) {
if (strcmp(execArgs[1], "commit") == 0 && strcmp(execArgs[2], "-m") == 0) {
logCommit(execArgs[3]);
} //CLI git integration
else if (argc > 6) {
if (strcmp(execArgs[5], "commit") == 0 && strcmp(execArgs[6], "-F") == 0) {
logCommit(getFileContent(execArgs[7]));
} //Jetbrain git integration
}
}
int status;
waitpid(pid, &status, 0);
if (WIFSIGNALED(status)) {
std::cerr << "Git was terminated by signal " << WTERMSIG(status) << "\n";
}
else if (WIFEXITED(status)) {
return WEXITSTATUS(status);
}
}
else {
perror("Fork failed");
return 1;
}
return 0;
}