1 | // $Id: File.cc 1239 2010-10-23 23:57:56Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005, 2006, 2007, 2008, 2009 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2010 Peter Johansson |
---|
6 | |
---|
7 | This file is part of svndigest, http://dev.thep.lu.se/svndigest |
---|
8 | |
---|
9 | svndigest is free software; you can redistribute it and/or modify it |
---|
10 | under the terms of the GNU General Public License as published by |
---|
11 | the Free Software Foundation; either version 3 of the License, or |
---|
12 | (at your option) any later version. |
---|
13 | |
---|
14 | svndigest is distributed in the hope that it will be useful, but |
---|
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | General Public License for more details. |
---|
18 | |
---|
19 | You should have received a copy of the GNU General Public License |
---|
20 | along with svndigest. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | */ |
---|
22 | |
---|
23 | #include "File.h" |
---|
24 | |
---|
25 | #include "Alias.h" |
---|
26 | #include "Colors.h" |
---|
27 | #include "Configuration.h" |
---|
28 | #include "Date.h" |
---|
29 | #include "Graph.h" |
---|
30 | #include "html_utility.h" |
---|
31 | #include "HtmlStream.h" |
---|
32 | #include "NodeVisitor.h" |
---|
33 | #include "Stats.h" |
---|
34 | #include "SVNblame.h" |
---|
35 | #include "SVNlog.h" |
---|
36 | #include "TinyStats.h" |
---|
37 | |
---|
38 | #include <algorithm> |
---|
39 | #include <cassert> |
---|
40 | #include <cstdio> |
---|
41 | #include <ctime> |
---|
42 | #include <fstream> |
---|
43 | #include <iostream> |
---|
44 | #include <map> |
---|
45 | #include <stdexcept> |
---|
46 | #include <string> |
---|
47 | #include <sstream> |
---|
48 | #include <sys/stat.h> |
---|
49 | |
---|
50 | namespace theplu{ |
---|
51 | namespace svndigest{ |
---|
52 | |
---|
53 | |
---|
54 | File::File(const unsigned int level, const std::string& path, |
---|
55 | const std::string& output) |
---|
56 | : Node(level,path,output) |
---|
57 | { |
---|
58 | output_dir_=output; |
---|
59 | if (!output_dir_.empty()) |
---|
60 | output_dir_+='/'; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | std::string File::blame_output_file_name(void) const |
---|
65 | { |
---|
66 | return "blame_output/" + local_path() + ".html"; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | std::string File::href(void) const |
---|
71 | { |
---|
72 | return name()+".html"; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | svn_revnum_t File::last_changed_rev(void) const |
---|
77 | { |
---|
78 | return svn_info().last_changed_rev(); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | void File::log_core(SVNlog&) const |
---|
83 | { |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | std::string File::node_type(void) const |
---|
88 | { |
---|
89 | return std::string("file"); |
---|
90 | } |
---|
91 | |
---|
92 | |
---|
93 | std::string File::output_path(void) const |
---|
94 | { |
---|
95 | return output_dir()+name()+".html"; |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | const StatsCollection& File::parse(bool verbose, bool ignore) |
---|
100 | { |
---|
101 | if (verbose) |
---|
102 | std::cout << "Parsing '" << path_ << "'" << std::endl; |
---|
103 | stats_.reset(); |
---|
104 | std::string cache_dir = directory_name(path()) + std::string(".svndigest/"); |
---|
105 | std::string cache_file = cache_dir + name()+std::string(".svndigest-cache"); |
---|
106 | if (!ignore && node_exist(cache_file)){ |
---|
107 | std::ifstream is(cache_file.c_str()); |
---|
108 | if (stats_.load_cache(is)) { |
---|
109 | is.close(); |
---|
110 | return stats_; |
---|
111 | } |
---|
112 | is.close(); |
---|
113 | } |
---|
114 | else |
---|
115 | stats_.parse(path_); |
---|
116 | if (!node_exist(cache_dir)) |
---|
117 | mkdir(cache_dir); |
---|
118 | std::string tmp_cache_file(cache_file+"~"); |
---|
119 | std::ofstream os(tmp_cache_file.c_str()); |
---|
120 | assert(os); |
---|
121 | stats_.print(os); |
---|
122 | os.close(); |
---|
123 | rename(tmp_cache_file, cache_file); |
---|
124 | return stats_; |
---|
125 | } |
---|
126 | |
---|
127 | |
---|
128 | void File::print_blame(std::ofstream& os) const |
---|
129 | { |
---|
130 | os << "<br /><h3>" << local_path() << "</h3>"; |
---|
131 | os << "<div class=\"blame_legend\">\n"; |
---|
132 | os << "<dl>\n"; |
---|
133 | os << "<dt class=\"code\"></dt><dd>Code</dd>\n"; |
---|
134 | os << "<dt class=\"comment\"></dt><dd>Comments</dd>\n"; |
---|
135 | os << "<dt class=\"other\"></dt><dd>Other</dd>\n"; |
---|
136 | os << "</dl>\n</div>\n"; |
---|
137 | os << "<table class=\"blame\">\n"; |
---|
138 | os << "<thead>\n"; |
---|
139 | os << "<tr>\n"; |
---|
140 | os << "<th class=\"rev\">Rev</th>\n"; |
---|
141 | os << "<th class=\"date\">Date</th>\n"; |
---|
142 | os << "<th class=\"author\">Author</th>\n"; |
---|
143 | os << "<th class=\"line\">Line</th>\n"; |
---|
144 | os << "<th></th>\n"; |
---|
145 | os << "</tr>\n</thead>\n"; |
---|
146 | os << "<tbody>\n"; |
---|
147 | HtmlStream hs(os); |
---|
148 | SVNblame blame(path_); |
---|
149 | LineTypeParser parser(path_); |
---|
150 | while (blame.valid()) { |
---|
151 | parser.parse(blame.line()); |
---|
152 | blame.next_line(); |
---|
153 | } |
---|
154 | blame.reset(); |
---|
155 | |
---|
156 | std::vector<LineTypeParser::line_type>::const_iterator |
---|
157 | line_type(parser.type().begin()); |
---|
158 | int last=0; |
---|
159 | int first=0; |
---|
160 | bool using_dates=true; |
---|
161 | if (!Graph::date_xticks()) { |
---|
162 | using_dates=false; |
---|
163 | last = stats_["classic"].revision(); |
---|
164 | } |
---|
165 | else { |
---|
166 | last = Graph::xticks().back(); |
---|
167 | // earliest date corresponds either to revision 0 or revision 1 |
---|
168 | first = std::min(Graph::xticks()[0],Graph::xticks()[1]); |
---|
169 | } |
---|
170 | // color is calculated linearly on time, c = kt + m |
---|
171 | // brightest color (for oldest rev in log) is set to 192. |
---|
172 | double k = 192.0/(first-last); |
---|
173 | double m = -last*k; |
---|
174 | while (blame.valid()) { |
---|
175 | std::string color; |
---|
176 | if (using_dates) |
---|
177 | color = hex(static_cast<int>(k*Date(blame.date()).seconds()+m),2); |
---|
178 | else |
---|
179 | color = hex(static_cast<int>(k*blame.revision()+m),2); |
---|
180 | os << "<tr>\n<td class=\"rev\">"; |
---|
181 | std::stringstream color_ss; |
---|
182 | color_ss << "#" << color << color << color; |
---|
183 | os << "<font color=\"" << color_ss.str() << "\">" |
---|
184 | << trac_revision(blame.revision(), color_ss.str()) |
---|
185 | << "</font></td>\n<td class=\"date\"><font color=\"#" << color |
---|
186 | << color << color << "\">" ; |
---|
187 | hs << Date(blame.date())("%d %b %y"); |
---|
188 | os << "</font></td>\n<td class=\"author\">"; |
---|
189 | const std::string& author_color = |
---|
190 | Colors::instance().color_str(blame.author()); |
---|
191 | assert(!author_color.empty()); |
---|
192 | os << "<font color=\"#" << author_color << "\">"; |
---|
193 | hs << blame.author(); |
---|
194 | os << "</td>\n<td class=\""; |
---|
195 | assert(line_type!=parser.type().end()); |
---|
196 | if (*line_type==LineTypeParser::other) |
---|
197 | os << "line-other"; |
---|
198 | else if (*line_type==LineTypeParser::comment || |
---|
199 | *line_type==LineTypeParser::copyright) |
---|
200 | os << "line-comment"; |
---|
201 | else if (*line_type==LineTypeParser::code) |
---|
202 | os << "line-code"; |
---|
203 | else { |
---|
204 | std::string msg="File::print_blame(): unexpected line type found"; |
---|
205 | throw std::runtime_error(msg); |
---|
206 | } |
---|
207 | os << "\">" << blame.line_no()+1 |
---|
208 | << "</td>\n<td>"; |
---|
209 | hs << blame.line(); |
---|
210 | os << "</td>\n</tr>\n"; |
---|
211 | blame.next_line(); |
---|
212 | ++line_type; |
---|
213 | } |
---|
214 | os << "</tbody>\n"; |
---|
215 | os << "</table>\n"; |
---|
216 | } |
---|
217 | |
---|
218 | |
---|
219 | void File::print_core(const bool verbose) const |
---|
220 | { |
---|
221 | mkdir_p(directory_name(blame_output_file_name())); |
---|
222 | std::ofstream os(blame_output_file_name().c_str()); |
---|
223 | assert(os.good()); |
---|
224 | print_html_start(os, "svndigest", level_+1); |
---|
225 | if (Configuration::instance().output_blame_information()) |
---|
226 | print_blame(os); |
---|
227 | print_footer(os); |
---|
228 | os.close(); |
---|
229 | } |
---|
230 | |
---|
231 | |
---|
232 | void File::print_core(const std::string& stats_type, |
---|
233 | const std::string& user, const std::string& line_type, |
---|
234 | const SVNlog& log) const |
---|
235 | { |
---|
236 | std::string lpath = local_path(); |
---|
237 | if (lpath.empty()) |
---|
238 | lpath = "index"; |
---|
239 | std::string outpath = stats_type+"/"+user+"/"+line_type+"/"+lpath; |
---|
240 | std::string imagefile = stats_type+"/"+"images/"+line_type+"/"+lpath; |
---|
241 | std::string html_name(outpath + ".html"); |
---|
242 | mkdir_p(directory_name(html_name)); |
---|
243 | mkdir_p(directory_name(imagefile)); |
---|
244 | std::ofstream os(html_name.c_str()); |
---|
245 | assert(os); |
---|
246 | print_header(os, name(), level_+3, user, line_type, lpath+".html", |
---|
247 | stats_type); |
---|
248 | path_anchor(os); |
---|
249 | |
---|
250 | std::stringstream ss; |
---|
251 | for (size_t i=0; i<level_; ++i) |
---|
252 | ss << "../"; |
---|
253 | ss << "../../../"; |
---|
254 | if (user=="all") |
---|
255 | ss << stats_[stats_type].plot(imagefile,line_type); |
---|
256 | else |
---|
257 | ss << imagefile; |
---|
258 | os << "<p class=\"plot\">\n"; |
---|
259 | os << image(ss.str()); |
---|
260 | os << "</p>\n"; |
---|
261 | |
---|
262 | print_author_summary(os, stats_[stats_type], line_type, log); |
---|
263 | os << "\n"; |
---|
264 | os << "<h3>" |
---|
265 | << anchor(blame_output_file_name(), |
---|
266 | "Blame Information", level_+3) |
---|
267 | << "</h3>\n"; |
---|
268 | |
---|
269 | print_footer(os); |
---|
270 | os.close(); |
---|
271 | } |
---|
272 | |
---|
273 | |
---|
274 | void File::traverse(NodeVisitor& visitor) |
---|
275 | { |
---|
276 | visitor.visit(*this); |
---|
277 | } |
---|
278 | |
---|
279 | }} // end of namespace svndigest and namespace theplu |
---|