source: trunk/lib/Directory.cc @ 1013

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

Replacing local_path_ with local_path() for consistency. Moved local_path_ to Node::private scope.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.8 KB
Line 
1// $Id: Directory.cc 1013 2010-01-05 19:19:25Z 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 outdir = stats_type+"/"+user+"/"+line_type+"/"+local_path();
167    if (local_path()=="")
168      outdir = stats_type+"/"+user+"/"+line_type;
169
170    mkdir(outdir);
171    std::string imagedir = stats_type+"/"+"images/"+line_type+"/"+local_path();
172    if (user=="all")
173      mkdir(imagedir);
174    std::string html_name = outdir+"/index.html";
175    std::ofstream os(html_name.c_str());
176    assert(os.good());
177    if (local_path().empty())
178      print_header(os, name(), level_+3, user, line_type, "index.html", 
179                   stats_type);
180    else
181      print_header(os, name(), level_+3, user, line_type, 
182                   local_path()+"/index.html", stats_type);
183    path_anchor(os);
184
185    std::stringstream ss;
186    for (size_t i=0; i<level_; ++i)
187      ss << "../";
188    ss << "../../../";
189    if (user=="all")
190      ss << stats.plot(imagedir+"index", line_type);
191    else
192      ss << imagedir << "index";
193    os << "<p class=\"plot\">\n"; 
194    os << image(Configuration::instance().image_format(), ss.str());
195    os << "</p>\n";
196
197    os << "<h3>File Summary";
198    if (user!="all")
199      os << " for " << user;
200    os << "</h3>";     
201    os << "<table class=\"listings\">\n";
202    os << "<thead>\n";
203    os << "<tr>\n";
204    os << "<th>Node</th>\n";
205    os << "<th>Lines</th>\n";
206    os << "<th>Code</th>\n";
207    os << "<th>Comments</th>\n";
208    os << "<th>Other</th>\n";
209    os << "<th>Revision</th>\n";
210    os << "<th>Author</th>\n";
211    os << "</tr>\n</thead>\n";
212    os << "<tbody>\n";
213
214    std::string color("light");
215    if (level_){
216      os << "<tr class=\"light\">\n";
217      os << "<td class=\"directory\" colspan=\"7\">";
218      os << anchor("../index.html", "../");
219      os << "</td>\n</tr>\n";
220      color = "dark";
221    }
222
223    // print html links to daughter nodes
224    for (NodeConstIterator d = daughters_.begin(); d!=daughters_.end(); ++d) {
225      (*d)->html_tablerow(os,stats_type, color, user);
226      if (color=="dark")
227        color = "light";
228      else
229        color = "dark";
230    }
231    os << "<tr class=\"" << color << "\">\n";
232    os << "<td>Total</td>\n";
233    if (user=="all"){
234      os << "<td>" << stats.lines() << "</td>\n";
235      os << "<td>" << stats.code() << "</td>\n";
236      os << "<td>" << stats.comments() << "</td>\n";
237      os << "<td>" << stats.empty() << "</td>\n";
238    }
239    else {
240      os << "<td>" << stats.lines(user); 
241      if (stats.lines(user)) 
242        os << " (" << percent(stats.lines(user),stats.lines()) << "%)"; 
243      os << "</td>\n";
244      os << "<td>" << stats.code(user); 
245      if (stats.code(user)) 
246        os << " (" << percent(stats.code(user),stats.code()) << "%)"; 
247      os << "</td>\n";
248      os << "<td>" << stats.comments(user); 
249      if (stats.comments(user)) 
250        os << " (" << percent(stats.comments(user),stats.comments()) << "%)"; 
251      os << "</td>\n";
252      os << "<td>" << stats.empty(user); 
253      if (stats.empty(user)) 
254        os << " (" << percent(stats.empty(user),stats.empty()) << "%)"; 
255      os << "</td>\n";
256    }
257    os << "<td>" << trac_revision(last_changed_rev()) << "</td>\n";
258    os << "<td>" << author() << "</td>\n";
259    os << "</tr>\n";
260    os << "</tbody>\n";
261    os << "</table>\n";
262    print_author_summary(os, stats, line_type, log);
263    os << "\n";
264    print_footer(os);
265    os.close(); 
266  }
267
268
269  void Directory::print_copyright(std::map<std::string, Alias>& alias, 
270                                  bool verbose,
271                                  const std::map<int,svn_revnum_t>& y2r) const 
272  {
273    if (!ignore()){
274      // print daughter nodes, i.e, this function is recursive
275      for (NodeConstIterator i = daughters_.begin(); i!=daughters_.end(); ++i)
276        (*i)->print_copyright(alias, verbose, y2r);
277    }
278  }
279
280}} // end of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.