source: trunk/lib/File.cc @ 398

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

refs #200 using same grey-scale in blame output

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.4 KB
Line 
1// $Id: File.cc 398 2007-06-27 16:24:29Z 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    assert(GnuplotFE::instance()->dates().size());
100    time_t last = Date(GnuplotFE::instance()->dates().back()).seconds();
101    // color is calculated linearly on time, c = kt + m
102    // brightest color (for oldest rev in log) is set to 192.
103    double k = 192.0/(Date(GnuplotFE::instance()->dates()[0]).seconds()-last);
104    double m = -last*k; 
105    while (blame.valid()) {
106      std::string color(hex(static_cast<int>(k*Date(blame.date()).seconds()+m),
107                            2));
108      os << "<tr>\n<td class=\"number\"><font color=\"#" << color
109         << color << color << "\">" << blame.revision()
110         << "</td>\n<td class=\"date\"><font color=\"#" << color
111         << color << color << "\">" << Date(blame.date())("%e %b %y")
112         << "</td>\n<td class=\"author\">";
113      hs << blame.author();
114      os << "</td>\n<td class=\"number\">" << blame.line_no()+1
115         << "</td>\n<td class=\"code\">";
116      hs << blame.line();
117      os << "</td>\n</tr>\n";
118      blame.next_line();
119    }
120    os << "</tbody>\n";
121    os << "</table>\n";
122  }
123
124
125  void File::print_copyright(std::map<std::string, Alias>& alias) const 
126  {
127    if (ignore())
128      return;
129    using namespace std;
130
131    SVNlog log(path());
132
133    map<int, set<Alias> > year_authors;
134
135    assert(log.author().size()==log.date().size());
136    vector<string>::const_iterator author=log.author().begin();
137    for (vector<string>::const_iterator date=log.date().begin();
138         date!=log.date().end(); ++date, ++author) {
139      time_t sec = str2time(*date);
140      tm* timeinfo = gmtime(&sec);
141
142      // find username in map of aliases
143      std::map<string,Alias>::iterator name(alias.lower_bound(*author));
144
145      // if alias exist insert alias
146      if (name != alias.end() && name->first==*author)
147        year_authors[timeinfo->tm_year].insert(name->second);
148      else {
149        // else insert user name
150        Alias a(*author,alias.size());
151        year_authors[timeinfo->tm_year].insert(a);
152        std::cerr << "svndigest: warning: no copyright alias found for `" 
153                  << *author << "`\n";
154        // insert alias to avoid multiple warnings.
155        alias.insert(name, std::make_pair(*author, a));
156      }
157    }
158
159    // Code copied from Gnuplot -r70
160    char tmpname[]="/tmp/svndigestXXXXXX";
161    int fd=mkstemp(tmpname);  // mkstemp return a file descriptor
162    if (fd == -1)
163      throw std::runtime_error(std::string("Failed to get unique filename: ") +
164                               tmpname);
165    // Jari would like to do something like 'std::ofstream tmp(fd);'
166    // but has to settle for (which is stupid since the file is
167    // already open for writing.
168    std::ofstream tmp(tmpname);
169
170    ifstream is(path().c_str());
171    assert(is.good());
172    string line;
173    bool found_copyright = false;
174    bool after_copyright = false;
175    string prefix;
176    while(getline(is, line)){
177      if (after_copyright) 
178        tmp << line << "\n";
179      else if (found_copyright){
180        // check if line is end of copyright statement, i.e. contains
181        // no alphanumerical character
182        after_copyright = true;
183        for (size_t i=0; i<line.size()&&after_copyright; ++i)
184          if (isalnum(line[i]))
185            after_copyright = false;
186        if (after_copyright)
187          tmp << line << "\n";
188         
189      }
190      else {
191        // check whether copyright starts on this line
192        string::iterator i = search(line.begin(), line.end(), "Copyright (C)");
193        if (i==line.end()) {
194          tmp << line << "\n";
195        }     
196        else {
197          prefix = line.substr(0, distance(line.begin(), i));
198          found_copyright = true;
199          // Printing copyright statement
200          for (map<int, set<Alias> >::const_iterator i(year_authors.begin());
201               i!=year_authors.end();) {
202          tmp << prefix << "Copyright (C) "
203              << 1900+i->first;
204          map<int, set<Alias> >::const_iterator j = i;
205          assert(i!=year_authors.end());
206          while (++j!=year_authors.end() && 
207                 i->second == j->second){
208            tmp << ", " << 1900+(j->first);
209          }
210          // printing authors
211          std::vector<Alias> vec_alias;
212          back_insert_iterator<std::vector<Alias> > ii(vec_alias);
213          std::copy(i->second.begin(), i->second.end(), ii);
214          // sort with respect to id
215          std::sort(vec_alias.begin(), vec_alias.end(), IdCompare());
216          for (std::vector<Alias>::iterator a=vec_alias.begin();
217               a!=vec_alias.end(); ++a){
218            if (a!=vec_alias.begin())
219              tmp << ",";
220            tmp << " " << a->name();
221          }
222          tmp << "\n";
223          i = j;
224          }
225        }
226      }
227    }
228    is.close();
229    tmp.close();
230    close(fd);
231    // finally move printed temporary file to original file
232    rename(tmpname, path().c_str());
233  }
234
235
236  void File::print_core(const bool verbose) const 
237  {
238  }
239
240
241  void File::print_core(const std::string& user, const std::string& line_type,
242                        const SVNlog& log) const 
243  {
244    std::string outpath = user+"/"+line_type+"/"+local_path();
245    std::string imagefile = "images/"+line_type+"/"+local_path_+".png";
246    std::string html_name(outpath + ".html");
247    std::ofstream os(html_name.c_str());
248    print_header(os, name(), level_+2, user, line_type, local_path()+".html");
249    path_anchor(os);
250
251    os << "<p align=center>\n<img src='"; 
252    for (size_t i=0; i<level_; ++i)
253      os << "../";
254    os << "../../";
255    if (user=="all")
256      os << stats_.plot(imagefile,line_type);
257    else
258      os << imagefile;
259    os << "' alt='[plot]' border=0>\n</p>";
260
261    print_author_summary(os, line_type, log);
262    os << "</p>\n";
263
264    print_blame(os);
265
266    print_footer(os);
267    os.close(); 
268  }
269
270}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.