source: trunk/lib/File.cc @ 399

Last change on this file since 399 was 399, checked in by Peter Johansson, 15 years ago

fixes #200. shading is done linearly on x-axis in plot, which mean on time default and on revs when --revisions are used

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