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
Line 
1// $Id: File.cc 456 2007-08-21 07:56:06Z jari $
2
3/*
4  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson
5
6  This file is part of svndigest, http://trac.thep.lu.se/trac/svndigest
7
8  svndigest is free software; you can redistribute it and/or modify it
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
13  svndigest is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
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
24#include "File.h"
25
26#include "Alias.h"
27#include "Date.h"
28#include "GnuplotFE.h"
29#include "html_utility.h"
30#include "HtmlStream.h"
31#include "Stats.h"
32#include "SVNblame.h"
33#include "SVNlog.h"
34
35#include <cassert>
36#include <cstdio>
37#include <ctime>
38#include <fstream>
39#include <iostream>
40#include <map>
41#include <string>
42
43namespace theplu{
44namespace svndigest{
45
46
47  File::File(const u_int level, const std::string& path, 
48             const std::string& output) 
49    : Node(level,path,output) 
50  {
51    output_dir_=output;
52    if (!output_dir_.empty())
53      output_dir_+='/';
54  }
55
56
57  std::string File::href(void) const
58  { 
59    return name()+".html"; 
60  }
61
62
63  std::string File::node_type(void) const
64  {
65    return std::string("file");
66  }
67
68
69  std::string File::output_path(void) const
70  {
71    return output_dir()+name()+".html";
72  }
73
74
75  const Stats& File::parse(const bool verbose)
76  {
77    if (verbose)
78      std::cout << "Parsing " << path_ << std::endl; 
79    stats_.reset();
80    stats_.parse(path_);
81    return stats_;
82  }
83
84
85  void File::print_blame(std::ofstream& os) const
86  {
87    os << "<br /><h3>Blame Information</h3>";
88    os << "<table class=\"blame\">\n";
89    os << "<thead>\n";
90    os << "<tr>\n";
91    os << "<th class=\"rev\">Rev</th>\n";
92    os << "<th class=\"date\">Date</th>\n";
93    os << "<th class=\"author\">Author</th>\n";
94    os << "<th class=\"line\">Line</th>\n";
95    os << "<th></th>\n";
96    os << "</tr>\n</thead>\n";
97    os << "<tbody>\n";
98    HtmlStream hs(os);
99    SVNblame blame(path_);
100    Parser parser(path_);
101    std::vector<Parser::line_type>::const_iterator
102      line_type(parser.type().begin());
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    }
114    // color is calculated linearly on time, c = kt + m
115    // brightest color (for oldest rev in log) is set to 192.
116    double k = 192.0/(first-last);
117    double m = -last*k; 
118    while (blame.valid()) {
119      std::string color;
120      if (using_dates)
121        color = hex(static_cast<int>(k*Date(blame.date()).seconds()+m),2);
122      else
123        color = hex(static_cast<int>(k*blame.revision()+m),2);
124      os << "<tr>\n<td class=\"rev\"><font color=\"#" << color
125         << color << color << "\">" << blame.revision()
126         << "</font></td>\n<td class=\"date\"><font color=\"#" << color
127         << color << color << "\">" ;
128      hs << Date(blame.date())("%d %b %y");
129      os << "</font></td>\n<td class=\"author\">";
130      hs << blame.author();
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
140         << "</td>\n<td>";
141      hs << blame.line();
142      os << "</td>\n</tr>\n";
143      blame.next_line();
144      ++line_type;
145    }
146    os << "</tbody>\n";
147    os << "</table>\n";
148  }
149
150
151  void File::print_copyright(std::map<std::string, Alias>& alias) const 
152  {
153    if (ignore())
154      return;
155    using namespace std;
156
157    SVNlog log(path());
158
159    map<int, set<Alias> > year_authors;
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);
166      tm* timeinfo = gmtime(&sec);
167
168      // find username in map of aliases
169      std::map<string,Alias>::iterator name(alias.lower_bound(*author));
170
171      // if alias exist insert alias
172      if (name != alias.end() && name->first==*author)
173        year_authors[timeinfo->tm_year].insert(name->second);
174      else {
175        // else insert user name
176        Alias a(*author,alias.size());
177        year_authors[timeinfo->tm_year].insert(a);
178        std::cerr << "svndigest: warning: no copyright alias found for `" 
179                  << *author << "`\n";
180        // insert alias to avoid multiple warnings.
181        alias.insert(name, std::make_pair(*author, a));
182      }
183    }
184
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());
197    assert(is.good());
198    string line;
199    bool found_copyright = false;
200    bool after_copyright = false;
201    string prefix;
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         
215      }
216      else {
217        // check whether copyright starts on this line
218        string::iterator i = search(line.begin(), line.end(), "Copyright (C)");
219        if (i==line.end()) {
220          tmp << line << "\n";
221        }     
222        else {
223          prefix = line.substr(0, distance(line.begin(), i));
224          found_copyright = true;
225          // Printing copyright statement
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);
235          }
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          }
251        }
252      }
253    }
254    is.close();
255    tmp.close();
256    close(fd);
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);
274  }
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
292    os << "<p class=\"plot\">\n<img src='"; 
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;
300    os << "' alt='[plot]' />\n</p>";
301
302    print_author_summary(os, line_type, log);
303    os << "\n";
304
305    print_blame(os);
306
307    print_footer(os);
308    os.close(); 
309  }
310
311}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.