/
commitlog.cpp
326 lines (232 loc) · 7.48 KB
/
commitlog.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
Copyright (C) 2009 Andrew Caudwell (acaudwell@gmail.com)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version
3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "commitlog.h"
std::string munge_utf8(const std::string& str) {
std::string munged;
try {
utf8::replace_invalid(str.begin(), str.end(), back_inserter(munged), '?');
}
catch(...) {
munged = "???";
}
return munged;
}
//RCommitLog
RCommitLog::RCommitLog(const std::string& logfile, int firstChar) {
logf = 0;
seekable = false;
success = false;
is_dir = false;
buffered = false;
if(logfile == "-") {
//check first char
if(checkFirstChar(firstChar, std::cin)) {
logf = new StreamLog();
is_dir = false;
seekable = false;
success = true;
}
return;
}
struct stat fileinfo;
int rc = stat(logfile.c_str(), &fileinfo);
if(rc==0) {
is_dir = (fileinfo.st_mode & S_IFDIR) ? true : false;
if(!is_dir) {
//check first char
std::ifstream testf(logfile.c_str());
bool firstOK = checkFirstChar(firstChar, testf);
testf.close();
if(firstOK) {
logf = new SeekLog(logfile);
seekable = true;
success = true;
}
}
}
}
RCommitLog::~RCommitLog() {
if(logf!=0) delete logf;
if(!temp_file.empty()) {
remove(temp_file.c_str());
}
}
//check firstChar of stream is as expected. if no firstChar defined just returns true.
bool RCommitLog::checkFirstChar(int firstChar, std::istream& stream) {
//cant check this
if(firstChar == -1) return true;
int c = stream.peek();
if(firstChar == c) return true;
return false;
}
bool RCommitLog::checkFormat() {
if(!success) return false;
//read a commit to see if the log is in the correct format
if(nextCommit(lastCommit)) {
if(seekable) {
//if the log is seekable, go back to the start
((SeekLog*)logf)->seekTo(0.0);
} else {
//otherwise set the buffered flag as we have bufferd one commit
buffered = true;
}
return true;
}
return false;
}
std::string RCommitLog::getLogCommand() {
return log_command;
}
bool RCommitLog::isSeekable() {
return seekable;
}
bool RCommitLog::getCommitAt(float percent, RCommit& commit) {
if(!seekable) return false;
SeekLog* seeklog = ((SeekLog*)logf);
//save settings
long currpointer = seeklog->getPointer();
std::string currlastline = lastline;
seekTo(percent);
bool success = findNextCommit(commit,500);
//restore settings
seeklog->setPointer(currpointer);
lastline = currlastline;
return success;
}
bool RCommitLog::getNextLine(std::string& line) {
if(!lastline.empty()) {
line = lastline;
lastline = std::string("");
return true;
}
return logf->getNextLine(line);
}
void RCommitLog::seekTo(float percent) {
if(!seekable) return;
lastline = "";
((SeekLog*)logf)->seekTo(percent);
}
float RCommitLog::getPercent() {
if(seekable) return ((SeekLog*)logf)->getPercent();
return 0.0;
}
bool RCommitLog::findNextCommit(RCommit& commit, int attempts) {
for(int i=0;i<attempts;i++) {
RCommit c;
if(nextCommit(c)) {
commit = c;
return true;
}
}
return false;
}
bool RCommitLog::nextCommit(RCommit& commit) {
if(buffered) {
commit = lastCommit;
buffered = false;
return true;
}
bool success = parseCommit(commit);
if(!success) return false;
return commit.isValid();
}
bool RCommitLog::isFinished() {
if(seekable && logf->isFinished()) return true;
return false;
}
//create temp file
void RCommitLog::createTempLog() {
std::string tempdir;
#ifdef _WIN32
DWORD tmplen = GetTempPath(0, "");
if(tmplen == 0) return;
std::vector<TCHAR> temp(tmplen+1);
tmplen = GetTempPath(static_cast<DWORD>(temp.size()), &temp[0]);
if(tmplen == 0 || tmplen >= temp.size()) return;
tempdir = std::string(temp.begin(), temp.begin() + static_cast<std::size_t>(tmplen));
tempdir += "\\";
#else
tempdir = "/tmp/";
#endif
char tmplate[1024];
snprintf(tmplate, 1024, "%sgource-XXXXXX", tempdir.c_str());
#ifdef _WIN32
if(mktemp(tmplate) < 0) return;
#else
if(mkstemp(tmplate) < 0) return;
#endif
temp_file = std::string(tmplate);
}
// RCommitFile
RCommitFile::RCommitFile(const std::string& filename, const std::string& action, const vec3f& colour)
: action(action), colour(colour) {
this->filename = munge_utf8(filename);
//prepend a root slash
if(this->filename[0] != '/') {
this->filename.insert(0, 1, '/');
}
}
RCommitFile::RCommitFile(const std::string& filename, const std::string& destination, const std::string& action, const vec3f& colour)
: action(action), colour(colour) {
this->filename = munge_utf8(filename);
this->destination = munge_utf8(destination);
//prepend a root slash
if(this->filename[0] != '/') {
this->filename.insert(0, 1, '/');
}
if(this->destination[0] != '/') {
this->destination.insert(0, 1, '/');
}
}
//RCommit
RCommit::RCommit() {
timestamp = 0;
}
vec3f RCommit::fileColour(const std::string& filename) {
size_t slash = filename.rfind('/');
size_t dot = filename.rfind('.');
if(dot != std::string::npos && dot+1<filename.size() && (slash == std::string::npos || slash < dot)) {
std::string file_ext = filename.substr(dot+1);
return colourHash(file_ext);
} else {
return vec3f(1.0, 1.0, 1.0);
}
}
void RCommit::addFile(const std::string& filename, const std::string& action) {
files.push_back(RCommitFile(filename, action, fileColour(filename)));
}
void RCommit::addFile(const std::string& filename, const std::string& action, const vec3f& colour) {
files.push_back(RCommitFile(filename, action, colour));
}
void RCommit::addFile(const std::string& filename, const std::string& destination, const std::string& action) {
files.push_back(RCommitFile(filename, destination, action, fileColour(destination)));
}
void RCommit::addFile(const std::string& filename, const std::string& destination, const std::string& action, const vec3f& colour) {
files.push_back(RCommitFile(filename, destination, action, colour));
}
bool RCommit::isValid() {
username = munge_utf8(username);
return true;
}
void RCommit::debug() {
debugLog("files:\n");
for(std::list<RCommitFile>::iterator it = files.begin(); it != files.end(); it++) {
RCommitFile f = *it;
if(f.action == "C" || f.action == "R") {
debugLog("%s %s => %s\n", f.action.c_str(), f.filename.c_str(), f.destination.c_str());
} else {
debugLog("%s %s\n", f.action.c_str(), f.filename.c_str());
}
}
}