Changeset 1085 for trunk


Ignore:
Timestamp:
Jun 12, 2010, 6:43:29 AM (11 years ago)
Author:
Peter Johansson
Message:

new function chdir - wrapper around GNU C Library function chrdir

split svndigest::main into several sub-functions to increase readability

refs #307

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/bin/Parameter.cc

    r1082 r1085  
    9797      check_existence(root_.value());
    9898      check_readable(root_.value());
    99       if (chdir(root_.value().c_str())) {
    100         std::stringstream ss;
    101         ss << cmd_.program_name() << ": cannot read `" << root_.value() << "': "
    102            << strerror(errno);
    103         throw yat::utility::cmd_error(ss.str());
    104       }
     99      chdir(root_.value());
    105100      root_.value(pwd());
    106       chdir(save_wd.c_str());
    107 
     101      chdir(save_wd);
    108102    }
    109103
  • trunk/bin/svncopyright.cc

    r1072 r1085  
    4545    return EXIT_FAILURE;
    4646  }
     47  catch (std::runtime_error& e) {
     48    std::cerr << cmd.program_name() << ": " << e.what() << std::endl;
     49    return EXIT_FAILURE;
     50  }
    4751
    4852  try {
  • trunk/bin/svndigest.cc

    r1081 r1085  
    3131#include "main_utility.h"
    3232#include "rmdirhier.h"
    33 #include <sstream>
    3433#include "Stats.h"
    3534#include "StatsCollection.h"
     
    5857                        const theplu::svndigest::Stats& stats);
    5958
     59void write_report(const svndigestParameter& option, const std::string& repo,
     60                  const Node& tree, const StatsCollection& stats);
     61
    6062int main( int argc, char* argv[])
    6163{
     
    7173    std::cerr << e.what() << std::endl;
    7274    exit(EXIT_FAILURE);
     75  }
     76  catch (std::runtime_error& e) {
     77    std::cerr << cmd.program_name() << ": " << e.what() << std::endl;
     78    return EXIT_FAILURE;
    7379  }
    7480
     
    8490      std::cout << "Initializing SVN singleton." << std::endl;
    8591    SVN::instance(option.root());
    86   }
    87   catch (std::runtime_error& e) {
    88     std::cerr << "svndigest: " << e.what() << "\n";
    89     return EXIT_FAILURE;
    90   }
    91 
    92   // just for convenience
    93   Configuration& config = Configuration::instance();
    94 
    95   // Extract repository location
    96   std::string repo;
    97   try {
    98     if (option.verbose())
    99       std::cout << "Acquiring repository information" << std::endl;
    100     repo=SVNinfo(option.root()).repos_root_url();
    101   }
    102   catch (SVNException e) {
    103     std::cerr << "\nsvndigest: " << e.what()
    104               << "\nsvndigest: Failed to determine repository for "
    105               << option.root() << '\n' << std::endl;
    106     exit(EXIT_FAILURE);
    107   }
    108 
    109   // build directory tree already here ... if WC is not upto date with
    110   // repo an exception is thrown. This avoids several costly
    111   // statements below and will not remove a digest tree below if a
    112   // tree already exists.
    113   if (option.verbose())
    114     std::cout << "Building directory tree" << std::endl;
    115   Node* tree=NULL;
    116   try {
    117     tree = new Directory(0,option.root(),"");
    118   }
    119   catch (NodeException e) {
    120     std::cerr << "svndigest: " << e.what() << std::endl;
    121     exit(EXIT_FAILURE);
    122   }
    123   assert(tree);
    124 
    125   if (option.verbose())
    126     std::cout << "Parsing directory tree" << std::endl;
    127   const StatsCollection& stats(tree->parse(option.verbose(), option.ignore_cache()));
    128 
    129   if (option.report()) {
    130     // remove target if needed
    131     std::string target_path = concatenate_path(option.targetdir(),
    132                                                file_name(option.root()));
    133     if (node_exist(target_path)) {
    134       assert(option.force());
    135       if (option.verbose())
    136         std::cout << "Removing old target tree: " << target_path << "\n";
    137       rmdirhier(target_path);
    138       // exit if remove failed
    139       if (node_exist(target_path)) {
    140         std::cerr << "svndigest: remove failed\n";
    141         exit(EXIT_FAILURE);
    142       }
    143     }
    144 
    145 
    146     if (option.verbose())
    147       std::cout << "Generating output" << std::endl;
    148     if (!option.revisions()) {
    149       SVNlog log(repo);
    150       std::vector<std::string> dates;
    151       dates.reserve(log.commits().size());
    152       for (SVNlog::container::const_iterator iter=log.commits().begin();
    153            iter!=log.commits().end(); ++iter) {
    154         assert(static_cast<svn_revnum_t>(dates.size())==iter->revision());
    155         dates.push_back(iter->date());
    156       }
    157       // all plots uses the same dates
    158       Graph::set_dates(dates);
    159     }
    160     if (chdir(option.targetdir().c_str()) ) {
    161       std::cerr << "svndigest: chdir " << option.targetdir() << " failed\n";
    162       exit(EXIT_FAILURE);
    163     }
    164     mkdir(tree->name());
    165     if (chdir(tree->name().c_str()) ) {
    166       std::cerr << "svndigest: chdir " << tree->name() << " failed\n";
    167       exit(EXIT_FAILURE);
    168     }
    169     print_css("svndigest.css");
    170     print_main_page(tree->name(), tree->log(), stats, tree->url());
    171     // create structure StatsType/Author/LineType
    172     for (std::map<std::string, Stats*>::const_iterator i(stats.stats().begin());
    173          i!=stats.stats().end(); ++i)
    174       create_file_struct(i->first+std::string("/"), *i->second);
    175     try {
    176       tree->print(option.verbose());
    177     }
    178     catch (const std::runtime_error& x) {
    179       std::cerr << "svndigest: " << x.what() << std::endl;
    180       exit(EXIT_FAILURE);
    181     }
    182   }
    183 
    184   if (option.copyright()){
    185     try {
     92
     93    // just for convenience
     94    Configuration& config = Configuration::instance();
     95
     96    // Extract repository location
     97    std::string repo=SVNinfo(option.root()).repos_root_url();
     98
     99    // build directory tree already here ... if WC is not upto date with
     100    // repo an exception is thrown. This avoids several costly
     101    // statements below and will not remove a digest tree below if a
     102    // tree already exists.
     103    if (option.verbose())
     104      std::cout << "Building directory tree" << std::endl;
     105    Directory tree(0,option.root(),"");
     106
     107    if (option.verbose())
     108      std::cout << "Parsing directory tree" << std::endl;
     109    const StatsCollection& stats(tree.parse(option.verbose(),
     110                                            option.ignore_cache()));
     111
     112    if (option.report())
     113      write_report(option, repo, tree, stats);
     114
     115    if (option.copyright()){
    186116      if (option.verbose())
    187117        std::cout << "Updating copyright statements" << std::endl;
    188118      std::map<std::string, Alias> alias(config.copyright_alias());
    189       tree->print_copyright(alias, option.verbose());
     119      tree.print_copyright(alias, option.verbose());
    190120    }
    191     catch (const std::runtime_error& x) {
    192       std::cerr << "svndigest: " << x.what() << std::endl;
    193       exit(EXIT_FAILURE);
    194     }
    195   }
    196 
    197   if (option.verbose())
    198     std::cout << "Finalizing" << std::endl;
    199 
    200   delete tree;
     121
     122    if (option.verbose())
     123      std::cout << "Finalizing" << std::endl;
     124
     125  }
     126  catch (std::runtime_error& e) {
     127    std::cerr << "svndigest: " << e.what() << "\n";
     128    return EXIT_FAILURE;
     129  }
     130
    201131  if (option.verbose())
    202132    std::cout << "Done!" << std::endl;
    203   exit(EXIT_SUCCESS);       // normal exit
     133  return EXIT_SUCCESS;        // normal exit
    204134}
    205135
     
    236166
    237167
    238   void create_file_struct(std::string stats_type,
    239                           const theplu::svndigest::Stats& stats)
    240   {
    241     using namespace theplu::svndigest;
    242     mkdir(stats_type);
    243     touch(stats_type+std::string("index.html"));
    244     mkdir(stats_type+std::string("all"));
    245     mkdir(stats_type+std::string("images"));
    246     touch(stats_type+std::string("all/index.html"));
    247     touch(stats_type+std::string("images/index.html"));
    248     for (std::set<std::string>::const_iterator i = stats.authors().begin();
    249          i!=stats.authors().end(); ++i) {
    250       mkdir(stats_type+*i);
    251       touch(stats_type+*i+std::string("/index.html"));
    252     }
    253   }
     168void create_file_struct(std::string stats_type,
     169                        const theplu::svndigest::Stats& stats)
     170{
     171  using namespace theplu::svndigest;
     172  mkdir(stats_type);
     173  touch(stats_type+std::string("index.html"));
     174  mkdir(stats_type+std::string("all"));
     175  mkdir(stats_type+std::string("images"));
     176  touch(stats_type+std::string("all/index.html"));
     177  touch(stats_type+std::string("images/index.html"));
     178  for (std::set<std::string>::const_iterator i = stats.authors().begin();
     179       i!=stats.authors().end(); ++i) {
     180    mkdir(stats_type+*i);
     181    touch(stats_type+*i+std::string("/index.html"));
     182  }
     183}
     184
     185void remove_target(const std::string& target_path, bool verbose)
     186{
     187  if (verbose)
     188    std::cout << "Removing old target tree: " << target_path << "\n";
     189  rmdirhier(target_path);
     190  // exit if remove failed
     191  if (node_exist(target_path))
     192    throw std::runtime_error("remove failed");
     193}
     194
     195
     196void set_dates(const std::string& repo, bool verbose)
     197{
     198  if (verbose)
     199    std::cout << "retrieving dates" << std::endl;
     200  SVNlog log(repo);
     201  std::vector<std::string> dates;
     202  dates.reserve(log.commits().size());
     203  for (SVNlog::container::const_iterator iter=log.commits().begin();
     204       iter!=log.commits().end(); ++iter) {
     205    assert(static_cast<svn_revnum_t>(dates.size())==iter->revision());
     206    dates.push_back(iter->date());
     207  }
     208  // all plots uses the same dates
     209  Graph::set_dates(dates);
     210}
     211
     212void write_report(const svndigestParameter& option, const std::string& repo,
     213                  const Node& tree, const StatsCollection& stats)
     214{
     215  std::string target_path = concatenate_path(option.targetdir(),
     216                                             file_name(option.root()));
     217  // remove target if needed
     218  if (node_exist(target_path)) {
     219    assert(option.force());
     220    remove_target(target_path, option.verbose());
     221  }
     222 
     223  if (!option.revisions())
     224    set_dates(repo, option.verbose());
     225
     226  chdir(option.targetdir());
     227
     228  if (option.verbose())
     229    std::cout << "Generating output" << std::endl;
     230  mkdir(tree.name());
     231  chdir(tree.name().c_str());
     232  print_css("svndigest.css");
     233  print_main_page(tree.name(), tree.log(), stats, tree.url());
     234  // create structure StatsType/Author/LineType
     235  for (std::map<std::string, Stats*>::const_iterator i(stats.stats().begin());
     236       i!=stats.stats().end(); ++i)
     237    create_file_struct(i->first+std::string("/"), *i->second);
     238  tree.print(option.verbose());
     239}
  • trunk/bin/svndigestParameter.cc

    r1082 r1085  
    110110          throw yat::utility::cmd_error(ss.str());
    111111        }
    112         if (chdir(target_.value().c_str())) {
    113           std::stringstream ss;
    114           ss << "svndigest: cannot read `" << target_.value() << "': "
    115              << strerror(errno);
    116           throw yat::utility::cmd_error(ss.str());
    117         }
     112        chdir(target_.value());
    118113        target_.value(pwd());
    119         chdir(save_wd.c_str());
     114        chdir(save_wd);
    120115      }
    121116    }
  • trunk/lib/rmdirhier.cc

    r978 r1085  
    7878      return;
    7979    }
    80     if (chdir(dir.c_str()))
     80    if (::chdir(dir.c_str()))
    8181      throw BadDirectory(concatenate_path(pwd(),dir));
    8282
  • trunk/lib/utility.cc

    r1052 r1085  
    2424#include "subversion_info.h"
    2525#include "config.h"
     26
     27#include "yat/Exception.h"
    2628
    2729#include <cassert>
     
    6769
    6870
     71  void chdir(const std::string& dir)
     72  {
     73    if (::chdir(dir.c_str()) )
     74      throw yat::utility::errno_error("chdir: " + dir + ":");
     75  }
     76
     77
    6978  std::string concatenate_path(std::string dir, std::string base)
    7079  {
  • trunk/lib/utility.h

    r1052 r1085  
    5555  ///
    5656  int access_rights(const std::string& path,const std::string& bits);
     57
     58  /**
     59     wrapper around GNU C Library function chdir
     60
     61     \throw if underlying chdir call does not return 0
     62   */
     63  void chdir(const std::string& dir);
    5764
    5865  /**
Note: See TracChangeset for help on using the changeset viewer.