source: trunk/lib/File.cc @ 1234

Last change on this file since 1234 was 1234, checked in by Peter Johansson, 12 years ago

refs #476. Merged visitor branch into trunk. Used 'svn merge /branches/visitor' because --reintegrate did (currently) not work on our repo. Since I could not merge as suggested in subversion manual, I reverted all mergeinfo properties to avoid future confusion by svn-client. This means (I think) that there will be no connection about this commit and the visitor branch, but to svn-client this will look like a large changeset (just like a merge in the old days).

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