source: trunk/lib/Node.cc @ 1137

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

closes #245. support for svncopyright:ignore property.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.3 KB
Line 
1// $Id: Node.cc 1137 2010-07-18 20:45:45Z peter $
2
3/*
4  Copyright (C) 2005, 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2009 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 "Node.h"
24
25#include "Configuration.h"
26#include "Date.h"
27#include "HtmlStream.h"
28#include "html_utility.h"
29#include "SVNlog.h"
30#include "SVNproperty.h"
31#include "utility.h"
32
33#include <algorithm>
34#include <cassert>
35#include <ctime>
36#include <fstream>
37#include <iostream>
38#include <sstream>
39
40#include <dirent.h>
41#include <sys/stat.h>
42
43namespace theplu{
44namespace svndigest{
45
46  std::string Node::project_=std::string();
47
48  Node::Node(const unsigned int level, const std::string& path, 
49             const std::string& local_path)
50    : level_(level), path_(path), stats_(path), log_(NULL), 
51      svninfo_(path)
52  { 
53    SVNproperty property(path);
54    binary_=property.binary();
55    svndigest_ignore_=property.svndigest_ignore();
56    svncopyright_ignore_=property.svncopyright_ignore();
57    if (Node::project_==std::string()) // no root directory in local path
58      Node::project_ = file_name(path);
59    else if (local_path.empty())
60      local_path_ = file_name(path);
61    else
62      local_path_ = local_path + "/" + file_name(path);
63
64    struct stat nodestat;                // C api from sys/stat.h
65    lstat(path.c_str(),&nodestat);   // C api from sys/stat.h
66    link_ = S_ISLNK(nodestat.st_mode);
67  }
68
69
70  Node::~Node(void)
71  {
72    if (log_)
73      delete log_;
74  }
75
76
77  std::string Node::author(void) const
78  { 
79    if (ignore())
80      return svninfo_.last_changed_author(); 
81    assert(log().commits().size());
82    return log().latest_commit().author();
83  }
84
85
86  bool Node::dir(void) const
87  {
88    return false;
89  }
90
91
92  void Node::html_tablerow(std::ostream& os, 
93                           const std::string& stats_type,
94                           const std::string& css_class,
95                           const std::string& user) const
96  {
97    const Stats& stats = stats_[stats_type];
98    os << "<tr class=\"" << css_class << "\">\n"
99       << "<td class=\"" << node_type() << "\">";
100    if (svndigest_ignore())
101      os << name() << " (<i>svndigest:ignore</i>)";
102    else if (binary())
103      os << name() << " (<i>binary</i>)";
104    else if (link_)
105      os << name() << " (<i>link</i>)";
106    // there is no output for nodes when user has zero contribution
107    else if (user!="all" && !stats.lines(user))
108      os << name();
109    else if (!Configuration::instance().output_file() && !this->dir())
110      os << name();
111    else
112      os << anchor(href(), name()); 
113    os << "</td>\n"; 
114    if (user=="all") {
115      os << "<td>" << stats.lines() << "</td>\n"
116         << "<td>" << stats.code() << "</td>\n"
117         << "<td>" << stats.comments() << "</td>\n"
118         << "<td>" << stats.empty() << "</td>\n";
119    }
120    else {
121      os << "<td>" << stats.lines(user); 
122      if (stats.lines(user)) 
123        os << " (" << percent(stats.lines(user),stats.lines()) << "%)"; 
124      os << "</td>\n";
125      os << "<td>" << stats.code(user); 
126      if (stats.code(user)) 
127        os << " (" << percent(stats.code(user),stats.code()) << "%)"; 
128      os << "</td>\n";
129      os << "<td>" << stats.comments(user); 
130      if (stats.comments(user)) 
131        os << " (" << percent(stats.comments(user),stats.comments()) << "%)"; 
132      os << "</td>\n";
133      os << "<td>" << stats.empty(user); 
134      if (stats.empty(user)) 
135        os << " (" << percent(stats.empty(user),stats.empty()) << "%)"; 
136      os << "</td>\n";
137
138    }
139
140    os << "<td>" << trac_revision(last_changed_rev()) << "</td>\n"
141       << "<td>" << author() << "</td>\n"
142       << "</tr>\n";
143  }
144
145
146  const SVNlog& Node::log(void) const
147  {
148    if (!log_) {
149      if (ignore())
150        log_ = new SVNlog;
151      else {
152        log_ = new SVNlog(path());
153        log_core(*log_);
154      }
155    }
156    return *log_;
157  }
158
159
160  std::string Node::name(void) const 
161  { 
162    std::string res = file_name(path_); 
163    return res;
164  }
165
166
167  std::string Node::output_dir(void) const
168  {
169    return output_dir_;
170  }
171
172
173  void Node::path_anchor(std::ostream& os) const
174  {
175    os << "<h2 class=\"path\">\n";
176    std::vector<std::string> words;
177    words.reserve(level_+1);
178    std::string word;
179    words.push_back(Node::project_);
180    std::stringstream ss(local_path());
181    while(getline(ss,word,'/'))
182      if (!word.empty()) // ignore double slash in path
183        words.push_back(word);
184    if (words.size()==1)
185      os << anchor("index.html", Node::project_,0, "View " + Node::project_);
186    else {
187      for (size_t i=0; i<words.size()-1; ++i){
188        os << anchor("index.html", words[i], level_-i, "View " + words[i]);
189        os << "<span class=\"sep\">/</span>";
190      }
191      os << anchor(href(), words.back(), level_+2-words.size(), 
192             "View " + words.back()); 
193    }
194    os << "\n</h2>\n";
195  }
196
197
198  void Node::print(const bool verbose) const
199  {
200    if (ignore())
201      return;
202    if (!Configuration::instance().output_file() && !this->dir())
203      return;
204    if (verbose)
205      std::cout << "Printing output for " << path_ << std::endl;
206    const SVNlog& log = this->log();
207    typedef std::map<std::string, Stats*>::const_iterator iter;
208
209    const iter end(stats_.stats().end());
210    for (iter i=stats_.stats().begin();i!=end; ++i){
211      print_core(i->first, "all", "total", log);
212      print_core(i->first, "all", "code", log);
213      print_core(i->first, "all", "comments", log);
214      print_core(i->first, "all", "empty", log);
215      for (std::set<std::string>::const_iterator j=i->second->authors().begin();
216         j!=i->second->authors().end(); ++j) {
217        print_core(i->first, *j, "total", log);
218        print_core(i->first, *j, "code", log);
219        print_core(i->first, *j, "comments", log);
220        print_core(i->first, *j, "empty", log);
221      }
222    }
223      print_core(verbose);
224  }
225
226
227  void Node::print_author_summary(std::ostream& os, 
228                                  const Stats& stats,
229                                  const std::string& line_type,
230                                  const SVNlog& log) const
231  { 
232    HtmlStream hs(os);
233    os << "<h3>Author Summary</h3>";
234    os << "<table class=\"listings\">\n";
235    os << "<thead>\n";
236    os << "<tr>\n";
237    os << "<th>Author</th>\n";
238    os << "<th>Lines</th>\n";
239    os << "<th>Code</th>\n";
240    os << "<th>Comments</th>\n";
241    os << "<th>Other</th>\n";
242    os << "<th>Revision</th>\n";
243    os << "<th>Date</th>\n";
244    os << "</tr>\n</thead>\n";
245    os << "<tbody>\n";
246
247    std::string color("light");
248    if (!dir()) {
249      os << "<tr class=\"" << color << "\">\n";
250      os << "<td class=\"directory\" colspan=\"7\">";
251      os << anchor("index.html", "../");
252      os << "</td>\n</tr>\n";
253    }
254
255    // print authors
256    const std::string timefmt("%Y-%m-%d  %H:%M");
257    for (std::set<std::string>::const_iterator i=stats.authors().begin();
258         i!=stats.authors().end(); ++i){
259      if (color=="dark")
260        color="light";
261      else
262        color="dark";
263      os << "<tr class=\"" << color << "\"><td>"; 
264      os << anchor(*i+"/"+line_type+"/"+output_path()
265                   ,*i, level_+2, "View statistics for "+*i); 
266      os << "</td><td>" << stats.lines(*i)
267         << "</td><td>" << stats.code(*i)
268         << "</td><td>" << stats.comments(*i)
269         << "</td><td>" << stats.empty(*i);
270      if (log.exist(*i)) {
271        const Commitment& lc(log.latest_commit(*i));
272        os << "</td>" << "<td>" << trac_revision(lc.revision()) 
273           << "</td>" << "<td>";
274        hs << Date(lc.date())(timefmt);
275      }
276      else {
277        os << "</td>" << "<td>N/A"
278           << "</td>" << "<td>N/A";
279      }
280      os << "</td></tr>\n";
281    }
282
283    os << "<tr class=\"" << color << "\">\n";
284    os << "<td>"; 
285    if (dir())
286      if (local_path().empty())
287        os << anchor("all/"+line_type+"/index.html"
288                     ,"Total", level_+2, "View statistics for all"); 
289      else
290        os << anchor("all/"+line_type+"/"+local_path()+"/index.html"
291                     ,"Total", level_+2, "View statistics for all"); 
292    else
293      os << anchor("all/"+line_type+"/"+local_path()+".html"
294                   ,"Total", level_+2, "View statistics for all"); 
295    os << "</td>\n";
296    os << "<td>" << stats.lines() << "</td>\n";
297    os << "<td>" << stats.code() << "</td>\n";
298    os << "<td>" << stats.comments() << "</td>\n";
299    os << "<td>" << stats.empty() << "</td>\n";
300    const Commitment& lc(log.latest_commit());
301    os << "<td>" << trac_revision(lc.revision()) << "</td>\n";
302    os << "<td>";
303    hs << Date(lc.date())(timefmt);
304    os << "</td>\n";
305    os << "</tr>\n";
306    os << "</tbody>\n";
307    os << "</table>\n";
308  }
309
310 
311  void Node::print_copyright(std::map<std::string,Alias>& alias, 
312                             bool verbose) const
313  {
314    // map with last rev for every year
315    std::map<int, svn_revnum_t> year2rev;
316    // get log for entire project
317    SVNlog log(SVNinfo(path()).repos_root_url());
318    typedef SVNlog::container::const_iterator LogIterator;
319    for (LogIterator i=log.commits().begin(); i!=log.commits().end(); ++i){
320      time_t sec = str2time(i->date());
321      tm* timeinfo = gmtime(&sec);
322      // ignore commits in repository not present in wc
323      year2rev[timeinfo->tm_year] = std::min(i->revision(), last_changed_rev());
324    }
325    print_copyright(alias, verbose, year2rev);
326  }
327
328
329  bool Node::svncopyright_ignore(void) const
330  {
331    return svncopyright_ignore_;
332  }
333
334
335  std::string Node::url(void) const
336  {
337    return svninfo_.url();
338  }
339
340}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.