source: trunk/lib/File.cc @ 831

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

fixes #240 - explain linetype coloring in blame output

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