source: branches/0.6-stable/lib/File.cc @ 502

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

fixes #278

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.8 KB
RevLine 
[7]1// $Id: File.cc 502 2007-10-20 00:06:32Z peter $
[4]2
[84]3/*
[379]4  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson
[84]5
[430]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"
[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
[502]35#include <algorithm>
[206]36#include <cassert>
[456]37#include <cstdio>
[225]38#include <ctime>
[4]39#include <fstream>
40#include <iostream>
[226]41#include <map>
[4]42#include <string>
43
44namespace theplu{
[149]45namespace svndigest{
[4]46
[176]47
[307]48  File::File(const u_int level, const std::string& path, 
49             const std::string& output) 
50    : Node(level,path,output) 
[343]51  {
52    output_dir_=output;
53    if (!output_dir_.empty())
[356]54      output_dir_+='/';
[343]55  }
[307]56
57
[175]58  std::string File::href(void) const
[100]59  { 
[175]60    return name()+".html"; 
[100]61  }
62
63
[343]64  std::string File::node_type(void) const
[182]65  {
66    return std::string("file");
67  }
68
69
[343]70  std::string File::output_path(void) const
71  {
[356]72    return output_dir()+name()+".html";
[343]73  }
74
75
[185]76  const Stats& File::parse(const bool verbose)
77  {
[60]78    if (verbose)
79      std::cout << "Parsing " << path_ << std::endl; 
[185]80    stats_.reset();
81    stats_.parse(path_);
[129]82    return stats_;
[185]83  }
[4]84
[10]85
[398]86  void File::print_blame(std::ofstream& os) const
[207]87  {
[400]88    os << "<br /><h3>Blame Information</h3>";
[207]89    os << "<table class=\"blame\">\n";
[379]90    os << "<thead>\n";
91    os << "<tr>\n";
[403]92    os << "<th class=\"rev\">Rev</th>\n";
[380]93    os << "<th class=\"date\">Date</th>\n";
94    os << "<th class=\"author\">Author</th>\n";
[403]95    os << "<th class=\"line\">Line</th>\n";
[401]96    os << "<th></th>\n";
[379]97    os << "</tr>\n</thead>\n";
98    os << "<tbody>\n";
[380]99    HtmlStream hs(os);
100    SVNblame blame(path_);
[403]101    Parser parser(path_);
102    std::vector<Parser::line_type>::const_iterator
103      line_type(parser.type().begin());
[399]104    int last=0;
105    int first=0;
106    bool using_dates=true;
107    if (GnuplotFE::instance()->dates().empty()){
108      using_dates=false;
109      last = stats_.revision();
110    }
111    else {
112      last = Date(GnuplotFE::instance()->dates().back()).seconds();
[502]113      // earliest date corresponds either to revision 0 or revision 1
114      first = std::min(Date(GnuplotFE::instance()->dates()[0]).seconds(),
115                       Date(GnuplotFE::instance()->dates()[1]).seconds());
[399]116    }
[395]117    // color is calculated linearly on time, c = kt + m
118    // brightest color (for oldest rev in log) is set to 192.
[399]119    double k = 192.0/(first-last);
[395]120    double m = -last*k; 
[381]121    while (blame.valid()) {
[399]122      std::string color;
123      if (using_dates)
[400]124        color = hex(static_cast<int>(k*Date(blame.date()).seconds()+m),2);
[399]125      else
[400]126        color = hex(static_cast<int>(k*blame.revision()+m),2);
[403]127      os << "<tr>\n<td class=\"rev\"><font color=\"#" << color
[395]128         << color << color << "\">" << blame.revision()
[400]129         << "</font></td>\n<td class=\"date\"><font color=\"#" << color
[401]130         << color << color << "\">" ;
[405]131      hs << Date(blame.date())("%d %b %y");
[401]132      os << "</font></td>\n<td class=\"author\">";
[381]133      hs << blame.author();
[403]134      os << "</td>\n<td class=\"";
135      assert(line_type!=parser.type().end());
136      if (*line_type==Parser::empty)
137        os << "line-other";
138      else if (*line_type==Parser::comment)
139        os << "line-comment";
140      else
141        os << "line-code";
142      os << "\">" << blame.line_no()+1
[401]143         << "</td>\n<td>";
[381]144      hs << blame.line();
[380]145      os << "</td>\n</tr>\n";
[381]146      blame.next_line();
[403]147      ++line_type;
[380]148    }
[379]149    os << "</tbody>\n";
[207]150    os << "</table>\n";
151  }
152
153
[303]154  void File::print_copyright(std::map<std::string, Alias>& alias) const 
155  {
[198]156    if (ignore())
157      return;
[225]158    using namespace std;
[198]159
[234]160    SVNlog log(path());
161
[303]162    map<int, set<Alias> > year_authors;
[234]163
164    assert(log.author().size()==log.date().size());
165    vector<string>::const_iterator author=log.author().begin();
166    for (vector<string>::const_iterator date=log.date().begin();
167         date!=log.date().end(); ++date, ++author) {
168      time_t sec = str2time(*date);
[225]169      tm* timeinfo = gmtime(&sec);
[226]170
171      // find username in map of aliases
[303]172      std::map<string,Alias>::iterator name(alias.lower_bound(*author));
173
[226]174      // if alias exist insert alias
[234]175      if (name != alias.end() && name->first==*author)
[303]176        year_authors[timeinfo->tm_year].insert(name->second);
[230]177      else {
[226]178        // else insert user name
[303]179        Alias a(*author,alias.size());
180        year_authors[timeinfo->tm_year].insert(a);
[235]181        std::cerr << "svndigest: warning: no copyright alias found for `" 
182                  << *author << "`\n";
[230]183        // insert alias to avoid multiple warnings.
[303]184        alias.insert(name, std::make_pair(*author, a));
[230]185      }
[198]186    }
[234]187
[227]188    // Code copied from Gnuplot -r70
189    char tmpname[]="/tmp/svndigestXXXXXX";
190    int fd=mkstemp(tmpname);  // mkstemp return a file descriptor
191    if (fd == -1)
192      throw std::runtime_error(std::string("Failed to get unique filename: ") +
193                               tmpname);
194    // Jari would like to do something like 'std::ofstream tmp(fd);'
195    // but has to settle for (which is stupid since the file is
196    // already open for writing.
197    std::ofstream tmp(tmpname);
198
199    ifstream is(path().c_str());
[234]200    assert(is.good());
[227]201    string line;
202    bool found_copyright = false;
203    bool after_copyright = false;
[225]204    string prefix;
[227]205    while(getline(is, line)){
[470]206      if (after_copyright) {
207        tmp << line;
208        if (is.good()) // not end of file
209          tmp << "\n";
210        else { // check if file ends with newline character
211          is.unget();
212          char c = is.get();
213          if (c=='\n') 
214            tmp << "\n";
215        }
216      }
[227]217      else if (found_copyright){
218        // check if line is end of copyright statement, i.e. contains
219        // no alphanumerical character
220        after_copyright = true;
221        for (size_t i=0; i<line.size()&&after_copyright; ++i)
222          if (isalnum(line[i]))
223            after_copyright = false;
224        if (after_copyright)
225          tmp << line << "\n";
226         
[198]227      }
[227]228      else {
229        // check whether copyright starts on this line
230        string::iterator i = search(line.begin(), line.end(), "Copyright (C)");
[234]231        if (i==line.end()) {
[227]232          tmp << line << "\n";
[234]233        }     
[227]234        else {
235          prefix = line.substr(0, distance(line.begin(), i));
236          found_copyright = true;
[234]237          // Printing copyright statement
[303]238          for (map<int, set<Alias> >::const_iterator i(year_authors.begin());
239               i!=year_authors.end();) {
240          tmp << prefix << "Copyright (C) "
241              << 1900+i->first;
242          map<int, set<Alias> >::const_iterator j = i;
243          assert(i!=year_authors.end());
244          while (++j!=year_authors.end() && 
245                 i->second == j->second){
246            tmp << ", " << 1900+(j->first);
[227]247          }
[303]248          // printing authors
249          std::vector<Alias> vec_alias;
250          back_insert_iterator<std::vector<Alias> > ii(vec_alias);
251          std::copy(i->second.begin(), i->second.end(), ii);
252          // sort with respect to id
253          std::sort(vec_alias.begin(), vec_alias.end(), IdCompare());
254          for (std::vector<Alias>::iterator a=vec_alias.begin();
255               a!=vec_alias.end(); ++a){
256            if (a!=vec_alias.begin())
257              tmp << ",";
258            tmp << " " << a->name();
259          }
260          tmp << "\n";
261          i = j;
262          }
[227]263        }
[198]264      }
265    }
[227]266    is.close();
267    tmp.close();
268    close(fd);
[456]269    // finally copy temporary file to replace original file, and
270    // remove the temporary file
271    try {
272      copy_file(tmpname, path());
273    }
274    catch (std::runtime_error e) {
275      // catch exception, cleanup, and rethrow
276      std::cerr << "File::print_copyright: Exception caught, "
277                << "removing temporary file " << tmpname << std::endl;
278      if (unlink(tmpname))
279        throw runtime_error(std::string("File::print_copyright: ") +
280                            "failed to unlink temporary file" + tmpname);
281      throw;
282    }
283    if (unlink(tmpname))
284      throw runtime_error(std::string("File::print_copyright: ") +
285                          "failed to unlink temporary file" + tmpname);
[198]286  }
[379]287
288
289  void File::print_core(const bool verbose) const 
290  {
291  }
292
293
294  void File::print_core(const std::string& user, const std::string& line_type,
295                        const SVNlog& log) const 
296  {
297    std::string outpath = user+"/"+line_type+"/"+local_path();
298    std::string imagefile = "images/"+line_type+"/"+local_path_+".png";
299    std::string html_name(outpath + ".html");
300    std::ofstream os(html_name.c_str());
301    print_header(os, name(), level_+2, user, line_type, local_path()+".html");
302    path_anchor(os);
303
[400]304    os << "<p class=\"plot\">\n<img src='"; 
[379]305    for (size_t i=0; i<level_; ++i)
306      os << "../";
307    os << "../../";
308    if (user=="all")
309      os << stats_.plot(imagefile,line_type);
310    else
311      os << imagefile;
[400]312    os << "' alt='[plot]' />\n</p>";
[379]313
314    print_author_summary(os, line_type, log);
[400]315    os << "\n";
[379]316
[398]317    print_blame(os);
[379]318
319    print_footer(os);
320    os.close(); 
321  }
322
[149]323}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.