source: trunk/lib/File.cc @ 1287

Last change on this file since 1287 was 1287, checked in by Peter Johansson, 11 years ago

use same range of revisions in blame output colors as in plots

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.2 KB
Line 
1// $Id: File.cc 1287 2010-11-09 03:47:22Z peter $
2
3/*
4  Copyright (C) 2005, 2006, 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2010 Peter Johansson
6
7  This file is part of svndigest, http://dev.thep.lu.se/svndigest
8
9  svndigest is free software; you can redistribute it and/or modify it
10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 3 of the License, or
12  (at your option) any later version.
13
14  svndigest is distributed in the hope that it will be useful, but
15  WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "File.h"
24
25#include "Alias.h"
26#include "Colors.h"
27#include "Configuration.h"
28#include "Date.h"
29#include "Graph.h"
30#include "html_utility.h"
31#include "HtmlStream.h"
32#include "NodeVisitor.h"
33#include "Stats.h"
34#include "SVNblame.h"
35#include "SVNlog.h"
36#include "TinyStats.h"
37
38#include <algorithm>
39#include <cassert>
40#include <cstdio>
41#include <ctime>
42#include <fstream>
43#include <iostream>
44#include <map>
45#include <stdexcept>
46#include <string>
47#include <sstream>
48#include <sys/stat.h>
49
50namespace theplu{
51namespace svndigest{
52
53
54  File::File(const unsigned int level, const std::string& path, 
55             const std::string& output) 
56    : Node(level,path,output)
57  {
58    output_dir_=output;
59    if (!output_dir_.empty())
60      output_dir_+='/';
61  }
62
63
64  std::string File::blame_output_file_name(void) const
65  {
66    return "blame_output/" + local_path() + ".html";
67  }
68
69
70  std::string File::href(void) const
71  { 
72    return name()+".html"; 
73  }
74
75
76  svn_revnum_t File::last_changed_rev(void) const
77  {
78    return svn_info().last_changed_rev();
79  }
80
81
82  void File::log_core(SVNlog&) const
83  {
84  }
85
86
87  std::string File::node_type(void) const
88  {
89    return std::string("file");
90  }
91
92
93  std::string File::output_path(void) const
94  {
95    return output_dir()+name()+".html";
96  }
97
98
99  const StatsCollection& File::parse(bool verbose, bool ignore)
100  {
101    if (verbose)
102      std::cout << "Parsing '" << path_ << "'" << std::endl; 
103    stats_.reset();
104    std::string cache_dir = directory_name(path()) + std::string(".svndigest/"); 
105    std::string cache_file = cache_dir + name()+std::string(".svndigest-cache");
106    if (!ignore && node_exist(cache_file)){
107      std::ifstream is(cache_file.c_str());
108      if (stats_.load_cache(is)) {
109        is.close();
110        return stats_;
111      }
112      is.close();
113    }
114    else 
115      stats_.parse(path_);
116    if (!node_exist(cache_dir))
117      mkdir(cache_dir);
118    std::string tmp_cache_file(cache_file+"~");
119    std::ofstream os(tmp_cache_file.c_str());
120    assert(os);
121    stats_.print(os);
122    os.close();
123    rename(tmp_cache_file, cache_file);
124    return stats_;
125  }
126
127
128  void File::print_blame(std::ofstream& os) const
129  {
130    os << "<br /><h3>" << local_path() << "</h3>";
131    os << "<div class=\"blame_legend\">\n";
132    os << "<dl>\n";
133    os << "<dt class=\"code\"></dt><dd>Code</dd>\n";
134    os << "<dt class=\"comment\"></dt><dd>Comments</dd>\n";
135    os << "<dt class=\"other\"></dt><dd>Other</dd>\n";
136    os << "</dl>\n</div>\n";
137    os << "<table class=\"blame\">\n";
138    os << "<thead>\n";
139    os << "<tr>\n";
140    os << "<th class=\"rev\">Rev</th>\n";
141    os << "<th class=\"date\">Date</th>\n";
142    os << "<th class=\"author\">Author</th>\n";
143    os << "<th class=\"line\">Line</th>\n";
144    os << "<th></th>\n";
145    os << "</tr>\n</thead>\n";
146    os << "<tbody>\n";
147    HtmlStream hs(os);
148    SVNblame blame(path_);
149    LineTypeParser parser(path_);
150    while (blame.valid()) {
151      parser.parse(blame.line());
152      blame.next_line();
153    }
154    blame.reset();
155
156    std::vector<LineTypeParser::line_type>::const_iterator
157      line_type(parser.type().begin());
158    int last=0;
159    int first=0;
160    bool using_dates=true;
161    if (!Graph::date_xticks()) {
162      using_dates=false;
163      first = Graph::rev_min();
164      last = Graph::rev_max();
165    }
166    else {
167      first = Graph::xticks()[Graph::rev_min()];
168      assert(Graph::rev_max()<Graph::xticks().size());
169      last = Graph::xticks()[Graph::rev_max()];
170    }
171    assert(last>first);
172    // color is calculated linearly on time, c = kt + m
173    // brightest color (for oldest rev in log) is set to 192.
174    double k = 192.0/(first-last);
175    double m = -last*k; 
176    while (blame.valid()) {
177      std::string color;
178      Date date(blame.date());
179      if (using_dates)
180        color = hex(static_cast<int>(k*date.seconds()+m),2);
181      else
182        color = hex(static_cast<int>(k*blame.revision()+m),2);
183      os << "<tr>\n<td class=\"rev\">";
184      std::stringstream color_ss;
185      color_ss << "#" << color << color << color; 
186      os << "<font color=\"" << color_ss.str() << "\">"
187         << trac_revision(blame.revision(), color_ss.str())
188         << "</font></td>\n<td class=\"date\"><font color=\"#" << color
189         << color << color << "\">" ;
190      hs << date("%d %b %y");
191      os << "</font></td>\n<td class=\"author\">";
192      const std::string& author_color = 
193        Colors::instance().color_str(blame.author());
194      assert(!author_color.empty());
195      os << "<font color=\"#" << author_color << "\">";
196      hs << blame.author();
197      os << "</td>\n<td class=\"";
198      assert(line_type!=parser.type().end());
199      if (*line_type==LineTypeParser::other)
200        os << "line-other";
201      else if (*line_type==LineTypeParser::comment || 
202               *line_type==LineTypeParser::copyright)       
203        os << "line-comment";
204      else if (*line_type==LineTypeParser::code)
205        os << "line-code";
206      else {
207        std::string msg="File::print_blame(): unexpected line type found";
208        throw std::runtime_error(msg);
209      }
210      os << "\">" << blame.line_no()+1
211         << "</td>\n<td>";
212      hs << blame.line();
213      os << "</td>\n</tr>\n";
214      blame.next_line();
215      ++line_type;
216    }
217    os << "</tbody>\n";
218    os << "</table>\n";
219  }
220
221
222  void File::print_core(const bool verbose) const 
223  {
224    mkdir_p(directory_name(blame_output_file_name()));
225    std::ofstream os(blame_output_file_name().c_str());
226    assert(os.good());
227    print_html_start(os, "svndigest", level_+1);
228    if (Configuration::instance().output_blame_information())
229      print_blame(os);
230    print_footer(os);
231    os.close();
232  }
233
234
235  void File::print_core(const std::string& stats_type, 
236                        const std::string& user, const std::string& line_type,
237                        const SVNlog& log) const 
238  {
239    std::string lpath = local_path();
240    if (lpath.empty())
241      lpath = "index";
242    std::string outpath = stats_type+"/"+user+"/"+line_type+"/"+lpath;
243    std::string imagefile = stats_type+"/"+"images/"+line_type+"/"+lpath;
244    std::string html_name(outpath + ".html");
245    mkdir_p(directory_name(html_name));
246    mkdir_p(directory_name(imagefile));
247    std::ofstream os(html_name.c_str());
248    assert(os);
249    print_header(os, name(), level_+3, user, line_type, lpath+".html",
250                 stats_type);
251    path_anchor(os);
252
253    std::stringstream ss;
254    for (size_t i=0; i<level_; ++i)
255      ss << "../";
256    ss << "../../../";
257    if (user=="all")
258      ss << stats_[stats_type].plot(imagefile,line_type);
259    else
260      ss << imagefile;
261    os << "<p class=\"plot\">\n"; 
262    os << image(ss.str());
263    os << "</p>\n";
264
265    print_author_summary(os, stats_[stats_type], line_type, log);
266    os << "\n";
267    os << "<h3>"
268       << anchor(blame_output_file_name(), 
269                 "Blame Information", level_+3) 
270       << "</h3>\n";
271
272    print_footer(os);
273    os.close(); 
274  }
275
276
277  void File::traverse(NodeVisitor& visitor)
278  {
279    visitor.visit(*this);
280  }
281
282}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.