source: trunk/lib/File.cc @ 498

Last change on this file since 498 was 498, checked in by Peter Johansson, 14 years ago

fixes #223

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