source: trunk/bin/Parameter.cc @ 687

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

Merged patch release 0.6.6 to trunk. Delta 0.6.6 - 0.6.5

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 7.8 KB
RevLine 
[37]1// $Id: Parameter.cc 687 2008-08-04 19:37:10Z peter $
2
[84]3/*
[475]4  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson
[579]5  Copyright (C) 2008 Peter Johansson
[84]6
[687]7  This file is part of svndigest, http://dev.thep.lu.se/svndigest
[84]8
[149]9  svndigest is free software; you can redistribute it and/or modify it
[84]10  under the terms of the GNU General Public License as published by
11  the Free Software Foundation; either version 2 of the License, or
12  (at your option) any later version.
13
[149]14  svndigest is distributed in the hope that it will be useful, but
[84]15  WITHOUT ANY WARRANTY; without even the implied warranty of
[149]16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
[84]17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with this program; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22  02111-1307, USA.
23*/
24
[37]25#include "Parameter.h"
[234]26
27#include "ColumnStream.h"
[540]28#include "subversion_info.h"
[116]29#include "utility.h"
[83]30#include <config.h> // this header file is created by configure
[37]31
[540]32#include <cstddef>
[226]33#include <fstream>
[37]34#include <iostream>
[226]35#include <sstream>
[49]36#include <stdexcept>
[37]37#include <string>
[104]38#include <sys/stat.h>
[37]39
40namespace theplu {
[149]41namespace svndigest {
[37]42
[518]43  Parameter::Parameter( int argc, char *argv[])
[625]44    : config_file_(""), copyright_(false), force_(false), 
45      generate_config_(false), ignore_cache_(false), report_(true),
[632]46      revisions_(false), root_("."), root_node_("."), targetdir_("."), 
47      verbose_(false)
[37]48  {
[173]49    for (int i=1; i<argc; ++i) {
[475]50      std::stringstream ss(argv[i]);
51      std::string myargv("");
52      std::string value("");
53      getline(ss, myargv, '=');
54      getline(ss, value);
55
[234]56      if (myargv=="--config-file"){
[475]57        if (value.size()) {
[625]58          config_file_.value()= value;
[475]59        }
60        else if (++i<argc){
[625]61          config_file_.value()= std::string(argv[i]);
[226]62        }
[626]63        else {
64          missing_argument(myargv);
65        }
[37]66      }
[226]67      else if (myargv=="--copyright"){
[625]68          copyright_.value()=true;
[198]69      }
[226]70      else if (myargv=="-f" || myargv=="--force"){
[625]71          force_.value()=true;
[226]72      }
[229]73      else if (myargv=="-g" || myargv=="--generate-config"){
[625]74          generate_config_.value()=true;
[229]75      }
[39]76      else if (myargv=="-h" || myargv=="--help"){
[37]77        help();
78        exit(0);      // always exit after printing help
79      }
[538]80      else if (myargv=="--ignore-cache"){
[625]81          ignore_cache_.value()=true;
[538]82      }
[52]83      else if (myargv=="-r" || myargv=="--root"){
[475]84        if (value.size()) {
[625]85          root_.value()= value;
[475]86        }
87        else if (++i<argc){
[625]88          root_.value()= std::string(argv[i]);
[52]89        }
[626]90        else {
91          missing_argument(myargv);
92        }
[52]93      }
[423]94      else if (myargv=="--report") {
[625]95          report_.value()=true;
[423]96      }
97      else if (myargv=="--no-report") {
[625]98          report_.value()=false;
[423]99      }
[240]100      else if (myargv=="--revisions") {
[625]101          revisions_.value()=true;
[73]102      }
[52]103      else if (myargv=="-t" || myargv=="--target"){
[475]104        if (value.size()) {
[625]105          targetdir_.value()= value;
[475]106        }
107        else if (++i<argc){
[625]108          targetdir_.value()= std::string(argv[i]);
[52]109        }
[626]110        else {
111          missing_argument(myargv);
112        }
[52]113      }
[39]114      else if (myargv=="-v" || myargv=="--verbose"){
[625]115          verbose_.value()=true;
[37]116      }
[39]117      else if (myargv=="--version"){
[625]118        version();
119        exit(0);
[37]120      }
[180]121      else if (myargv=="-vf" || myargv=="-fv"){
[625]122          verbose_.value()=true;
123          force_.value()=true;
[180]124      }
[626]125      else {
[149]126        throw std::runtime_error("svndigest: invalid option: " + myargv +
[431]127                                 "\nTry `svndigest --help' for usage.");
[626]128      } 
129  }
[37]130
131    analyse();
132  }
133
134
135  void Parameter::analyse(void)
136  {
[151]137    using namespace std;
138
139    string workdir(pwd()); // remember current working directory (cwd).
140
[632]141    if (!node_exist(root_.value()))
142      throw runtime_error(string("svndigest: cannot stat `" + root_.value() +
143                                 "': no such file or directory"));
[579]144
[632]145    if (access_rights(root_.value(), "r"))
146      throw runtime_error(string("svndigest: cannot open `" + root_.value() +
147                                 "' for reading: Permission denied"));
[116]148
[632]149    // Check whether root is directory or file
150    struct stat nodestat;
151    stat(root_.value().c_str(), &nodestat);
152    if (S_ISDIR(nodestat.st_mode)) {
153      chdir(root_.value().c_str());
154      root_node_.value() = root_.value() = pwd();
155    }
156    else {
157      std::string fname = file_name(root_.value());
158      root_.value() = directory_name(root_.value());
159      chdir(root_.value().c_str());
160      root_.value() = pwd();
161      root_node_.value() = root_.value() + "/" + fname;
162    }
[579]163
[151]164    // need to get back to cwd if relative paths are used.
165    if (chdir(workdir.c_str()))
166      runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
167
168    // Checking that targetdir_ exists and retrieve the absolute path
169    // to targetdir_
[631]170    if (report()) {
171      if (chdir(targetdir_.value().c_str()))
172        throw runtime_error(string("svndigest: Target directory (") + 
173                            targetdir_.value() + ") access failed.");
174      targetdir_.value() = pwd();
175      // Checking write permissions for targetdir_
176      if (access_rights(targetdir_.value(),"w"))
177        throw runtime_error(string("svndigest: No write permission on target ") +
178                            "directory (" + targetdir_.value() +").");
179    }
[116]180
[151]181    // return back to cwd
182    if (chdir(workdir.c_str()))
[240]183      throw runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
[229]184
[240]185  }
[229]186
[240]187
188  std::string Parameter::config_file(void) const
189  {
190    // not default
[625]191    if (!config_file_.value().empty())
192      return config_file_.value();
[240]193   
194    // default behaviour
195    return root()+"/.svndigest/config";
[37]196  }
197
198
[625]199  void Parameter::help(void) const
[73]200  {
[234]201    ColumnStream cs(std::cout, 1);
[280]202    cs.width(0)=79;
203    cs.margin(0)=0;
[241]204    ColumnStream cs2(std::cout, 2);
[628]205    // Widest line should fit exactly
206    cs2.width(0)=21;
[269]207    cs2.width(1)=52;
[234]208    cs2.margin(0)=2;
[629]209    // Gnits standard suggest three characters gap between option and description
[628]210    cs2.margin(1)=3;
[234]211
[280]212    std::cout << "Usage: svndigest [OPTION]...\n"
[234]213              << "\n";
214
[280]215    cs << "Generate statistical report for a subversion repository.\n";
[234]216
[280]217    cs << "\nMandatory arguments to long options are mandatory for "
218       << "short options too.\n";
219
[619]220    cs2  << "    --config-file=ARG\tconfiguration file " 
[240]221         << "[<ROOT>/.svndigest/config]\n"
[619]222         << "    --copyright\tupdate copyright statement\n" 
[374]223         << "-f, --force\tif sub-directory named <ROOT> exists in "
[423]224         << "target directory, remove sub-directory before writing results\n"
[241]225         << "-g, --generate-config\twrite configuration file " 
[234]226         << "to standard output and exit\n"
[241]227         << "-h, --help\tdisplay this help and exit\n"
[645]228         << "    --ignore-cache\tignore cache files and analyze "
[538]229         << "everything from repository\n"
[645]230         << "    --no-report\tcreate no HTML report\n"
231         << "    --revisions\tuse revision numbers as time scale "
[423]232         << "instead of dates [dates]\n"
[241]233         << "-r, --root=ROOT\tsvn controlled directory to perform "
[625]234         << "statistics calculation on [" << root_.default_value() << "]\n"
235         << "-t, --target=TARGET\toutput directory [" 
236         << targetdir_.default_value() << "]\n"
[241]237         << "-v, --verbose\texplain what is being done\n"
238         << "    --version\tprint version information and exit\n";
[280]239
240    std::cout << "\nReport bugs to <" << PACKAGE_BUGREPORT << ">." 
241              << std::endl;
[37]242  }
243
244
[626]245  void Parameter::missing_argument(std::string opt) const
246  {
247    if (opt.size()>0 && opt[0]=='-')
248      opt = opt.substr(1);
249    if (opt.size()>0 && opt[0]=='-')
250      opt = opt.substr(1);
251    std::stringstream ss;
252    ss << "svndigest: option requires an argument -- " << opt << "\n"
253       << "Try `svndigest --help' for usage."; 
254    throw std::runtime_error(ss.str());
255  }
256
[540]257  void Parameter::version(bool verbose) const
[37]258  {
[280]259    ColumnStream cs(std::cout, 1);
260    cs.width(0)=79;
[620]261    cs << PACKAGE_STRING << version_string() << "\n";
[540]262    cs << "\nCopyright (C) " << svn_year() 
[581]263       << " Jari H\u00E4kkinen and Peter Johansson.\n"
[544]264       << "This is free software. You may redistribute copies of it under "
265       << "the terms of the GNU General Public License "
266       << "<http://www.gnu.org/licenses/gpl.html>.\n"
267       << "There is NO WARRANTY; to the extent permitted by law.\n";
[37]268  }
269
[442]270}} // of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.