source: trunk/lib/Node.cc @ 1136

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

fixes #329. config option to exclude pages for files in report (only output directories).

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