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

Last change on this file since 677 was 677, checked in by Peter Johansson, 13 years ago

trac has changed - refs #346

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