source: branches/replacing_gnuplot/lib/Node.cc @ 845

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

Merged trunk changes -r782:844 to replacing_gnuplot branch.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.0 KB
Line 
1// $Id: Node.cc 845 2009-11-16 22:27:19Z jari $
2
3/*
4  Copyright (C) 2005, 2006, 2007 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2008 Jari Häkkinen
6  Copyright (C) 2009 Peter Johansson
7
8  This file is part of svndigest, http://dev.thep.lu.se/svndigest
9
10  svndigest is free software; you can redistribute it and/or modify it
11  under the terms of the GNU General Public License as published by
12  the Free Software Foundation; either version 3 of the License, or
13  (at your option) any later version.
14
15  svndigest is distributed in the hope that it will be useful, but
16  WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with svndigest. If not, see <http://www.gnu.org/licenses/>.
22*/
23
24#include "Node.h"
25
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
109      os << anchor(href(), name()); 
110    os << "</td>\n"; 
111    if (user=="all") {
112      os << "<td>" << stats.lines() << "</td>\n"
113         << "<td>" << stats.code() << "</td>\n"
114         << "<td>" << stats.comments() << "</td>\n"
115         << "<td>" << stats.empty() << "</td>\n";
116    }
117    else {
118      os << "<td>" << stats.lines(user); 
119      if (stats.lines(user)) 
120        os << " (" << percent(stats.lines(user),stats.lines()) << "%)"; 
121      os << "</td>\n";
122      os << "<td>" << stats.code(user); 
123      if (stats.code(user)) 
124        os << " (" << percent(stats.code(user),stats.code()) << "%)"; 
125      os << "</td>\n";
126      os << "<td>" << stats.comments(user); 
127      if (stats.comments(user)) 
128        os << " (" << percent(stats.comments(user),stats.comments()) << "%)"; 
129      os << "</td>\n";
130      os << "<td>" << stats.empty(user); 
131      if (stats.empty(user)) 
132        os << " (" << percent(stats.empty(user),stats.empty()) << "%)"; 
133      os << "</td>\n";
134
135    }
136
137    os << "<td>" << trac_revision(last_changed_rev()) << "</td>\n"
138       << "<td>" << author() << "</td>\n"
139       << "</tr>\n";
140  }
141
142
143  const SVNlog& Node::log(void) const
144  {
145    if (!log_) {
146      if (ignore())
147        log_ = new SVNlog;
148      else {
149        log_ = new SVNlog(path());
150        log_core(*log_);
151      }
152    }
153    return *log_;
154  }
155
156
157  std::string Node::name(void) const 
158  { 
159    std::string res = file_name(path_); 
160    return res;
161  }
162
163
164  std::string Node::output_dir(void) const
165  {
166    return output_dir_;
167  }
168
169
170  void Node::path_anchor(std::ostream& os) const
171  {
172    os << "<h2 class=\"path\">\n";
173    std::vector<std::string> words;
174    words.reserve(level_+1);
175    std::string word;
176    words.push_back(Node::project_);
177    std::stringstream ss(local_path());
178    while(getline(ss,word,'/'))
179      if (!word.empty()) // ignore double slash in path
180        words.push_back(word);
181    if (words.size()==1)
182      os << anchor("index.html", Node::project_,0, "View " + Node::project_);
183    else {
184      for (size_t i=0; i<words.size()-1; ++i){
185        os << anchor("index.html", words[i], level_-i, "View " + words[i]);
186        os << "<span class=\"sep\">/</span>";
187      }
188      os << anchor(href(), words.back(), level_+2-words.size(), 
189             "View " + words.back()); 
190    }
191    os << "\n</h2>\n";
192  }
193
194
195  void Node::print(const bool verbose) const
196  {
197    if (ignore())
198      return;
199    if (verbose)
200      std::cout << "Printing output for " << path_ << std::endl;
201    const SVNlog& log = this->log();
202    typedef std::map<std::string, Stats*>::const_iterator iter;
203
204    const iter end(stats_.stats().end());
205    for (iter i=stats_.stats().begin();i!=end; ++i){
206      print_core(i->first, "all", "total", log);
207      print_core(i->first, "all", "code", log);
208      print_core(i->first, "all", "comments", log);
209      print_core(i->first, "all", "empty", log);
210      for (std::set<std::string>::const_iterator j=i->second->authors().begin();
211         j!=i->second->authors().end(); ++j) {
212        print_core(i->first, *j, "total", log);
213        print_core(i->first, *j, "code", log);
214        print_core(i->first, *j, "comments", log);
215        print_core(i->first, *j, "empty", log);
216      }
217    }
218      print_core(verbose);
219  }
220
221
222  void Node::print_author_summary(std::ostream& os, 
223                                  const Stats& stats,
224                                  const std::string& line_type,
225                                  const SVNlog& log) const
226  { 
227    HtmlStream hs(os);
228    os << "<h3>Author Summary</h3>";
229    os << "<table class=\"listings\">\n";
230    os << "<thead>\n";
231    os << "<tr>\n";
232    os << "<th>Author</th>\n";
233    os << "<th>Lines</th>\n";
234    os << "<th>Code</th>\n";
235    os << "<th>Comments</th>\n";
236    os << "<th>Other</th>\n";
237    os << "<th>Revision</th>\n";
238    os << "<th>Date</th>\n";
239    os << "</tr>\n</thead>\n";
240    os << "<tbody>\n";
241
242    std::string color("light");
243    if (!dir()) {
244      os << "<tr class=\"" << color << "\">\n";
245      os << "<td class=\"directory\" colspan=\"7\">";
246      os << anchor("index.html", "../");
247      os << "</td>\n</tr>\n";
248    }
249
250    // print authors
251    const std::string timefmt("%Y-%m-%d  %H:%M");
252    for (std::set<std::string>::const_iterator i=stats.authors().begin();
253         i!=stats.authors().end(); ++i){
254      if (color=="dark")
255        color="light";
256      else
257        color="dark";
258      os << "<tr class=\"" << color << "\"><td>"; 
259      os << anchor(*i+"/"+line_type+"/"+output_path()
260                   ,*i, level_+2, "View statistics for "+*i); 
261      os << "</td><td>" << stats.lines(*i)
262         << "</td><td>" << stats.code(*i)
263         << "</td><td>" << stats.comments(*i)
264         << "</td><td>" << stats.empty(*i);
265      if (log.exist(*i)) {
266        const Commitment& lc(log.latest_commit(*i));
267        os << "</td>" << "<td>" << trac_revision(lc.revision()) 
268           << "</td>" << "<td>";
269        hs << Date(lc.date())(timefmt);
270      }
271      else {
272        os << "</td>" << "<td>N/A"
273           << "</td>" << "<td>N/A";
274      }
275      os << "</td></tr>\n";
276    }
277
278    os << "<tr class=\"" << color << "\">\n";
279    os << "<td>"; 
280    if (dir())
281      if (local_path().empty())
282        os << anchor("all/"+line_type+"/index.html"
283                     ,"Total", level_+2, "View statistics for all"); 
284      else
285        os << anchor("all/"+line_type+"/"+local_path()+"/index.html"
286                     ,"Total", level_+2, "View statistics for all"); 
287    else
288      os << anchor("all/"+line_type+"/"+local_path()+".html"
289                   ,"Total", level_+2, "View statistics for all"); 
290    os << "</td>\n";
291    os << "<td>" << stats.lines() << "</td>\n";
292    os << "<td>" << stats.code() << "</td>\n";
293    os << "<td>" << stats.comments() << "</td>\n";
294    os << "<td>" << stats.empty() << "</td>\n";
295    const Commitment& lc(log.latest_commit());
296    os << "<td>" << trac_revision(lc.revision()) << "</td>\n";
297    os << "<td>";
298    hs << Date(lc.date())(timefmt);
299    os << "</td>\n";
300    os << "</tr>\n";
301    os << "</tbody>\n";
302    os << "</table>\n";
303  }
304
305 
306  void Node::print_copyright(std::map<std::string,Alias>& alias, 
307                             bool verbose) const
308  {
309    // map with last rev for every year
310    std::map<int, svn_revnum_t> year2rev;
311    // get log for entire project
312    SVNlog log(SVNinfo(path()).repos_root_url());
313    typedef SVNlog::container::const_iterator LogIterator;
314    for (LogIterator i=log.commits().begin(); i!=log.commits().end(); ++i){
315      time_t sec = str2time(i->date());
316      tm* timeinfo = gmtime(&sec);
317      // ignore commits in repository not present in wc
318      year2rev[timeinfo->tm_year] = std::min(i->revision(), last_changed_rev());
319    }
320    print_copyright(alias, verbose, year2rev);
321  }
322
323
324  std::string Node::url(void) const
325  {
326    return svninfo_.url();
327  }
328
329}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.