source: trunk/lib/Directory.cc @ 796

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

closes #185 and avoid copying in recursive log function

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