1 | // $Id: File.cc 610 2008-04-20 17:47:59Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson |
---|
5 | |
---|
6 | This file is part of svndigest, http://trac.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 2 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 this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "File.h" |
---|
25 | |
---|
26 | #include "Alias.h" |
---|
27 | #include "Configuration.h" |
---|
28 | #include "Date.h" |
---|
29 | #include "GnuplotFE.h" |
---|
30 | #include "html_utility.h" |
---|
31 | #include "HtmlStream.h" |
---|
32 | #include "Stats.h" |
---|
33 | #include "SVNblame.h" |
---|
34 | #include "SVNlog.h" |
---|
35 | |
---|
36 | #include <algorithm> |
---|
37 | #include <cassert> |
---|
38 | #include <cstdio> |
---|
39 | #include <ctime> |
---|
40 | #include <fstream> |
---|
41 | #include <iostream> |
---|
42 | #include <map> |
---|
43 | #include <string> |
---|
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::map<int, std::set<Alias> > |
---|
61 | File::copyright_map(std::map<std::string, Alias>& alias) const |
---|
62 | { |
---|
63 | using namespace std; |
---|
64 | map<int, set<Alias> > year_authors; |
---|
65 | SVNlog log(path()); |
---|
66 | const Stats& stats = stats_["add"]; |
---|
67 | |
---|
68 | |
---|
69 | for (LogIterator i=log.begin(); i<log.end(); ++i){ |
---|
70 | if (i->revision() && |
---|
71 | (stats(LineTypeParser::code, i->author(), i->revision()) > |
---|
72 | stats(LineTypeParser::code, i->author(), i->revision()-1) || |
---|
73 | stats(LineTypeParser::comment, i->author(), i->revision()) > |
---|
74 | stats(LineTypeParser::comment, i->author(), i->revision()-1)) |
---|
75 | ) { |
---|
76 | |
---|
77 | time_t sec = str2time(i->date()); |
---|
78 | tm* timeinfo = gmtime(&sec); |
---|
79 | |
---|
80 | // find username in map of aliases |
---|
81 | std::map<string,Alias>::iterator name(alias.lower_bound(i->author())); |
---|
82 | |
---|
83 | // if alias exist insert alias |
---|
84 | if (name != alias.end() && name->first==i->author()) |
---|
85 | year_authors[timeinfo->tm_year].insert(name->second); |
---|
86 | else { |
---|
87 | // else insert user name |
---|
88 | Alias a(i->author(),alias.size()); |
---|
89 | year_authors[timeinfo->tm_year].insert(a); |
---|
90 | std::cerr << "svndigest: warning: no copyright alias found for `" |
---|
91 | << i->author() << "'\n"; |
---|
92 | // insert alias to avoid multiple warnings. |
---|
93 | alias.insert(name, std::make_pair(i->author(), a)); |
---|
94 | } |
---|
95 | } |
---|
96 | } |
---|
97 | return year_authors; |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | std::string |
---|
102 | File::copyright_block(const std::map<int, std::set<Alias> >& year_authors, |
---|
103 | const std::string& prefix) const |
---|
104 | { |
---|
105 | using namespace std; |
---|
106 | stringstream ss; |
---|
107 | for (map<int, set<Alias> >::const_iterator i(year_authors.begin()); |
---|
108 | i!=year_authors.end();) { |
---|
109 | ss << prefix << "Copyright (C) " |
---|
110 | << 1900+i->first; |
---|
111 | map<int, set<Alias> >::const_iterator j = i; |
---|
112 | assert(i!=year_authors.end()); |
---|
113 | while (++j!=year_authors.end() && |
---|
114 | i->second == j->second){ |
---|
115 | ss << ", " << 1900+(j->first); |
---|
116 | } |
---|
117 | // printing authors |
---|
118 | std::vector<Alias> vec_alias; |
---|
119 | back_insert_iterator<std::vector<Alias> > ii(vec_alias); |
---|
120 | std::copy(i->second.begin(), i->second.end(), ii); |
---|
121 | // sort with respect to id |
---|
122 | std::sort(vec_alias.begin(), vec_alias.end(), IdCompare()); |
---|
123 | for (std::vector<Alias>::iterator a=vec_alias.begin(); |
---|
124 | a!=vec_alias.end(); ++a){ |
---|
125 | if (a!=vec_alias.begin()) |
---|
126 | ss << ","; |
---|
127 | ss << " " << a->name(); |
---|
128 | } |
---|
129 | ss << "\n"; |
---|
130 | i = j; |
---|
131 | } |
---|
132 | return ss.str(); |
---|
133 | } |
---|
134 | |
---|
135 | bool File::detect_copyright(std::string& block, size_t& start_at_line, |
---|
136 | size_t& end_at_line, std::string& prefix) const |
---|
137 | { |
---|
138 | using namespace std; |
---|
139 | LineTypeParser parser(path()); |
---|
140 | std::ifstream is(path().c_str()); |
---|
141 | std::string line; |
---|
142 | while (std::getline(is, line)) |
---|
143 | parser.parse(line); |
---|
144 | if (!parser.copyright_found()) |
---|
145 | return false; |
---|
146 | block = parser.block(); |
---|
147 | start_at_line = parser.start_line(); |
---|
148 | end_at_line = parser.end_line(); |
---|
149 | prefix = parser.prefix(); |
---|
150 | return true; |
---|
151 | } |
---|
152 | |
---|
153 | |
---|
154 | std::string File::href(void) const |
---|
155 | { |
---|
156 | return name()+".html"; |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | SVNlog File::log_core(void) const |
---|
161 | { |
---|
162 | return SVNlog(path()); |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | std::string File::node_type(void) const |
---|
167 | { |
---|
168 | return std::string("file"); |
---|
169 | } |
---|
170 | |
---|
171 | |
---|
172 | std::string File::output_path(void) const |
---|
173 | { |
---|
174 | return output_dir()+name()+".html"; |
---|
175 | } |
---|
176 | |
---|
177 | |
---|
178 | const StatsCollection& File::parse(bool verbose, bool ignore) |
---|
179 | { |
---|
180 | if (verbose) |
---|
181 | std::cout << "Parsing " << path_ << std::endl; |
---|
182 | stats_.reset(); |
---|
183 | std::string cache_dir = directory_name(path()) + std::string(".svndigest/"); |
---|
184 | std::string cache_file = cache_dir + name()+std::string(".svndigest-cache"); |
---|
185 | if (node_exist(cache_file)){ |
---|
186 | std::ifstream is(cache_file.c_str()); |
---|
187 | if (stats_.load_cache(is)){ |
---|
188 | return stats_; |
---|
189 | } |
---|
190 | is.close(); |
---|
191 | } |
---|
192 | else |
---|
193 | stats_.parse(path_); |
---|
194 | if (!node_exist(cache_dir)) |
---|
195 | mkdir(cache_dir); |
---|
196 | std::ofstream os(cache_file.c_str()); |
---|
197 | stats_.print(os); |
---|
198 | os.close(); |
---|
199 | return stats_; |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | void File::print_blame(std::ofstream& os) const |
---|
204 | { |
---|
205 | os << "<br /><h3>Blame Information</h3>"; |
---|
206 | os << "<table class=\"blame\">\n"; |
---|
207 | os << "<thead>\n"; |
---|
208 | os << "<tr>\n"; |
---|
209 | os << "<th class=\"rev\">Rev</th>\n"; |
---|
210 | os << "<th class=\"date\">Date</th>\n"; |
---|
211 | os << "<th class=\"author\">Author</th>\n"; |
---|
212 | os << "<th class=\"line\">Line</th>\n"; |
---|
213 | os << "<th></th>\n"; |
---|
214 | os << "</tr>\n</thead>\n"; |
---|
215 | os << "<tbody>\n"; |
---|
216 | HtmlStream hs(os); |
---|
217 | SVNblame blame(path_); |
---|
218 | LineTypeParser parser(path_); |
---|
219 | while (blame.valid()) { |
---|
220 | parser.parse(blame.line()); |
---|
221 | blame.next_line(); |
---|
222 | } |
---|
223 | blame.reset(); |
---|
224 | |
---|
225 | std::vector<LineTypeParser::line_type>::const_iterator |
---|
226 | line_type(parser.type().begin()); |
---|
227 | int last=0; |
---|
228 | int first=0; |
---|
229 | bool using_dates=true; |
---|
230 | if (GnuplotFE::instance()->dates().empty()){ |
---|
231 | using_dates=false; |
---|
232 | last = stats_["classic"].revision(); |
---|
233 | } |
---|
234 | else { |
---|
235 | last = Date(GnuplotFE::instance()->dates().back()).seconds(); |
---|
236 | // earliest date corresponds either to revision 0 or revision 1 |
---|
237 | first = std::min(Date(GnuplotFE::instance()->dates()[0]).seconds(), |
---|
238 | Date(GnuplotFE::instance()->dates()[1]).seconds()); |
---|
239 | } |
---|
240 | // color is calculated linearly on time, c = kt + m |
---|
241 | // brightest color (for oldest rev in log) is set to 192. |
---|
242 | double k = 192.0/(first-last); |
---|
243 | double m = -last*k; |
---|
244 | while (blame.valid()) { |
---|
245 | std::string color; |
---|
246 | if (using_dates) |
---|
247 | color = hex(static_cast<int>(k*Date(blame.date()).seconds()+m),2); |
---|
248 | else |
---|
249 | color = hex(static_cast<int>(k*blame.revision()+m),2); |
---|
250 | os << "<tr>\n<td class=\"rev\">"; |
---|
251 | std::stringstream color_ss; |
---|
252 | color_ss << "#" << color << color << color; |
---|
253 | os << "<font color=\"" << color_ss.str() << "\">" |
---|
254 | << trac_revision(blame.revision(), color_ss.str()) |
---|
255 | << "</font></td>\n<td class=\"date\"><font color=\"#" << color |
---|
256 | << color << color << "\">" ; |
---|
257 | hs << Date(blame.date())("%d %b %y"); |
---|
258 | os << "</font></td>\n<td class=\"author\">"; |
---|
259 | hs << blame.author(); |
---|
260 | os << "</td>\n<td class=\""; |
---|
261 | assert(line_type!=parser.type().end()); |
---|
262 | if (*line_type==LineTypeParser::other) |
---|
263 | os << "line-other"; |
---|
264 | else if (*line_type==LineTypeParser::comment_or_copy) |
---|
265 | os << "line-comment"; |
---|
266 | else |
---|
267 | os << "line-code"; |
---|
268 | os << "\">" << blame.line_no()+1 |
---|
269 | << "</td>\n<td>"; |
---|
270 | hs << blame.line(); |
---|
271 | os << "</td>\n</tr>\n"; |
---|
272 | blame.next_line(); |
---|
273 | ++line_type; |
---|
274 | } |
---|
275 | os << "</tbody>\n"; |
---|
276 | os << "</table>\n"; |
---|
277 | } |
---|
278 | |
---|
279 | |
---|
280 | void File::print_copyright(std::map<std::string, Alias>& alias, |
---|
281 | bool verbose) const |
---|
282 | { |
---|
283 | if (ignore()) |
---|
284 | return; |
---|
285 | |
---|
286 | std::string old_block; |
---|
287 | size_t start_line=0; |
---|
288 | size_t end_line=0; |
---|
289 | std::string prefix; |
---|
290 | if (!detect_copyright(old_block, start_line, end_line, prefix)){ |
---|
291 | if (Configuration::instance().missing_copyright_warning()) |
---|
292 | std::cerr << "svndigest: warning: no copyright statement found in `" |
---|
293 | << path_ << "'\n"; |
---|
294 | return; |
---|
295 | } |
---|
296 | std::map<int, std::set<Alias> > map=copyright_map(alias); |
---|
297 | std::string new_block = copyright_block(map, prefix); |
---|
298 | if (old_block==new_block) |
---|
299 | return; |
---|
300 | if (verbose) |
---|
301 | std::cout << "Updating copyright in " << path_ << std::endl; |
---|
302 | update_copyright(new_block, start_line, end_line); |
---|
303 | } |
---|
304 | |
---|
305 | |
---|
306 | void File::print_core(const bool verbose) const |
---|
307 | { |
---|
308 | } |
---|
309 | |
---|
310 | |
---|
311 | void File::print_core(const std::string& stats_type, |
---|
312 | const std::string& user, const std::string& line_type, |
---|
313 | const SVNlog& log) const |
---|
314 | { |
---|
315 | std::string outpath = stats_type+"/"+user+"/"+line_type+"/"+local_path(); |
---|
316 | std::string imagefile = stats_type+"/"+"images/"+line_type+"/"+ |
---|
317 | local_path_+".png"; |
---|
318 | std::string html_name(outpath + ".html"); |
---|
319 | std::ofstream os(html_name.c_str()); |
---|
320 | print_header(os, name(), level_+3, user, line_type, local_path()+".html", |
---|
321 | stats_type); |
---|
322 | path_anchor(os); |
---|
323 | |
---|
324 | os << "<p class=\"plot\">\n<img src='"; |
---|
325 | for (size_t i=0; i<level_; ++i) |
---|
326 | os << "../"; |
---|
327 | os << "../../../"; |
---|
328 | if (user=="all") |
---|
329 | os << stats_[stats_type].plot(imagefile,line_type); |
---|
330 | else |
---|
331 | os << imagefile; |
---|
332 | os << "' alt='[plot]' />\n</p>"; |
---|
333 | |
---|
334 | print_author_summary(os, stats_[stats_type], line_type, log); |
---|
335 | os << "\n"; |
---|
336 | |
---|
337 | print_blame(os); |
---|
338 | |
---|
339 | print_footer(os); |
---|
340 | os.close(); |
---|
341 | } |
---|
342 | |
---|
343 | void File::update_copyright(const std::string& new_block, |
---|
344 | size_t start_at_line, size_t end_at_line) const |
---|
345 | { |
---|
346 | // Code copied from Gnuplot -r70 |
---|
347 | char tmpname[]="/tmp/svndigestXXXXXX"; |
---|
348 | int fd=mkstemp(tmpname); // mkstemp return a file descriptor |
---|
349 | if (fd == -1) |
---|
350 | throw std::runtime_error(std::string("Failed to get unique filename: ") + |
---|
351 | tmpname); |
---|
352 | // Jari would like to do something like 'std::ofstream tmp(fd);' |
---|
353 | // but has to settle for (which is stupid since the file is |
---|
354 | // already open for writing. |
---|
355 | std::ofstream tmp(tmpname); |
---|
356 | |
---|
357 | using namespace std; |
---|
358 | ifstream is(path().c_str()); |
---|
359 | assert(is.good()); |
---|
360 | string line; |
---|
361 | // Copy lines before block |
---|
362 | for (size_t i=1; i<start_at_line; ++i){ |
---|
363 | assert(is.good()); |
---|
364 | getline(is, line); |
---|
365 | tmp << line << "\n"; |
---|
366 | } |
---|
367 | // Printing copyright statement |
---|
368 | tmp << new_block; |
---|
369 | // Ignore old block lines |
---|
370 | for (size_t i=start_at_line; i<end_at_line; ++i){ |
---|
371 | assert(is.good()); |
---|
372 | getline(is, line); |
---|
373 | } |
---|
374 | // Copy lines after block |
---|
375 | while(is.good()) { |
---|
376 | char ch=is.get(); |
---|
377 | if (is.good()) |
---|
378 | tmp.put(ch); |
---|
379 | } |
---|
380 | |
---|
381 | is.close(); |
---|
382 | tmp.close(); |
---|
383 | close(fd); |
---|
384 | // get file permission |
---|
385 | struct stat nodestat; |
---|
386 | stat(path().c_str(),&nodestat); |
---|
387 | |
---|
388 | // finally copy temporary file to replace original file, and |
---|
389 | // remove the temporary file |
---|
390 | try { |
---|
391 | copy_file(tmpname, path()); |
---|
392 | } |
---|
393 | catch (std::runtime_error e) { |
---|
394 | // catch exception, cleanup, and rethrow |
---|
395 | std::cerr << "File::print_copyright: Exception caught, " |
---|
396 | << "removing temporary file " << tmpname << std::endl; |
---|
397 | if (unlink(tmpname)) |
---|
398 | throw runtime_error(std::string("File::print_copyright: ") + |
---|
399 | "failed to unlink temporary file" + tmpname); |
---|
400 | throw; |
---|
401 | } |
---|
402 | if (unlink(tmpname)) |
---|
403 | throw runtime_error(std::string("File::print_copyright: ") + |
---|
404 | "failed to unlink temporary file" + tmpname); |
---|
405 | |
---|
406 | chmod(path().c_str(), nodestat.st_mode); |
---|
407 | } |
---|
408 | |
---|
409 | |
---|
410 | }} // end of namespace svndigest and namespace theplu |
---|