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

Last change on this file since 456 was 456, checked in by Jari Häkkinen, 14 years ago

Refs #251.

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