source: trunk/lib/File.cc @ 654

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

Hopefully fixes #328 - need to be tested though

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.9 KB
Line 
1// $Id: File.cc 654 2008-06-08 04:58:38Z peter $
2
3/*
4  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson
5
6  This file is part of svndigest, http://trac.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 "Configuration.h"
28#include "Date.h"
29#include "GnuplotFE.h"
30#include "html_utility.h"
31#include "HtmlStream.h"
32#include "Stats.h"
33#include "SVNblame.h"
34#include "SVNlog.h"
35
36#include <algorithm>
37#include <cassert>
38#include <cstdio>
39#include <ctime>
40#include <fstream>
41#include <iostream>
42#include <map>
43#include <stdexcept>
44#include <string>
45#include <sys/stat.h>
46
47namespace theplu{
48namespace svndigest{
49
50
51  File::File(const unsigned int level, const std::string& path, 
52             const std::string& output) 
53    : Node(level,path,output) 
54  {
55    output_dir_=output;
56    if (!output_dir_.empty())
57      output_dir_+='/';
58  }
59
60
61  std::map<int, std::set<Alias> >
62  File::copyright_map(std::map<std::string, Alias>& alias,
63                      const std::map<int, svn_revnum_t>& year2rev) const
64  {
65    using namespace std;
66    map<int, set<Alias> > year_authors;
67    const Stats& stats = stats_["add"];
68
69    // loop over all years
70    for (std::map<int, svn_revnum_t>::const_iterator rev_iter=year2rev.begin();
71         rev_iter!=year2rev.end(); ++rev_iter) {
72
73      svn_revnum_t last_rev_this_year = rev_iter->second;
74      svn_revnum_t last_rev_last_year = 0;
75      if (rev_iter != year2rev.begin()) {
76        last_rev_last_year = (--rev_iter)->second;
77        ++rev_iter;
78      }
79      // loop over authors
80      for (std::set<std::string>::const_iterator a_iter=stats.authors().begin();
81           a_iter!=stats.authors().end(); ++a_iter) {
82
83        // check if anything has been added since last year
84        if ( (stats(LineTypeParser::code, *a_iter, last_rev_this_year) >
85              stats(LineTypeParser::code, *a_iter, last_rev_last_year)) || 
86             (stats(LineTypeParser::comment, *a_iter, last_rev_this_year) >
87              stats(LineTypeParser::comment, *a_iter, last_rev_last_year)) ) {
88       
89       
90          // find username in map of aliases
91          std::map<string,Alias>::iterator name(alias.lower_bound(*a_iter));
92         
93          // if alias exist insert alias
94          if (name != alias.end() && name->first==*a_iter)
95            year_authors[rev_iter->first].insert(name->second);
96          else {
97            // else insert user name
98            Alias a(*a_iter,alias.size());
99            year_authors[rev_iter->first].insert(a);
100            std::cerr << "svndigest: warning: no copyright alias found for `" 
101                      << *a_iter << "'\n";
102            // insert alias to avoid multiple warnings.
103            alias.insert(name, std::make_pair(*a_iter, a));
104          }
105        }
106      }
107    }
108    return year_authors;
109  }
110
111
112  std::string
113  File::copyright_block(const std::map<int, std::set<Alias> >& year_authors,
114                        const std::string& prefix) const
115  {
116    using namespace std;
117    stringstream ss;
118    for (map<int, set<Alias> >::const_iterator i(year_authors.begin());
119         i!=year_authors.end();) {
120      ss << prefix << "Copyright (C) "
121          << 1900+i->first;
122      map<int, set<Alias> >::const_iterator j = i;
123      assert(i!=year_authors.end());
124      while (++j!=year_authors.end() && 
125             i->second == j->second){
126        ss << ", " << 1900+(j->first);
127      }
128      // printing authors
129      std::vector<Alias> vec_alias;
130      back_insert_iterator<std::vector<Alias> > ii(vec_alias);
131      std::copy(i->second.begin(), i->second.end(), ii);
132      // sort with respect to id
133      std::sort(vec_alias.begin(), vec_alias.end(), IdCompare());
134      for (std::vector<Alias>::iterator a=vec_alias.begin();
135           a!=vec_alias.end(); ++a){
136        if (a!=vec_alias.begin())
137          ss << ",";
138        ss << " " << a->name();
139      }
140      ss << "\n";
141      i = j;
142    }
143    return ss.str();
144  }
145
146  bool File::detect_copyright(std::string& block, size_t& start_at_line,
147                              size_t& end_at_line, std::string& prefix) const
148  {
149    using namespace std;
150    LineTypeParser parser(path());
151    std::ifstream is(path().c_str());
152    std::string line;
153    while (std::getline(is, line)) 
154      parser.parse(line);
155    if (!parser.copyright_found())
156      return false;
157    block = parser.block();
158    start_at_line = parser.start_line();
159    end_at_line = parser.end_line();
160    prefix = parser.prefix();
161    return true;
162  }
163
164
165  std::string File::href(void) const
166  { 
167    return name()+".html"; 
168  }
169
170
171  SVNlog File::log_core(void) const
172  {
173    return SVNlog(path());
174  }
175
176
177  std::string File::node_type(void) const
178  {
179    return std::string("file");
180  }
181
182
183  std::string File::output_path(void) const
184  {
185    return output_dir()+name()+".html";
186  }
187
188
189  const StatsCollection& File::parse(bool verbose, bool ignore)
190  {
191    if (verbose)
192      std::cout << "Parsing " << path_ << std::endl; 
193    stats_.reset();
194    std::string cache_dir = directory_name(path()) + std::string(".svndigest/"); 
195    std::string cache_file = cache_dir + name()+std::string(".svndigest-cache");
196    if (!ignore && node_exist(cache_file)){
197      std::ifstream is(cache_file.c_str());
198      if (stats_.load_cache(is)){
199        return stats_;
200      }
201      is.close();
202    }
203    else {
204      stats_.parse(path_);
205    }
206    if (!node_exist(cache_dir))
207      mkdir(cache_dir);
208    std::ofstream os(cache_file.c_str());
209    stats_.print(os);
210    os.close();
211    return stats_;
212  }
213
214
215  void File::print_blame(std::ofstream& os) const
216  {
217    os << "<br /><h3>Blame Information</h3>";
218    os << "<table class=\"blame\">\n";
219    os << "<thead>\n";
220    os << "<tr>\n";
221    os << "<th class=\"rev\">Rev</th>\n";
222    os << "<th class=\"date\">Date</th>\n";
223    os << "<th class=\"author\">Author</th>\n";
224    os << "<th class=\"line\">Line</th>\n";
225    os << "<th></th>\n";
226    os << "</tr>\n</thead>\n";
227    os << "<tbody>\n";
228    HtmlStream hs(os);
229    SVNblame blame(path_);
230    LineTypeParser parser(path_);
231    while (blame.valid()) {
232      parser.parse(blame.line());
233      blame.next_line();
234    }
235    blame.reset();
236
237    std::vector<LineTypeParser::line_type>::const_iterator
238      line_type(parser.type().begin());
239    int last=0;
240    int first=0;
241    bool using_dates=true;
242    if (GnuplotFE::instance()->dates().empty()){
243      using_dates=false;
244      last = stats_["classic"].revision();
245    }
246    else {
247      last = Date(GnuplotFE::instance()->dates().back()).seconds();
248      // earliest date corresponds either to revision 0 or revision 1
249      first = std::min(Date(GnuplotFE::instance()->dates()[0]).seconds(),
250                       Date(GnuplotFE::instance()->dates()[1]).seconds());
251    }
252    // color is calculated linearly on time, c = kt + m
253    // brightest color (for oldest rev in log) is set to 192.
254    double k = 192.0/(first-last);
255    double m = -last*k; 
256    while (blame.valid()) {
257      std::string color;
258      if (using_dates)
259        color = hex(static_cast<int>(k*Date(blame.date()).seconds()+m),2);
260      else
261        color = hex(static_cast<int>(k*blame.revision()+m),2);
262      os << "<tr>\n<td class=\"rev\">";
263      std::stringstream color_ss;
264      color_ss << "#" << color << color << color; 
265      os << "<font color=\"" << color_ss.str() << "\">"
266         << trac_revision(blame.revision(), color_ss.str())
267         << "</font></td>\n<td class=\"date\"><font color=\"#" << color
268         << color << color << "\">" ;
269      hs << Date(blame.date())("%d %b %y");
270      os << "</font></td>\n<td class=\"author\">";
271      hs << blame.author();
272      os << "</td>\n<td class=\"";
273      assert(line_type!=parser.type().end());
274      if (*line_type==LineTypeParser::other)
275        os << "line-other";
276      else if (*line_type==LineTypeParser::comment || 
277               *line_type==LineTypeParser::copyright)       
278        os << "line-comment";
279      else if (*line_type==LineTypeParser::code)
280        os << "line-code";
281      else {
282        std::string msg="File::print_blame(): unexpected line type found";
283        throw std::runtime_error(msg);
284      }
285      os << "\">" << blame.line_no()+1
286         << "</td>\n<td>";
287      hs << blame.line();
288      os << "</td>\n</tr>\n";
289      blame.next_line();
290      ++line_type;
291    }
292    os << "</tbody>\n";
293    os << "</table>\n";
294  }
295
296
297  void File::print_copyright(std::map<std::string, Alias>& alias, 
298                             bool verbose,
299                             const std::map<int,svn_revnum_t>& y2rev) const 
300  {
301    if (ignore())
302      return;
303
304    std::string old_block;
305    size_t start_line=0;
306    size_t end_line=0;
307    std::string prefix;
308    if (!detect_copyright(old_block, start_line, end_line, prefix)){
309      if (Configuration::instance().missing_copyright_warning())
310        std::cerr << "svndigest: warning: no copyright statement found in `" 
311                  << path_ << "'\n";
312      return;
313    }
314    std::map<int, std::set<Alias> > map=copyright_map(alias, y2rev);
315    std::string new_block = copyright_block(map, prefix);
316    if (old_block==new_block)
317      return;
318    if (verbose)
319      std::cout << "Updating copyright in " << path_ << std::endl; 
320    update_copyright(new_block, start_line, end_line);
321  }
322
323
324  void File::print_core(const bool verbose) const 
325  {
326  }
327
328
329  void File::print_core(const std::string& stats_type, 
330                        const std::string& user, const std::string& line_type,
331                        const SVNlog& log) const 
332  {
333    std::string lpath = local_path();
334    if (lpath.empty())
335      lpath = "index";
336    std::string outpath = stats_type+"/"+user+"/"+line_type+"/"+lpath;
337    std::string imagefile = stats_type+"/"+"images/"+line_type+"/"+
338      lpath+".png";
339    std::string html_name(outpath + ".html");
340    std::ofstream os(html_name.c_str());
341    print_header(os, name(), level_+3, user, line_type, lpath+".html",
342                 stats_type);
343    path_anchor(os);
344    os << "<p class=\"plot\">\n<img src='"; 
345    for (size_t i=0; i<level_; ++i)
346      os << "../";
347    os << "../../../";
348    if (user=="all")
349      os << stats_[stats_type].plot(imagefile,line_type);
350    else
351      os << imagefile;
352    os << "' alt='[plot]' />\n</p>";
353
354    print_author_summary(os, stats_[stats_type], line_type, log);
355    os << "\n";
356
357    print_blame(os);
358
359    print_footer(os);
360    os.close(); 
361  }
362
363  void File::update_copyright(const std::string& new_block,
364                              size_t start_at_line, size_t end_at_line) const
365  {
366    // Code copied from Gnuplot -r70
367    char tmpname[]="/tmp/svndigestXXXXXX";
368    int fd=mkstemp(tmpname);  // mkstemp return a file descriptor
369    if (fd == -1)
370      throw std::runtime_error(std::string("Failed to get unique filename: ") +
371                               tmpname);
372    // Jari would like to do something like 'std::ofstream tmp(fd);'
373    // but has to settle for (which is stupid since the file is
374    // already open for writing.
375    std::ofstream tmp(tmpname);
376
377    using namespace std;
378    ifstream is(path().c_str());
379    assert(is.good());
380    string line;
381    // Copy lines before block
382    for (size_t i=1; i<start_at_line; ++i){
383      assert(is.good());
384      getline(is, line);
385      tmp << line << "\n";
386    }
387    // Printing copyright statement
388    tmp << new_block;
389    // Ignore old block lines
390    for (size_t i=start_at_line; i<end_at_line; ++i){
391      assert(is.good());
392      getline(is, line);
393    }
394    // Copy lines after block
395    while(is.good()) {
396      char ch=is.get();
397      if (is.good())
398        tmp.put(ch);
399    }
400
401    is.close();
402    tmp.close();
403    close(fd);
404    // get file permission
405    struct stat nodestat;
406    stat(path().c_str(),&nodestat);
407   
408    // finally copy temporary file to replace original file, and
409    // remove the temporary file
410    try {
411      copy_file(tmpname, path());
412    }
413    catch (std::runtime_error e) {
414      // catch exception, cleanup, and rethrow
415      std::cerr << "File::print_copyright: Exception caught, "
416                << "removing temporary file " << tmpname << std::endl;
417      if (unlink(tmpname))
418        throw runtime_error(std::string("File::print_copyright: ") +
419                            "failed to unlink temporary file" + tmpname);
420      throw;
421    }
422    if (unlink(tmpname))
423      throw runtime_error(std::string("File::print_copyright: ") +
424                          "failed to unlink temporary file" + tmpname);
425
426    chmod(path().c_str(), nodestat.st_mode);
427  }
428
429
430}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.