source: trunk/lib/Directory.cc @ 1014

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

Fixes #439.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1// $Id: Directory.cc 1014 2010-01-05 20:22:59Z jari $
2
3/*
4  Copyright (C) 2005, 2006, 2007, 2008, 2009 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2010 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 "Directory.h"
24
25#include "Alias.h"
26#include "Configuration.h"
27#include "File.h"
28#include "html_utility.h"
29#include "Node.h"
30#include "SVN.h"
31#include "SVNlog.h"
32#include "utility.h"
33
34#include <algorithm>
35#include <cassert>
36#include <fstream>
37#include <functional>
38#include <iostream>
39#include <iterator>
40#include <list>
41#include <map>
42#include <sstream>
43
44#include <cerrno> // Needed to check error state below.
45#include <dirent.h>
46#include <sys/stat.h>
47
48namespace theplu{
49namespace svndigest{
50
51
52  Directory::Directory(const unsigned int level, const std::string& path, 
53                       const std::string& output)
54    : Node(level,path,output)
55  {
56    output_dir_=local_path();
57    if (!output_dir_.empty())
58      output_dir_+='/';
59
60    using namespace std;
61    DIR* directory=opendir(path.c_str());    // C API from dirent.h
62    if (!directory)
63      throw NodeException("ERROR: opendir() failed; " + path +
64                          " is not a directory");
65    list<string> entries;
66    struct dirent* entry;
67    errno=0;  // Global variable used by C to track errors, from errno.h
68    while ((entry=readdir(directory)))       // C API from dirent.h
69      entries.push_back(string(entry->d_name));
70    if (errno)
71      throw NodeException("ERROR: readdir() failed on " + path);
72    closedir(directory);
73
74    SVN* svn=SVN::instance();
75    for (list<string>::iterator i=entries.begin(); i!=entries.end(); ++i)
76      if ((*i)!=string(".") && (*i)!=string("..") && (*i)!=string(".svn")) {
77        string fullpath(path_+'/'+(*i));
78        switch (svn->version_controlled(fullpath)) {
79        case SVN::uptodate:
80          struct stat nodestat;                // C api from sys/stat.h
81          lstat(fullpath.c_str(),&nodestat);   // C api from sys/stat.h
82          if (S_ISDIR(nodestat.st_mode))       // C api from sys/stat.h
83            daughters_.push_back(new Directory(level_+1,fullpath,local_path()));
84          else
85            daughters_.push_back(new File(level_,fullpath,local_path()));
86          break;
87        case SVN::unresolved:
88          throw NodeException(fullpath+" is not up to date");
89        case SVN::unversioned: ; // do nothing
90        }
91      }
92    std::sort(daughters_.begin(), daughters_.end(), NodePtrLess());
93  }
94
95
96  Directory::~Directory(void)
97  {
98    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
99      delete *i;
100  }
101
102  bool Directory::dir(void) const
103  {
104    return true;
105  }
106
107  std::string Directory::href(void) const
108  { 
109    return name() + "/index.html";
110  }
111
112
113  svn_revnum_t Directory::last_changed_rev(void) const
114  {
115    svn_revnum_t res = svn_info().last_changed_rev();
116    for (NodeConstIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
117      res = std::max(res, (*i)->last_changed_rev());
118    return res;
119  }
120
121
122  void Directory::log_core(SVNlog& log) const
123  {
124    for (NodeConstIterator i(daughters_.begin()); i != daughters_.end(); ++i)
125      log += (*i)->log();
126  }
127
128  std::string Directory::node_type(void) const
129  {
130    return std::string("directory");
131  }
132
133
134  std::string Directory::output_path(void) const
135  {
136    return output_dir()+"index.html";
137  }
138
139  const StatsCollection& Directory::parse(bool verbose, bool ignore)
140  {
141    stats_.reset();
142    // Directories themselved give no contribution to statistics.
143    for (NodeIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
144      if (!(*i)->ignore())
145        stats_ += (*i)->parse(verbose, ignore);
146    return stats_;
147  }
148
149
150  void Directory::print_core(const bool verbose) const
151  {
152    mkdir("blame_output/" + local_path());
153    // print daughter nodes
154    for (NodeConstIterator i=daughters_.begin(); i!=daughters_.end(); ++i)
155      (*i)->print(verbose);
156  }
157
158
159  void Directory::print_core(const std::string& stats_type,
160                             const std::string& user, 
161                             const std::string& line_type,
162                             const SVNlog& log) const
163  {
164
165    const Stats& stats = stats_[stats_type];
166    std::string imagedir = stats_type+"/"+"images/"+line_type;
167    std::string outdir   = stats_type+"/"+user+"/" +line_type;
168    if (local_path()!="") {
169      imagedir += "/"+local_path();
170      outdir   += "/"+local_path();
171    }
172    mkdir(outdir);
173    if (user=="all")
174      mkdir(imagedir);
175    std::string html_name = outdir+"/index.html";
176    std::ofstream os(html_name.c_str());
177    assert(os.good());
178    if (local_path().empty())
179      print_header(os, name(), level_+3, user, line_type, "index.html", 
180                   stats_type);
181    else
182      print_header(os, name(), level_+3, user, line_type, 
183                   local_path()+"/index.html", stats_type);
184    path_anchor(os);
185
186    std::stringstream ss;
187    for (size_t i=0; i<level_; ++i)
188      ss << "../";
189    ss << "../../../";
190    if (user=="all")
191      ss << stats.plot(imagedir+"/index", line_type);
192    else
193      ss << imagedir << "/index";
194    os << "<p class=\"plot\">\n"; 
195    os << image(Configuration::instance().image_format(), ss.str());
196    os << "</p>\n";
197
198    os << "<h3>File Summary";
199    if (user!="all")
200      os << " for " << user;
201    os << "</h3>";     
202    os << "<table class=\"listings\">\n";
203    os << "<thead>\n";
204    os << "<tr>\n";
205    os << "<th>Node</th>\n";
206    os << "<th>Lines</th>\n";
207    os << "<th>Code</th>\n";
208    os << "<th>Comments</th>\n";
209    os << "<th>Other</th>\n";
210    os << "<th>Revision</th>\n";
211    os << "<th>Author</th>\n";
212    os << "</tr>\n</thead>\n";
213    os << "<tbody>\n";
214
215    std::string color("light");
216    if (level_){
217      os << "<tr class=\"light\">\n";
218      os << "<td class=\"directory\" colspan=\"7\">";
219      os << anchor("../index.html", "../");
220      os << "</td>\n</tr>\n";
221      color = "dark";
222    }
223
224    // print html links to daughter nodes
225    for (NodeConstIterator d = daughters_.begin(); d!=daughters_.end(); ++d) {
226      (*d)->html_tablerow(os,stats_type, color, user);
227      if (color=="dark")
228        color = "light";
229      else
230        color = "dark";
231    }
232    os << "<tr class=\"" << color << "\">\n";
233    os << "<td>Total</td>\n";
234    if (user=="all"){
235      os << "<td>" << stats.lines() << "</td>\n";
236      os << "<td>" << stats.code() << "</td>\n";
237      os << "<td>" << stats.comments() << "</td>\n";
238      os << "<td>" << stats.empty() << "</td>\n";
239    }
240    else {
241      os << "<td>" << stats.lines(user); 
242      if (stats.lines(user)) 
243        os << " (" << percent(stats.lines(user),stats.lines()) << "%)"; 
244      os << "</td>\n";
245      os << "<td>" << stats.code(user); 
246      if (stats.code(user)) 
247        os << " (" << percent(stats.code(user),stats.code()) << "%)"; 
248      os << "</td>\n";
249      os << "<td>" << stats.comments(user); 
250      if (stats.comments(user)) 
251        os << " (" << percent(stats.comments(user),stats.comments()) << "%)"; 
252      os << "</td>\n";
253      os << "<td>" << stats.empty(user); 
254      if (stats.empty(user)) 
255        os << " (" << percent(stats.empty(user),stats.empty()) << "%)"; 
256      os << "</td>\n";
257    }
258    os << "<td>" << trac_revision(last_changed_rev()) << "</td>\n";
259    os << "<td>" << author() << "</td>\n";
260    os << "</tr>\n";
261    os << "</tbody>\n";
262    os << "</table>\n";
263    print_author_summary(os, stats, line_type, log);
264    os << "\n";
265    print_footer(os);
266    os.close(); 
267  }
268
269
270  void Directory::print_copyright(std::map<std::string, Alias>& alias, 
271                                  bool verbose,
272                                  const std::map<int,svn_revnum_t>& y2r) const 
273  {
274    if (!ignore()){
275      // print daughter nodes, i.e, this function is recursive
276      for (NodeConstIterator i = daughters_.begin(); i!=daughters_.end(); ++i)
277        (*i)->print_copyright(alias, verbose, y2r);
278    }
279  }
280
281}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.