source: trunk/lib/File.cc @ 395

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

font color shading depending on age of line in blame output (refs #160). Yet to be implemented is when x-axis is rev rather than default time, in which case the shading should depend on the revision.

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