source: trunk/lib/File.cc @ 381

Last change on this file since 381 was 381, checked in by Jari Häkkinen, 14 years ago

SVNblame redesigned.

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