1 | // $Id: File.cc 380 2007-06-21 19:25:39Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson |
---|
5 | |
---|
6 | This file is part of svndigest, http://lev.thep.lu.se/trac/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 "Date.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 <cassert> |
---|
35 | #include <ctime> |
---|
36 | #include <fstream> |
---|
37 | #include <iostream> |
---|
38 | #include <map> |
---|
39 | #include <string> |
---|
40 | |
---|
41 | namespace theplu{ |
---|
42 | namespace svndigest{ |
---|
43 | |
---|
44 | |
---|
45 | File::File(const u_int level, const std::string& path, |
---|
46 | const std::string& output) |
---|
47 | : Node(level,path,output) |
---|
48 | { |
---|
49 | output_dir_=output; |
---|
50 | if (!output_dir_.empty()) |
---|
51 | output_dir_+='/'; |
---|
52 | } |
---|
53 | |
---|
54 | |
---|
55 | std::string File::href(void) const |
---|
56 | { |
---|
57 | return name()+".html"; |
---|
58 | } |
---|
59 | |
---|
60 | |
---|
61 | std::string File::node_type(void) const |
---|
62 | { |
---|
63 | return std::string("file"); |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | std::string File::output_path(void) const |
---|
68 | { |
---|
69 | return output_dir()+name()+".html"; |
---|
70 | } |
---|
71 | |
---|
72 | |
---|
73 | const Stats& File::parse(const bool verbose) |
---|
74 | { |
---|
75 | if (verbose) |
---|
76 | std::cout << "Parsing " << path_ << std::endl; |
---|
77 | stats_.reset(); |
---|
78 | stats_.parse(path_); |
---|
79 | return stats_; |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | void File::print_blame(std::ofstream& os) const |
---|
84 | { |
---|
85 | os << "<br><h3>Blame Information</h3>"; |
---|
86 | os << "<table class=\"blame\">\n"; |
---|
87 | os << "<thead>\n"; |
---|
88 | os << "<tr>\n"; |
---|
89 | os << "<th class=\"number\">Rev</th>\n"; |
---|
90 | os << "<th class=\"date\">Date</th>\n"; |
---|
91 | os << "<th class=\"author\">Author</th>\n"; |
---|
92 | os << "<th class=\"number\">Lineno</th>\n"; |
---|
93 | os << "<th>Line</th>\n"; |
---|
94 | os << "</tr>\n</thead>\n"; |
---|
95 | os << "<tbody>\n"; |
---|
96 | HtmlStream hs(os); |
---|
97 | SVNblame blame(path_); |
---|
98 | while (const struct SVNblame::blame_information* bi=blame.next()) { |
---|
99 | os << "<tr>\n<td class=\"number\">" << bi->revision |
---|
100 | << "</td>\n<td class=\"date\">" << Date(bi->date)("%e %b %y") |
---|
101 | << "</td>\n<td class=\"author\">"; |
---|
102 | hs << bi->author; |
---|
103 | os << "</td>\n<td class=\"number\">" << bi->line_no+1 |
---|
104 | << "</td>\n<td class=\"code\">"; |
---|
105 | hs << bi->line; |
---|
106 | os << "</td>\n</tr>\n"; |
---|
107 | } |
---|
108 | os << "</tbody>\n"; |
---|
109 | os << "</table>\n"; |
---|
110 | } |
---|
111 | |
---|
112 | |
---|
113 | void File::print_copyright(std::map<std::string, Alias>& alias) const |
---|
114 | { |
---|
115 | if (ignore()) |
---|
116 | return; |
---|
117 | using namespace std; |
---|
118 | |
---|
119 | SVNlog log(path()); |
---|
120 | |
---|
121 | map<int, set<Alias> > year_authors; |
---|
122 | |
---|
123 | assert(log.author().size()==log.date().size()); |
---|
124 | vector<string>::const_iterator author=log.author().begin(); |
---|
125 | for (vector<string>::const_iterator date=log.date().begin(); |
---|
126 | date!=log.date().end(); ++date, ++author) { |
---|
127 | time_t sec = str2time(*date); |
---|
128 | tm* timeinfo = gmtime(&sec); |
---|
129 | |
---|
130 | // find username in map of aliases |
---|
131 | std::map<string,Alias>::iterator name(alias.lower_bound(*author)); |
---|
132 | |
---|
133 | // if alias exist insert alias |
---|
134 | if (name != alias.end() && name->first==*author) |
---|
135 | year_authors[timeinfo->tm_year].insert(name->second); |
---|
136 | else { |
---|
137 | // else insert user name |
---|
138 | Alias a(*author,alias.size()); |
---|
139 | year_authors[timeinfo->tm_year].insert(a); |
---|
140 | std::cerr << "svndigest: warning: no copyright alias found for `" |
---|
141 | << *author << "`\n"; |
---|
142 | // insert alias to avoid multiple warnings. |
---|
143 | alias.insert(name, std::make_pair(*author, a)); |
---|
144 | } |
---|
145 | } |
---|
146 | |
---|
147 | // Code copied from Gnuplot -r70 |
---|
148 | char tmpname[]="/tmp/svndigestXXXXXX"; |
---|
149 | int fd=mkstemp(tmpname); // mkstemp return a file descriptor |
---|
150 | if (fd == -1) |
---|
151 | throw std::runtime_error(std::string("Failed to get unique filename: ") + |
---|
152 | tmpname); |
---|
153 | // Jari would like to do something like 'std::ofstream tmp(fd);' |
---|
154 | // but has to settle for (which is stupid since the file is |
---|
155 | // already open for writing. |
---|
156 | std::ofstream tmp(tmpname); |
---|
157 | |
---|
158 | ifstream is(path().c_str()); |
---|
159 | assert(is.good()); |
---|
160 | string line; |
---|
161 | bool found_copyright = false; |
---|
162 | bool after_copyright = false; |
---|
163 | string prefix; |
---|
164 | while(getline(is, line)){ |
---|
165 | if (after_copyright) |
---|
166 | tmp << line << "\n"; |
---|
167 | else if (found_copyright){ |
---|
168 | // check if line is end of copyright statement, i.e. contains |
---|
169 | // no alphanumerical character |
---|
170 | after_copyright = true; |
---|
171 | for (size_t i=0; i<line.size()&&after_copyright; ++i) |
---|
172 | if (isalnum(line[i])) |
---|
173 | after_copyright = false; |
---|
174 | if (after_copyright) |
---|
175 | tmp << line << "\n"; |
---|
176 | |
---|
177 | } |
---|
178 | else { |
---|
179 | // check whether copyright starts on this line |
---|
180 | string::iterator i = search(line.begin(), line.end(), "Copyright (C)"); |
---|
181 | if (i==line.end()) { |
---|
182 | tmp << line << "\n"; |
---|
183 | } |
---|
184 | else { |
---|
185 | prefix = line.substr(0, distance(line.begin(), i)); |
---|
186 | found_copyright = true; |
---|
187 | // Printing copyright statement |
---|
188 | for (map<int, set<Alias> >::const_iterator i(year_authors.begin()); |
---|
189 | i!=year_authors.end();) { |
---|
190 | tmp << prefix << "Copyright (C) " |
---|
191 | << 1900+i->first; |
---|
192 | map<int, set<Alias> >::const_iterator j = i; |
---|
193 | assert(i!=year_authors.end()); |
---|
194 | while (++j!=year_authors.end() && |
---|
195 | i->second == j->second){ |
---|
196 | tmp << ", " << 1900+(j->first); |
---|
197 | } |
---|
198 | // printing authors |
---|
199 | std::vector<Alias> vec_alias; |
---|
200 | back_insert_iterator<std::vector<Alias> > ii(vec_alias); |
---|
201 | std::copy(i->second.begin(), i->second.end(), ii); |
---|
202 | // sort with respect to id |
---|
203 | std::sort(vec_alias.begin(), vec_alias.end(), IdCompare()); |
---|
204 | for (std::vector<Alias>::iterator a=vec_alias.begin(); |
---|
205 | a!=vec_alias.end(); ++a){ |
---|
206 | if (a!=vec_alias.begin()) |
---|
207 | tmp << ","; |
---|
208 | tmp << " " << a->name(); |
---|
209 | } |
---|
210 | tmp << "\n"; |
---|
211 | i = j; |
---|
212 | } |
---|
213 | } |
---|
214 | } |
---|
215 | } |
---|
216 | is.close(); |
---|
217 | tmp.close(); |
---|
218 | close(fd); |
---|
219 | // finally move printed temporary file to original file |
---|
220 | rename(tmpname, path().c_str()); |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | void File::print_core(const bool verbose) const |
---|
225 | { |
---|
226 | } |
---|
227 | |
---|
228 | |
---|
229 | void File::print_core(const std::string& user, const std::string& line_type, |
---|
230 | const SVNlog& log) const |
---|
231 | { |
---|
232 | std::string outpath = user+"/"+line_type+"/"+local_path(); |
---|
233 | std::string imagefile = "images/"+line_type+"/"+local_path_+".png"; |
---|
234 | std::string html_name(outpath + ".html"); |
---|
235 | std::ofstream os(html_name.c_str()); |
---|
236 | print_header(os, name(), level_+2, user, line_type, local_path()+".html"); |
---|
237 | path_anchor(os); |
---|
238 | |
---|
239 | os << "<p align=center>\n<img src='"; |
---|
240 | for (size_t i=0; i<level_; ++i) |
---|
241 | os << "../"; |
---|
242 | os << "../../"; |
---|
243 | if (user=="all") |
---|
244 | os << stats_.plot(imagefile,line_type); |
---|
245 | else |
---|
246 | os << imagefile; |
---|
247 | os << "' alt='[plot]' border=0>\n</p>"; |
---|
248 | |
---|
249 | print_author_summary(os, line_type, log); |
---|
250 | os << "</p>\n"; |
---|
251 | |
---|
252 | print_blame(os); |
---|
253 | |
---|
254 | print_footer(os); |
---|
255 | os.close(); |
---|
256 | } |
---|
257 | |
---|
258 | }} // end of namespace svndigest and namespace theplu |
---|