source: trunk/bin/Parameter.cc @ 687

Last change on this file since 687 was 687, checked in by Peter Johansson, 15 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
Line 
1// $Id: Parameter.cc 687 2008-08-04 19:37:10Z peter $
2
3/*
4  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2008 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 2 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 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
25#include "Parameter.h"
26
27#include "ColumnStream.h"
28#include "subversion_info.h"
29#include "utility.h"
30#include <config.h> // this header file is created by configure
31
32#include <cstddef>
33#include <fstream>
34#include <iostream>
35#include <sstream>
36#include <stdexcept>
37#include <string>
38#include <sys/stat.h>
39
40namespace theplu {
41namespace svndigest {
42
43  Parameter::Parameter( int argc, char *argv[])
44    : config_file_(""), copyright_(false), force_(false), 
45      generate_config_(false), ignore_cache_(false), report_(true),
46      revisions_(false), root_("."), root_node_("."), targetdir_("."), 
47      verbose_(false)
48  {
49    for (int i=1; i<argc; ++i) {
50      std::stringstream ss(argv[i]);
51      std::string myargv("");
52      std::string value("");
53      getline(ss, myargv, '=');
54      getline(ss, value);
55
56      if (myargv=="--config-file"){
57        if (value.size()) {
58          config_file_.value()= value;
59        }
60        else if (++i<argc){
61          config_file_.value()= std::string(argv[i]);
62        }
63        else {
64          missing_argument(myargv);
65        }
66      }
67      else if (myargv=="--copyright"){
68          copyright_.value()=true;
69      }
70      else if (myargv=="-f" || myargv=="--force"){
71          force_.value()=true;
72      }
73      else if (myargv=="-g" || myargv=="--generate-config"){
74          generate_config_.value()=true;
75      }
76      else if (myargv=="-h" || myargv=="--help"){
77        help();
78        exit(0);      // always exit after printing help
79      }
80      else if (myargv=="--ignore-cache"){
81          ignore_cache_.value()=true;
82      }
83      else if (myargv=="-r" || myargv=="--root"){
84        if (value.size()) {
85          root_.value()= value;
86        }
87        else if (++i<argc){
88          root_.value()= std::string(argv[i]);
89        }
90        else {
91          missing_argument(myargv);
92        }
93      }
94      else if (myargv=="--report") {
95          report_.value()=true;
96      }
97      else if (myargv=="--no-report") {
98          report_.value()=false;
99      }
100      else if (myargv=="--revisions") {
101          revisions_.value()=true;
102      }
103      else if (myargv=="-t" || myargv=="--target"){
104        if (value.size()) {
105          targetdir_.value()= value;
106        }
107        else if (++i<argc){
108          targetdir_.value()= std::string(argv[i]);
109        }
110        else {
111          missing_argument(myargv);
112        }
113      }
114      else if (myargv=="-v" || myargv=="--verbose"){
115          verbose_.value()=true;
116      }
117      else if (myargv=="--version"){
118        version();
119        exit(0);
120      }
121      else if (myargv=="-vf" || myargv=="-fv"){
122          verbose_.value()=true;
123          force_.value()=true;
124      }
125      else {
126        throw std::runtime_error("svndigest: invalid option: " + myargv +
127                                 "\nTry `svndigest --help' for usage.");
128      } 
129  }
130
131    analyse();
132  }
133
134
135  void Parameter::analyse(void)
136  {
137    using namespace std;
138
139    string workdir(pwd()); // remember current working directory (cwd).
140
141    if (!node_exist(root_.value()))
142      throw runtime_error(string("svndigest: cannot stat `" + root_.value() +
143                                 "': no such file or directory"));
144
145    if (access_rights(root_.value(), "r"))
146      throw runtime_error(string("svndigest: cannot open `" + root_.value() +
147                                 "' for reading: Permission denied"));
148
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    }
163
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_
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    }
180
181    // return back to cwd
182    if (chdir(workdir.c_str()))
183      throw runtime_error(string("svndigest: Failed to access cwd: ") + workdir);
184
185  }
186
187
188  std::string Parameter::config_file(void) const
189  {
190    // not default
191    if (!config_file_.value().empty())
192      return config_file_.value();
193   
194    // default behaviour
195    return root()+"/.svndigest/config";
196  }
197
198
199  void Parameter::help(void) const
200  {
201    ColumnStream cs(std::cout, 1);
202    cs.width(0)=79;
203    cs.margin(0)=0;
204    ColumnStream cs2(std::cout, 2);
205    // Widest line should fit exactly
206    cs2.width(0)=21;
207    cs2.width(1)=52;
208    cs2.margin(0)=2;
209    // Gnits standard suggest three characters gap between option and description
210    cs2.margin(1)=3;
211
212    std::cout << "Usage: svndigest [OPTION]...\n"
213              << "\n";
214
215    cs << "Generate statistical report for a subversion repository.\n";
216
217    cs << "\nMandatory arguments to long options are mandatory for "
218       << "short options too.\n";
219
220    cs2  << "    --config-file=ARG\tconfiguration file " 
221         << "[<ROOT>/.svndigest/config]\n"
222         << "    --copyright\tupdate copyright statement\n" 
223         << "-f, --force\tif sub-directory named <ROOT> exists in "
224         << "target directory, remove sub-directory before writing results\n"
225         << "-g, --generate-config\twrite configuration file " 
226         << "to standard output and exit\n"
227         << "-h, --help\tdisplay this help and exit\n"
228         << "    --ignore-cache\tignore cache files and analyze "
229         << "everything from repository\n"
230         << "    --no-report\tcreate no HTML report\n"
231         << "    --revisions\tuse revision numbers as time scale "
232         << "instead of dates [dates]\n"
233         << "-r, --root=ROOT\tsvn controlled directory to perform "
234         << "statistics calculation on [" << root_.default_value() << "]\n"
235         << "-t, --target=TARGET\toutput directory [" 
236         << targetdir_.default_value() << "]\n"
237         << "-v, --verbose\texplain what is being done\n"
238         << "    --version\tprint version information and exit\n";
239
240    std::cout << "\nReport bugs to <" << PACKAGE_BUGREPORT << ">." 
241              << std::endl;
242  }
243
244
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
257  void Parameter::version(bool verbose) const
258  {
259    ColumnStream cs(std::cout, 1);
260    cs.width(0)=79;
261    cs << PACKAGE_STRING << version_string() << "\n";
262    cs << "\nCopyright (C) " << svn_year() 
263       << " Jari H\u00E4kkinen and Peter Johansson.\n"
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";
268  }
269
270}} // of namespace svndigest and namespace theplu
Note: See TracBrowser for help on using the repository browser.