source: branches/0.8-stable/lib/Node.cc @ 1246

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

fixes #477

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