source: trunk/lib/Directory.cc @ 1005

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

refs #405. Move replicated code to a function

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