source: trunk/lib/File.cc @ 978

Last change on this file since 978 was 978, checked in by Peter Johansson, 13 years ago

refs #429. Convert copyright statements to UTF-8

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