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

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

fixes #264

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.7 KB
Line 
1// $Id: File.cc 470 2007-08-31 12:05:42Z peter $
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;
205        if (is.good()) // not end of file
206          tmp << "\n";
207        else { // check if file ends with newline character
208          is.unget();
209          char c = is.get();
210          if (c=='\n') 
211            tmp << "\n";
212        }
213      }
214      else if (found_copyright){
215        // check if line is end of copyright statement, i.e. contains
216        // no alphanumerical character
217        after_copyright = true;
218        for (size_t i=0; i<line.size()&&after_copyright; ++i)
219          if (isalnum(line[i]))
220            after_copyright = false;
221        if (after_copyright)
222          tmp << line << "\n";
223         
224      }
225      else {
226        // check whether copyright starts on this line
227        string::iterator i = search(line.begin(), line.end(), "Copyright (C)");
228        if (i==line.end()) {
229          tmp << line << "\n";
230        }     
231        else {
232          prefix = line.substr(0, distance(line.begin(), i));
233          found_copyright = true;
234          // Printing copyright statement
235          for (map<int, set<Alias> >::const_iterator i(year_authors.begin());
236               i!=year_authors.end();) {
237          tmp << prefix << "Copyright (C) "
238              << 1900+i->first;
239          map<int, set<Alias> >::const_iterator j = i;
240          assert(i!=year_authors.end());
241          while (++j!=year_authors.end() && 
242                 i->second == j->second){
243            tmp << ", " << 1900+(j->first);
244          }
245          // printing authors
246          std::vector<Alias> vec_alias;
247          back_insert_iterator<std::vector<Alias> > ii(vec_alias);
248          std::copy(i->second.begin(), i->second.end(), ii);
249          // sort with respect to id
250          std::sort(vec_alias.begin(), vec_alias.end(), IdCompare());
251          for (std::vector<Alias>::iterator a=vec_alias.begin();
252               a!=vec_alias.end(); ++a){
253            if (a!=vec_alias.begin())
254              tmp << ",";
255            tmp << " " << a->name();
256          }
257          tmp << "\n";
258          i = j;
259          }
260        }
261      }
262    }
263    is.close();
264    tmp.close();
265    close(fd);
266    // finally copy temporary file to replace original file, and
267    // remove the temporary file
268    try {
269      copy_file(tmpname, path());
270    }
271    catch (std::runtime_error e) {
272      // catch exception, cleanup, and rethrow
273      std::cerr << "File::print_copyright: Exception caught, "
274                << "removing temporary file " << tmpname << std::endl;
275      if (unlink(tmpname))
276        throw runtime_error(std::string("File::print_copyright: ") +
277                            "failed to unlink temporary file" + tmpname);
278      throw;
279    }
280    if (unlink(tmpname))
281      throw runtime_error(std::string("File::print_copyright: ") +
282                          "failed to unlink temporary file" + tmpname);
283  }
284
285
286  void File::print_core(const bool verbose) const 
287  {
288  }
289
290
291  void File::print_core(const std::string& user, const std::string& line_type,
292                        const SVNlog& log) const 
293  {
294    std::string outpath = user+"/"+line_type+"/"+local_path();
295    std::string imagefile = "images/"+line_type+"/"+local_path_+".png";
296    std::string html_name(outpath + ".html");
297    std::ofstream os(html_name.c_str());
298    print_header(os, name(), level_+2, user, line_type, local_path()+".html");
299    path_anchor(os);
300
301    os << "<p class=\"plot\">\n<img src='"; 
302    for (size_t i=0; i<level_; ++i)
303      os << "../";
304    os << "../../";
305    if (user=="all")
306      os << stats_.plot(imagefile,line_type);
307    else
308      os << imagefile;
309    os << "' alt='[plot]' />\n</p>";
310
311    print_author_summary(os, line_type, log);
312    os << "\n";
313
314    print_blame(os);
315
316    print_footer(os);
317    os.close(); 
318  }
319
320}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.