source: trunk/lib/File.cc @ 1097

Last change on this file since 1097 was 1097, checked in by Peter Johansson, 11 years ago

avoid string 'svndigest: ' in exception message as we print 'svndigest: e.what()' in main. Also change svndigest to svncopyright in copyright warnings.

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