1 | // $Id: File.cc 938 2009-12-03 04:02:44Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005, 2006, 2007, 2008, 2009 Jari Häkkinen, Peter Johansson |
---|
5 | |
---|
6 | This file is part of svndigest, http://dev.thep.lu.se/svndigest |
---|
7 | |
---|
8 | svndigest is free software; you can redistribute it and/or modify it |
---|
9 | under the terms of the GNU General Public License as published by |
---|
10 | the Free Software Foundation; either version 3 of the License, or |
---|
11 | (at your option) any later version. |
---|
12 | |
---|
13 | svndigest is distributed in the hope that it will be useful, but |
---|
14 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with svndigest. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | */ |
---|
21 | |
---|
22 | #include "File.h" |
---|
23 | |
---|
24 | #include "Alias.h" |
---|
25 | #include "Configuration.h" |
---|
26 | #include "Date.h" |
---|
27 | #include "Graph.h" |
---|
28 | #include "html_utility.h" |
---|
29 | #include "HtmlStream.h" |
---|
30 | #include "Stats.h" |
---|
31 | #include "SVNblame.h" |
---|
32 | #include "SVNlog.h" |
---|
33 | |
---|
34 | #include <algorithm> |
---|
35 | #include <cassert> |
---|
36 | #include <cstdio> |
---|
37 | #include <ctime> |
---|
38 | #include <fstream> |
---|
39 | #include <iostream> |
---|
40 | #include <map> |
---|
41 | #include <stdexcept> |
---|
42 | #include <string> |
---|
43 | #include <sstream> |
---|
44 | #include <sys/stat.h> |
---|
45 | |
---|
46 | namespace theplu{ |
---|
47 | namespace svndigest{ |
---|
48 | |
---|
49 | |
---|
50 | File::File(const unsigned int level, const std::string& path, |
---|
51 | const std::string& output) |
---|
52 | : Node(level,path,output) |
---|
53 | { |
---|
54 | output_dir_=output; |
---|
55 | if (!output_dir_.empty()) |
---|
56 | output_dir_+='/'; |
---|
57 | } |
---|
58 | |
---|
59 | |
---|
60 | std::string File::blame_output_file_name(void) const |
---|
61 | { |
---|
62 | return "blame_output/" + local_path() + ".html"; |
---|
63 | } |
---|
64 | |
---|
65 | |
---|
66 | std::map<int, std::set<Alias> > |
---|
67 | File::copyright_map(std::map<std::string, Alias>& alias, |
---|
68 | const std::map<int, svn_revnum_t>& year2rev) const |
---|
69 | { |
---|
70 | using namespace std; |
---|
71 | map<int, set<Alias> > year_authors; |
---|
72 | const Stats& stats = stats_["add"]; |
---|
73 | |
---|
74 | // loop over all years |
---|
75 | for (std::map<int, svn_revnum_t>::const_iterator rev_iter=year2rev.begin(); |
---|
76 | rev_iter!=year2rev.end(); ++rev_iter) { |
---|
77 | |
---|
78 | svn_revnum_t last_rev_this_year = rev_iter->second; |
---|
79 | svn_revnum_t last_rev_last_year = 0; |
---|
80 | if (rev_iter != year2rev.begin()) { |
---|
81 | last_rev_last_year = (--rev_iter)->second; |
---|
82 | ++rev_iter; |
---|
83 | } |
---|
84 | // do not go beyond BASE rev of file |
---|
85 | last_rev_this_year = std::min(last_rev_this_year, last_changed_rev()); |
---|
86 | last_rev_last_year = std::min(last_rev_last_year, last_changed_rev()); |
---|
87 | // loop over authors |
---|
88 | for (std::set<std::string>::const_iterator a_iter=stats.authors().begin(); |
---|
89 | a_iter!=stats.authors().end(); ++a_iter) { |
---|
90 | |
---|
91 | // check if anything has been added since last year |
---|
92 | if ( (stats(LineTypeParser::code, *a_iter, last_rev_this_year) > |
---|
93 | stats(LineTypeParser::code, *a_iter, last_rev_last_year)) || |
---|
94 | (stats(LineTypeParser::comment, *a_iter, last_rev_this_year) > |
---|
95 | stats(LineTypeParser::comment, *a_iter, last_rev_last_year)) ) { |
---|
96 | |
---|
97 | |
---|
98 | // find username in map of aliases |
---|
99 | std::map<string,Alias>::iterator name(alias.lower_bound(*a_iter)); |
---|
100 | |
---|
101 | // if alias exist insert alias |
---|
102 | if (name != alias.end() && name->first==*a_iter) |
---|
103 | year_authors[rev_iter->first].insert(name->second); |
---|
104 | else { |
---|
105 | // else insert user name |
---|
106 | Alias a(*a_iter,alias.size()); |
---|
107 | year_authors[rev_iter->first].insert(a); |
---|
108 | std::cerr << "svndigest: warning: no copyright alias found for `" |
---|
109 | << *a_iter << "'\n"; |
---|
110 | // insert alias to avoid multiple warnings. |
---|
111 | alias.insert(name, std::make_pair(*a_iter, a)); |
---|
112 | } |
---|
113 | } |
---|
114 | } |
---|
115 | } |
---|
116 | return year_authors; |
---|
117 | } |
---|
118 | |
---|
119 | |
---|
120 | std::string |
---|
121 | File::copyright_block(const std::map<int, std::set<Alias> >& year_authors, |
---|
122 | const std::string& prefix) const |
---|
123 | { |
---|
124 | using namespace std; |
---|
125 | stringstream ss; |
---|
126 | for (map<int, set<Alias> >::const_iterator i(year_authors.begin()); |
---|
127 | i!=year_authors.end();) { |
---|
128 | ss << prefix << "Copyright (C) " |
---|
129 | << 1900+i->first; |
---|
130 | map<int, set<Alias> >::const_iterator j = i; |
---|
131 | assert(i!=year_authors.end()); |
---|
132 | while (++j!=year_authors.end() && |
---|
133 | i->second == j->second){ |
---|
134 | ss << ", " << 1900+(j->first); |
---|
135 | } |
---|
136 | // printing authors |
---|
137 | std::vector<Alias> vec_alias; |
---|
138 | back_insert_iterator<std::vector<Alias> > ii(vec_alias); |
---|
139 | std::copy(i->second.begin(), i->second.end(), ii); |
---|
140 | // sort with respect to id |
---|
141 | std::sort(vec_alias.begin(), vec_alias.end(), IdCompare()); |
---|
142 | for (std::vector<Alias>::iterator a=vec_alias.begin(); |
---|
143 | a!=vec_alias.end(); ++a){ |
---|
144 | if (a!=vec_alias.begin()) |
---|
145 | ss << ","; |
---|
146 | ss << " " << a->name(); |
---|
147 | } |
---|
148 | ss << "\n"; |
---|
149 | i = j; |
---|
150 | } |
---|
151 | return ss.str(); |
---|
152 | } |
---|
153 | |
---|
154 | bool File::detect_copyright(std::string& block, size_t& start_at_line, |
---|
155 | size_t& end_at_line, std::string& prefix) const |
---|
156 | { |
---|
157 | using namespace std; |
---|
158 | LineTypeParser parser(path()); |
---|
159 | std::ifstream is(path().c_str()); |
---|
160 | std::string line; |
---|
161 | while (std::getline(is, line)) |
---|
162 | parser.parse(line); |
---|
163 | if (!parser.copyright_found()) |
---|
164 | return false; |
---|
165 | block = parser.block(); |
---|
166 | start_at_line = parser.start_line(); |
---|
167 | end_at_line = parser.end_line(); |
---|
168 | prefix = parser.prefix(); |
---|
169 | return true; |
---|
170 | } |
---|
171 | |
---|
172 | |
---|
173 | std::string File::href(void) const |
---|
174 | { |
---|
175 | return name()+".html"; |
---|
176 | } |
---|
177 | |
---|
178 | |
---|
179 | svn_revnum_t File::last_changed_rev(void) const |
---|
180 | { |
---|
181 | return svn_info().last_changed_rev(); |
---|
182 | } |
---|
183 | |
---|
184 | |
---|
185 | void File::log_core(SVNlog&) const |
---|
186 | { |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | std::string File::node_type(void) const |
---|
191 | { |
---|
192 | return std::string("file"); |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | std::string File::output_path(void) const |
---|
197 | { |
---|
198 | return output_dir()+name()+".html"; |
---|
199 | } |
---|
200 | |
---|
201 | |
---|
202 | const StatsCollection& File::parse(bool verbose, bool ignore) |
---|
203 | { |
---|
204 | if (verbose) |
---|
205 | std::cout << "Parsing " << path_ << std::endl; |
---|
206 | stats_.reset(); |
---|
207 | std::string cache_dir = directory_name(path()) + std::string(".svndigest/"); |
---|
208 | std::string cache_file = cache_dir + name()+std::string(".svndigest-cache"); |
---|
209 | if (!ignore && node_exist(cache_file)){ |
---|
210 | std::ifstream is(cache_file.c_str()); |
---|
211 | if (stats_.load_cache(is)) { |
---|
212 | is.close(); |
---|
213 | return stats_; |
---|
214 | } |
---|
215 | is.close(); |
---|
216 | } |
---|
217 | else |
---|
218 | stats_.parse(path_); |
---|
219 | if (!node_exist(cache_dir)) |
---|
220 | mkdir(cache_dir); |
---|
221 | std::string tmp_cache_file(cache_file+"~"); |
---|
222 | std::ofstream os(tmp_cache_file.c_str()); |
---|
223 | stats_.print(os); |
---|
224 | os.close(); |
---|
225 | rename(tmp_cache_file.c_str(), cache_file.c_str()); |
---|
226 | return stats_; |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | void File::print_blame(std::ofstream& os) const |
---|
231 | { |
---|
232 | os << "<br /><h3>" << local_path() << "</h3>"; |
---|
233 | os << "<div class=\"blame_legend\">\n"; |
---|
234 | os << "<dl>\n"; |
---|
235 | os << "<dt class=\"code\"></dt><dd>Code</dd>\n"; |
---|
236 | os << "<dt class=\"comment\"></dt><dd>Comments</dd>\n"; |
---|
237 | os << "<dt class=\"other\"></dt><dd>Other</dd>\n"; |
---|
238 | os << "</dl>\n</div>\n"; |
---|
239 | os << "<table class=\"blame\">\n"; |
---|
240 | os << "<thead>\n"; |
---|
241 | os << "<tr>\n"; |
---|
242 | os << "<th class=\"rev\">Rev</th>\n"; |
---|
243 | os << "<th class=\"date\">Date</th>\n"; |
---|
244 | os << "<th class=\"author\">Author</th>\n"; |
---|
245 | os << "<th class=\"line\">Line</th>\n"; |
---|
246 | os << "<th></th>\n"; |
---|
247 | os << "</tr>\n</thead>\n"; |
---|
248 | os << "<tbody>\n"; |
---|
249 | HtmlStream hs(os); |
---|
250 | SVNblame blame(path_); |
---|
251 | LineTypeParser parser(path_); |
---|
252 | while (blame.valid()) { |
---|
253 | parser.parse(blame.line()); |
---|
254 | blame.next_line(); |
---|
255 | } |
---|
256 | blame.reset(); |
---|
257 | |
---|
258 | std::vector<LineTypeParser::line_type>::const_iterator |
---|
259 | line_type(parser.type().begin()); |
---|
260 | int last=0; |
---|
261 | int first=0; |
---|
262 | bool using_dates=true; |
---|
263 | if (!Graph::date_xticks()) { |
---|
264 | using_dates=false; |
---|
265 | last = stats_["classic"].revision(); |
---|
266 | } |
---|
267 | else { |
---|
268 | last = Date(Graph::xticks().back()).seconds(); |
---|
269 | // earliest date corresponds either to revision 0 or revision 1 |
---|
270 | first = std::min(Date(Graph::xticks()[0]).seconds(), |
---|
271 | Date(Graph::xticks()[1]).seconds()); |
---|
272 | } |
---|
273 | // color is calculated linearly on time, c = kt + m |
---|
274 | // brightest color (for oldest rev in log) is set to 192. |
---|
275 | double k = 192.0/(first-last); |
---|
276 | double m = -last*k; |
---|
277 | while (blame.valid()) { |
---|
278 | std::string color; |
---|
279 | if (using_dates) |
---|
280 | color = hex(static_cast<int>(k*Date(blame.date()).seconds()+m),2); |
---|
281 | else |
---|
282 | color = hex(static_cast<int>(k*blame.revision()+m),2); |
---|
283 | os << "<tr>\n<td class=\"rev\">"; |
---|
284 | std::stringstream color_ss; |
---|
285 | color_ss << "#" << color << color << color; |
---|
286 | os << "<font color=\"" << color_ss.str() << "\">" |
---|
287 | << trac_revision(blame.revision(), color_ss.str()) |
---|
288 | << "</font></td>\n<td class=\"date\"><font color=\"#" << color |
---|
289 | << color << color << "\">" ; |
---|
290 | hs << Date(blame.date())("%d %b %y"); |
---|
291 | os << "</font></td>\n<td class=\"author\">"; |
---|
292 | std::string author_color = |
---|
293 | Configuration::instance().author_str_color(blame.author()); |
---|
294 | if (!author_color.empty()) |
---|
295 | os << "<font color=\"#" << author_color << "\">"; |
---|
296 | hs << blame.author(); |
---|
297 | os << "</td>\n<td class=\""; |
---|
298 | assert(line_type!=parser.type().end()); |
---|
299 | if (*line_type==LineTypeParser::other) |
---|
300 | os << "line-other"; |
---|
301 | else if (*line_type==LineTypeParser::comment || |
---|
302 | *line_type==LineTypeParser::copyright) |
---|
303 | os << "line-comment"; |
---|
304 | else if (*line_type==LineTypeParser::code) |
---|
305 | os << "line-code"; |
---|
306 | else { |
---|
307 | std::string msg="File::print_blame(): unexpected line type found"; |
---|
308 | throw std::runtime_error(msg); |
---|
309 | } |
---|
310 | os << "\">" << blame.line_no()+1 |
---|
311 | << "</td>\n<td>"; |
---|
312 | hs << blame.line(); |
---|
313 | os << "</td>\n</tr>\n"; |
---|
314 | blame.next_line(); |
---|
315 | ++line_type; |
---|
316 | } |
---|
317 | os << "</tbody>\n"; |
---|
318 | os << "</table>\n"; |
---|
319 | } |
---|
320 | |
---|
321 | |
---|
322 | void File::print_copyright(std::map<std::string, Alias>& alias, |
---|
323 | bool verbose, |
---|
324 | const std::map<int,svn_revnum_t>& y2rev) const |
---|
325 | { |
---|
326 | if (ignore()) |
---|
327 | return; |
---|
328 | |
---|
329 | std::string old_block; |
---|
330 | size_t start_line=0; |
---|
331 | size_t end_line=0; |
---|
332 | std::string prefix; |
---|
333 | if (!detect_copyright(old_block, start_line, end_line, prefix)){ |
---|
334 | if (Configuration::instance().missing_copyright_warning()) |
---|
335 | std::cerr << "svndigest: warning: no copyright statement found in `" |
---|
336 | << path_ << "'\n"; |
---|
337 | return; |
---|
338 | } |
---|
339 | std::map<int, std::set<Alias> > map=copyright_map(alias, y2rev); |
---|
340 | std::string new_block = copyright_block(map, prefix); |
---|
341 | if (old_block==new_block) |
---|
342 | return; |
---|
343 | if (verbose) |
---|
344 | std::cout << "Updating copyright in " << path_ << std::endl; |
---|
345 | update_copyright(new_block, start_line, end_line); |
---|
346 | } |
---|
347 | |
---|
348 | |
---|
349 | void File::print_core(const bool verbose) const |
---|
350 | { |
---|
351 | std::ofstream os(blame_output_file_name().c_str()); |
---|
352 | assert(os.good()); |
---|
353 | print_html_start(os, "svndigest", level_+1); |
---|
354 | print_blame(os); |
---|
355 | print_footer(os); |
---|
356 | os.close(); |
---|
357 | } |
---|
358 | |
---|
359 | |
---|
360 | void File::print_core(const std::string& stats_type, |
---|
361 | const std::string& user, const std::string& line_type, |
---|
362 | const SVNlog& log) const |
---|
363 | { |
---|
364 | std::string lpath = local_path(); |
---|
365 | if (lpath.empty()) |
---|
366 | lpath = "index"; |
---|
367 | std::string outpath = stats_type+"/"+user+"/"+line_type+"/"+lpath; |
---|
368 | std::string imagefile = stats_type+"/"+"images/"+line_type+"/"+ |
---|
369 | lpath+".svg"; |
---|
370 | std::string html_name(outpath + ".html"); |
---|
371 | std::ofstream os(html_name.c_str()); |
---|
372 | print_header(os, name(), level_+3, user, line_type, lpath+".html", |
---|
373 | stats_type); |
---|
374 | path_anchor(os); |
---|
375 | |
---|
376 | std::stringstream ss; |
---|
377 | for (size_t i=0; i<level_; ++i) |
---|
378 | ss << "../"; |
---|
379 | ss << "../../../"; |
---|
380 | if (user=="all") |
---|
381 | ss << stats_[stats_type].plot(imagefile,line_type); |
---|
382 | else |
---|
383 | ss << imagefile; |
---|
384 | os << "<p class=\"plot\">\n"; |
---|
385 | os << "<object data='" << ss.str() << "' type='image/svg+xml' width='600'>\n" |
---|
386 | << "<embed src='" << ss.str() << "' type='image/svg+xml' width='600' />\n" |
---|
387 | << "</object>\n"; |
---|
388 | os << "</p>\n"; |
---|
389 | |
---|
390 | print_author_summary(os, stats_[stats_type], line_type, log); |
---|
391 | os << "\n"; |
---|
392 | os << "<h3>" |
---|
393 | << anchor(blame_output_file_name(), |
---|
394 | "Blame Information", level_+3) |
---|
395 | << "</h3>\n"; |
---|
396 | |
---|
397 | print_footer(os); |
---|
398 | os.close(); |
---|
399 | } |
---|
400 | |
---|
401 | void File::update_copyright(const std::string& new_block, |
---|
402 | size_t start_at_line, size_t end_at_line) const |
---|
403 | { |
---|
404 | // Code copied from Gnuplot -r70 |
---|
405 | char tmpname[]="/tmp/svndigestXXXXXX"; |
---|
406 | int fd=mkstemp(tmpname); // mkstemp return a file descriptor |
---|
407 | if (fd == -1) |
---|
408 | throw std::runtime_error(std::string("Failed to get unique filename: ") + |
---|
409 | tmpname); |
---|
410 | // Jari would like to do something like 'std::ofstream tmp(fd);' |
---|
411 | // but has to settle for (which is stupid since the file is |
---|
412 | // already open for writing. |
---|
413 | std::ofstream tmp(tmpname); |
---|
414 | |
---|
415 | using namespace std; |
---|
416 | ifstream is(path().c_str()); |
---|
417 | assert(is.good()); |
---|
418 | string line; |
---|
419 | // Copy lines before block |
---|
420 | for (size_t i=1; i<start_at_line; ++i){ |
---|
421 | assert(is.good()); |
---|
422 | getline(is, line); |
---|
423 | tmp << line << "\n"; |
---|
424 | } |
---|
425 | // Printing copyright statement |
---|
426 | tmp << new_block; |
---|
427 | // Ignore old block lines |
---|
428 | for (size_t i=start_at_line; i<end_at_line; ++i){ |
---|
429 | assert(is.good()); |
---|
430 | getline(is, line); |
---|
431 | } |
---|
432 | // Copy lines after block |
---|
433 | while(is.good()) { |
---|
434 | char ch=is.get(); |
---|
435 | if (is.good()) |
---|
436 | tmp.put(ch); |
---|
437 | } |
---|
438 | |
---|
439 | is.close(); |
---|
440 | tmp.close(); |
---|
441 | close(fd); |
---|
442 | // get file permission |
---|
443 | struct stat nodestat; |
---|
444 | stat(path().c_str(),&nodestat); |
---|
445 | |
---|
446 | // finally copy temporary file to replace original file, and |
---|
447 | // remove the temporary file |
---|
448 | try { |
---|
449 | copy_file(tmpname, path()); |
---|
450 | } |
---|
451 | catch (std::runtime_error e) { |
---|
452 | // catch exception, cleanup, and rethrow |
---|
453 | std::cerr << "svndigest: File::print_copyright: Exception caught, " |
---|
454 | << "removing temporary file " << tmpname << std::endl; |
---|
455 | if (unlink(tmpname)) |
---|
456 | throw runtime_error(std::string("File::print_copyright: ") + |
---|
457 | "failed to unlink temporary file" + tmpname); |
---|
458 | throw; |
---|
459 | } |
---|
460 | if (unlink(tmpname)) |
---|
461 | throw runtime_error(std::string("File::print_copyright: ") + |
---|
462 | "failed to unlink temporary file" + tmpname); |
---|
463 | |
---|
464 | chmod(path().c_str(), nodestat.st_mode); |
---|
465 | } |
---|
466 | |
---|
467 | |
---|
468 | }} // end of namespace svndigest and namespace theplu |
---|